restrict sms in nagios / icinga

Im using nagios as primary monitoring tool. And to get alerts we use an sms gateway. The problem is that sometimes when we work we bring down and server and we get so many sms from icinga that you trow away you phone. So for bringing the sms cost down and to have not so many sms to you phone i build a small email blocking script. This will take the address of the sms and only send one sms / email every 5 min (can be set to anything).

We hook this small script up to our icinga sms sender and now we get one sms every 5 min and we don’t get flooded. If icinga is sending an sms within the five minutes that alerts is redirected to an gmail inbox. And all us sysop have that gmail on our phones.

the python script

#!/usr/bin/env python
# Mattias Hemmingsson
# Script for rescricting nagios messages
# Only send one sms / email and then paus for some time.
# This for not flouding sms system while system reboot 
import csv
import time
import datetime
import os.path, time
from time import gmtime, strftime, mktime
import smtplib
import sys
def send_email(to,message):
 Sending the email.
sender = ''
 smtpObj = smtplib.SMTP('localhost')
 smtpObj.sendmail(sender, to, message) 
 print "Successfully sent email"
 except SMTPException:
 print "Error: unable to send email"
def save_to_file(to,message,host):
 Saves the oncall to file
 f = open("/opt/tmp/"+to+".txt", "w")
def is_message_sent(to,message,host):
 Cheks if teh message has bean sent before
 #print strftime("%Y-%m-%d %H:%M:%S",os.path.getmtime("/tmp/"+ to + '.txt'))
 #print time.mktime(time.gmtime())
 if os.path.isfile("/opt/tmp/"+ to + '.txt'): 
 with open("/opt/tmp/"+to + '.txt', 'r') as f:
 diffrent = time.time() - os.path.getmtime("/opt/tmp/" + to + '.txt')
 print diffrent
 reader = csv.reader(f)
 for row in reader:
 if diffrent < 900:
 return True
 return False
 except IOError:
 return False
 return False
def incomming(to,message,host):
 Incomming messages
 #Has en sms bean send the latest our
 if is_message_sent(to,message,host):
 print "Sending to gmail"
 print "Seding to sms"
 #Sending to sms
 #Saving message to file
if len(sys.argv) ==5:
 args = 'Argument List:', str(sys.argv)
 headers = ["from:",
 "subject: "+ sys.argv[1],
 "to: " + sys.argv[4],
 "mime-version: 1.0",
 "content-type: text/html"]
 headers = "\r\n".join(headers)
messages="""Alert {0} has state {1} {2}
 incomming(sys.argv[4],headers+"\r\n\r\n"+ messages,sys.argv[2])
 print "Error number of arguments please use './ hoststate hostalias message contactemail'"



The icinga command for sending the alert.


define command{
 command_name notify-host-by-sms
define command{
 command_name notify-service-by-sms


set up so that the script and tmp folder match you server setup.