1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-10-08 23:22:21 +02:00
Files
jc/jc/parsers/systemctl_lj.py

140 lines
3.1 KiB
Python
Raw Normal View History

2019-11-15 18:58:17 -08:00
"""jc - JSON CLI output utility systemctl-lj Parser
2019-11-15 18:36:12 -08:00
Usage:
2019-11-15 19:26:22 -08:00
specify --systemctl-lj as the first argument if the piped input is coming from systemctl list-jobs
2019-11-15 18:36:12 -08:00
2019-12-12 09:35:42 -08:00
Compatibility:
'linux'
2019-11-15 18:36:12 -08:00
Examples:
2019-11-15 18:58:17 -08:00
$ systemctl list-jobs| jc --systemctl-lj -p
2019-11-15 18:36:12 -08:00
[
{
2019-11-15 18:58:17 -08:00
"job": 3543,
"unit": "nginxAfterGlusterfs.service",
"type": "start",
"state": "waiting"
2019-11-15 18:36:12 -08:00
},
{
2019-11-15 18:58:17 -08:00
"job": 3545,
"unit": "glusterReadyForLocalhostMount.service",
"type": "start",
"state": "running"
2019-11-15 18:36:12 -08:00
},
{
2019-11-15 18:58:17 -08:00
"job": 3506,
"unit": "nginx.service",
"type": "start",
"state": "waiting"
}
]
$ systemctl list-jobs| jc --systemctl-lj -p -r
[
{
"job": "3543",
"unit": "nginxAfterGlusterfs.service",
"type": "start",
"state": "waiting"
2019-11-15 18:36:12 -08:00
},
2019-11-15 18:58:17 -08:00
{
"job": "3545",
"unit": "glusterReadyForLocalhostMount.service",
"type": "start",
"state": "running"
},
{
"job": "3506",
"unit": "nginx.service",
"type": "start",
"state": "waiting"
}
2019-11-15 18:36:12 -08:00
]
2019-11-15 18:58:17 -08:00
2019-11-15 18:36:12 -08:00
"""
import jc.utils
def process(proc_data):
"""
Final processing to conform to the schema.
Parameters:
proc_data: (dictionary) raw structured data to process
Returns:
dictionary structured data with the following schema:
[
{
2019-11-15 18:58:17 -08:00
"job": integer,
"unit": string,
"type": string,
"state": string
2019-11-15 18:36:12 -08:00
}
]
"""
2019-11-15 18:58:17 -08:00
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
2019-11-15 18:36:12 -08:00
return proc_data
def parse(data, raw=False, quiet=False):
"""
Main text parsing function
Parameters:
data: (string) text data to parse
raw: (boolean) output preprocessed JSON if True
quiet: (boolean) suppress warning messages if True
Returns:
dictionary raw or processed structured data
"""
# compatible options: linux, darwin, cygwin, win32, aix, systemctlbsd
compatible = ['linux']
if not quiet:
jc.utils.compatibility(__name__, compatible)
linedata = data.splitlines()
# Clear any blank lines
linedata = list(filter(None, linedata))
# clean up non-ascii characters, if any
cleandata = []
for entry in linedata:
cleandata.append(entry.encode('ascii', errors='ignore').decode())
header_text = cleandata[0]
2019-11-15 18:58:17 -08:00
header_text = header_text.lower()
2019-11-15 18:36:12 -08:00
header_list = header_text.split()
raw_output = []
for entry in cleandata[1:]:
2019-11-15 19:02:57 -08:00
if entry.find('No jobs running.') != -1 or entry.find('jobs listed.') != -1:
2019-11-15 18:36:12 -08:00
break
else:
entry_list = entry.split(maxsplit=4)
output_line = dict(zip(header_list, entry_list))
raw_output.append(output_line)
if raw:
return raw_output
else:
return process(raw_output)