#!/usr/bin/python from optparse import OptionParser import socket import sys import syslog import re import httplib, urllib def sendtosyslogonerror(msg): """ send msg on syslog then exit """ syslog.syslog(syslog.LOG_ERR,"SENDSMS: error " + msg) print "SENDSMS: error " , msg sys.exit(1) # define the SMS page for sending sms #smsSendPage = '/smsEnvoi.php' smsSendPage = '/smsNumber.php' # define NAGIOS return codes OK=0 WARN=1 CRIT=2 UNKNOWN=3 """ check parameters """ parser = OptionParser() parser.add_option("-L", "--login", dest="login", help="login sms auth") parser.add_option("-p", "--passwd", dest="passwd", help="Passwd sms auth") parser.add_option("-S", "--smsserver", dest="smsserver", help="Sms remote server") parser.add_option("-P", "--port", dest="port", help="SMTP port, default 80",default=80, type="int") parser.add_option("-d", "--debug", dest="debug", action="store_true",default=False,help="activate debug") parser.add_option("-t", "--timeout", dest="timeout", help="Timeout for all socket operation, default 4s",default=4, type="int") parser.add_option("-w", "--minwarnsms", dest="minwarnsms", help="number of free SMS considered warning",default=50, type="int") parser.add_option("-c", "--mincritsms", dest="mincritsms", help="number of free SMS considered critical",default=10, type="int") (options, args) = parser.parse_args() options.auth = True if not options.login or not options.passwd or not options.smsserver: sendtosyslogonerror("Error: missing parameters") params = urllib.urlencode({'userLogin': options.login, 'userPassword': options.passwd, 'domain': options.smsserver, 'path': smsSendPage}) headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} try: conn = httplib.HTTPConnection(options.smsserver,options.port,options.timeout) if options.debug == True: conn.set_debuglevel(1) except httplib.NotConnected: sendtosyslogonerror("Can't connect to server %s" % options.smsserver) try: conn.request("POST", smsSendPage, params, headers) except httplib.HTTPException, e: sendtosyslogonerror("ERROR" + str(e.reason)) except socket.gaierror: sendtosyslogonerror('Connection failed unexpectedly') try: response = conn.getresponse() except httplib.HTTPException, e: sendtosyslogonerror("ERROR" + str(e.reason)) if response.status == 200: RET=OK else: RET=CRIT data = response.read() conn.close() if options.debug == True: print "response status : ",response.status print "response reason : ", response.reason print "data : ", data retcode=re.match(r"(..)(.*)", data) smsret= retcode.group(1) if smsret == "OK": retcount=re.match(r"(\d*)\.*",retcode.group(2)) smscount=int(retcount.group(1)) else: # we are not OK, display error and quit print ("UNKNOWN - %s"% data) sys.exit(UNKNOWN) # check the SMS free number if smscount < options.mincritsms: RET=CRIT print ("CRITICAL - sms free count is below %s with %s" % (options.mincritsms, smscount)) elif smscount < options.minwarnsms: RET=WARN print ("WARNING - sms free count is below %s with %s" % (options.minwarnsms, smscount)) else: print ("OK - sms free count is %s" % smscount) sys.exit(RET)