From 60b05b8cae588a15d246110622764479396a0e66 Mon Sep 17 00:00:00 2001 From: bav6096 <benedikt.deike@informatik.uni-hamburg.de> Date: Sat, 30 Oct 2021 01:41:06 +0200 Subject: [PATCH] updated monitor --- CMakeLists.txt | 2 +- msg/Metric.msg | 8 +-- msg/Monitoring.msg | 3 -- scripts/monitor.py | 121 +++++++++++++++++++------------------------- scripts/watchdog.py | 4 ++ 5 files changed, 60 insertions(+), 78 deletions(-) delete mode 100644 msg/Monitoring.msg diff --git a/CMakeLists.txt b/CMakeLists.txt index a5e0db0..fe7a707 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,7 +49,7 @@ find_package(catkin REQUIRED COMPONENTS ## Generate messages in the 'msg' folder add_message_files( FILES - Monitoring.msg + Metric.msg ) ## Generate services in the 'srv' folder diff --git a/msg/Metric.msg b/msg/Metric.msg index c583db8..8ae1810 100644 --- a/msg/Metric.msg +++ b/msg/Metric.msg @@ -1,4 +1,4 @@ -string key # Label of the value. -string val # Value to track. -string unit # Unit of the value. -float32 err # Error level of the value. \ No newline at end of file +string key # Label of the value. +string val # Value to track. +string unit # Unit of the value. +float32 err_lvl # Error level of the value. \ No newline at end of file diff --git a/msg/Monitoring.msg b/msg/Monitoring.msg deleted file mode 100644 index 0c0d06e..0000000 --- a/msg/Monitoring.msg +++ /dev/null @@ -1,3 +0,0 @@ -string name # Name and origin of the monitor message. -string desc # Text describing the monitor message. -Metric metric # Values to monitor. \ No newline at end of file diff --git a/scripts/monitor.py b/scripts/monitor.py index 2e396d2..5b78558 100755 --- a/scripts/monitor.py +++ b/scripts/monitor.py @@ -1,98 +1,79 @@ #!/usr/bin/env python -import socket import rospy -from monitoring.msg import Monitoring -from monitoring.msg import Metic +from monitoring.msg import Metric class Monitor: - def __init__(self, desc, freq=1, auto=True): - self.host = socket.gethostname() - self.node = rospy.get_name() - self.name = self.host + self.node - self.desc = desc + def __init__(self, mode, freq): + self.log = {} # Logging dictionary. Is used to accomplish different publishing modes. + self.pub = rospy.Publisher('metric', Metric, queue_size=1) + self.mode = mode + self.metric = Metric() if not freq > 0: - print("Frequency must be greater than 0!") - quit() + rospy.logwarn("Frequency must be greater then 0! Using 1 as frequency!") + freq = 1 - if auto: - self.timer = rospy.Timer(rospy.Duration(1.0 / freq), self.pub_monitoring) + self.timer = rospy.Timer(rospy.Duration(1.0 / freq), self.publish_metric) - self.log = {} - self.pub = rospy.Publisher('monitoring', Monitoring, queue_size=1) - self.pub_times = 0 + def reset_metric(self): + self.metric = Metric() - self.monitoring = Monitoring() - self.monitoring.name = self.name - self.monitoring.desc = self.desc + def publish_metric(self): + self.pub.publish(self.metric) + self.reset_metric() - def rst_log_key(self, key): - self.log[key] = {'num': 0, 'val': 0, 'sum': 0, 'dur': rospy.get_rostime()} + def update_metric(self, key, val, unit, err_lvl): + def set_metric(): + self.metric.key = key + self.metric.val = val + self.metric.unit = unit + self.metric.err_lvl = err_lvl + + def rst_key(): # Reset the value of a key in the logging dictionary. + self.log[key] = {'num': 0, 'val': 0, 'sum': 0, 'dur': rospy.get_rostime()} - def add_log_val(self, key, val, unit, err, mode=2): if " " in key: # A key cannot contain whitespace! - rospy.logwarn("[%s] whitespaces are not allowed in monitoring keys!", self.node) + rospy.logwarn("Whitespaces are not allowed in metric keys!") else: - if key in self.log: - # Mode 1: Write the first obtained value into the - # published message. - if mode == 1 and self.pub_times == 0: - self.set_metric(key, val, unit, err) - self.pub_times = 1 + self.reset_metric() # Reset metric. - # Mode 2: Write the last obtained value into the - # published message. - elif mode == 2: - self.set_metric(key, val, unit, err) + if key in self.log: + # Mode 1: Set the last obtained value. + if self.mode == 1: + set_metric() - # Mode 3: Remember and write the minimum obtained - # value, after mode selection, into the - # published message. - elif mode == 3: + # Mode 2: Remember and set the minimum value, + # obtained after mode selection. + elif self.mode == 2: if val < self.log[key]['val']: self.log[key]['val'] = val + else: + val = self.log[key]['val'] - self.set_metric(key, self.log[key]['val'], unit, err) + set_metric() - # Mode 4: Remember and write the maximum obtained - # value, after mode selection, into the - # published message. - elif mode == 4: + # Mode 3: Remember and set the maximum value, + # obtained after mode selection. + elif self.mode == 3: if val > self.log[key]['val']: self.log[key]['val'] = val + else: + val = self.log[key]['val'] - self.set_metric(key, self.log[key]['val'], unit, err) + set_metric() - # Mode 5: Write the average value over five seconds - # into the published message. - elif mode == 5: + # Mode 4: Set the average obtained value over five + # seconds. + elif self.mode == 4: dur = rospy.get_rostime() - self.log[key]['dur'] if dur < rospy.Duration(5): - self.log[self.name][key]['num'] += 1 - self.log[self.name][key]['sum'] += val + self.log[key]['num'] += 1 + self.log[key]['sum'] += val else: - quotient = self.log[key]['sum'] / (self.log[key]['num'] + 0.001) - self.set_metric(key, quotient, unit, err) - self.rst_log_key(key) + val = self.log[key]['sum'] / (self.log[key]['num'] + 0.001) + set_metric() + rst_key() else: - self.rst_log_key(key) # Add key to the logging dictionary. - self.set_metric(key, val, unit, err) - - def set_metric(self, key, val, unit, err): - metric = Metic() - metric.key = str(key) - metric.val = str(val) - metric.unit = str(unit) - metric.err = err - self.monitoring.metric = metric - - def rst_monitoring(self): - self.monitoring = Monitoring() - self.monitoring.name = self.name - self.monitoring.desc = self.desc - - def pub_monitoring(self): - self.pub.publish(self.monitoring) - self.rst_monitoring() - + rst_key() # Add key to the logging dictionary. + set_metric() diff --git a/scripts/watchdog.py b/scripts/watchdog.py index e69de29..8c8599c 100755 --- a/scripts/watchdog.py +++ b/scripts/watchdog.py @@ -0,0 +1,4 @@ +#!/usr/bin/env python +import rospy +from monitoring.msg import Metric + -- GitLab