Oct-27-2019, 07:33 AM
(This post was last modified: Oct-27-2019, 07:34 AM by Gribouillis.)
Comment lines 23 and 24.
first try with python class, suggestion for improvement please
|
Oct-27-2019, 07:33 AM
(This post was last modified: Oct-27-2019, 07:34 AM by Gribouillis.)
Comment lines 23 and 24.
Oct-27-2019, 08:13 AM
Thanks, something is going wrong and not able to understand, if I am accessing class in loop and get class method variable values.
from easysnmp import Session import time from datetime import datetime import datetime #from collections import namedtuple class fan: """\ this class to collect information about fan speed and fan status final output will be like below ------------------------------------------------------------------------------- Environment Information Number of fan trays : 1 Number of fans : 3 Fan tray number : 1 Speed : half speed Status : up -------------------------------------------------------------------------------""" #convert oid number in to fan speed , str oid value # dict for FanSpeed oid output value fan_speed = { 0:'Not Applicable', 1:'Unknown', 2:'Half Speed', 3:'Full Speed', 4:'Low Speed' } #convert oid output in fan status, str oid value #Dict for Fanstatus oid output values fan_status = { '1':'Unknown', '2':'Fan Removed', '3':'Up', '4':'Fail', '5':'Out of Service' } def __init__(self,host,community,version,timeout,value): self.host = host self.community = community self.version = version self.timeout = timeout #@classmethod # return fan speed and parameter will be oid value def get_fan_speed(self, value): return self.fan_speed.get(value,"other") #return fan status, parameter will be oid value def get_fan_status(self,value): return self.fan_status.get(value,"other") def get_fan_details(host): # establish easysnmp session with host session = Session(hostname=host, community="cacti", version=2,timeout=5) # start bulkwalking snmp oids tmnxChassisNumFanTrays = session.bulkwalk('1.3.6.1.4.1.6527.3.1.2.2.1.3.1.10') NumFanTrays = str(u"{value}".format(value=tmnxChassisNumFanTrays[0].value)) tmnxChassisNumFans = session.bulkwalk('1.3.6.1.4.1.6527.3.1.2.2.1.3.1.11') NumFans = str(u"{value}".format(value=tmnxChassisNumFans[0].value)) tmnxChassisFanSpeed = session.bulkwalk('1.3.6.1.4.1.6527.3.1.2.2.1.4.1.3') FanSpeed = str(u"{value}".format(value=tmnxChassisFanSpeed[0].value)) tmnxChassisFanOperStatus = session.bulkwalk('1.3.6.1.4.1.6527.3.1.2.2.1.4.1.2') FanOperStatus = str(u"{value}".format(value=tmnxChassisFanOperStatus[0].value)) return NumFanTrays, NumFans, FanSpeed,FanOperStatus # returning output as str if __name__ == "__main__": hosts = ['10.124.209.66','10.124.209.74'] for host in hosts: #print(host) try: fandetails = fan.get_fan_details(host) except: break print(fandetails) # able to print NumFanTrays = fandetails[0] # able to print NumFans = fandetails[1] # able to print #Fanspeed = fandetails[2] #FanStatus = fandetails[3] #print(type(FanStatus)) Fanspeed = fan.get_fan_speed(str(2)) FanStatus = fan.get_fan_status(str(2)) print('Num Of Tray:{} Number of Fans: {} Fan Speed:{} Fan Status:{}'.format(NumFanTrays,NumFans,Fanspeed,FanStatus))able to print fandetails, however not able to access dict values
Oct-27-2019, 01:42 PM
(This post was last modified: Oct-27-2019, 01:42 PM by Gribouillis.)
Here is a simple example to show what's happening in your case
>>> class A: ... def get_spam(self, ham): ... return 3.14 ... >>> A.get_spam() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: get_spam() missing 2 required positional arguments: 'self' and 'ham' >>> a = A() # <--- Creating a class INSTANCE >>> a.get_spam() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: get_spam() missing 1 required positional argument: 'ham' >>> a.get_spam('egg') 3.14
Oct-27-2019, 01:57 PM
thanks, got some idea... will let you know.
Oct-27-2019, 04:07 PM
thanks,Gribouillis and all admin and moderator
Changed code, its working for me, but having small question, how can I can parse data inside class and return dict. from easysnmp import Session import time from datetime import datetime import datetime #from collections import namedtuple class fan: """\ this class to collect information about fan speed and fan status final output will be like below ------------------------------------------------------------------------------- Environment Information Number of fan trays : 1 Number of fans : 3 Fan tray number : 1 Speed : half speed Status : up -------------------------------------------------------------------------------""" #convert oid number in to fan speed , str oid value # dict for FanSpeed oid output value fan_speed = { 0:'Not Applicable', 1:'Unknown', 2:'Half Speed', 3:'Full Speed', 4:'Low Speed' } #convert oid output in fan status, str oid value #Dict for Fanstatus oid output values fan_status = { '1':'Unknown', '2':'Fan Removed', '3':'Up', '4':'Fail', '5':'Out of Service' } def __init__(self,host,community,version,timeout): self.host = host self.community = community self.version = version self.timeout = timeout def get_fan_details(self): # establish easysnmp session with host session = Session(hostname=self.host, community='cacti', version=2,timeout=5) # start bulkwalking snmp oids tmnxChassisNumFanTrays = session.bulkwalk('1.3.6.1.4.1.6527.3.1.2.2.1.3.1.10') NumFanTrays = str(u"{value}".format(value=tmnxChassisNumFanTrays[0].value)) tmnxChassisNumFans = session.bulkwalk('1.3.6.1.4.1.6527.3.1.2.2.1.3.1.11') NumFans = str(u"{value}".format(value=tmnxChassisNumFans[0].value)) tmnxChassisFanSpeed = session.bulkwalk('1.3.6.1.4.1.6527.3.1.2.2.1.4.1.3') FanSpeed = str(u"{value}".format(value=tmnxChassisFanSpeed[0].value)) tmnxChassisFanOperStatus = session.bulkwalk('1.3.6.1.4.1.6527.3.1.2.2.1.4.1.2') FanOperStatus = str(u"{value}".format(value=tmnxChassisFanOperStatus[0].value)) # returning dict return { 'NumOfFantrays':NumFanTrays, 'NumofFans':NumFans, 'FanSpeed':FanSpeed, 'FanStatus':FanOperStatus } """ how can I return dict value from this class... like return { 'NumOfFantrays':NumFanTrays, 'NumofFans':NumFans, 'FanSpeed':fan.get_fan_speed(FanSpeed), 'FanStatus':fan.fan_status.get(FanOperStatus) } """ # return fan speed and parameter will be oid value def get_fan_speed(self, value): return self.fan_speed.get(value,"other") #return fan status, parameter will be oid value def get_fan_status(self,value): return self.fan_status.get(value,"other") if __name__ == "__main__": a = fan('10.124.209.66','cacti',2,5) data = a.get_fan_details() for item in data.items(): print(item)current out put expected output
Oct-27-2019, 05:06 PM
I don't understand the output that you want. Obviously the method already returns a dict.
your
def get_fan_details() method does not use the other two methods def get_fan_speed() and get_fan_status() :return { 'NumOfFantrays':NumFanTrays, 'NumofFans':NumFans, 'FanSpeed':self.get_fan_speed(FanSpeed), 'FanStatus':self.get_fan_status(FanOperStatus) }if you are not able to debug soemthing like this, better read again some class basics tutorial
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs
Oct-27-2019, 05:37 PM
Nov-01-2019, 11:16 AM
Thanks, Yoriz,Gribouillis,ichabod801,buran
got some knowledge on Class, method. |
|