#!/usr/bin/env python
# -*- coding: UTF-8 -*-
##########################################################################################
#Basic SNMP plugin to check the Outgoing Mailqueue on the Barrcuda SPAM and Virus Firewall #
#Copyright (C) 2009  Jeremy Schoenhaar <jeremy.schoenhaar@noris.net>                       #
#                                                                                          #
#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 3 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.                                              #
#                                                                                          #
#You should have received a copy of the GNU General Public License                         #
#along with this program.  If not, see <http://www.gnu.org/licenses/>.                     #
###########################################################################################

#################################################################################
#  import some basic modules that are most likely needed by any plugin :-)      #
#################################################################################

import sys, os
from optparse import OptionParser
version = "0.0.1alpha" # version of the plugin, increment after update

def main():
    parser = OptionParser()
    parser.add_option("-V", "--Version", action="store_true", dest="output_version") # output the plugin version and exit
    parser.add_option("-w", "--warn", dest="warning", default=10)
    parser.add_option("-c", "--critical", dest="critical", default=20)
    parser.add_option("-t", "--timeout", dest="timeout", default=20)
    parser.add_option("-H", "--host", dest="host", default="localhost")
    parser.add_option("-s", "--service", dest="service")

    options, args = parser.parse_args()

    if options.output_version:
        print os.path.basename(sys.argv[0])+" "+version
        return(0)
    service = options.service
    if (service == "queue_in"):
	MIB = "1.3.6.1.4.1.2021.8.1.101.1"
	cmd = os.popen('/usr/bin/snmpwalk -v 1 -c public '+options.host+' '+MIB)
	cmd = cmd.readline()
	command = cmd
	cmd = cmd.split(" ")
	cmd = cmd[3].strip()
	cmd = int(cmd)
	print str(cmd) + " incoming mails waiting to be Processed"
	if (cmd >= options.critical):
		return(2)
        elif (cmd >= options.warning):
		return(1)
	else:
		return(0)
    elif ( service == "queue_out"):
	MIB = "1.3.6.1.4.1.2021.8.1.101.2"
	cmd = os.popen('/usr/bin/snmpwalk -v 1 -c public '+options.host+' '+MIB)
        cmd = cmd.readline()
        command = cmd
        cmd = cmd.split(" ")
        cmd = cmd[3].strip()
        #cmd = int(cmd)
	print str(cmd) + " outgoing mails waiting to be Processed"
        if (cmd >= options.critical):
                return(2)
        elif (cmd >= options.warning):
                return(1)
        else:
                return(0)
    elif ( service == "queue_bounce" ):
	MIB = "1.3.6.1.4.1.2021.8.1.101.3"
	cmd = os.popen('/usr/bin/snmpwalk -v 1 -c public '+options.host+' '+MIB)
        cmd = cmd.readline()
        command = cmd
        cmd = cmd.split(" ")
        cmd = cmd[3].strip()
        cmd = int(cmd)
	print str(cmd) + " bounced mails waiting to be Processed"
        if (cmd >= options.critical):
                return(2)
        elif (cmd >= options.warning):
                return(1)
        else:
                return(0)
    elif (service == "RAID_Status"):
	MIB = "1.3.6.1.4.1.2021.8.1.101.4"
	cmd = os.popen('/usr/bin/snmpwalk -v 1 -c public '+options.host+' '+MIB)
        cmd = cmd.readline()
        command = cmd
        cmd = cmd.split(" ")
        cmd = cmd[5].strip()
	print service + " is " + cmd 
        if (not cmd == "OK"):
                return(2)
        else:
                return(0)
    elif (service == "Disk"):
	MIB = "1.3.6.1.4.1.2021.9.1.9"
	cmd = os.popen('/usr/bin/snmpwalk -v 1 -c public '+options.host+' '+MIB)
        cmd = cmd.readline()
        command = cmd
        cmd = cmd.split(" ")
        cmd = cmd[3].strip()
        cmd = int(cmd)
	print str(cmd) + "% Disk used"
        if (cmd >= int(options.critical)):
                return(2)
        elif (cmd >= int(options.warning)):
                return(1)
        else:
                return(0)
    elif (service == "CPU"):
	MIB = "1.3.6.1.4.1.2021.11.11"
	cmd = os.popen('/usr/bin/snmpwalk -v 1 -c public '+options.host+' '+MIB)
	cmd = cmd.readline()
	command = cmd
	cmd = cmd.split(" ")
	cmd = cmd[3]
	cmd = int(cmd)
	cmd = 100 - cmd #CPU used
	print str(cmd) + "% CPU used"
	if (cmd >= int(options.critical)):
		return(2)
	elif (cmd >= int(options.warning)):
		return(1)
	else:
		return(0)
    else:
	print "Accepted Services are:\nqueue_in\nqueue_out\nqueue_bounce\nDisk\nRAID_Status\nCPU\n"
	return(3)


    return(3) # return "Unknown" if the script could be completed without errors but couldn't find the Service Requested or unknown worth ist returned

if __name__ == "__main__":
    sys.exit(main())

