diff --git a/msg/Metric.msg b/msg/Metric.msg index 8ae181039884d6daffc53e615023b4b9c0249f62..15c828ab5785563323de32f7ad3f9efd322470d1 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_lvl # Error level of the value. \ No newline at end of file +string key # Label of the metric. +string value # Value of the metric. +string unit # Unit of the metric. +float32 critical # Critical level of the metric. \ No newline at end of file diff --git a/msg/Monitoring.msg b/msg/Monitoring.msg new file mode 100644 index 0000000000000000000000000000000000000000..4d9d229acf5f0a30e06115bcae2b66d808840315 --- /dev/null +++ b/msg/Monitoring.msg @@ -0,0 +1,2 @@ +string origin # Origin of the monitoring message. +Metric metric # Monitored metric. \ No newline at end of file diff --git a/scripts/monitor.py b/scripts/monitor.py index 5b78558736af10db10d67b1a56906786e0a9489b..cb0120ca0f344ce19c4f6f72632a86f4e967be08 100755 --- a/scripts/monitor.py +++ b/scripts/monitor.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +import socket import rospy +from monitoring.msg import Monitoring from monitoring.msg import Metric @@ -8,36 +10,37 @@ class Monitor: 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() + self.monitoring = Monitoring() + self.monitoring.origin = socket.gethostname() + rospy.get_name() if not freq > 0: rospy.logwarn("Frequency must be greater then 0! Using 1 as frequency!") freq = 1 - self.timer = rospy.Timer(rospy.Duration(1.0 / freq), self.publish_metric) + self.timer = rospy.Timer(rospy.Duration(1.0 / freq), self.publish_monitoring) - def reset_metric(self): - self.metric = Metric() + # At the moment the last metric update ist published over and over. + def publish_monitoring(self): + self.pub.publish(self.monitoring) - def publish_metric(self): - self.pub.publish(self.metric) - self.reset_metric() - - def update_metric(self, key, val, unit, err_lvl): + def update_metric(self, key, value, unit, critical): def set_metric(): - self.metric.key = key - self.metric.val = val - self.metric.unit = unit - self.metric.err_lvl = err_lvl + metric = Metric() + metric.key = key + metric.value = value + metric.unit = unit + metric.critical = critical + + self.monitoring.metric = metric 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()} if " " in key: # A key cannot contain whitespace! rospy.logwarn("Whitespaces are not allowed in metric keys!") + elif critical > 1 or critical < 0: + rospy.logwarn("Critical level must be between 1 and 0!") else: - self.reset_metric() # Reset metric. - if key in self.log: # Mode 1: Set the last obtained value. if self.mode == 1: @@ -46,32 +49,32 @@ class Monitor: # 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 + if value < self.log[key]['val']: + self.log[key]['val'] = value else: - val = self.log[key]['val'] + value = self.log[key]['val'] set_metric() # 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 + if value > self.log[key]['val']: + self.log[key]['val'] = value else: - val = self.log[key]['val'] + value = self.log[key]['val'] set_metric() # Mode 4: Set the average obtained value over five - # seconds. + # or more seconds. elif self.mode == 4: - dur = rospy.get_rostime() - self.log[key]['dur'] - if dur < rospy.Duration(5): + duration = rospy.get_rostime() - self.log[key]['dur'] + if duration < rospy.Duration(5): self.log[key]['num'] += 1 - self.log[key]['sum'] += val + self.log[key]['sum'] += value else: - val = self.log[key]['sum'] / (self.log[key]['num'] + 0.001) + value = self.log[key]['sum'] / (self.log[key]['num'] + 0.001) set_metric() rst_key() else: diff --git a/scripts/watchdog.py b/scripts/watchdog.py index 8c8599ce67d231ff6e866aae6370fb2d95f4732a..66758bda7cae21e8e48ae46dc7f13527080df452 100755 --- a/scripts/watchdog.py +++ b/scripts/watchdog.py @@ -2,3 +2,14 @@ import rospy from monitoring.msg import Metric +class Watchdog: + def __init__(self): + self.state = 0 + self.observing = {} + rospy.init_node("watchdog") + rospy.Subscriber("metric", Metric, callback) + + def callback(self, monitoring): + + if monitoring.origin in self.observing: + self.observing[metric] \ No newline at end of file