#!/usr/bin/python import os, sys, socket, string from optparse import * """ Check_peer_status plugin for Nagios Copyright (c) 2013 Andrea Zorzetto Version 0.2.2 updated at 21/11/2013 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. """ # Process the command line... parser = OptionParser(usage="Check_peer_status [options]", version="%prog 0.1.0") parser.set_defaults(hostname='127.0.0.1') parser.set_defaults(port=5038) parser.set_defaults(peer="") parser.add_option("-u", "--username", action="store", dest="user", help="username for AMI.") parser.add_option("-s", "--secret", action="store", dest="secret", help="password for AMI.") parser.add_option("-H", "--host", action="store", dest="hostname", help="the host to connect to. The default is localhost.") parser.add_option("-P", "--port", action="store", dest="port", help="the port to contact. Default is 5038.") parser.add_option("-t", "--type", action="store", dest="type", help="sip or iax are allowed values.") parser.add_option("-p", "--peer", action="store", dest="peer", help="the peer name to check.") parser.add_option("-a", "--all", action="store_true", dest="all", help="print the whole output.") parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="print the whole output.") (options, args) = parser.parse_args() # Define the socket connection mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) login = """Action: login\r\nUsername: """ + options.user + """\r\nSecret: """ + options.secret + """\r\nEvents: off\r\n\r\n""" if (options.type=="sip"): command="sip show peer" commandall="sip show peers" elif (options.type=="iax"): command="iax2 show peer" commandall="iax2 show peers" else: print "Type peer error"+options.type sys.exit(1) if (options.all): action = """Action: command\r\nCommand: """ + commandall + """\r\n\r\n""" else: action = """Action: command\r\nCommand: """ + command + """ """+ options.peer +"""\r\n\r\n""" logout = """Action: logoff\r\n\r\n""" #global port host = options.hostname port = int(options.port) user = options.user password = options.secret def connect(host, user, password): mysocket.connect((host, port)) mysocket.send(login) def disconnect(logout): send_command(logout) mysocket.send(logout) mysocket.close() def send_command(action): mysocket.send(action) global myrcvd myrcvd = "" while 1: data = mysocket.recv(4096) #The output bytes from the socket connection. You can adjust size to taste. myrcvd = myrcvd + data #print "$"+ data +"_" #print len(data) if (len(data)==0) or (string.find(data,'END COMMAND')>0): break return myrcvd def get_peer_status(myrcvd): #Search peer status pos1= string.find(myrcvd,'Status') pos2= string.find(myrcvd[pos1:],'\n') #get peer status status=myrcvd[pos1:] status=status[:pos2] return status # Perform the operation... try: connect(host, user, password) result=send_command(action) disconnect(logout) #check auth if string.find(result,'accepted') != -1: if (options.all): print result elif (options.verbose): print result else: status=get_peer_status(result) if (string.find(status,'OK') >0): print status elif (string.find(status,'LAGGED') >0): print status sys.exit(1) elif (string.find(status,'UNKNOWN') >0): print status sys.exit(3) elif (string.find(status,'unmonitored') >0): print status sys.exit(1) else: print status sys.exit(2) else: print "Critical - Authentication failed" sys.exit(2) sys.exit(0) except socket.error: print "Critical - Cannot contact Asterisk!" sys.exit(2)