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
#1
#!/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
           fan1 = session.get('1.3.6.1.4.1.890.1.5.13.1.2.1.1.2.0.1')
           fan2 = session.get('1.3.6.1.4.1.890.1.5.13.1.2.1.1.2.0.2')
           fan3 = session.get('1.3.6.1.4.1.890.1.5.13.1.2.1.1.2.0.3')
           temp1 = session.get('1.3.6.1.4.1.890.1.5.13.1.2.3.1.2.0.0.1')
           temp2 = session.get('1.3.6.1.4.1.890.1.5.13.1.2.3.1.2.0.0.2')
           temp3 = session.get('1.3.6.1.4.1.890.1.5.13.1.2.3.1.2.0.0.3')
           fan1_status= fan1.value.encode('ascii')
           if fan1_status < str(2000):
                   fan1_status = 'Fail'
           else:
                   fan1_status = 'Ok'
           fan2_status = fan2.value.encode('ascii')
           if fan2_status < str(2000):
                   fan2_status = 'Fail'
           else:
                   fan2_status = 'Ok'
           fan3_status = fan3.value.encode('ascii')
           if fan3_status < str(2000):
                   fan3_status = 'Fail'
           else:
                   fan3_status = 'Ok'
           temp1_status = temp1.value.encode('ascii')
           temp2_status = temp2.value.encode('ascii')
           temp3_status = temp3.value.encode('ascii')
           print('{} {} {} {} {} {} {} {}'.format(sr_no, ip, fan1_status, fan2_status, fan3_status, temp1_status, temp2_status, temp3_status))
       except Exception as excp:
           print('Something went wrong!')
           print(excp)
above code is working, is there any scope for improvement.
Reply
#2
I don't think this is correct comparison fan1_status < str(2000), specifically conversion of 2000 to str. what are possible fan_status values and when is OK and when Fail?
Reply
#3
FAN speed is returning value like below

snmpwalk -v2c -c public 10.217.129.116 .1.3.6.1.4.1.890.1.5.13.1.2.1.1.2.0
SNMPv2-SMI::enterprises.890.1.5.13.1.2.1.1.2.0.1 = INTEGER: 4333
SNMPv2-SMI::enterprises.890.1.5.13.1.2.1.1.2.0.2 = INTEGER: 4397
SNMPv2-SMI::enterprises.890.1.5.13.1.2.1.1.2.0.3 = INTEGER: 4386

< 2000 RPM means fan malfunctioning and > 2000 RPM means FAN is working fine.

Fan Values can be 0 (if stopped) to > 4200 RPM if working properly.
Reply
#4
#!/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_bulk(fans_oids)]
            temp_status = [temp.value for value in session.get_bulk(temps_oids)]
            print('{} {} {} {} {} {} {} {}'.format(sr_no, ip, *fans_status, *temp_status))            
        except Exception as excp:
            print('Something went wrong!')
            print(excp)
conversion to int - int(fan.value) may not be required if returned values is actually an integer - INTEGER: 4333.
I assume it is still str, so I convert it.
some of the above can be done a bit different, e.g. there could be better way to construct oids, but I'm not familiar with the module - just reading the docs
Reply
#5
Thanks Buran,
showing syntax error

File "zyxelenvstatus.py", line 17
print('{} {} {} {} {} {} {} {}'.format(sr_no, ip, *fans_status, *temp_status))
^
SyntaxError: invalid syntax
Reply
#6
sorry, my mistake

#!/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' for int(fan.value) < 2000 else 'Ok' for fan in session.get_bulk(fans_oids)]
            temp_status = [temp.value 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('Something went wrong!')
            print(excp)
Reply
#7
Sorry Buran, its still showing syntax error

[root@localhost scripts]# python zyxelenvstatus.py
File "zyxelenvstatus.py", line 15
fans_status = ['Fail' for int(fan.value) < 2000 else 'Ok' for fan in session.get_bulk(fans_oids)]
^
SyntaxError: invalid syntax
Reply
#8
Sorry, I'm not able to test the code. First for should be if.

#!/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_bulk(fans_oids)]
            temp_status = [temp.value 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('Something went wrong!')
            print(excp)
Reply
#9
due to below error
invalid literal for int() with base 10: 'fan1'
Connection time out!
invalid literal for int() with base 10: 'fan1'
 
fans_status=['Fail'if int(fan.value) <2000else'Ok'forfaninsession.get_bulk(fans_oids)]
removed int

now issue is that... it should print temperature.. like 43,49,50 etc but its printing as 

249 10.217.128.93 Ok Ok Ok Ok Ok Ok
Reply
#10
I don't use fan1 at all, and from your example output in post#3 it's not present either. so probably you are doing something wrong. would you post the code you are running in full. Also I don't have "OK" for temperatures, so again something is different from my code.
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 977 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,866 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,850 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