Python Forum
working code, suggestion required for improvement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
working code, suggestion required for improvement
#11
Hi Buran,

this is my full code

#!/usr/bin/python
from easysnmp import Session
community = 'public'
with open('zyxeldslam.txt') as ipfile:
    for sr_no, line in enumerate(ipfile, start=1):
        ip = line.strip()
        try:
# Create an SNMP session to be used for all our requests
            session = Session(hostname=ip, community=community, version=2)
# You may retrieve an individual OID using an SNMP GET
            fan_oids_templ = '1.3.6.1.4.1.890.1.5.13.1.2.1.1.2.0.{}'
            temp_oids_templ = '1.3.6.1.4.1.890.1.5.13.1.2.3.1.2.0.0.{}'
            fans_oids = [fan_oids_templ.format(i) for i in range(1,4)]
            temps_oids = [temp_oids_templ.format(i) for i in range(1,4)]
            fans_status = ['Fail' if fan.value < 2000 else 'Ok' for fan in session.get_bulk(fans_oids)]
            temp_status = ['Cool' if temp.value < 50 else 'Hot' for temp in session.get_bulk(temps_oids)]
            data = fans_status
            data.extend(temp_status)
            print('{} {} {} {} {} {} {} {}'.format(sr_no, ip, *data))
        except Exception as excp:
            print('Connection time out!')
            print(excp)
Reply
#12
Looks fine. This code could not produce the output from your previous post.
I guess fan.value and temp.values is not what you expect - i.e. just RPM or degrees.
could you run following code and post the output

#!/usr/bin/python
from easysnmp import Session
community = 'public'
with open('zyxeldslam.txt') as ipfile:
    for sr_no, line in enumerate(ipfile, start=1):
        ip = line.strip()
# Create an SNMP session to be used for all our requests
        session = Session(hostname=ip, community=community, version=2)
# You may retrieve an individual OID using an SNMP GET
        fan_oids_templ = '1.3.6.1.4.1.890.1.5.13.1.2.1.1.2.0.{}'
        temp_oids_templ = '1.3.6.1.4.1.890.1.5.13.1.2.3.1.2.0.0.{}'
        fans_oids = [fan_oids_templ.format(i) for i in range(1,4)]
        temps_oids = [temp_oids_templ.format(i) for i in range(1,4)]
        fan_values = [fan.value for fan in session.get_bulk(fans_oids)]
        temp_status = [temp.value for temp in session.get_bulk(temps_oids)]
        print (fan_values)
        print (temp_values)
Reply
#13
Hi buran,


changed
 print(temp_values)
to print(temp_status)


output of script

