123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- # -*- coding: utf-8 -*-
- from __future__ import print_function
- import willie
- monitored_nodes = None
- def setup(bot):
- global monitored_nodes
- # load list of monitored nodes from disk
- monitored_nodes = shelve.open('monitorednodes', writeback=True)
- def shutdown(bot):
- global monitored_nodes
- # store list of monitored nodes to disk
- 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 jeden Knoten der gemonitort wird."""
- notify_target = bot.config.core.owner
- if (not bot.config.ffpb.msg_target is None):
- notify_target = bot.config.ffpb.msg_target
- for node in monitored_nodes:
- added = monitored_nodes[node][0]
- last_status = monitored_nodes[node][1]
- last_check = monitored_nodes[node][2]
- current_status = ffpb_ping(bot=None, target_name=node)
- monitored_nodes[node] = ( added, current_status, 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_check)))
- else:
- bot.msg(notify_target, 'Monitoring: Knoten \'{0}\' DOWN'.format(node))
- @willie.module.commands('monitor')
- def ffpb_monitor(bot, trigger):
- """Monitoring-Funktion des Bots,
- Subkommandos add, del, info und list"""
- # command is for bot admins only
- 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('Da s 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)
- if cmd == "add":
- if node in monitored_nodes:
- bot.say('Knoten \'{0}\' wird bereits gemonitored.'.format(node))
- return
- monitored_nodes[node] = ( trigger.sender, None, None )
- bot.say('Knoten \'{0}\' wird jetzt ganz genau beobachtet.'.format(node))
- return
- 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}\' war mir sowieso egal.'.format(node))
- return
- if cmd == "info":
- if node in monitored_nodes:
- info = monitored_nodes[node]
- bot.say('Knoten \'{0}\' wurde zuletzt {1} gepingt (Ergebnis: {2}) - der Auftrag kam von {3}'.format(node, pretty_date(info[2]) if not info[2] is None else "^W noch nie", info[1], info[0]))
- else:
- bot.say('Knoten \'{0}\' ist nicht im Monitoring.'.format(node))
- return
- if cmd == "list":
- nodes = ""
- for node in monitored_nodes:
- nodes = nodes + " " + node
- bot.say('Monitoring aktiv für:' + nodes)
- return
- if cmd == "help":
- bot.say('Entweder "!monitor list" oder "!monitor {add|del|info} <node>"')
- return
- bot.say('Mit "' + str(cmd) + '" kann ich nix anfangen, probier doch mal "!monitor help".')
|