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


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

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)



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

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)



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

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


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

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/session_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.


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

Thanks Buran for your kind and quick support.


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

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


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

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)



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

Thanks... for such great help.


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

It was interesting to familiarize myself with this package. Took a bit more iterations than normal, but anyway....