2019-10-22 11:10:11 -07:00
|
|
|
"""jc - JSON CLI output utility free Parser
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
specify --free as the first argument if the piped input is coming from free
|
|
|
|
|
2019-12-12 09:21:20 -08:00
|
|
|
Compatibility:
|
|
|
|
'linux'
|
|
|
|
|
2019-11-04 15:11:18 -08:00
|
|
|
Examples:
|
2019-11-04 15:19:50 -08:00
|
|
|
|
2019-11-11 18:30:46 -08:00
|
|
|
$ free | jc --free -p
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"type": "Mem",
|
|
|
|
"total": 3861340,
|
|
|
|
"used": 220508,
|
|
|
|
"free": 3381972,
|
|
|
|
"shared": 11800,
|
|
|
|
"buff_cache": 258860,
|
|
|
|
"available": 3397784
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "Swap",
|
|
|
|
"total": 2097148,
|
|
|
|
"used": 0,
|
|
|
|
"free": 2097148
|
|
|
|
}
|
|
|
|
]
|
2019-10-22 11:10:11 -07:00
|
|
|
|
2019-11-11 18:30:46 -08:00
|
|
|
$ free | jc --free -p -r
|
2019-11-04 15:11:18 -08:00
|
|
|
[
|
|
|
|
{
|
2019-11-11 18:30:46 -08:00
|
|
|
"type": "Mem",
|
|
|
|
"total": "2017300",
|
|
|
|
"used": "213104",
|
|
|
|
"free": "1148452",
|
|
|
|
"shared": "1176",
|
|
|
|
"buff_cache": "655744",
|
|
|
|
"available": "1622204"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "Swap",
|
|
|
|
"total": "2097148",
|
|
|
|
"used": "0",
|
|
|
|
"free": "2097148"
|
2019-11-04 15:11:18 -08:00
|
|
|
}
|
|
|
|
]
|
2019-11-11 18:30:46 -08:00
|
|
|
"""
|
|
|
|
import jc.utils
|
|
|
|
|
|
|
|
|
|
|
|
def process(proc_data):
|
|
|
|
"""
|
2019-11-12 11:28:10 -08:00
|
|
|
Final processing to conform to the schema.
|
|
|
|
|
|
|
|
Parameters:
|
2019-11-14 16:32:11 -08:00
|
|
|
|
2019-11-13 08:04:40 -08:00
|
|
|
proc_data: (dictionary) raw structured data to process
|
2019-11-12 11:28:10 -08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
dictionary structured data with the following schema:
|
2019-11-11 18:30:46 -08:00
|
|
|
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"type": string,
|
|
|
|
"total": integer,
|
|
|
|
"used": integer,
|
|
|
|
"free": integer,
|
|
|
|
"shared": integer,
|
|
|
|
"buff_cache": integer,
|
|
|
|
"available": integer
|
|
|
|
}
|
|
|
|
]
|
|
|
|
"""
|
2019-11-04 15:11:18 -08:00
|
|
|
|
|
|
|
for entry in proc_data:
|
|
|
|
int_list = ['total', 'used', 'free', 'shared', 'buff_cache', 'available']
|
|
|
|
for key in int_list:
|
|
|
|
if key in entry:
|
|
|
|
try:
|
|
|
|
key_int = int(entry[key])
|
|
|
|
entry[key] = key_int
|
2019-11-06 08:47:54 -06:00
|
|
|
except (ValueError):
|
2019-11-04 15:11:18 -08:00
|
|
|
entry[key] = None
|
|
|
|
|
|
|
|
return proc_data
|
|
|
|
|
|
|
|
|
2019-11-07 08:07:43 -08:00
|
|
|
def parse(data, raw=False, quiet=False):
|
2019-11-11 18:30:46 -08:00
|
|
|
"""
|
2019-11-12 11:17:33 -08:00
|
|
|
Main text parsing function
|
2019-11-11 18:30:46 -08:00
|
|
|
|
2019-11-12 11:17:33 -08:00
|
|
|
Parameters:
|
2019-11-14 16:32:11 -08:00
|
|
|
|
2019-11-12 11:17:33 -08:00
|
|
|
data: (string) text data to parse
|
|
|
|
raw: (boolean) output preprocessed JSON if True
|
|
|
|
quiet: (boolean) suppress warning messages if True
|
2019-11-11 18:30:46 -08:00
|
|
|
|
2019-11-12 11:17:33 -08:00
|
|
|
Returns:
|
|
|
|
|
|
|
|
dictionary raw or processed structured data
|
2019-11-11 18:30:46 -08:00
|
|
|
"""
|
2019-11-14 16:32:11 -08:00
|
|
|
|
2019-11-05 22:42:48 -06:00
|
|
|
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
|
2019-11-07 08:07:43 -08:00
|
|
|
compatible = ['linux']
|
|
|
|
|
|
|
|
if not quiet:
|
|
|
|
jc.utils.compatibility(__name__, compatible)
|
2019-10-22 11:10:11 -07:00
|
|
|
|
|
|
|
# code adapted from Conor Heine at:
|
|
|
|
# https://gist.github.com/cahna/43a1a3ff4d075bcd71f9d7120037a501
|
|
|
|
|
|
|
|
cleandata = data.splitlines()
|
2019-10-25 10:55:09 -07:00
|
|
|
headers = [h for h in ' '.join(cleandata[0].lower().strip().split()).split() if h]
|
2019-10-22 11:10:11 -07:00
|
|
|
headers.insert(0, "type")
|
|
|
|
|
2019-10-25 09:46:03 -07:00
|
|
|
# clean up 'buff/cache' header
|
|
|
|
# even though forward slash in a key is valid json, it can make things difficult
|
|
|
|
headers = ['buff_cache' if x == 'buff/cache' else x for x in headers]
|
|
|
|
|
2019-10-22 11:10:11 -07:00
|
|
|
raw_data = map(lambda s: s.strip().split(None, len(headers) - 1), cleandata[1:])
|
2019-11-04 15:11:18 -08:00
|
|
|
raw_output = [dict(zip(headers, r)) for r in raw_data]
|
2019-10-22 11:26:58 -07:00
|
|
|
|
2019-11-04 15:11:18 -08:00
|
|
|
for entry in raw_output:
|
2019-10-22 11:26:58 -07:00
|
|
|
entry['type'] = entry['type'].rstrip(':')
|
|
|
|
|
2019-11-04 15:11:18 -08:00
|
|
|
if raw:
|
|
|
|
return raw_output
|
|
|
|
else:
|
|
|
|
return process(raw_output)
|