Skip to content

Commit

Permalink
Monitor parser works at single node
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemartinlogan committed Mar 29, 2024
1 parent 8aeea0c commit afd4ef5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 22 deletions.
8 changes: 4 additions & 4 deletions bin/pymonitor
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
42 changes: 24 additions & 18 deletions jarvis_util/introspect/monitor.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions test/unit/test_monitor.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit afd4ef5

Please sign in to comment.