Skip to content

Commit

Permalink
Initial setup with distfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Tucker committed Oct 18, 2011
0 parents commit 36b3060
Show file tree
Hide file tree
Showing 10 changed files with 1,097 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
*.swp
*.pyc
35 changes: 35 additions & 0 deletions MozillaIRCPager.py
@@ -0,0 +1,35 @@
import subprocess
from MozillaIRCPager_settings import *
from settings import logger

class MozillaIRCPager:
def __init__(self, connection):
self.PAGE_SCRIPT = PAGE_SCRIPT
self.message_commands = []
self.build_regex_list()

def build_regex_list(self):
self.message_commands.append({'regex':'^page\s+([A-Za-z][_A-Za-z0-9]+?)\s+(.+)\s*$', 'callback':self.page,})

###Default entry point for each plugin. Simply returns a regex and which static method to call upon matching the regex
def return_plugins(self):
return self.message_commands

def page(self, event, message, options):
should_page = False
recipient = options.group(1)
message = options.group(2)

##Check that we have a valid message and recipient and set should_page to true
if message is not None and recipient is not None:
should_page = True
##If we should page, than page. If not set the return code to a non-zero value so we can display a message to the caller
if should_page is True:
ret = subprocess.call([self.PAGE_SCRIPT, recipient, message])
else:
ret = -1

if ret is 0:
return event.target, "%s: %s has been paged" % (event.source, recipient)
else:
return event.target, "%s: %s could not be paged" % (event.source, recipient)
1 change: 1 addition & 0 deletions MozillaIRCPager_settings.py-dist
@@ -0,0 +1 @@
PAGE_SCRIPT = "page.pl"
626 changes: 626 additions & 0 deletions MozillaNagiosStatus.py

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions MozillaNagiosStatus_settings.py-dist
@@ -0,0 +1,16 @@
ACKABLE_LIST = []
LIST_OFFSET 100
NAGIOS_CMD = "<path_to_nagios_cmd_file>"
NAGIOS_LOG = "<path_to_nagios_log_file>"
ONCALL_FILE = "<path_to_oncall_file"
SERVICE_OUTPUT_LIMIT = 15
CHANNELS = [
{'name':'#channel-name',},
]
STATUS_FILE = "status.dat"
LIST_SIZE = 100
CHANNEL_GROUPS = {
'nagioscontact':'#channel_for_alert',
}
DEFAULT_CHANNEL_GROUP = '#default'

68 changes: 68 additions & 0 deletions NagiosLogLine.py
@@ -0,0 +1,68 @@
import re
class NagiosLogLine:
def __init__(self, line):
self.is_service = False
self.line = line.strip()
self.notification_list = []
self.notification_recipient = None
self.notification_type = None
self.host = None
self.service = None
self.message = None
self._is_notification()

if self.is_notification:
self.notification_type = self._get_notification_type()
self._build_notification_list()
self.notification_recipient = self._get_notification_recipient()
self.host = self._get_host()
self.service = self._get_service()
self.state = self._get_state()
self.message = self._get_message()

def _get_host(self):
return self.notification_list[1]

def _get_service(self):
if self.is_service:
self.is_service = True
return self.notification_list[2]
else:
return None

def _get_state(self):
if self.is_service:
return self.notification_list[3]
else:
return self.notification_list[2]

def _get_message(self):
if self.is_service:
return self.notification_list[5]
else:
return self.notification_list[4]

def _is_notification(self):
m = re.search("^\[\d+\]\s(HOST|SERVICE) NOTIFICATION:", self.line)
if m:
self.is_notification = True
else:
self.is_notification = False

def _build_notification_list(self):
m = re.search("^\[\d+\]\s(HOST|SERVICE) NOTIFICATION: ((?:sysalertsonly|guest|servicesalertslist|sysalertslist|buildteam|dougt|camino|seamonkey|tdsmirrors|sumo-dev|socorroalertlist|metrics|laura);(.*))$", self.line)
self.notification_list = m.group(2).split(";")

def _get_notification_recipient(self):
return self.notification_list[0]

