#!/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 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: lines = snmpwalk(host, '.1.3.6.1.4.1.2021.91.4.1.2') avm = int(str(lines[-2][0][1]).split()[3])/1024 free = int(str(lines[-2][0][1]).split()[4])/1024 if free > 100: return ("MEM OK - %s MB | free=%s, avm=%s" % (free, free, avm), ERRNO.OK) elif free > 50: return ("MEM WARNING - %s MB | free=%s, avm=%s" % (free, free, avm), ERRNO.WARNING) else: return ("MEM CRITICAL - %s MB | free=%s, avm=%s" % (free, free, avm), ERRNO.CRITICAL) 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)