mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-13 01:20:24 +02:00
enhance uptime parser for busybox output with no user info
This commit is contained in:
@ -3,6 +3,7 @@ jc changelog
|
|||||||
20240210 v1.25.1
|
20240210 v1.25.1
|
||||||
- Fix for crash when optional libraries are not installed (e.g. xmltodict)
|
- Fix for crash when optional libraries are not installed (e.g. xmltodict)
|
||||||
- Fix for `ini` parser crashing with some keys with no values
|
- Fix for `ini` parser crashing with some keys with no values
|
||||||
|
- Enhance `uptime` parser to support output with no user information
|
||||||
- Add tests for missing optional libraries
|
- Add tests for missing optional libraries
|
||||||
- Documentation updates
|
- Documentation updates
|
||||||
|
|
||||||
|
@ -92,4 +92,4 @@ Source: [`jc/parsers/uptime.py`](https://github.com/kellyjonbrazil/jc/blob/maste
|
|||||||
|
|
||||||
This parser can be used with the `--slurp` command-line option.
|
This parser can be used with the `--slurp` command-line option.
|
||||||
|
|
||||||
Version 1.8 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
Version 1.9 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||||
|
@ -65,7 +65,7 @@ import jc.utils
|
|||||||
|
|
||||||
class info():
|
class info():
|
||||||
"""Provides parser metadata (version, author, etc.)"""
|
"""Provides parser metadata (version, author, etc.)"""
|
||||||
version = '1.8'
|
version = '1.9'
|
||||||
description = '`uptime` command parser'
|
description = '`uptime` command parser'
|
||||||
author = 'Kelly Brazil'
|
author = 'Kelly Brazil'
|
||||||
author_email = 'kellyjonbrazil@gmail.com'
|
author_email = 'kellyjonbrazil@gmail.com'
|
||||||
@ -160,10 +160,11 @@ def parse(data, raw=False, quiet=False):
|
|||||||
jc.utils.input_type_check(data)
|
jc.utils.input_type_check(data)
|
||||||
|
|
||||||
raw_output = {}
|
raw_output = {}
|
||||||
cleandata = data.splitlines()
|
|
||||||
|
|
||||||
if jc.utils.has_data(data):
|
if jc.utils.has_data(data):
|
||||||
time, _, *uptime, users, _, _, _, load_1m, load_5m, load_15m = cleandata[0].split()
|
if 'users' in data:
|
||||||
|
# standard uptime output
|
||||||
|
time, _, *uptime, users, _, _, _, load_1m, load_5m, load_15m = data.split()
|
||||||
|
|
||||||
raw_output['time'] = time
|
raw_output['time'] = time
|
||||||
raw_output['uptime'] = ' '.join(uptime).rstrip(',')
|
raw_output['uptime'] = ' '.join(uptime).rstrip(',')
|
||||||
@ -172,7 +173,14 @@ def parse(data, raw=False, quiet=False):
|
|||||||
raw_output['load_5m'] = load_5m.rstrip(',')
|
raw_output['load_5m'] = load_5m.rstrip(',')
|
||||||
raw_output['load_15m'] = load_15m
|
raw_output['load_15m'] = load_15m
|
||||||
|
|
||||||
if raw:
|
|
||||||
return raw_output
|
|
||||||
else:
|
else:
|
||||||
return _process(raw_output)
|
# users information missing (e.g. busybox)
|
||||||
|
time, _, *uptime, _, _, load_1m, load_5m, load_15m = data.split()
|
||||||
|
|
||||||
|
raw_output['time'] = time
|
||||||
|
raw_output['uptime'] = ' '.join(uptime).rstrip(',')
|
||||||
|
raw_output['load_1m'] = load_1m.rstrip(',')
|
||||||
|
raw_output['load_5m'] = load_5m.rstrip(',')
|
||||||
|
raw_output['load_15m'] = load_15m
|
||||||
|
|
||||||
|
return raw_output if raw else _process(raw_output)
|
||||||
|
@ -65,6 +65,14 @@ class MyTests(unittest.TestCase):
|
|||||||
"""
|
"""
|
||||||
self.assertEqual(jc.parsers.uptime.parse(self.osx_10_14_6_uptime, quiet=True), self.osx_10_14_6_uptime_json)
|
self.assertEqual(jc.parsers.uptime.parse(self.osx_10_14_6_uptime, quiet=True), self.osx_10_14_6_uptime_json)
|
||||||
|
|
||||||
|
def test_uptime_busybox(self):
|
||||||
|
"""
|
||||||
|
Test 'uptime' on busybox with no user information
|
||||||
|
"""
|
||||||
|
data = '00:03:32 up 3 min, load average: 0.00, 0.00, 0.00'
|
||||||
|
expected = {"time":"00:03:32","uptime":"3 min","load_1m":0.0,"load_5m":0.0,"load_15m":0.0,"time_hour":0,"time_minute":3,"time_second":32,"uptime_days":0,"uptime_hours":0,"uptime_minutes":3,"uptime_total_seconds":180}
|
||||||
|
self.assertEqual(jc.parsers.uptime.parse(data, quiet=True), expected)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Reference in New Issue
Block a user