def _get_notification_type(self):
m = re.search("^\[\d+\]\s(HOST|SERVICE) NOTIFICATION:", self.line)
if m:
if m.group(1) == 'HOST':
return 'HOST'
elif m.group(1) == 'SERVICE':
self.is_service = True
return 'SERVICE'
else:
return False
59 changes: 59 additions & 0 deletions nagios-bot.py
@@ -0,0 +1,59 @@
#!/usr/bin/python2.6
import sys

from ircutils import bot
import re
import os
from settings import *
import subprocess
from MozillaIRCPager import MozillaIRCPager
from MozillaNagiosStatus import MozillaNagiosStatus
class NagiosBot(bot.SimpleBot):
my_nick = ''
to_me = False
message = ''
### message_commands is a list of dictionary objects. The regex object is the regex to match, the function object is the function name to call at a match

plugins = [
{'plugin':MozillaIRCPager},
{'plugin':MozillaNagiosStatus},
]
message_commands = []
def load_plugins(self):
self.message_commands.append({'class':NagiosBot, 'regex':'help$', 'function':'print_help'})
for plugin in self.plugins:
plugin = plugin['plugin'](self)
for mc in plugin.return_plugins():
self.message_commands.append(mc)
def on_channel_message(self, event):
if re.search('^%s[,: ]' % self.bot_name, event.message):
self.message = re.sub('^%s[,: ]+' % self.bot_name, '', event.message).strip()
_is_found = False
for message_command in self.message_commands:
if _is_found is False:
m = re.search(message_command['regex'], self.message)
if m is not None:
_is_found = True
try:
target, message = message_command['callback'](event, event.message, m)
self.send_message(target, message)
except Exception, e:
self.send_message(event.target, "%s: %s From Exception I'm sorry but I don't understand your command" % (e, event.source) )
if _is_found is False:
self.send_message(event.target, "%s: I'm sorry but I don't understand your command" % (event.source) )

@staticmethod
def print_help(conn, event, options):
messages = []
messages.append("page <id> (Optional) <recipient> (Required) <message> (Reqired)")
print event.target
for message in messages:
conn.send_message(event.target, message)


if __name__ == "__main__":
nagios_bot = NagiosBot(bot_name)
nagios_bot.bot_name = bot_name
nagios_bot.connect(server, port=port, use_ssl=use_ssl, channel = channels, ssl_options=ssl_options)
nagios_bot.load_plugins()
nagios_bot.start()
12 changes: 12 additions & 0 deletions settings.py-dist
@@ -0,0 +1,12 @@
port = 6667
use_ssl = False
bot_name = '<nagios-bot_name>'
server = 'irc.yourhost.com'
channels = [
'#channelhere password',
]
ssl_options = {'do_handshake_on_connect':False, 'cert_reqs':'None', 'server_side':False}
try:
from settings_local.py import *
except:
pass
27 changes: 27 additions & 0 deletions testMozillaIRCPager.py
@@ -0,0 +1,27 @@
#!/usr/bin/python
from mock import Mock
import unittest
from MozillaIRCPager import MozillaIRCPager
import re
from settings import *
class MozillaNagiosStatusTest(unittest.TestCase):
tc = None

def setUp(self):
self.event = Mock()
self.event.source = 'rtucker'
self.event.target = '#sysadmins'
self.connection = Mock()
self.tc = MozillaIRCPager(self.connection)

def test_get_page_plugin(self):
plugins = self.tc.return_plugins()
self.assertEqual('^page\\s+([A-Za-z][_A-Za-z0-9]+?)\\s+(.+)\\s*$',plugins[0]['regex'])

def test_correct_page(self):
page_message = 'page %s this is a test message' % (self.event.source)
plugins = self.tc.return_plugins()
m = re.search(plugins[0]['regex'], page_message)
target, message = self.tc.page(self.event, page_message, m)
self.assertEqual(self.event.target, target)
self.assertEqual(message, '%s: %s has been paged' % (self.event.source, self.event.source) )

0 comments on commit 36b3060

Please sign in to comment.