Go to All Forums

Plugin Modification of Textual Values

Hello, as Text Attributes cannot be used for thresholds, i'm modifying the iDrac_powersupply.py original plugin to return a value instead of text.

I did a sequence of if's that upon a string received, I put on a number instead. Nevertheless it is perceived as text.

I tried to add Units to it but cannot change its nature. 

Any help to convert this into numeric would be very appreciated. 

The objective is to set up alarm based on power supply failure.

Thank you very much

David

 


#!/usr/bin/env python

import commands
import json
import sys
import re
import SNMPUtil


### Monitoring iDRAC Servers - Powersource Performance

### It uses snmpwalk command to get the hadrware data from the iDRAC Servers.
### SNMPUtil.py is used to get the snmp raw data and parsed to get the output json
### Download and install the latest version of Site24x7 Linux Agent. The agent will execute the plugin and push the data to the Site24x7 server
###
### Author: Anita, Zoho Corp
### Language : Python
### Tested in Ubuntu
### Tested for snmp version 2c

### iDRAC Server Configuration Details
HOST = '172.16.40.194'
VERSION = '2c'
COMMUNITY = 'public'
MIB = '/opt/site24x7/monagent/plugins/mibs/iDRAC-SMIv1.mib'


### OIDS for Getting Powersupply Details
OIDS = {'PS' : ['powerSupplyTable','amperageProbeReading','voltageProbeReading']}
### OID Attributes
hardware = {'PS' : ['powerSupplyStatus','powerSupplyOutputWatts','powerSupplyInputVoltage','powerSupplyRatedInputWattage','amperageProbeReading','voltageProbeReading']}
### Output Keys and their units
names = {'PS' : ['status',{'owatt':'Watts'},{'ivolt':'Volts'},{'iwatt':'Watts'},{'current':'Ampere'},{'ovolt':'Volts'}]}


class HardwareParser:
def __init__(self):
self.hardware = ''
self.oids = ''
self.pattern = ''

def getData(self):
output_data = {}
output_data['data'] = {}
output_data['units'] = {}
for _ in OIDS:
self.hardware = _
self.oids = OIDS[self.hardware]
self.keys = set()

for _ in self.oids:
### SNMPUtil module is used to get the snmp output for the input OIDS
snmpdata = SNMPUtil.SNMPPARSER('snmpwalk',HOST,VERSION,COMMUNITY,_,MIB,hardware[self.hardware])
### get Raw SNMP Output as a dict
self.snmp_data = snmpdata.getRawData()
### Method to parse the SNMP command output data
output_data = self.parseSNMPData(output_data)
return output_data

### Method to parse the SNMP command output data
def parseSNMPData(self,output_data):
jsondata = output_data['data']
unitdata = output_data['units']

appendkeys = False;

if not jsondata: appendkeys = True
for _ in self.snmp_data:
for index, __ in enumerate(hardware[self.hardware]) :
if __ in _:

name = ''.join(_.split("::")[1:]).replace('"','').split(' ')[0].split('.')
elementname = name[len(name)-1] # Name

if appendkeys : self.keys.add(elementname);

value = ''.join(_.split()[1:]).replace('"','') # Value

if ':' in value:
val = value.split(':')[1:]
value = val[len(val)-1]

if __ == 'powerSupplyOutputWatts' : value = int(value)/float(10)
if __ == 'powerSupplyRatedInputWattage' : value = int(value)/float(10)
if __ == 'amperageProbeReading' : value = int(value)/float(10)
if __ == 'voltageProbeReading' : value = int(value)/float(1000)

elem = names[self.hardware][index]
attribute = '' # Attribute Name
unit = '' # Attribute Value

if type(elem) is str: # Attributes with no units specified
attribute = elem
unit = "Value"
elif type(elem) is dict: # Attributes with units
attribute = (elem.keys())[0]
unit = elem[(elem.keys())[0]]

key = (attribute +'_'+elementname).replace(' ','')

###############################################################
# MODIFIED TO READ POWER SOURCE STATUS AND RETURN A VALUE #
# other = 1 #
# unknown(2) #
# ok(3) #
# nonCritical(4) #
# critical(5) #
# nonRecoverable(6) #
# #
# To get the original: #
# if appendkeys: #
# jsondata key = value #
# if unit!='': unitdata key = unit #
###############################################################
if appendkeys :
if value == "other":
jsondata[key] = "1"
unitdata[key] = unit
elif value == "unknown" :
jsondata[key] = "2"
unitdata[key] = unit
elif value == "ok" :
jsondata[key] = "3"
unitdata[key] = unit
elif value == "nonCritical" :
jsondata[key] = "4"
unitdata[key] = unit
elif value == "critical" :
jsondata[key] = "5"
unitdata[key] = unit
elif value == "nonRecoverable" :
jsondata[key] = "6"
unitdata[key] = unit
else:
jsondata[key] = value
if unit!='': unitdata[key] = unit

#################################################################
# END OF MODIFIED CODE #
#################################################################

elif elementname in self.keys :
jsondata[key] = value
if unit!='': unitdata[key] = unit


output_data['data'] = jsondata
output_data['units'] = unitdata

return output_data

 

if __name__ == '__main__':

parser = HardwareParser()
data = parser.getData()
result = data['data']
result['units'] = data['units']
print(json.dumps(result, indent=2, sort_keys=True))

 

 

 

Like (2) Reply
Replies (1)

Just in case someone needs, here's the solution

when in the plugin, substitute

if appendkeys: 
 jsondata key = value 
 if unit!='': unitdata key = unit

 

with this (ident correctly):

if appendkeys :
if value == "other":
jsondata[key] = 6
unitdata[key] = unit
elif value == "unknown" :
jsondata[key] = 2
unitdata[key] = unit
elif value == "ok" :
jsondata[key] = 1
unitdata[key] = unit
elif value == "nonCritical" :
jsondata[key] = 3
unitdata[key] = unit
elif value == "critical" :
jsondata[key] = 4
unitdata[key] = unit
elif value == "nonRecoverable" :
jsondata[key] = 5
unitdata[key] = unit
else:
jsondata[key] = value
if unit!='': unitdata[key] = unit

I'm reading the possible returnable strings from the mib and associating possible integer values.

Bear in mind that the association is not the same. In the mib, the integer 3 is "ok". In 24x7, the thresholds fields can only be referred once, so there's only the option to say that if it is greater than 1, something is not ok.

If you run the script directly you can then see the issue.

Not perfect, but decent enough to detect hardware problems. The same approach for memory and pdisks.

 

 

Like (0) Reply

Was this post helpful?