Article Figure 1 Listing 1 mar2005.tar

Listing 1 snmptraphandling.py

#!/usr/bin/python -u
"""
Written by Francois Meehan (Cedval Info)
First release 2004/09/15

This script receives input from sec.pl concerning translated snmptraps

*** Important note: sec must send DATA within quotes


Ex: ./services.py $1 $2 $3
"""
import commands, string, os, sys, time
global return_code

def check_arg():

        try:
                host = sys.argv[1]
        except:
                print  "usage: services.py <HOST> <SEVERITY> <DATA>"
                sys.exit()
        try:
                severity = sys.argv[2]
        except:
                print  "usage: services.py <HOST> <SEVERITY> <DATA>"
                sys.exit()
        try:
                mondata_res = sys.argv[3]
        except:
                print  "usage: services.py <HOST> <SEVERITY> <DATA>"
                sys.exit()

        return (host, severity, mondata_res)

def post_results(host, mondata_res, return_code, service_suffix):
        mytime = time.time()
        mytime = str(mytime)
        mytime = mytime[:-3]
        mondata_res = '" ' + mondata_res + '" '
        #print mondata_res
        output = open('/var/nagios/rw/nagios.cmd', 'w')
        results = "[" + mytime + "] " + "PROCESS_SERVICE_CHECK_RESULT;" \
          + host + ";" + "snmp_trap_handling_" + service_suffix + ";" \
          + return_code + ";" + mondata_res + "\n"
        output.write(results)


def get_return_code():
        if severity == "INFORMATIONAL":
                return_code = "0"
                service_suffix = "ok"
        elif severity == "Normal":
                return_code = "0"
                service_suffix = "ok"
        elif severity == "SEVERE":
                return_code = "2"
        service_suffix = "critical"
        elif severity == "MAJOR":
                return_code = "2"
        service_suffix = "critical"
    elif severity == "CRITICAL":
                return_code = "2"
        service_suffix = "critical"
        elif severity == "WARNING":
                return_code = "1"
        service_suffix = "warning"
        elif severity == "MINOR":
                return_code = "1"
        service_suffix = "warning"
        return return_code, service_suffix


# Main routine...
if __name__ == '__main__':
        (host, severity, mondata_res) = check_arg()   # validating 
                                                      # parameters
        return_code, service_suffix = get_return_code()
        post_results(host, mondata_res, return_code, service_suffix)