#!/usr/bin/env python
# check_foundry_modulestate - nagios plugin
#
# Copyright (C) 2008 S. Barbereau
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#
# Report bugs to: nagios@barbich.net
#
# Description:
#  This module checks the state of all modules on a Foundry Sw/Rt
#
# Nagios command:
#   define command {
#       command_name        check_foundry_modulestate
#       command_line        $USER1$/check_foundry_modulestate.py -H $HOSTNAME$ -C $ARG1$
#   }
# Nagios usage;
#    define service {
#        host_name                           sx-switch,rx-switch
#        service_description                 Foundry Modules State
#        normal_check_interval               5
#        notification_period                 24x7
#        check_command                       check_foundry_modulestate!SNMPCOMMUNITY
#    }
# CLI usage:
# bash~: /usr/local/nagios/libexec # ./check_foundry_modulestate.py -H rx-switch -C PUBLIC
# Foundry module interface: Running:10 ,empty:8 , (total: 18)

from pysnmp.entity.rfc3413.oneliner import cmdgen
import sys,re,os
from optparse import OptionParser

def arg_parse():
    usage="%prog [options] arg"
    parser = OptionParser(usage)
    parser.add_option("-H", "--hostname", dest="hostname",help="device to query",default='localhost')
    parser.add_option("-C", "--community", dest="community",help="snmp community",default='public')
    (options, args) = parser.parse_args(args=sys.argv[1:])
    if len(args)!=0:
	parser.error("Error in command line options")
    return options

if __name__=="__main__":
    try:
        import psyco
        psyco.full()
    except ImportError:
        pass
    my_options=arg_parse()
    my_oid=(1,3,6,1,4,1,1991,1,1,2,2,1,1,12)
    my_community=my_options.community
    my_router=my_options.hostname
    nagios_states={'OK':0,'WARNING':1,'CRITICAL':2,'UNKNOWN':3,'DEPENDENT':4}
    my_state='UNKNOWN'
    errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().nextCmd(
	cmdgen.CommunityData('my-agent', my_community, 1),
	cmdgen.UdpTransportTarget((my_router, 161)),
	my_oid
	)
    if errorIndication:
	print "Error setting up context : %s." % errorIndication
	sys.exit(4)
    my_status={'empty':0,'Going Down':0,'Rejected':0,'Bad':0,'Coming Up':0,'Running':0,'total':0}
    for varBindTableRow in varBinds:
	if varBindTableRow[0][1]==10:
	    my_status['Running']=my_status['Running']+1
	    my_status['total']=my_status['total']+1
	    continue
	if varBindTableRow[0][1]==0:
	    my_status['empty']=my_status['empty']+1
	    my_status['total']=my_status['total']+1
	    continue
	if varBindTableRow[0][1]==2:
	    my_status['Going Down']=my_status['Going Down']+1
	    my_status['total']=my_status['total']+1
	    continue
	if varBindTableRow[0][1]==3:
	    my_status['Rejected']=my_status['Rejected']+1
	    my_status['total']=my_status['total']+1
	    continue
	if varBindTableRow[0][1]==4:
	    my_status['Bad']=my_status['Bad']+1
	    my_status['total']=my_status['total']+1
	    continue	    
	if varBindTableRow[0][1]==9:
	    my_status['Coming Up']=my_status['Coming Up']+1
	    my_status['total']=my_status['total']+1
	    continue
    response_text= "Foundry module interface: "
    for k in my_status:
	if my_status[k]>0 and k!='total':
    	    response_text = response_text + "%s:%s ," % (k,my_status[k])
    response_text = response_text + " (total: %s)" % my_status['total']
    if my_status['Rejected']==0 and my_status['Going Down']==0 and my_status['Bad']==0 and my_status['Coming Up']==0:
	my_state='OK'
    if my_status['Rejected']==0 and my_status['Going Down']==0 and my_status['Bad']==0 and my_status['Coming Up']>0:
	my_state='WARNING'
    if my_status['Rejected']>0 or my_status['Going Down']>0 and my_status['Bad']>0:
	my_state='CRITICAL'
    print response_text
    sys.exit(nagios_states[my_state])
