Posts: 4,786
Threads: 76
Joined: Jan 2018
Oct-27-2019, 07:33 AM
(This post was last modified: Oct-27-2019, 07:34 AM by Gribouillis.)
Posts: 221
Threads: 71
Joined: Dec 2017
Thanks, something is going wrong and not able to understand, if I am accessing class in loop and get class method variable values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
from easysnmp import Session
import time
from datetime import datetime
import datetime
class fan:
fan_speed = { 0 : 'Not Applicable' ,
1 : 'Unknown' ,
2 : 'Half Speed' ,
3 : 'Full Speed' ,
4 : 'Low Speed'
}
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
def get_fan_speed( self , value):
return self .fan_speed.get(value, "other" )
def get_fan_status( self ,value):
return self .fan_status.get(value, "other" )
def get_fan_details(host):
session = Session(hostname = host, community = "cacti" , version = 2 ,timeout = 5 )
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
if __name__ = = "__main__" :
hosts = [ '10.124.209.66' , '10.124.209.74' ]
for host in hosts:
try :
fandetails = fan.get_fan_details(host)
except :
break
print (fandetails)
NumFanTrays = fandetails[ 0 ]
NumFans = fandetails[ 1 ]
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
Output: ('1', '3', '2', '3')
Error: Traceback (most recent call last):
File "testclass3.py", line 87, in <module>
Fanspeed = fan.get_fan_speed(str(2))
TypeError: get_fan_speed() missing 1 required positional argument: 'value'
Posts: 4,786
Threads: 76
Joined: Jan 2018
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
>>> 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()
>>> 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
|
Posts: 221
Threads: 71
Joined: Dec 2017
thanks, got some idea... will let you know.
Posts: 221
Threads: 71
Joined: Dec 2017
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
from easysnmp import Session
import time
from datetime import datetime
import datetime
class fan:
fan_speed = { 0 : 'Not Applicable' ,
1 : 'Unknown' ,
2 : 'Half Speed' ,
3 : 'Full Speed' ,
4 : 'Low Speed'
}
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 ):
session = Session(hostname = self .host, community = 'cacti' , version = 2 ,timeout = 5 )
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 {
'NumOfFantrays' :NumFanTrays,
'NumofFans' :NumFans,
'FanSpeed' :FanSpeed,
'FanStatus' :FanOperStatus
}
def get_fan_speed( self , value):
return self .fan_speed.get(value, "other" )
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
Output: 'NumOfFantrays', '1')
('NumofFans', '3')
('FanSpeed', '2')
('FanStatus', '3')
expected output
Output: ('NumOfFantrays', '1')
('NumofFans', '3')
('FanSpeed', 'Half Speed')
('FanStatus', 'Up')
Posts: 4,786
Threads: 76
Joined: Jan 2018
I don't understand the output that you want. Obviously the method already returns a dict.
Posts: 8,158
Threads: 160
Joined: Sep 2016
Oct-27-2019, 05:11 PM
(This post was last modified: Oct-27-2019, 05:13 PM by buran.)
your def get_fan_details() method does not use the other two methods def get_fan_speed() and get_fan_status() :
1 2 3 4 5 6 |
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
Posts: 221
Threads: 71
Joined: Dec 2017
(Oct-27-2019, 05:06 PM)Gribouillis Wrote: I don't understand the output that you want. Obviously the method already returns a dict.
but i want return dict values from class itself, instead of converting outof class.
Posts: 221
Threads: 71
Joined: Dec 2017
Thanks, Yoriz,Gribouillis,ichabod801,buran
got some knowledge on Class, method.
|