[u'4157', u'4186', u'5465', u'4186', u'5465', u'5595', u'5465', u'5595', u'5642', u'5595', u'5642', u'4044', u'5642', u'4044', u'4118', u'4044', u'4118', u'3994', u'4118', u'3994', u'2000', u'3994', u'2000', u'2000', u'2000', u'2000', u'2000', u'2000', u'2000', u'fan1']
[u'30', u'32', u'48', u'32', u'48', u'43', u'48', u'43', u'44', u'43', u'44', u'36', u'44', u'36', u'29', u'36', u'29', u'31', u'29', u'31', u'97', u'31', u'97', u'97', u'97', u'97', u'97', u'97', u'97', u'T1']
[u'4057', u'4072', u'5394', u'4072', u'5394', u'5427', u'5394', u'5427', u'5532', u'5427', u'5532', u'3848', u'5532', u'3848', u'3868', u'3848', u'3868', u'3851', u'3868', u'3851', u'2000', u'3851', u'2000', u'2000', u'2000', u'2000', u'2000', u'2000', u'2000', u'fan1']
[u'36', u'37', u'50', u'37', u'50', u'44', u'50', u'44', u'46', u'44', u'46', u'23', u'46', u'23', u'16', u'23', u'16', u'18', u'16', u'18', u'97', u'18', u'97', u'97', u'97', u'97', u'97', u'97', u'97', u'T1']
[u'4639', u'4687', u'5521', u'4687', u'5521', u'5578', u'5521', u'5578', u'5642', u'5578', u'5642', u'4150', u'5642', u'4150', u'4225', u'4150', u'4225', u'4258', u'4225', u'4258', u'2000', u'4258', u'2000', u'2000', u'2000', u'2000', u'2000', u'2000', u'2000', u'fan1']
[u'36', u'37', u'50', u'37', u'50', u'44', u'50', u'44', u'45', u'44', u'45', u'28', u'45', u'28', u'27', u'28', u'27', u'27', u'27', u'27', u'97', u'27', u'97', u'97', u'97', u'97', u'97', u'97', u'97', u'T1']
[u'4195', u'4225', u'5330', u'4225', u'5330', u'5561', u'5330', u'5561', u'5613', u'5561', u'5613', u'3958', u'5613', u'3958', u'4128', u'3958', u'4128', u'4163', u'4128', u'4163', u'2000', u'4163', u'2000', u'2000', u'2000', u'2000', u'2000', u'2000', u'2000', u'fan1']
[u'34', u'35', u'44', u'35', u'44', u'38', u'44', u'38', u'40', u'38', u'40', u'35', u'40', u'35', u'29', u'35', u'29', u'30', u'29', u'30', u'97', u'30', u'97', u'97', u'97', u'97', u'97', u'97', u'97', u'T1']
[u'4400', u'4372', u'5465', u'4372', u'5465', u'5590', u'5465', u'5590', u'5532', u'5590', u'5532', u'4137', u'5532', u'4137', u'4202', u'4137', u'4202', u'4182', u'4202', u'4182', u'2000', u'4182', u'2000', u'2000', u'2000', u'2000', u'2000', u'2000', u'2000', u'fan1']
[u'33', u'35', u'54', u'35', u'54', u'46', u'54', u'46', u'48', u'46', u'48', u'39', u'48', u'39', u'30', u'39', u'30', u'32', u'30', u'32', u'97', u'32', u'97', u'97', u'97', u'97', u'97', u'97', u'97', u'T1']
[u'4451', u'4481', u'5421', u'4481', u'5421', u'5515', u'5421', u'5515', u'5527', u'5515', u'5527', u'4069', u'5527', u'4069', u'4125', u'4069', u'4125', u'4128', u'4125', u'4128', u'2000', u'4128', u'2000', u'2000', u'2000', u'2000', u'2000', u'2000', u'2000', u'fan1']
[u'36', u'36', u'50', u'36', u'50', u'44', u'50', u'44', u'44', u'44', u'44', u'26', u'44', u'26', u'25', u'26', u'25', u'26', u'25', u'26', u'97', u'26', u'97', u'97', u'97', u'97', u'97', u'97', u'97', u'T1']
[u'4192', u'4218', u'5521', u'4218', u'5521', u'5584', u'5521', u'5584', u'5672', u'5584', u'5672', u'4109', u'5672', u'4109', u'4169', u'4109', u'4169', u'4212', u'4169', u'4212', u'2000', u'4212', u'2000', u'2000', u'2000', u'2000', u'2000', u'2000', u'2000', u'fan1']
[u'32', u'33', u'47', u'33', u'47', u'39', u'47', u'39', u'40', u'39', u'40', u'38', u'40', u'38', u'32', u'38', u'32', u'33', u'32', u'33', u'97', u'33', u'97', u'97', u'97', u'97', u'97', u'97', u'97', u'T1']
Traceback (most recent call last):
  File "zyxelheath.py", line 14, in <module>
    fan_values = [fan.value for fan in session.get_bulk(fans_oids)]
  File "/usr/lib64/python2.7/site-packages/easysnmp/session.py", line 440, in get_bulk
    interface.getbulk(self, non_repeaters, max_repetitions, varlist)
easysnmp.exceptions.EasySNMPTimeoutError: timed out while connecting to remote host
Reply
#14
well, I admit I didn't expect that kind of output - i.e. does it make sense to get that many values - e.g. ~30 values for fan speed or temperature? Nor I understand why you get the last elements - 'fan1' and 'T1'

maybe I misunderstand the difference between get() and get_bulk() methods of session object. Here is the doc I was referring to.
http://easysnmp.readthedocs.io/en/latest...n_api.html

If you are not tired of experiments, would run the same but replace the get_bulk with just get as now I noticed that you can pass a list of oids to get() method too. My understanding was that get is for single oids and get_bulk - for multiple oids.
Reply
#15
Thanks Buran for your kind and quick support.
Reply
#16
Hi Buran,

changed some code... used snmp.get instead of snmp.get_bulk

now i am getting below report


#!/usr/bin/python
from easysnmp import Session
community = 'public'
with open('zyxeldslam.txt') as ipfile:
    for sr_no, line in enumerate(ipfile, start=1):
        ip = line.strip()
# Create an SNMP session to be used for all our requests
        session = Session(hostname=ip, community=community, version=2)
