#!/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 gettext import gettext as _ import argparse import logging import operator import socket import sys import subprocess import yapsnmp logging.basicConfig(level=logging.INFO) logger = logging.getLogger() class ERRNO(): OK = 0 WARNING = 1 CRITICAL = 2 UNKNOWN = 3 def snmpwalk(host, oid): systemtree = yapsnmp.Session(host, version=2).get(oid) return systemtree def main(host): try: uptime = int(snmpwalk(host, 'DISMAN-EVENT-MIB::sysUpTimeInstance')) if uptime < 1440: return ("UPTIME CRITICAL - %i | uptime=%i" % (uptime, uptime), ERRNO.CRITICAL) elif uptime < 2880: return ("UPTIME WARNING - %i | uptime=%i" % (uptime, uptime), ERRNO.WARNING) else: return ("UPTIME OK - %i | uptime=%i" % (uptime, uptime), 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)