From afd4ef53e1d97bf938bc47d35353b140d4a6a9cc Mon Sep 17 00:00:00 2001 From: lukemartinlogan Date: Fri, 29 Mar 2024 15:06:49 -0500 Subject: [PATCH] Monitor parser works at single node --- bin/pymonitor | 8 +++--- jarvis_util/introspect/monitor.py | 42 ++++++++++++++++++------------- test/unit/test_monitor.py | 20 +++++++++++++++ 3 files changed, 48 insertions(+), 22 deletions(-) create mode 100644 test/unit/test_monitor.py diff --git a/bin/pymonitor b/bin/pymonitor index e4232e8..f68ac82 100644 --- a/bin/pymonitor +++ b/bin/pymonitor @@ -20,7 +20,7 @@ Mkdir(parent) path = f'{parent}/{socket.gethostname()}.yaml' def disk_log_to_yaml(cur_time, disk, disk_counter): - return '-' + json.dumps({ + return json.dumps({ 'type': 'DSK', 'time': cur_time, 'disk': disk, @@ -33,7 +33,7 @@ def disk_log_to_yaml(cur_time, disk, disk_counter): }) + '\n' def net_log_to_yaml(cur_time, network_counter): - return '-' + json.dumps({ + return json.dumps({ 'type': 'NET', 'time': cur_time, 'bytes_sent': network_counter.bytes_sent, @@ -47,7 +47,7 @@ def net_log_to_yaml(cur_time, network_counter): }) + '\n' def mem_log_to_yaml(cur_time, mem_usage): - return '-' + json.dumps({ + return json.dumps({ 'type': 'MEM', 'time': cur_time, 'total': mem_usage.total, @@ -65,7 +65,7 @@ def mem_log_to_yaml(cur_time, mem_usage): def cpu_log_to_yaml(cur_time, cpu_usage): - return '-' + json.dumps({ + return json.dumps({ 'type': 'CPU', 'time': cur_time, 'cpu': cpu_usage, diff --git a/jarvis_util/introspect/monitor.py b/jarvis_util/introspect/monitor.py index d6f71af..bdda74e 100644 --- a/jarvis_util/introspect/monitor.py +++ b/jarvis_util/introspect/monitor.py @@ -1,6 +1,7 @@ from jarvis_util.shell.exec import Exec from jarvis_util.serialize.yaml_file import YamlFile import os +import yaml class Callgrind(Exec): def __init__(self, cmd, exec_info=None): @@ -25,24 +26,29 @@ def parse(self): paths = os.listdir(self.monitor_dir) for hostname in paths: path = os.path.join(self.monitor_dir, hostname) - yaml_list = YamlFile(path).load() - for yaml_dict in yaml_list: - if yaml_dict['type'] == 'DSK': - if hostname not in self.disk: - self.disk[hostname] = [] - self.disk[hostname].append(yaml_dict) - elif yaml_dict['type'] == 'NET': - if hostname not in self.net: - self.net[hostname] = [] - self.net[hostname].append(yaml_dict) - elif yaml_dict['type'] == 'MEM': - if hostname not in self.mem: - self.mem[hostname] = [] - self.mem[hostname].append(yaml_dict) - elif yaml_dict['type'] == 'CPU': - if hostname not in self.cpu: - self.cpu[hostname] = [] - self.cpu[hostname].append(yaml_dict) + with open(path, 'r') as fp: + lines = fp.readlines() + for line in lines: + try: + yaml_dict = yaml.load(line, Loader=yaml.FullLoader) + except yaml.YAMLError: + continue + if yaml_dict['type'] == 'DSK': + if hostname not in self.disk: + self.disk[hostname] = [] + self.disk[hostname].append(yaml_dict) + elif yaml_dict['type'] == 'NET': + if hostname not in self.net: + self.net[hostname] = [] + self.net[hostname].append(yaml_dict) + elif yaml_dict['type'] == 'MEM': + if hostname not in self.mem: + self.mem[hostname] = [] + self.mem[hostname].append(yaml_dict) + elif yaml_dict['type'] == 'CPU': + if hostname not in self.cpu: + self.cpu[hostname] = [] + self.cpu[hostname].append(yaml_dict) def avg_memory(self): total = 0 diff --git a/test/unit/test_monitor.py b/test/unit/test_monitor.py new file mode 100644 index 0000000..67abc79 --- /dev/null +++ b/test/unit/test_monitor.py @@ -0,0 +1,20 @@ +from jarvis_util.util.argparse import ArgParse +from jarvis_util.shell.exec import Exec +from jarvis_util.shell.local_exec import LocalExecInfo +from jarvis_util.util.hostfile import Hostfile +from jarvis_util.introspect.system_info import Lsblk, \ + ListFses, FiInfo, Blkid, ResourceGraph, StorageDeviceType +from jarvis_util.util.size_conv import SizeConv +import pathlib +import itertools +import os +from unittest import TestCase +from jarvis_util.introspect.monitor import Monitor, MonitorParser + +class TestSystemInfo(TestCase): + + def test_monitor_parser(self): + parser = MonitorParser(os.path.join(os.environ['HOME'], 'monitor')) + parser.parse() + avg = parser.avg_memory() + print(avg)