# You may retrieve an individual OID using an SNMP GET
        fan_oids_templ = '1.3.6.1.4.1.890.1.5.13.1.2.1.1.2.0.{}'
        temp_oids_templ = '1.3.6.1.4.1.890.1.5.13.1.2.3.1.2.0.0.{}'
        fans_oids = [fan_oids_templ.format(i) for i in range(1,4)]
        temps_oids = [temp_oids_templ.format(i) for i in range(1,4)]
        fan_values = [fan.value for fan in session.get(fans_oids)]
        temp_status = [temp.value for temp in session.get(temps_oids)]
        print (fan_values)
        print (temp_status)
output is as below

Output:
[root@localhost scripts]# python zyxelheath.py [u'4320', u'4386', u'4354'] [u'42', u'34', u'35'] [u'3958', u'4020', u'4032'] [u'41', u'32', u'33']
can you please help to print like Sr No, IP, Fan1 Fan2 Fan3 Temp1 Temp2 Temp3
Reply
#17
OK, this is what I expected as output
so, this will print fan and temp status - OK/Fail and Cool/Hot respectively

#!/usr/bin/python
from easysnmp import Session
community = 'public'
with open('zyxeldslam.txt') as ipfile:
    for sr_no, line in enumerate(ipfile, start=1):
        ip = line.strip()
        try:
# Create an SNMP session to be used for all our requests
            session = Session(hostname=ip, community=community, version=2)
# You may retrieve an individual OID using an SNMP GET
            fan_oids_templ = '1.3.6.1.4.1.890.1.5.13.1.2.1.1.2.0.{}'
            temp_oids_templ = '1.3.6.1.4.1.890.1.5.13.1.2.3.1.2.0.0.{}'
            fans_oids = [fan_oids_templ.format(i) for i in range(1,4)]
            temps_oids = [temp_oids_templ.format(i) for i in range(1,4)]
            fans_status = ['Fail' if int(fan.value) < 2000 else 'Ok' for fan in session.get(fans_oids)]
            temp_status = ['Cool' if int(temp.value) < 50 else 'Hot' for temp in session.get(temps_oids)]
            data = fans_status
            data.extend(temp_status)
            print('{} {} {} {} {} {} {} {}'.format(sr_no, ip, *data))            
        except Exception as excp:
            print('Something went wrong!')
            print(excp)

and this will print RPM and degrees instead of OK/Fail and Cool/Hot

#!/usr/bin/python
from easysnmp import Session
community = 'public'
with open('zyxeldslam.txt') as ipfile:
    for sr_no, line in enumerate(ipfile, start=1):
        ip = line.strip()
        try:
# Create an SNMP session to be used for all our requests
            session = Session(hostname=ip, community=community, version=2)
# You may retrieve an individual OID using an SNMP GET
            fan_oids_templ = '1.3.6.1.4.1.890.1.5.13.1.2.1.1.2.0.{}'
            temp_oids_templ = '1.3.6.1.4.1.890.1.5.13.1.2.3.1.2.0.0.{}'
            fans_oids = [fan_oids_templ.format(i) for i in range(1,4)]
            temps_oids = [temp_oids_templ.format(i) for i in range(1,4)]
            fans_status = [fan.value for fan in session.get(fans_oids)]
            temp_status = [temp.value for temp in session.get(temps_oids)]
            data = [str(sr_no), ip]
            data.extend(fans_status)
            data.extend(temp_status)
            print(' '.join(data))            
        except Exception as excp:
            print('Something went wrong!')
            print(excp)
Reply
#18
Thanks... for such great help.
Reply
#19
It was interesting to familiarize myself with this package. Took a bit more iterations than normal, but anyway....
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 909 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 836 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 974 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,031 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  My Code isn't working... End3r 4 1,864 Mar-21-2022, 10:12 AM
Last Post: End3r
  Can you give me some suggestion about PCEP Newbie1114 0 996 Oct-14-2021, 03:02 PM
Last Post: Newbie1114
  instagram followers name without suggestion for you jacklee26 1 3,127 Oct-02-2021, 04:57 AM
Last Post: ndc85430
  Random coordinate generator speed improvement saidc 0 2,021 Aug-01-2021, 11:09 PM
Last Post: saidc
  I don't undestand why my code isn't working. RuyCab 2 1,956 Jun-17-2021, 03:06 PM
Last Post: RuyCab
  code is not working , can anybody help? RandomPerson69 4 2,849 Mar-22-2021, 04:24 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020