mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-08-08 22:36:48 +02:00
add unit and byte conversion fields
This commit is contained in:
@ -43,23 +43,39 @@ Schema:
|
||||
"cpu_hardware": float,
|
||||
"cpu_software": float,
|
||||
"cpu_steal": float,
|
||||
"mem_total": float, # [0]
|
||||
"mem_free": float, # [0]
|
||||
"mem_used": float, # [0]
|
||||
"mem_buff_cache": float, # [0]
|
||||
"swap_total": float, # [0]
|
||||
"swap_free": float, # [0]
|
||||
"swap_used": float, # [0]
|
||||
"mem_available": float, # [0]
|
||||
"mem_unit": string,
|
||||
"swap_unit": string,
|
||||
"mem_total": float,
|
||||
"mem_total_bytes": integer,
|
||||
"mem_free": float,
|
||||
"mem_free_bytes": integer,
|
||||
"mem_used": float,
|
||||
"mem_used_bytes": integer,
|
||||
"mem_buff_cache": float,
|
||||
"mem_buff_cache_bytes": integer,
|
||||
"swap_total": float,
|
||||
"swap_total_bytes": integer,
|
||||
"swap_free": float,
|
||||
"swap_free_bytes": integer,
|
||||
"swap_used": float,
|
||||
"swap_used_bytes": integer,
|
||||
"mem_available": float,
|
||||
"mem_available_bytes": integer,
|
||||
"processes": [
|
||||
{
|
||||
"pid": integer,
|
||||
"user": string,
|
||||
"priority": integer,
|
||||
"nice": integer,
|
||||
"virtual_mem": float, # [1]
|
||||
"resident_mem": float, # [1]
|
||||
"shared_mem": float, # [1]
|
||||
"virtual_mem": float,
|
||||
"virtual_mem_unit": string,
|
||||
"virtual_mem_bytes": integer,
|
||||
"resident_mem": float,
|
||||
"resident_mem_unit": string,
|
||||
"resident_mem_bytes": integer,
|
||||
"shared_mem": float,
|
||||
"shared_mem_unit": string,
|
||||
"shared_mem_bytes": integer,
|
||||
"status": string,
|
||||
"percent_cpu": float,
|
||||
"percent_mem": float,
|
||||
@ -80,9 +96,15 @@ Schema:
|
||||
"thread_count": integer,
|
||||
"last_used_processor": integer,
|
||||
"time": string,
|
||||
"swap": float, # [1]
|
||||
"code": float, # [1]
|
||||
"data": float, # [1]
|
||||
"swap": float,
|
||||
"swap_unit": string,
|
||||
"swap_bytes": integer,
|
||||
"code": float,
|
||||
"code_unit": string,
|
||||
"code_bytes": integer
|
||||
"data": float,
|
||||
"data_unit": string,
|
||||
"data_bytes": integer,
|
||||
"major_page_fault_count": integer,
|
||||
"minor_page_fault_count": integer,
|
||||
"dirty_pages_count": integer,
|
||||
@ -101,7 +123,9 @@ Schema:
|
||||
]
|
||||
"major_page_fault_count_delta": integer,
|
||||
"minor_page_fault_count_delta": integer,
|
||||
"used": float, # [1]
|
||||
"used": float,
|
||||
"used_unit": string,
|
||||
"used_bytes": integer,
|
||||
"ipc_namespace_inode": integer,
|
||||
"mount_namespace_inode": integer,
|
||||
"net_namespace_inode": integer,
|
||||
@ -128,9 +152,6 @@ Schema:
|
||||
}
|
||||
}
|
||||
|
||||
[0] Values are in the units output by `top`
|
||||
[1] Unit suffix stripped during float conversion
|
||||
|
||||
Examples:
|
||||
|
||||
$ top -b | jc --top-s
|
||||
@ -153,7 +174,7 @@ from jc.parsers.universal import sparse_table_parse as parse_table
|
||||
|
||||
class info():
|
||||
"""Provides parser metadata (version, author, etc.)"""
|
||||
version = '1.2'
|
||||
version = '1.3'
|
||||
description = '`top -b` command streaming parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
@ -277,12 +298,24 @@ def _process(proc_data: Dict, idx=0, quiet=False) -> Dict:
|
||||
'mem_available', 'virtual_mem', 'resident_mem', 'shared_mem', 'swap', 'code', 'data', 'used'
|
||||
}
|
||||
|
||||
for key in proc_data:
|
||||
bytes_list: Set = {
|
||||
'mem_total', 'mem_free', 'mem_used', 'mem_available', 'mem_buff_cache',
|
||||
'swap_total', 'swap_free', 'swap_used', 'virtual_mem', 'resident_mem',
|
||||
'shared_mem', 'swap', 'code', 'data', 'used'
|
||||
}
|
||||
|
||||
for key in proc_data.copy():
|
||||
# root truncation warnings
|
||||
if isinstance(proc_data[key], str) and proc_data[key].endswith('+') and not quiet:
|
||||
jc.utils.warning_message([f'item[{idx}]["{key}"] was truncated by top'])
|
||||
|
||||
# root int and float conversions
|
||||
if key in bytes_list:
|
||||
if key.startswith('mem_'):
|
||||
proc_data[key + '_bytes'] = jc.utils.convert_size_to_int(proc_data[key] + proc_data['mem_unit'])
|
||||
if key.startswith('swap_'):
|
||||
proc_data[key + '_bytes'] = jc.utils.convert_size_to_int(proc_data[key] + proc_data['swap_unit'])
|
||||
|
||||
if key in int_list:
|
||||
proc_data[key] = jc.utils.convert_to_int(proc_data[key])
|
||||
|
||||
@ -299,7 +332,8 @@ def _process(proc_data: Dict, idx=0, quiet=False) -> Dict:
|
||||
jc.utils.warning_message([f'Unknown field detected at item[{idx}]["processes"]: {old_key}'])
|
||||
|
||||
# cleanup values
|
||||
for key in proc.keys():
|
||||
proc_copy = proc.copy()
|
||||
for key in proc_copy.keys():
|
||||
|
||||
# set dashes to nulls
|
||||
if proc[key] == '-':
|
||||
@ -318,6 +352,13 @@ def _process(proc_data: Dict, idx=0, quiet=False) -> Dict:
|
||||
|
||||
# do int/float conversions for the process objects
|
||||
if proc[key]:
|
||||
if key in bytes_list:
|
||||
if proc[key][-1] not in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'):
|
||||
proc[key + '_unit'] = proc[key][-1]
|
||||
else:
|
||||
proc[key + '_unit'] = 'b'
|
||||
proc[key + '_bytes'] = jc.utils.convert_size_to_int(proc[key], posix_mode=True)
|
||||
|
||||
if key in int_list:
|
||||
proc[key] = jc.utils.convert_to_int(proc[key])
|
||||
|
||||
@ -448,6 +489,7 @@ def parse(
|
||||
line_list = line.split()
|
||||
output_line.update(
|
||||
{
|
||||
'mem_unit': line_list[0],
|
||||
'mem_total': line_list[3],
|
||||
'mem_free': line_list[5],
|
||||
'mem_used': line_list[7],
|
||||
@ -461,6 +503,7 @@ def parse(
|
||||
line_list = line.split()
|
||||
output_line.update(
|
||||
{
|
||||
'swap_unit': line_list[0],
|
||||
'swap_total': line_list[2],
|
||||
'swap_free': line_list[4],
|
||||
'swap_used': line_list[6],
|
||||
@ -489,3 +532,5 @@ def parse(
|
||||
if process_list:
|
||||
output_line['processes'] = parse_table(process_list)
|
||||
yield output_line if raw else _process(output_line, idx=idx, quiet=quiet)
|
||||
|
||||
return None
|
||||
|
Reference in New Issue
Block a user