Skip to content
Snippets Groups Projects
Commit 0ebc7f43 authored by bav6096's avatar bav6096
Browse files

updated message structure

parent 60b05b8c
No related branches found
No related tags found
No related merge requests found
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
string origin # Origin of the monitoring message.
Metric metric # Monitored metric.
\ No newline at end of file
#!/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:
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment