2020-08-05 16:51:58 -07:00
|
|
|
"""jc - JSON CLI output utility `timedatectl` command output parser
|
2020-03-10 18:37:55 -07:00
|
|
|
|
2021-03-29 21:16:40 -07:00
|
|
|
The `epoch_utc` calculated timestamp field is timezone-aware and is only available if the universal_time field is available.
|
|
|
|
|
2020-08-05 13:32:59 -07:00
|
|
|
Usage (cli):
|
2020-03-10 18:37:55 -07:00
|
|
|
|
2020-08-05 16:51:58 -07:00
|
|
|
$ timedatectl | jc --timedatectl
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
$ jc timedatectl
|
2020-03-10 18:37:55 -07:00
|
|
|
|
2020-08-05 13:32:59 -07:00
|
|
|
Usage (module):
|
|
|
|
|
|
|
|
import jc.parsers.timedatectl
|
|
|
|
result = jc.parsers.timedatectl.parse(timedatectl_command_output)
|
|
|
|
|
2020-03-10 18:37:55 -07:00
|
|
|
Compatibility:
|
|
|
|
|
|
|
|
'linux'
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
$ timedatectl | jc --timedatectl -p
|
|
|
|
{
|
|
|
|
"local_time": "Tue 2020-03-10 17:53:21 PDT",
|
|
|
|
"universal_time": "Wed 2020-03-11 00:53:21 UTC",
|
|
|
|
"rtc_time": "Wed 2020-03-11 00:53:21",
|
|
|
|
"time_zone": "America/Los_Angeles (PDT, -0700)",
|
|
|
|
"ntp_enabled": true,
|
|
|
|
"ntp_synchronized": true,
|
|
|
|
"rtc_in_local_tz": false,
|
2021-03-29 21:16:40 -07:00
|
|
|
"dst_active": true,
|
|
|
|
"epoch_utc": 1583888001
|
2020-03-10 18:37:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
$ timedatectl | jc --timedatectl -p -r
|
|
|
|
{
|
|
|
|
"local_time": "Tue 2020-03-10 17:53:21 PDT",
|
|
|
|
"universal_time": "Wed 2020-03-11 00:53:21 UTC",
|
|
|
|
"rtc_time": "Wed 2020-03-11 00:53:21",
|
|
|
|
"time_zone": "America/Los_Angeles (PDT, -0700)",
|
|
|
|
"ntp_enabled": "yes",
|
|
|
|
"ntp_synchronized": "yes",
|
|
|
|
"rtc_in_local_tz": "no",
|
|
|
|
"dst_active": "yes"
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
import jc.utils
|
|
|
|
|
|
|
|
|
|
|
|
class info():
|
2021-03-29 21:16:40 -07:00
|
|
|
version = '1.2'
|
2020-03-10 18:37:55 -07:00
|
|
|
description = 'timedatectl status command parser'
|
|
|
|
author = 'Kelly Brazil'
|
|
|
|
author_email = 'kellyjonbrazil@gmail.com'
|
|
|
|
# details = 'enter any other details here'
|
|
|
|
|
|
|
|
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
|
|
|
|
compatible = ['linux']
|
|
|
|
magic_commands = ['timedatectl', 'timedatectl status']
|
|
|
|
|
|
|
|
|
|
|
|
__version__ = info.version
|
|
|
|
|
|
|
|
|
|
|
|
def process(proc_data):
|
|
|
|
"""
|
|
|
|
Final processing to conform to the schema.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
2021-01-04 18:01:16 -08:00
|
|
|
proc_data: (Dictionary) raw structured data to process
|
2020-03-10 18:37:55 -07:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
Dictionary. Structured data with the following schema:
|
|
|
|
|
|
|
|
{
|
2020-03-10 20:26:53 -07:00
|
|
|
"local_time": string,
|
|
|
|
"universal_time": string,
|
2021-03-29 21:16:40 -07:00
|
|
|
"epoch_utc": integer, # timezone-aware timestamp
|
2020-03-10 20:26:53 -07:00
|
|
|
"rtc_time": string,
|
|
|
|
"time_zone": string,
|
|
|
|
"ntp_enabled": boolean,
|
|
|
|
"ntp_synchronized": boolean,
|
|
|
|
"system_clock_synchronized": boolean,
|
|
|
|
"systemd-timesyncd.service_active": boolean,
|
|
|
|
"rtc_in_local_tz": boolean,
|
|
|
|
"dst_active": boolean
|
2020-03-10 18:37:55 -07:00
|
|
|
}
|
|
|
|
"""
|
|
|
|
# boolean changes
|
2020-03-10 20:26:53 -07:00
|
|
|
bool_list = ['ntp_enabled', 'ntp_synchronized', 'rtc_in_local_tz', 'dst_active',
|
|
|
|
'system_clock_synchronized', 'systemd-timesyncd.service_active']
|
2020-03-10 18:37:55 -07:00
|
|
|
for key in proc_data:
|
|
|
|
if key in bool_list:
|
|
|
|
try:
|
|
|
|
proc_data[key] = True if proc_data[key] == 'yes' else False
|
|
|
|
except (ValueError):
|
|
|
|
proc_data[key] = None
|
|
|
|
|
2021-03-29 21:16:40 -07:00
|
|
|
if 'universal_time' in proc_data:
|
2021-04-01 11:18:03 -07:00
|
|
|
ts = jc.utils.timestamp(proc_data['universal_time'])
|
|
|
|
proc_data['epoch_utc'] = ts.utc
|
2021-03-29 21:16:40 -07:00
|
|
|
|
2020-03-10 18:37:55 -07: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.
|
|
|
|
"""
|
|
|
|
if not quiet:
|
|
|
|
jc.utils.compatibility(__name__, info.compatible)
|
|
|
|
|
|
|
|
raw_output = {}
|
|
|
|
|
2020-06-14 17:17:40 -07:00
|
|
|
if jc.utils.has_data(data):
|
2020-03-10 18:37:55 -07:00
|
|
|
|
2020-06-14 17:17:40 -07:00
|
|
|
for line in filter(None, data.splitlines()):
|
|
|
|
linedata = line.split(':', maxsplit=1)
|
|
|
|
raw_output[linedata[0].strip().lower().replace(' ', '_')] = linedata[1].strip()
|
|
|
|
|
|
|
|
if linedata[0].strip() == 'DST active':
|
|
|
|
break
|
2020-03-10 18:37:55 -07:00
|
|
|
|
|
|
|
if raw:
|
|
|
|
return raw_output
|
|
|
|
else:
|
|
|
|
return process(raw_output)
|