Browse Source

split monitoring into seperate module

Helge Jung 9 years ago
parent
commit
0cbb57f38a
2 changed files with 131 additions and 114 deletions
  1. 2 114
      modules/ffpb.py
  2. 129 0
      modules/ffpb_monitoring.py

+ 2 - 114
modules/ffpb.py

@@ -24,7 +24,6 @@ import threading
 
 msgserver = None
 peers_repo = None
-monitored_nodes = None
 highscores = None
 
 nodeaccess = None
@@ -77,7 +76,7 @@ class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
 	pass
 
 def setup(bot):
-	global msgserver, peers_repo, alfred_method, highscores, monitored_nodes, nodeaccess
+	global msgserver, peers_repo, alfred_method, highscores, nodeaccess
 
 	# signal begin of setup routine
 	bot.memory['ffpb_in_setup'] = True
@@ -91,9 +90,6 @@ def setup(bot):
 		highscores['clients'] = 0
 		highscores['clients_ts'] = time.time()
 
-	# load list of monitored nodes and their last status from disk
-	monitored_nodes = shelve.open('nodes.monitored', writeback=True)
-
 	# load list of seen nodes from disk
 	seen_nodes = shelve.open('nodes.seen', writeback=True)
 	bot.memory['seen_nodes'] = seen_nodes
@@ -135,7 +131,7 @@ def setup(bot):
 	bot.memory['ffpb_in_setup'] = False
 
 def shutdown(bot):
-	global msgserver, highscores, monitored_nodes, nodeaccess
+	global msgserver, highscores, nodeaccess
 
 	# store highscores
 	if not highscores is None:
@@ -143,12 +139,6 @@ def shutdown(bot):
 		highscores.close()
 		highscores = None
 
-	# store monitored nodes
-	if not monitored_nodes is None:
-		monitored_nodes.sync()
-		monitored_nodes.close()
-		monitored_nodes = None
-
 	# store node acl
 	if not nodeaccess is None:
 		nodeaccess.sync()
@@ -934,108 +924,6 @@ def ffpb_ping(bot, trigger=None, target_name=None):
 		if not bot is None: bot.say('Uh oh, irgendwas ist kaputt. Chef, ping result = ' + str(result) + ' - darf ich das essen?')
 		return None
 
-@willie.module.interval(3*60)
-def ffpb_monitor_ping(bot):
-	"""Ping each node currently under surveillance."""
-
-	# determine where-to to send alerts
-	notify_target = bot.config.core.owner
-	if (not bot.config.ffpb.msg_target is None):
-		notify_target = bot.config.ffpb.msg_target
-
-	# check each node under surveillance
-	for node in monitored_nodes:
-		mon = monitored_nodes[node]
-		added = mon['added']
-		last_status = mon['status']
-		last_check = mon['last_check']
-		last_success = mon['last_success']
-
-		current_status = ffpb_ping(bot=None, target_name=node)
-		if current_status is None: current_status = False
-		mon['status'] = current_status
-		mon['last_check'] = time.time()
-		if current_status == True: mon['last_success'] = time.time()
-		print("Monitoring ({0}) {1} (last: {2} at {3})".format(node, current_status, last_status, time.strftime('%Y-%m-%d %H:%M', time.localtime(last_check))))
-		if last_status != current_status and (last_status or current_status):
-			if last_check is None:
-				# erster Check, keine Ausgabe
-				continue
-
-			if current_status == True:
-				bot.msg(notify_target, 'Monitoring: Knoten \'{0}\' pingt wieder (zuletzt {1})'.format(node, pretty_date(last_success) if not last_success is None else '[nie]'))
-			else:
-				bot.msg(notify_target, 'Monitoring: Knoten \'{0}\' DOWN'.format(node))
-
-@willie.module.commands('monitor')
-def ffpb_monitor(bot, trigger):
-	"""Monitoring capability of the bot, try subcommands add, del, info and list."""
-
-	# command is restricted to bot admins
-	if not trigger.admin:
-		bot.say('Ich ping hier nicht für jeden durch die Weltgeschichte.')
-		return
-
-	# ensure the user gave arguments (group 2 is the concatenation of all following groups)
-	if trigger.group(2) is None or len(trigger.group(2)) == 0:
-		bot.say('Das Monitoring sagt du hast doofe Ohren.')
-		return
-
-	# read additional arguments
-	cmd = trigger.group(3)
-	node = trigger.group(4)
-	if not node is None: node = str(node)
-
-	# subcommand 'add': add a node to monitoring
-	if cmd == "add":
-		if node in monitored_nodes:
-			bot.say('Knoten \'{0}\' wird bereits gemonitored.'.format(node))
-			return
-
-		monitored_nodes[node] = {
-			'added': trigger.sender, 
-			'status': None,
-			'last_check': None,
-			'last_success': None,
-		}
-
-		bot.say('Knoten \'{0}\' wird jetzt ganz genau beobachtet.'.format(node))
-		return
-
-	# subcommand 'del': remote a node from monitoring
-	if cmd == "del":
-		if not node in monitored_nodes:
-			bot.say('Knoten \'{0}\' war gar nicht im Monitoring?!?'.format(node))
-			return
-		del monitored_nodes[node]
-		bot.say('Okidoki, \'{0}\' lasse ich jetzt links liegen.'.format(node))
-		return
-
-	# subcommand 'info': monitoring status of a node
-	if cmd == "info":
-		if node in monitored_nodes:
-			info = monitored_nodes[node]
-			bot.say('Knoten \'{0}\' wurde zuletzt {1} gepingt (Ergebnis: {2})'.format(node, pretty_date(info['last_check']) if not info['last_check'] is None else "^W noch nie", info['status']))
-		else:
-			bot.say('Knoten \'{0}\' ist nicht im Monitoring.'.format(node))
-		return
-
-	# subcommand 'list': enumerate all monitored nodes
-	if cmd == "list":
-		nodes = ""
-		for node in monitored_nodes:
-			nodes = nodes + " " + node
-		bot.say('Monitoring aktiv für:' + nodes)
-		return
-
-	# subcommand 'help': give some hints what the user can do
-	if cmd == "help":
-		bot.say('Entweder "!monitor list" oder "!monitor {add|del|info} <node>"')
-		return
-
-	# no valid subcommand given: complain
-	bot.say('Mit "' + str(cmd) + '" kann ich nix anfangen, probier doch mal "!monitor help".')
-
 @willie.module.commands('providers')
 def ffpb_providers(bot, trigger):
 	"""Fetch the top 5 providers from BATCAVE."""

