1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-08-10 22:41:51 +02:00

add systemctl list-jobs parser

This commit is contained in:
Kelly Brazil
2019-11-15 18:58:17 -08:00
parent 5affd44499
commit 3a52fb725a
2 changed files with 56 additions and 30 deletions

View File

@@ -30,7 +30,7 @@ import jc.parsers.route
import jc.parsers.ss import jc.parsers.ss
import jc.parsers.stat import jc.parsers.stat
import jc.parsers.systemctl import jc.parsers.systemctl
# import jc.parsers.systemctl_lj import jc.parsers.systemctl_lj
# import jc.parsers.systemctl_lm # import jc.parsers.systemctl_lm
import jc.parsers.systemctl_ls import jc.parsers.systemctl_ls
import jc.parsers.systemctl_luf import jc.parsers.systemctl_luf
@@ -142,7 +142,7 @@ def main():
'--ss': jc.parsers.ss.parse, '--ss': jc.parsers.ss.parse,
'--stat': jc.parsers.stat.parse, '--stat': jc.parsers.stat.parse,
'--systemctl': jc.parsers.systemctl.parse, '--systemctl': jc.parsers.systemctl.parse,
# '--systemctl-lj': jc.parsers.systemctl_lj.parse, '--systemctl-lj': jc.parsers.systemctl_lj.parse,
# '--systemctl-lm': jc.parsers.systemctl_lm.parse, # '--systemctl-lm': jc.parsers.systemctl_lm.parse,
'--systemctl-ls': jc.parsers.systemctl_ls.parse, '--systemctl-ls': jc.parsers.systemctl_ls.parse,
'--systemctl-luf': jc.parsers.systemctl_luf.parse, '--systemctl-luf': jc.parsers.systemctl_luf.parse,

View File

@@ -1,35 +1,54 @@
"""jc - JSON CLI output utility systemctl-luf Parser """jc - JSON CLI output utility systemctl-lj Parser
Usage: Usage:
specify --systemctl-luf as the first argument if the piped input is coming from systemctl --list-unit-files specify --systemctl-luf as the first argument if the piped input is coming from systemctl list-jobs
Examples: Examples:
$ systemctl -a | jc --systemctl -p $ systemctl list-jobs| jc --systemctl-lj -p
[ [
{ {
"unit": "proc-sys-fs-binfmt_misc.automount", "job": 3543,
"load": "loaded", "unit": "nginxAfterGlusterfs.service",
"active": "active", "type": "start",
"sub": "waiting", "state": "waiting"
"description": "Arbitrary Executable File Formats File System Automount Point"
}, },
{ {
"unit": "dev-block-8:2.device", "job": 3545,
"load": "loaded", "unit": "glusterReadyForLocalhostMount.service",
"active": "active", "type": "start",
"sub": "plugged", "state": "running"
"description": "LVM PV 3klkIj-w1qk-DkJi-0XBJ-y3o7-i2Ac-vHqWBM on /dev/sda2 2"
}, },
{ {
"unit": "dev-cdrom.device", "job": 3506,
"load": "loaded", "unit": "nginx.service",
"active": "active", "type": "start",
"sub": "plugged", "state": "waiting"
"description": "VMware_Virtual_IDE_CDROM_Drive" }
},
...
] ]
$ systemctl list-jobs| jc --systemctl-lj -p -r
[
{
"job": "3543",
"unit": "nginxAfterGlusterfs.service",
"type": "start",
"state": "waiting"
},
{
"job": "3545",
"unit": "glusterReadyForLocalhostMount.service",
"type": "start",
"state": "running"
},
{
"job": "3506",
"unit": "nginx.service",
"type": "start",
"state": "waiting"
}
]
""" """
import jc.utils import jc.utils
@@ -48,15 +67,22 @@ def process(proc_data):
[ [
{ {
"unit": string, "job": integer,
"load": string, "unit": string,
"active": string, "type": string,
"sub": string, "state": string
"description": string
} }
] ]
""" """
# nothing more to process for entry in proc_data:
int_list = ['job']
for key in int_list:
if key in entry:
try:
key_int = int(entry[key])
entry[key] = key_int
except (ValueError):
entry[key] = None
return proc_data return proc_data
@@ -90,13 +116,13 @@ def parse(data, raw=False, quiet=False):
cleandata.append(entry.encode('ascii', errors='ignore').decode()) cleandata.append(entry.encode('ascii', errors='ignore').decode())
header_text = cleandata[0] header_text = cleandata[0]
header_text = header_text.lower().replace('unit file', 'unit_file') header_text = header_text.lower()
header_list = header_text.split() header_list = header_text.split()
raw_output = [] raw_output = []
for entry in cleandata[1:]: for entry in cleandata[1:]:
if entry.find('unit files listed.') != -1: if entry.find('No jobs running.') != -1:
break break
else: else: