Created
April 5, 2012 16:59
-
-
Save yyuu/2312514 to your computer and use it in GitHub Desktop.
mon alert plugin to send notice to irc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # | |
| # pyirc2.alert - mon alert plugin to send alerts to irc server | |
| # | |
| from __future__ import with_statement | |
| import contextlib | |
| import irclib | |
| import optparse | |
| import os | |
| import socket | |
| import sys | |
| class Channel(object): | |
| def __init__(self, connection, name): | |
| self.connection = connection | |
| self.name = name | |
| def join(self): | |
| self.connection.join(self.name) | |
| def notice(self, message): | |
| self.connection.notice(self.name, message) | |
| def privmsg(self, message): | |
| self.connection.privmsg(self.name, message) | |
| @contextlib.contextmanager | |
| def connect(server, port, nick, password=None, user=None): | |
| irc = irclib.IRC() | |
| connection = irc.server() | |
| connection.connect(server, port, nick, password, user) | |
| yield(connection) | |
| if connection.connected: | |
| connection.disconnect("bye") | |
| @contextlib.contextmanager | |
| def join(connection, channel): | |
| target = Channel(connection, channel) | |
| target.join() | |
| yield(target) | |
| def main(args): | |
| parser = optparse.OptionParser("usage %prog [OPTIONS]", add_help_option=False) | |
| parser.add_option('-O', '--traptimeout', default=False, action='store_true', dest='traptimeout', help='(if traptimeout)') | |
| parser.add_option('-P', '--password', default=None, dest='password', help='password') | |
| parser.add_option('-S', '--server', default='localhost', dest='server', help='server') | |
| parser.add_option('-T', '--trap', default=False, action='store_true', dest='trap', help='(if trap)') | |
| parser.add_option('-U', '--user', default=None, dest='user', help='user') | |
| parser.add_option('-c', '--channel', default='#mon', dest='channel', help='channel') | |
| parser.add_option('-d', '--detail', default=False, action='store_true', dest='detail', help='detail') | |
| parser.add_option('-g', '--group', default='localhost', dest='group', help='group') | |
| parser.add_option('-h', '--hosts', default='localhost', dest='hosts', help='hosts') | |
| parser.add_option('-n', '--nick', default='mon', dest='nick', help='nick') | |
| parser.add_option('--notice', default=True, action='store_true', dest='notice', help='use notice') | |
| parser.add_option('--privmsg', dest='notice', action='store_false', help='use privmsg') | |
| parser.add_option('-p', '--port', default=6667, type='int', dest='port', help='port') | |
| parser.add_option('-s', '--service', default='unknown', dest='service', help='service') | |
| parser.add_option('-t', '--tmnow', type='int', dest='tmnow', help='tmnow') | |
| parser.add_option('-u', '--upalert', default=False, action='store_true', dest='upalert', help='(if upalert)') | |
| parser.add_option('-v', '--verbose', default=False, action='store_true', dest='verbose', help='show verbose messages') | |
| (options, args) = parser.parse_args(args) | |
| if not options.channel.startswith('#'): | |
| options.channel = '#%s' % (options.channel,) | |
| localhost = socket.getfqdn() | |
| group = socket.getfqdn(options.group) if options.group != 'localhost' else localhost | |
| hosts = [ socket.getfqdn(host) if host != 'localhost' else localhost for host in options.hosts.split() ] | |
| group_is_localhost = group == localhost | |
| hosts_is_localhost = hosts == [ localhost ] | |
| if options.upalert: | |
| if group_is_localhost: | |
| summary = "UPALERT %s/%s is back." % (options.service, group) | |
| else: | |
| summary = "UPALERT %s/%s seems back from %s." % (options.service, group, localhost) | |
| else: | |
| if group_is_localhost: | |
| summary = "ALERT %s/%s is DOWN." % (options.service, group) | |
| else: | |
| summary = "ALERT %s/%s seems DOWN from %s." % (options.service, group, localhost) | |
| with connect(options.server, options.port, options.nick, options.password, options.user) as connection: | |
| with join(connection, options.channel) as channel: | |
| write = channel.notice if options.notice else channel.privmsg | |
| write(summary) | |
| if not hosts_is_localhost: | |
| for host in hosts: | |
| write("* %s" % (host,)) | |
| if __name__ == "__main__": | |
| main(sys.argv) | |
| # vim:set ft=python : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment