#!/usr/bin/env python # # Nagios check to verify signal level of nanostation5 # # Based on check_remoteneighbor # # By Richard van Mansom # OK = 15 WARNING = 10 OFFSET = -96 from collections import defaultdict from pysnmp.entity.rfc3413.oneliner import cmdgen from pysnmp.proto.rfc1902 import OctetString from gettext import gettext as _ import argparse import logging import operator import socket import sys import subprocess import time logging.basicConfig(level=logging.INFO) logger = logging.getLogger() class ERRNO(): OK = 0 WARNING = 1 CRITICAL = 2 UNKNOWN = 3 def snmpwalk(host, oid): errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().bulkCmd( cmdgen.CommunityData('my-agent', 'public', 1), cmdgen.UdpTransportTarget((host, 161)), 15, 0, # nonRepeaters, maxRepetitions oid ) return varBinds def main(host): try: start = int(time.time()) - 86400 c = 0 lines = snmpwalk(host, '.1.3.6.1.4.1.2021.71.4.1.2') for line in lines: try: line = str(line[0][1]).split() if int(line[0]) > start: c+=1 except: continue if c > 30 : return ("PORTAL CRITICAL - %i | c=%i" % (c, c), ERRNO.CRITICAL) elif c > 20: return ("PORTAL WARNING - %i | c=%i" % (c, c), ERRNO.WARNING) else: return ("PORTAL OK - %i | c=%i" % (c, c), ERRNO.OK) except socket.gaierror as e: return ("Failed: SNMP for host cannot be queried - %s" % e, ERRNO.CRITICAL) except Exception as e: return ("Failed: Script Exception - [%s] %s" % (type(e),e), ERRNO.CRITICAL) if __name__ == '__main__': class NagiosArgumentParser(argparse.ArgumentParser): ''' Like exit code 128 on errors''' def error(self, message): self.print_usage(sys.stderr) self.exit(128, _('%s: error: %s\n') % (self.prog, message)) parser = NagiosArgumentParser( description='Nagios Check InterLinks via SNMP.', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-g', dest='debug', action='store_true', default=False, help="Set logging to debug") parser.add_argument('host', action='store', type=str, help="Hostname to use") args = parser.parse_args() if args.debug: logger.setLevel(logging.DEBUG) (msg, errno) = main(args.host) sys.stdout.write(msg + "\n") sys.exit(errno)