Python Forum
working code, suggestion required for improvement - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: working code, suggestion required for improvement (/thread-7211.html)

Pages: 1 2


working code, suggestion required for improvement - anna - Dec-28-2017

#!/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.


RE: working code, suggestion required for improvement - buran - Dec-28-2017

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?


RE: working code, suggestion required for improvement - anna - Dec-28-2017

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.


RE: working code, suggestion required for improvement - buran - Dec-28-2017

#!/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


RE: working code, suggestion required for improvement - anna - Dec-28-2017

Thanks Buran,
showing syntax error

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


RE: working code, suggestion required for improvement - buran - Dec-28-2017

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)



RE: working code, suggestion required for improvement - anna - Dec-28-2017

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


RE: working code, suggestion required for improvement - buran - Dec-28-2017

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)



RE: working code, suggestion required for improvement - anna - Dec-29-2017

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


RE: working code, suggestion required for improvement - buran - Dec-29-2017

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.