+ 129 - 0
modules/ffpb_monitoring.py

@@ -0,0 +1,129 @@
+# -*- coding: utf-8 -*-
+from __future__ import print_function
+import willie
+
+import datetime
+import time
+
+from ffpb import ffpb_ping
+
+monitored_nodes = None
+
+def setup(bot):
+	global monitored_nodes
+
+	# load list of monitored nodes and their last status from disk
+	monitored_nodes = shelve.open('nodes.monitored', writeback=True)
+
+def shutdown(bot):
+	global monitored_nodes
+
+	# store monitored nodes
+	if not monitored_nodes is None:
+		monitored_nodes.sync()
+		monitored_nodes.close()
+		monitored_nodes = None
+
+@willie.module.interval(3*60)
+def ffpb_monitor_ping(bot):
+	"""Ping each node currently under surveillance."""
+
+	# determine where-to to send alerts
+	notify_target = bot.config.core.owner
+	if (not bot.config.ffpb.msg_target is None):
+		notify_target = bot.config.ffpb.msg_target
+
+	# check each node under surveillance
+	for node in monitored_nodes:
+		mon = monitored_nodes[node]
+		added = mon['added']
+		last_status = mon['status']
+		last_check = mon['last_check']
+		last_success = mon['last_success']
+
+		current_status = ffpb_ping(bot=None, target_name=node)
+		if current_status is None: current_status = False
+		mon['status'] = current_status
+		mon['last_check'] = time.time()
+		if current_status == True: mon['last_success'] = time.time()
+		print("Monitoring ({0}) {1} (last: {2} at {3})".format(node, current_status, last_status, time.strftime('%Y-%m-%d %H:%M', time.localtime(last_check))))
+		if last_status != current_status and (last_status or current_status):
+			if last_check is None:
+				# erster Check, keine Ausgabe
+				continue
+
+			if current_status == True:
+				bot.msg(notify_target, 'Monitoring: Knoten \'{0}\' pingt wieder (zuletzt {1})'.format(node, pretty_date(last_success) if not last_success is None else '[nie]'))
+			else:
+				bot.msg(notify_target, 'Monitoring: Knoten \'{0}\' DOWN'.format(node))
+
+@willie.module.commands('monitor')
+def ffpb_monitor(bot, trigger):
+	"""Monitoring capability of the bot, try subcommands add, del, info and list."""
+
+	# command is restricted to bot admins
+	if not trigger.admin:
+		bot.say('Ich ping hier nicht für jeden durch die Weltgeschichte.')
+		return
+
+	# ensure the user gave arguments (group 2 is the concatenation of all following groups)
+	if trigger.group(2) is None or len(trigger.group(2)) == 0:
+		bot.say('Das Monitoring sagt du hast doofe Ohren.')
+		return
+
+	# read additional arguments
+	cmd = trigger.group(3)
+	node = trigger.group(4)
+	if not node is None: node = str(node)
+
+	# subcommand 'add': add a node to monitoring
+	if cmd == "add":
+		if node in monitored_nodes:
+			bot.say('Knoten \'{0}\' wird bereits gemonitored.'.format(node))
+			return
+
+		monitored_nodes[node] = {
+			'added': trigger.sender, 
+			'status': None,
+			'last_check': None,
+			'last_success': None,
+		}
+
+		bot.say('Knoten \'{0}\' wird jetzt ganz genau beobachtet.'.format(node))
+		return
+
+	# subcommand 'del': remote a node from monitoring
+	if cmd == "del":
+		if not node in monitored_nodes:
+			bot.say('Knoten \'{0}\' war gar nicht im Monitoring?!?'.format(node))
+			return
+		del monitored_nodes[node]
+		bot.say('Okidoki, \'{0}\' lasse ich jetzt links liegen.'.format(node))
+		return
+
+	# subcommand 'info': monitoring status of a node
+	if cmd == "info":
+		if node in monitored_nodes:
+			info = monitored_nodes[node]
+			bot.say('Knoten \'{0}\' wurde zuletzt {1} gepingt (Ergebnis: {2})'.format(node, pretty_date(info['last_check']) if not info['last_check'] is None else "^W noch nie", info['status']))
+		else:
+			bot.say('Knoten \'{0}\' ist nicht im Monitoring.'.format(node))
+		return
+
+	# subcommand 'list': enumerate all monitored nodes
+	if cmd == "list":
+		nodes = ""
+		for node in monitored_nodes:
+			nodes = nodes + " " + node
+		bot.say('Monitoring aktiv für:' + nodes)
+		return
+
+	# subcommand 'help': give some hints what the user can do
+	if cmd == "help":
+		bot.say('Entweder "!monitor list" oder "!monitor {add|del|info} <node>"')
+		return
+
+	# no valid subcommand given: complain
+	bot.say('Mit "' + str(cmd) + '" kann ich nix anfangen, probier doch mal "!monitor help".')
+
+