mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-13 01:20:24 +02:00
last parser enhancements: augment hostname with CONSOLE for GUI login, add convenience fields when -F is used: login_epoch, logout_epoch, duration_seconds, calculate duration to hours:minutes
This commit is contained in:
@ -23,30 +23,36 @@ Compatibility:
|
||||
|
||||
Examples:
|
||||
|
||||
$ last | jc --last -p
|
||||
$ last -F | jc --last -p
|
||||
[
|
||||
{
|
||||
"user": "kbrazil",
|
||||
"tty": "ttys002",
|
||||
"hostname": null,
|
||||
"login": "Thu Feb 27 14:31",
|
||||
"login": "Mon Dec 28 17:24:10 2020",
|
||||
"logout": "still logged in"
|
||||
},
|
||||
{
|
||||
"user": "kbrazil",
|
||||
"tty": "ttys003",
|
||||
"hostname": null,
|
||||
"login": "Thu Feb 27 10:38",
|
||||
"logout": "10:38",
|
||||
"duration": "00:00"
|
||||
"login": "Mon Dec 28 17:24:10 2020",
|
||||
"logout": "Mon Dec 28 17:25:01 2020",
|
||||
"duration": "00:00",
|
||||
"login_epoch": 1565891826,
|
||||
"logout_epoch": 1565895404,
|
||||
"duration_seconds": 3578
|
||||
},
|
||||
{
|
||||
"user": "kbrazil",
|
||||
"tty": "ttys003",
|
||||
"hostname": null,
|
||||
"login": "Thu Feb 27 10:18",
|
||||
"logout": "10:18",
|
||||
"duration": "00:00"
|
||||
"login": "Mon Dec 28 17:24:10 2020",
|
||||
"logout": "Mon Dec 28 17:25:01 2020",
|
||||
"duration": "00:00",
|
||||
"login_epoch": 1565891826,
|
||||
"logout_epoch": 1565895404,
|
||||
"duration_seconds": 3578
|
||||
},
|
||||
...
|
||||
]
|
||||
@ -109,7 +115,10 @@ Returns:
|
||||
"hostname": string,
|
||||
"login": string,
|
||||
"logout": string,
|
||||
"duration": string
|
||||
"duration": string,
|
||||
"login_epoch": integer, # available with last -F option
|
||||
"logout_epoch": integer, # available with last -F option
|
||||
"duration_seconds": integer # available with last -F option
|
||||
}
|
||||
]
|
||||
|
||||
|
@ -21,30 +21,36 @@ Compatibility:
|
||||
|
||||
Examples:
|
||||
|
||||
$ last | jc --last -p
|
||||
$ last -F | jc --last -p
|
||||
[
|
||||
{
|
||||
"user": "kbrazil",
|
||||
"tty": "ttys002",
|
||||
"hostname": null,
|
||||
"login": "Thu Feb 27 14:31",
|
||||
"login": "Mon Dec 28 17:24:10 2020",
|
||||
"logout": "still logged in"
|
||||
},
|
||||
{
|
||||
"user": "kbrazil",
|
||||
"tty": "ttys003",
|
||||
"hostname": null,
|
||||
"login": "Thu Feb 27 10:38",
|
||||
"logout": "10:38",
|
||||
"duration": "00:00"
|
||||
"login": "Mon Dec 28 17:24:10 2020",
|
||||
"logout": "Mon Dec 28 17:25:01 2020",
|
||||
"duration": "00:00",
|
||||
"login_epoch": 1565891826,
|
||||
"logout_epoch": 1565895404,
|
||||
"duration_seconds": 3578
|
||||
},
|
||||
{
|
||||
"user": "kbrazil",
|
||||
"tty": "ttys003",
|
||||
"hostname": null,
|
||||
"login": "Thu Feb 27 10:18",
|
||||
"logout": "10:18",
|
||||
"duration": "00:00"
|
||||
"login": "Mon Dec 28 17:24:10 2020",
|
||||
"logout": "Mon Dec 28 17:25:01 2020",
|
||||
"duration": "00:00",
|
||||
"login_epoch": 1565891826,
|
||||
"logout_epoch": 1565895404,
|
||||
"duration_seconds": 3578
|
||||
},
|
||||
...
|
||||
]
|
||||
@ -79,6 +85,7 @@ Examples:
|
||||
|
||||
"""
|
||||
import re
|
||||
from datetime import datetime
|
||||
import jc.utils
|
||||
|
||||
|
||||
@ -87,7 +94,7 @@ class info():
|
||||
description = 'last and lastb command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
# details = 'enter any other details here'
|
||||
details = 'Enhancements by https://github.com/zerolagtime'
|
||||
|
||||
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
|
||||
compatible = ['linux', 'darwin', 'aix', 'freebsd']
|
||||
@ -116,7 +123,10 @@ def process(proc_data):
|
||||
"hostname": string,
|
||||
"login": string,
|
||||
"logout": string,
|
||||
"duration": string
|
||||
"duration": string,
|
||||
"login_epoch": integer, # available with last -F option
|
||||
"logout_epoch": integer, # available with last -F option
|
||||
"duration_seconds": integer # available with last -F option
|
||||
}
|
||||
]
|
||||
"""
|
||||
@ -133,12 +143,29 @@ def process(proc_data):
|
||||
if 'hostname' in entry and entry['hostname'] == '-':
|
||||
entry['hostname'] = None
|
||||
|
||||
if 'hostname' in entry and entry['hostname'] is not None and entry['hostname'][0] == ":":
|
||||
entry['hostname'] = f'CONSOLE{entry["hostname"]}'
|
||||
|
||||
if 'logout' in entry and entry['logout'] == 'still_logged_in':
|
||||
entry['logout'] = 'still logged in'
|
||||
|
||||
if 'logout' in entry and entry['logout'] == 'gone_-_no_logout':
|
||||
entry['logout'] = 'gone - no logout'
|
||||
|
||||
if 'login' in entry and re.match(r'.*\d\d:\d\d:\d\d \d\d\d\d.*',entry['login']):
|
||||
entry['login_epoch'] = int(datetime.strptime(entry['login'], '%a %b %d %H:%M:%S %Y').strftime('%s'))
|
||||
|
||||
if 'logout' in entry and re.match(r'.*\d\d:\d\d:\d\d \d\d\d\d.*',entry['logout']):
|
||||
entry['logout_epoch'] = int(datetime.strptime(entry['logout'], '%a %b %d %H:%M:%S %Y').strftime('%s'))
|
||||
|
||||
if 'login_epoch' in entry and 'logout_epoch' in entry:
|
||||
entry['duration_seconds'] = int(entry['logout_epoch']) - int(entry['login_epoch'])
|
||||
|
||||
if 'duration' in entry and re.match(r'^\d+\+', entry['duration']):
|
||||
m = re.match(r'^(?P<days>\d+)\+(?P<hours>\d\d):(?P<minutes>\d\d)', entry['duration'])
|
||||
days, hours, minutes = m.groups()
|
||||
entry['duration'] = f'{int(days)*24 + int(hours)}:{minutes}'
|
||||
|
||||
return proc_data
|
||||
|
||||
|
||||
|
2
tests/fixtures/centos-7.7/last-crash.json
vendored
2
tests/fixtures/centos-7.7/last-crash.json
vendored
File diff suppressed because one or more lines are too long
2
tests/fixtures/centos-7.7/last-w.json
vendored
2
tests/fixtures/centos-7.7/last-w.json
vendored
File diff suppressed because one or more lines are too long
2
tests/fixtures/centos-7.7/last-wF.json
vendored
2
tests/fixtures/centos-7.7/last-wF.json
vendored
File diff suppressed because one or more lines are too long
6
tests/fixtures/centos-7.7/last-wF.out
vendored
6
tests/fixtures/centos-7.7/last-wF.out
vendored
@ -180,5 +180,11 @@ reboot system boot 3.10.0-957.el7.x86_64 Thu Aug 15 11:57:07 2019 - Thu Aug 1
|
||||
kbrazil pts/0 192.168.71.1 Thu Aug 15 10:58:29 2019 - Thu Aug 15 11:56:42 2019 (00:58)
|
||||
root tty1 Thu Aug 15 10:57:26 2019 - Thu Aug 15 11:56:42 2019 (00:59)
|
||||
reboot system boot 3.10.0-957.el7.x86_64 Thu Aug 15 10:57:06 2019 - Thu Aug 15 11:56:44 2019 (00:59)
|
||||
reboot system boot 3.10.0-1127.19.1 Tue Dec 29 08:53:03 2020 - Wed Jan 6 09:28:15 2021 (8+00:35)
|
||||
user1234 pts/0 :0 Tue Dec 29 13:50:50 2020 - crash (-4:-57)
|
||||
user1234 :0 :0 Tue Dec 29 13:49:58 2020 - crash (-4:-56)
|
||||
user1234 pts/1 :pts/0:S.0 Mon Dec 28 17:24:33 2020 - Mon Dec 28 17:24:46 2020 (00:00)
|
||||
user1234 pts/0 :0 Mon Dec 28 17:24:10 2020 - Mon Dec 28 17:25:01 2020 (00:00)
|
||||
user1234 pts/2 fe80::fd8:7309:4 Mon Dec 28 12:43:33 2020 - Mon Dec 28 13:14:22 2020 (00:30)
|
||||
|
||||
wtmp begins Thu Aug 15 10:57:06 2019
|
||||
|
2
tests/fixtures/centos-7.7/last.json
vendored
2
tests/fixtures/centos-7.7/last.json
vendored
File diff suppressed because one or more lines are too long
2
tests/fixtures/fedora32/last.json
vendored
2
tests/fixtures/fedora32/last.json
vendored
@ -1 +1 @@
|
||||
[{"user": "kbrazil", "tty": "pts/0", "hostname": "192.168.71.1", "login": "Fri May 1 15:25", "logout": "gone - no logout"}, {"user": "kbrazil", "tty": "tty1", "hostname": null, "login": "Fri May 1 15:24", "logout": "gone - no logout"}, {"user": "reboot", "tty": "system boot", "hostname": "5.6.6-300.fc32.x", "login": "Fri May 1 15:24", "logout": "running"}, {"user": "kbrazil", "tty": "pts/0", "hostname": "192.168.71.1", "login": "Thu Apr 30 15:27", "logout": "15:22", "duration": "23:55"}, {"user": "kbrazil", "tty": "tty1", "hostname": null, "login": "Thu Apr 30 15:25", "logout": "down", "duration": "23:57"}, {"user": "reboot", "tty": "system boot", "hostname": "5.6.6-300.fc32.x", "login": "Thu Apr 30 15:22", "logout": "15:22", "duration": "1+00:00"}]
|
||||
[{"user": "kbrazil", "tty": "pts/0", "hostname": "192.168.71.1", "login": "Fri May 1 15:25", "logout": "gone - no logout"}, {"user": "kbrazil", "tty": "tty1", "hostname": null, "login": "Fri May 1 15:24", "logout": "gone - no logout"}, {"user": "reboot", "tty": "system boot", "hostname": "5.6.6-300.fc32.x", "login": "Fri May 1 15:24", "logout": "running"}, {"user": "kbrazil", "tty": "pts/0", "hostname": "192.168.71.1", "login": "Thu Apr 30 15:27", "logout": "15:22", "duration": "23:55"}, {"user": "kbrazil", "tty": "tty1", "hostname": null, "login": "Thu Apr 30 15:25", "logout": "down", "duration": "23:57"}, {"user": "reboot", "tty": "system boot", "hostname": "5.6.6-300.fc32.x", "login": "Thu Apr 30 15:22", "logout": "15:22", "duration": "24:00"}]
|
||||
|
2
tests/fixtures/osx-10.14.6/last.json
vendored
2
tests/fixtures/osx-10.14.6/last.json
vendored
File diff suppressed because one or more lines are too long
2
tests/fixtures/ubuntu-18.04/last-w.json
vendored
2
tests/fixtures/ubuntu-18.04/last-w.json
vendored
@ -1 +1 @@
|
||||
[{"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Fri Feb 28 23:03", "logout": "still logged in"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-88-generic", "login": "Fri Feb 28 23:03", "logout": "running"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Fri Feb 28 14:56", "logout": "down", "duration": "06:51"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-88-generic", "login": "Fri Feb 28 14:55", "logout": "21:47", "duration": "06:52"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Thu Feb 27 23:50", "logout": "down", "duration": "00:10"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-88-generic", "login": "Thu Feb 27 23:49", "logout": "00:00", "duration": "00:11"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Thu Feb 27 18:07", "logout": "down", "duration": "00:04"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-76-generic", "login": "Thu Feb 27 18:07", "logout": "18:12", "duration": "00:04"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Tue Feb 18 02:50", "logout": "down", "duration": "1+13:58"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-76-generic", "login": "Tue Feb 18 02:50", "logout": "16:48", "duration": "1+13:58"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Fri Feb 14 02:06", "logout": "01:48", "duration": "3+23:41"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-76-generic", "login": "Fri Feb 14 02:06", "logout": "01:48", "duration": "3+23:41"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Thu Feb 6 01:05", "logout": "down", "duration": "1+14:43"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-76-generic", "login": "Thu Feb 6 01:04", "logout": "15:48", "duration": "1+14:43"}]
|
||||
[{"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Fri Feb 28 23:03", "logout": "still logged in"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-88-generic", "login": "Fri Feb 28 23:03", "logout": "running"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Fri Feb 28 14:56", "logout": "down", "duration": "06:51"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-88-generic", "login": "Fri Feb 28 14:55", "logout": "21:47", "duration": "06:52"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Thu Feb 27 23:50", "logout": "down", "duration": "00:10"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-88-generic", "login": "Thu Feb 27 23:49", "logout": "00:00", "duration": "00:11"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Thu Feb 27 18:07", "logout": "down", "duration": "00:04"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-76-generic", "login": "Thu Feb 27 18:07", "logout": "18:12", "duration": "00:04"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Tue Feb 18 02:50", "logout": "down", "duration": "37:58"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-76-generic", "login": "Tue Feb 18 02:50", "logout": "16:48", "duration": "37:58"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Fri Feb 14 02:06", "logout": "01:48", "duration": "95:41"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-76-generic", "login": "Fri Feb 14 02:06", "logout": "01:48", "duration": "95:41"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Thu Feb 6 01:05", "logout": "down", "duration": "38:43"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-76-generic", "login": "Thu Feb 6 01:04", "logout": "15:48", "duration": "38:43"}]
|
||||
|
2
tests/fixtures/ubuntu-18.04/last.json
vendored
2
tests/fixtures/ubuntu-18.04/last.json
vendored
@ -1 +1 @@
|
||||
[{"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Fri Feb 28 23:03", "logout": "still logged in"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-88-generi", "login": "Fri Feb 28 23:03", "logout": "running"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Fri Feb 28 14:56", "logout": "down", "duration": "06:51"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-88-generi", "login": "Fri Feb 28 14:55", "logout": "21:47", "duration": "06:52"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Thu Feb 27 23:50", "logout": "down", "duration": "00:10"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-88-generi", "login": "Thu Feb 27 23:49", "logout": "00:00", "duration": "00:11"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Thu Feb 27 18:07", "logout": "down", "duration": "00:04"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-76-generi", "login": "Thu Feb 27 18:07", "logout": "18:12", "duration": "00:04"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Tue Feb 18 02:50", "logout": "down", "duration": "1+13:58"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-76-generi", "login": "Tue Feb 18 02:50", "logout": "16:48", "duration": "1+13:58"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Fri Feb 14 02:06", "logout": "01:48", "duration": "3+23:41"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-76-generi", "login": "Fri Feb 14 02:06", "logout": "01:48", "duration": "3+23:41"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Thu Feb 6 01:05", "logout": "down", "duration": "1+14:43"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-76-generi", "login": "Thu Feb 6 01:04", "logout": "15:48", "duration": "1+14:43"}]
|
||||
[{"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Fri Feb 28 23:03", "logout": "still logged in"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-88-generi", "login": "Fri Feb 28 23:03", "logout": "running"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Fri Feb 28 14:56", "logout": "down", "duration": "06:51"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-88-generi", "login": "Fri Feb 28 14:55", "logout": "21:47", "duration": "06:52"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Thu Feb 27 23:50", "logout": "down", "duration": "00:10"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-88-generi", "login": "Thu Feb 27 23:49", "logout": "00:00", "duration": "00:11"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Thu Feb 27 18:07", "logout": "down", "duration": "00:04"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-76-generi", "login": "Thu Feb 27 18:07", "logout": "18:12", "duration": "00:04"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Tue Feb 18 02:50", "logout": "down", "duration": "37:58"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-76-generi", "login": "Tue Feb 18 02:50", "logout": "16:48", "duration": "37:58"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Fri Feb 14 02:06", "logout": "01:48", "duration": "95:41"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-76-generi", "login": "Fri Feb 14 02:06", "logout": "01:48", "duration": "95:41"}, {"user": "kbrazil", "tty": "ttyS0", "hostname": null, "login": "Thu Feb 6 01:05", "logout": "down", "duration": "38:43"}, {"user": "reboot", "tty": "system boot", "hostname": "4.15.0-76-generi", "login": "Thu Feb 6 01:04", "logout": "15:48", "duration": "38:43"}]
|
||||
|
2
tests/fixtures/ubuntu-20.04/last-F.json
vendored
2
tests/fixtures/ubuntu-20.04/last-F.json
vendored
@ -1 +1 @@
|
||||
[{"user": "kbrazil", "tty": "pts/0", "hostname": "192.168.71.1", "login": "Tue Jan 5 20:03:51 2021", "logout": "still logged in"}, {"user": "kbrazil", "tty": "pts/0", "hostname": "192.168.71.1", "login": "Tue Jan 5 00:17:29 2021", "logout": "Tue Jan 5 20:03:47 2021", "duration": "19:46"}, {"user": "kbrazil", "tty": "tty1", "hostname": null, "login": "Tue Jan 5 00:08:46 2021", "logout": "gone - no logout"}, {"user": "root", "tty": "pts/0", "hostname": "developer-pc.exa", "login": "Mon Mar 23 16:44:55 2020", "logout": "down", "duration": "00:00"}, {"user": "reboot", "tty": "system boot", "hostname": "5.8.0-34-generic", "login": "Tue Jan 5 00:08:28 2021", "logout": "running"}]
|
||||
[{"user": "kbrazil", "tty": "pts/0", "hostname": "192.168.71.1", "login": "Tue Jan 5 20:03:51 2021", "logout": "still logged in", "login_epoch": 1609905831}, {"user": "kbrazil", "tty": "pts/0", "hostname": "192.168.71.1", "login": "Tue Jan 5 00:17:29 2021", "logout": "Tue Jan 5 20:03:47 2021", "duration": "19:46", "login_epoch": 1609834649, "logout_epoch": 1609905827, "duration_seconds": 71178}, {"user": "kbrazil", "tty": "tty1", "hostname": null, "login": "Tue Jan 5 00:08:46 2021", "logout": "gone - no logout", "login_epoch": 1609834126}, {"user": "root", "tty": "pts/0", "hostname": "developer-pc.exa", "login": "Mon Mar 23 16:44:55 2020", "logout": "down", "duration": "00:00", "login_epoch": 1585007095}, {"user": "reboot", "tty": "system boot", "hostname": "5.8.0-34-generic", "login": "Tue Jan 5 00:08:28 2021", "logout": "running", "login_epoch": 1609834108}]
|
||||
|
Reference in New Issue
Block a user