pep8 fixes

This commit is contained in:
Pall Sigurdsson 2014-02-21 10:52:39 +00:00
parent b96e973e60
commit b0b55dcd3e
1 changed files with 508 additions and 461 deletions

View File

@ -20,7 +20,6 @@
# This script will check the status of a remote IBM Bladecenter via SNMP.
# No real need to change anything below here
version = "1.1.2"
ok = 0
@ -31,7 +30,6 @@ not_present = -1
exit_status = -1
state = {}
state[not_present] = "Not Present"
state[ok] = "OK"
@ -52,10 +50,6 @@ from os import getenv,putenv,environ
import subprocess
# Parse some Arguments
from optparse import OptionParser
parser = OptionParser()
@ -87,12 +81,14 @@ parser.add_option("-d","--debug", dest="debug",
(opts, args) = parser.parse_args()
if opts.host == None:
if opts.host is None:
parser.error("Hostname (-H) is required.")
if opts.mode == None:
if opts.mode is None:
parser.error("Mode (--mode) is required.")
snmp_options = ""
def set_snmp_options():
global snmp_options
if opts.snmp_version is not None:
@ -101,33 +97,41 @@ def set_snmp_options():
if opts.snmp_username is None:
parser.error("--snmp_username required with --snmp_version=3")
if opts.snmp_seclevel is None:
parser.error("--snmp_security_level required with --snmp_version=3")
parser.error(
"--snmp_security_level required with --snmp_version=3")
if opts.snmp_password is None:
parser.error("--snmp_password required with --snmp_version=3")
snmp_options = snmp_options + " -u %s -l %s -A %s " % (opts.snmp_username, opts.snmp_seclevel,opts.snmp_password)
snmp_options = snmp_options + " -u %s -l %s -A %s " % (
opts.snmp_username, opts.snmp_seclevel, opts.snmp_password)
else:
if opts.snmp_community is None:
parser.error("--snmp_community is required with --snmp_version=1|2c")
parser.error(
"--snmp_community is required with --snmp_version=1|2c")
snmp_options = snmp_options + " -c %s " % opts.snmp_community
snmp_options += " -t %s " % (opts.snmp_timeout)
def error(errortext):
print "* Error: %s" % errortext
exit(unknown)
def debug(debugtext):
if opts.debug:
print debugtext
def nagios_status(newStatus):
global exit_status
exit_status = max(exit_status, newStatus)
return exit_status
'''runCommand: Runs command from the shell prompt. Exit Nagios style if unsuccessful'''
def runCommand(command):
'''runCommand: Runs command from the shell prompt. Exit Nagios style if unsuccessful'''
debug("Executing: %s" % command)
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE,)
proc = subprocess.Popen(
command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,)
stdout, stderr = proc.communicate('through stdin to stdout')
if proc.returncode > 0:
print "Error %s: %s\n command was: '%s'" % (proc.returncode, stderr.strip(), command)
@ -143,6 +147,7 @@ def runCommand(command):
else:
return stdout
def end():
global summary
global longserviceoutput
@ -150,36 +155,43 @@ def end():
global exit_status
print "%s - %s | %s" % (state[exit_status], summary, perfdata)
print longserviceoutput
if exit_status < 0: exit_status = unknown
if exit_status < 0:
exit_status = unknown
exit(exit_status)
def add_perfdata(text):
global perfdata
text = text.strip()
perfdata = perfdata + " %s " % (text)
def add_long(text):
global longserviceoutput
longserviceoutput = longserviceoutput + text + '\n'
def add_summary(text):
global summary
summary = summary + text
def set_path(path):
current_path = getenv('PATH')
if current_path.find('C:\\') > -1: # We are on this platform
if path == '':
pass
else: path = ';' + path
else:
path = ';' + path
else: # Unix/Linux, etc
if path == '': path = ":/usr/sbin"
else: path = ':' + path
if path == '':
path = ":/usr/sbin"
else:
path = ':' + path
current_path = "%s%s" % (current_path, path)
environ['PATH'] = current_path
def snmpget(oid):
snmpgetcommand = "snmpget %s %s %s" % (snmp_options, opts.host, oid)
output = runCommand(snmpgetcommand)
@ -189,12 +201,16 @@ def snmpget(oid):
resultValue = resultValue[1:-1]
return resultValue
# snmpwalk -v3 -u v3get mgmt-rek-proxy-p02 -A proxy2011 -l authNoPriv 1.3.6.1.4.1.15497
# snmpwalk -v3 -u v3get mgmt-rek-proxy-p02 -A proxy2011 -l authNoPriv
# 1.3.6.1.4.1.15497
def snmpwalk(base_oid):
snmpwalkcommand = "snmpwalk %s %s %s" % (snmp_options, opts.host, base_oid)
output = runCommand(snmpwalkcommand + " " + base_oid)
return output
def getTable(base_oid):
myTable = {}
output = snmpwalk(base_oid)
@ -215,10 +231,12 @@ def getTable(base_oid):
index = oid.strip().split('.')
column = int(index.pop())
row = int(index.pop())
if not myTable.has_key(column): myTable[column] = {}
if not myTable.has_key(column):
myTable[column] = {}
myTable[column][row] = resultValue
return myTable
def check_powermodules():
powermodules = getTable('1.3.6.1.4.1.2.3.51.2.2.4')
index = 1
@ -232,22 +250,27 @@ def check_powermodules():
myStatus = i[status]
myDetails = i[details]
myExists = i[exists]
if myIndex == opts.exclude: continue
if myIndex == opts.exclude:
continue
if myExists == "0":
num_no = num_no + 1
else:
if myStatus != "1":
nagios_status(warning)
add_summary( 'Powermodule "%s" status "%s". %s. ' % (myIndex,myStatus,myDetails) )
add_summary('Powermodule "%s" status "%s". %s. ' %
(myIndex, myStatus, myDetails))
else:
num_ok = num_ok + 1
add_long('Powersupply "%s" status "%s". %s. ' % (myIndex,myStatus,myDetails) )
add_summary( "%s out of %s powermodules are healthy" % (num_ok, len(powermodules) ) )
add_perfdata( "'Number of powermodules'=%s" % (len(powermodules) - num_no ) )
add_long('Powersupply "%s" status "%s". %s. ' %
(myIndex, myStatus, myDetails))
add_summary("%s out of %s powermodules are healthy" %
(num_ok, len(powermodules)))
add_perfdata("'Number of powermodules'=%s" %
(len(powermodules) - num_no))
nagios_status(ok)
def check_switchmodules():
switchmodules = getTable("1.3.6.1.4.1.2.3.51.2.22.3.1.1")
# The following oid is undocumented, but contains some useful extra info
@ -265,10 +288,12 @@ def check_switchmodules():
'this module is installed'
if healthstate == "1":
nagios_status(ok)
add_long("Module%s health good.\n post=%s" % (myIndex,resultvalue))
add_long("Module%s health good.\n post=%s" %
(myIndex, resultvalue))
else:
nagios_status(warning)
add_long("Module%s health bad(%s).\n post=%s" % (myIndex, healthstate,resultvalue) )
add_long("Module%s health bad(%s).\n post=%s" %
(myIndex, healthstate, resultvalue))
add_summary("Problem with Module %s. " % (myIndex))
if len(extrainfo) > int(myIndex):
try:
@ -276,7 +301,8 @@ def check_switchmodules():
module_type = myExtraInfo[22]
module_ip = myExtraInfo[6]
add_long(" type=%s ip=%s" % (module_type, module_ip))
except: pass
except:
pass
if exit_status == ok:
add_summary("All switchmodules healthy")
@ -408,14 +434,15 @@ def check_chassis_status():
nagios_status(warning)
add_summary("Media Trays NOT OK. ")
# status_oids, oids that where 0 == ok
status_oids = ( 2,3,5,7,8,9,10,11,14,18,19,20,21,22,23,24,25,26,27,28,29,30, )
status_oids = (2, 3, 5, 7, 8, 9, 10, 11, 14, 18,
19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, )
add_long("Other Sensors: ")
sensor_status = ok
for oid in status_oids:
if not chassis_oid.has_key(oid): continue
if not chassis_oid.has_key(oid):
continue
oidValue = oids[oid]
oidName = chassis_oid[oid]
if oidValue == "0":
@ -442,10 +469,14 @@ def check_bladehealth():
myBladeid = row[bladeid]
mySeverity = row[severity]
myDescription = row[description]
try: myName = bladestate[i][6]
except: myName = ""
if mySeverity == "(No severity)": continue
add_long( "blade%s (%s): %s %s" % (myBladeid,myName,mySeverity, myDescription) )
try:
myName = bladestate[i][6]
except:
myName = ""
if mySeverity == "(No severity)":
continue
add_long("blade%s (%s): %s %s" %
(myBladeid, myName, mySeverity, myDescription))
if opts.exclude:
if myDescription.find(opts.exclude) > -1:
continue
@ -455,25 +486,32 @@ def check_bladehealth():
good_blades += 1
else:
nagios_status(warning)
add_summary( "blade%s (%s): %s %s. " % (myBladeid,myName,mySeverity, myDescription) )
add_summary("blade%s (%s): %s %s. " %
(myBladeid, myName, mySeverity, myDescription))
if good_blades == total_blades:
add_summary( "%s out of %s blades in Good health. " % (good_blades, total_blades))
add_summary("%s out of %s blades in Good health. " %
(good_blades, total_blades))
nagios_status(ok)
else:
nagios_status(warning)
def check_systemhealth():
systemhealthstat = snmpget('1.3.6.1.4.1.2.3.51.2.2.7.1.0')
summary = getTable('1.3.6.1.4.1.2.3.51.2.2.7.2.1')
index, severity, description, date = (1, 2, 3, 4)
# Sometimes chassis delivers warning when absolutely nothing is going on. Lets work around that
workaround = {1: '1', 2: 'Good', 3: 'No critical or warning events', 4: 'No timestamp'}
# Sometimes chassis delivers warning when absolutely nothing is going on.
# Lets work around that
workaround = {1: '1', 2: 'Good', 3:
'No critical or warning events', 4: 'No timestamp'}
# Check overall health
if systemhealthstat == '255':
nagios_status(ok)
add_summary("Bladecenter health: OK. ")
elif systemhealthstat == "2":
if summary.values() == workaround:
add_summary("Non-Critical Error: %s " % workaround[0][description] )
add_summary("Non-Critical Error: %s " %
workaround[0][description])
nagios_status(ok)
return
nagios_status(warning)
@ -486,7 +524,9 @@ def check_systemhealth():
add_summary("Critical. ")
else:
nagios_status(unknown)
add_summary("Bladecenter health unkown (oid 1.3.6.1.4.1.2.3.51.2.2.7.1.0 returns %s). " % systemhealthstat)
add_summary(
"Bladecenter health unkown (oid 1.3.6.1.4.1.2.3.51.2.2.7.1.0 returns %s). " %
systemhealthstat)
for row in summary.values():
if row[severity] == 'Good':
nagios_status(ok)
@ -499,27 +539,34 @@ def check_systemhealth():
add_summary("%s. " % (row[description]))
add_long("* %s. " % (row[description]))
def check_temperature():
# set some sensible defaults
if opts.warning_threshold is None: opts.warning_threshold = 28
if opts.critical_threshold is None: opts.critical_threshold = 35
if opts.warning_threshold is None:
opts.warning_threshold = 28
if opts.critical_threshold is None:
opts.critical_threshold = 35
str_temp = snmpget('1.3.6.1.4.1.2.3.51.2.2.1.5.1.0')
float_temp, measurement = str_temp.split(None, 1)
float_temp = float(float_temp)
if opts.critical_threshold is not None and float_temp > opts.critical_threshold:
nagios_status(critical)
add_summary( "ambient temperature (%s) is over critical thresholds (%s). " % (str_temp, opts.critical_threshold) )
add_summary(
"ambient temperature (%s) is over critical thresholds (%s). " %
(str_temp, opts.critical_threshold))
elif opts.warning_threshold is not None and float_temp > opts.warning_threshold:
nagios_status(warning)
add_summary( "ambient temperature (%s) is over warning thresholds (%s). " % (str_temp, opts.warning_threshold) )
add_summary(
"ambient temperature (%s) is over warning thresholds (%s). " %
(str_temp, opts.warning_threshold))
else:
add_summary("Ambient temperature = %s. " % (str_temp))
add_perfdata( "'ambient_temp'=%s;%s;%s " % (float_temp,opts.warning_threshold,opts.critical_threshold) )
add_perfdata("'ambient_temp'=%s;%s;%s " %
(float_temp, opts.warning_threshold, opts.critical_threshold))
#add_long( "Ambient Temperature = %s" % (str_temp) )
nagios_status(ok)
if __name__ == '__main__':
try:
set_snmp_options()