From 417b70020accfa4910016e9d3d12690a5488f6f5 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Tue, 6 Apr 2021 10:51:41 -0700 Subject: [PATCH] add idle time fields to finger parser --- EXAMPLES.md | 18 +++++-- docs/parsers/finger.md | 53 ++++++++++++++---- jc/parsers/finger.py | 71 +++++++++++++++++++++---- tests/fixtures/ubuntu-18.04/finger.json | 1 + tests/fixtures/ubuntu-18.04/finger.out | 8 +++ 5 files changed, 129 insertions(+), 22 deletions(-) create mode 100644 tests/fixtures/ubuntu-18.04/finger.json create mode 100644 tests/fixtures/ubuntu-18.04/finger.out diff --git a/EXAMPLES.md b/EXAMPLES.md index 4758313a..fedb54cc 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -960,9 +960,14 @@ finger | jc --finger -p # or: jc -p finger { "login": "jdoe", "name": "John Doe", - "tty": "*tty1", - "idle": "13d", - "login_time": "Mar 22 21:14" + "tty": "tty1", + "idle": "14d", + "login_time": "Mar 22 21:14", + "tty_writeable": false, + "idle_minutes": 0, + "idle_hours": 0, + "idle_days": 14, + "total_idle_minutes": 20160 }, { "login": "jdoe", @@ -970,7 +975,12 @@ finger | jc --finger -p # or: jc -p finger "tty": "pts/0", "idle": null, "login_time": "Apr 5 15:33", - "details": "(192.168.1.22)" + "details": "(192.168.1.22)", + "tty_writeable": true, + "idle_minutes": 0, + "idle_hours": 0, + "idle_days": 0, + "total_idle_minutes": 0 } ] ``` diff --git a/docs/parsers/finger.md b/docs/parsers/finger.md index 4114225c..9e3b60b2 100644 --- a/docs/parsers/finger.md +++ b/docs/parsers/finger.md @@ -24,12 +24,42 @@ Compatibility: Examples: $ finger | jc --finger -p + [ + { + "login": "jdoe", + "name": "John Doe", + "tty": "tty1", + "idle": "14d", + "login_time": "Mar 22 21:14", + "tty_writeable": false, + "idle_minutes": 0, + "idle_hours": 0, + "idle_days": 14, + "total_idle_minutes": 20160 + }, + { + "login": "jdoe", + "name": "John Doe", + "tty": "pts/0", + "idle": null, + "login_time": "Apr 5 15:33", + "details": "(192.168.1.22)", + "tty_writeable": true, + "idle_minutes": 0, + "idle_hours": 0, + "idle_days": 0, + "total_idle_minutes": 0 + }, + ... + ] + + $ finger | jc --finger -p -r [ { "login": "jdoe", "name": "John Doe", "tty": "*tty1", - "idle": "13d", + "idle": "14d", "login_time": "Mar 22 21:14" }, { @@ -39,7 +69,8 @@ Examples: "idle": null, "login_time": "Apr 5 15:33", "details": "(192.168.1.22)" - } + }, + ... ] @@ -66,13 +97,17 @@ Returns: [ { - "login": string, - "name": string, - "tty": string, - "idle": string, # null if empty - "login_time": string, - "details": string, - "tty_writeable": boolean + "login": string, + "name": string, + "tty": string, + "idle": string, # null if empty + "login_time": string, + "details": string, + "tty_writeable": boolean, + "idle_minutes": integer, + "idle_hours": integer, + "idle_days": integer, + "total_idle_minutes": integer } ] diff --git a/jc/parsers/finger.py b/jc/parsers/finger.py index 018ee74b..6499b0ce 100644 --- a/jc/parsers/finger.py +++ b/jc/parsers/finger.py @@ -22,12 +22,42 @@ Compatibility: Examples: $ finger | jc --finger -p + [ + { + "login": "jdoe", + "name": "John Doe", + "tty": "tty1", + "idle": "14d", + "login_time": "Mar 22 21:14", + "tty_writeable": false, + "idle_minutes": 0, + "idle_hours": 0, + "idle_days": 14, + "total_idle_minutes": 20160 + }, + { + "login": "jdoe", + "name": "John Doe", + "tty": "pts/0", + "idle": null, + "login_time": "Apr 5 15:33", + "details": "(192.168.1.22)", + "tty_writeable": true, + "idle_minutes": 0, + "idle_hours": 0, + "idle_days": 0, + "total_idle_minutes": 0 + }, + ... + ] + + $ finger | jc --finger -p -r [ { "login": "jdoe", "name": "John Doe", "tty": "*tty1", - "idle": "13d", + "idle": "14d", "login_time": "Mar 22 21:14" }, { @@ -37,7 +67,8 @@ Examples: "idle": null, "login_time": "Apr 5 15:33", "details": "(192.168.1.22)" - } + }, + ... ] """ import re @@ -74,13 +105,17 @@ def process(proc_data): [ { - "login": string, - "name": string, - "tty": string, - "idle": string, # null if empty - "login_time": string, - "details": string, - "tty_writeable": boolean + "login": string, + "name": string, + "tty": string, + "idle": string, # null if empty + "login_time": string, + "details": string, + "tty_writeable": boolean, + "idle_minutes": integer, + "idle_hours": integer, + "idle_days": integer, + "total_idle_minutes": integer } ] """ @@ -92,9 +127,27 @@ def process(proc_data): entry['tty_writeable'] = False if 'idle' in entry: + entry['idle_minutes'] = 0 + entry['idle_hours'] = 0 + entry['idle_days'] = 0 + if entry['idle'] == '-': entry['idle'] = None + if entry['idle'] and entry['idle'].isnumeric(): + entry['idle_minutes'] = int(entry['idle']) + + if entry['idle'] and ':' in entry['idle']: + entry['idle_hours'] = int(entry['idle'].split(':')[0]) + entry['idle_minutes'] = int(entry['idle'].split(':')[1]) + + if entry['idle'] and 'd' in entry['idle']: + entry['idle_days'] = int(entry['idle'].replace('d', '')) + + entry['total_idle_minutes'] = (entry['idle_days'] * 1440) + \ + (entry['idle_hours'] * 60) + \ + entry['idle_minutes'] + return proc_data diff --git a/tests/fixtures/ubuntu-18.04/finger.json b/tests/fixtures/ubuntu-18.04/finger.json new file mode 100644 index 00000000..d04b610e --- /dev/null +++ b/tests/fixtures/ubuntu-18.04/finger.json @@ -0,0 +1 @@ +[{"login":"kbrazil","name":"Kelly Brazil","tty":"tty1","idle":"14d","login_time":"Mar 22 21:14","tty_writeable":false,"idle_minutes":0,"idle_hours":0,"idle_days":14,"total_idle_minutes":20160},{"login":"kbrazil","name":"Kelly Brazil","tty":"pts/0","idle":null,"login_time":"Apr 5 15:33","details":"(192.168.1.22)","tty_writeable":true,"idle_minutes":0,"idle_hours":0,"idle_days":0,"total_idle_minutes":0},{"login":"kbrazil","name":"Kelly Brazil","tty":"pts/1","idle":"1:00","login_time":"Apr 6 09:04","details":"(192.168.1.22)","tty_writeable":true,"idle_minutes":0,"idle_hours":1,"idle_days":0,"total_idle_minutes":60},{"login":"kbrazil","name":"Kelly Brazil","tty":"pts/0","idle":"14","login_time":"Apr 5 12:30","tty_writeable":true,"idle_minutes":14,"idle_hours":0,"idle_days":0,"total_idle_minutes":14},{"login":"kbrazil","name":"Kelly Brazil","tty":"pts/0","idle":"14","login_time":"Apr 5 2020","tty_writeable":true,"idle_minutes":14,"idle_hours":0,"idle_days":0,"total_idle_minutes":14},{"login":"kbrazil","name":"Kelly Brazil","tty":"pts/0","idle":"14","login_time":"Apr 5 2021","details":"(192.168.1.23)","tty_writeable":true,"idle_minutes":14,"idle_hours":0,"idle_days":0,"total_idle_minutes":14},{"login":"kbrazil","name":"Kelly Brazil","tty":"pts/1","idle":"1:00","login_time":"Apr 6 09:04","details":"(192.168.1.22) other details","tty_writeable":true,"idle_minutes":0,"idle_hours":1,"idle_days":0,"total_idle_minutes":60}] diff --git a/tests/fixtures/ubuntu-18.04/finger.out b/tests/fixtures/ubuntu-18.04/finger.out new file mode 100644 index 00000000..dfb00f6e --- /dev/null +++ b/tests/fixtures/ubuntu-18.04/finger.out @@ -0,0 +1,8 @@ +Login Name Tty Idle Login Time Office Office Phone +kbrazil Kelly Brazil *tty1 14d Mar 22 21:14 +kbrazil Kelly Brazil pts/0 Apr 5 15:33 (192.168.1.22) +kbrazil Kelly Brazil pts/1 1:00 Apr 6 09:04 (192.168.1.22) +kbrazil Kelly Brazil pts/0 14 Apr 5 12:30 +kbrazil Kelly Brazil pts/0 14 Apr 5 2020 +kbrazil Kelly Brazil pts/0 14 Apr 5 2021 (192.168.1.23) +kbrazil Kelly Brazil pts/1 1:00 Apr 6 09:04 (192.168.1.22) other details