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 key # Label of the metric.
string val # Value to track. string value # Value of the metric.
string unit # Unit of the value. string unit # Unit of the metric.
float32 err_lvl # Error level of the value. float32 critical # Critical level of the metric.
\ No newline at end of file \ 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 #!/usr/bin/env python
import socket
import rospy import rospy
from monitoring.msg import Monitoring
from monitoring.msg import Metric from monitoring.msg import Metric
...@@ -8,36 +10,37 @@ class Monitor: ...@@ -8,36 +10,37 @@ class Monitor:
self.log = {} # Logging dictionary. Is used to accomplish different publishing modes. self.log = {} # Logging dictionary. Is used to accomplish different publishing modes.
self.pub = rospy.Publisher('metric', Metric, queue_size=1) self.pub = rospy.Publisher('metric', Metric, queue_size=1)
self.mode = mode self.mode = mode
self.metric = Metric() self.monitoring = Monitoring()
self.monitoring.origin = socket.gethostname() + rospy.get_name()
if not freq > 0: if not freq > 0:
rospy.logwarn("Frequency must be greater then 0! Using 1 as frequency!") rospy.logwarn("Frequency must be greater then 0! Using 1 as frequency!")
freq = 1 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): # At the moment the last metric update ist published over and over.
self.metric = Metric() def publish_monitoring(self):
self.pub.publish(self.monitoring)
def publish_metric(self): def update_metric(self, key, value, unit, critical):
self.pub.publish(self.metric)
self.reset_metric()
def update_metric(self, key, val, unit, err_lvl):
def set_metric(): def set_metric():
self.metric.key = key metric = Metric()
self.metric.val = val metric.key = key
self.metric.unit = unit metric.value = value
self.metric.err_lvl = err_lvl metric.unit = unit
metric.critical = critical
self.monitoring.metric = metric
def rst_key(): # Reset the value of a key in the logging dictionary. 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()} self.log[key] = {'num': 0, 'val': 0, 'sum': 0, 'dur': rospy.get_rostime()}
if " " in key: # A key cannot contain whitespace! if " " in key: # A key cannot contain whitespace!
rospy.logwarn("Whitespaces are not allowed in metric keys!") 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: else:
self.reset_metric() # Reset metric.
if key in self.log: if key in self.log:
# Mode 1: Set the last obtained value. # Mode 1: Set the last obtained value.
if self.mode == 1: if self.mode == 1:
...@@ -46,32 +49,32 @@ class Monitor: ...@@ -46,32 +49,32 @@ class Monitor:
# Mode 2: Remember and set the minimum value, # Mode 2: Remember and set the minimum value,
# obtained after mode selection. # obtained after mode selection.
elif self.mode == 2: elif self.mode == 2:
if val < self.log[key]['val']: if value < self.log[key]['val']:
self.log[key]['val'] = val self.log[key]['val'] = value
else: else:
val = self.log[key]['val'] value = self.log[key]['val']
set_metric() set_metric()
# Mode 3: Remember and set the maximum value, # Mode 3: Remember and set the maximum value,
# obtained after mode selection. # obtained after mode selection.
elif self.mode == 3: elif self.mode == 3:
if val > self.log[key]['val']: if value > self.log[key]['val']:
self.log[key]['val'] = val self.log[key]['val'] = value
else: else:
val = self.log[key]['val'] value = self.log[key]['val']
set_metric() set_metric()
# Mode 4: Set the average obtained value over five # Mode 4: Set the average obtained value over five
# seconds. # or more seconds.
elif self.mode == 4: elif self.mode == 4:
dur = rospy.get_rostime() - self.log[key]['dur'] duration = rospy.get_rostime() - self.log[key]['dur']
if dur < rospy.Duration(5): if duration < rospy.Duration(5):
self.log[key]['num'] += 1 self.log[key]['num'] += 1
self.log[key]['sum'] += val self.log[key]['sum'] += value
else: 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() set_metric()
rst_key() rst_key()
else: else:
......
...@@ -2,3 +2,14 @@ ...@@ -2,3 +2,14 @@
import rospy import rospy
from monitoring.msg import Metric 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