Python Forum
Help required for faster execution of working code - 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: Help required for faster execution of working code (/thread-8187.html)



Help required for faster execution of working code - anna - Feb-08-2018

Hi All,

#!/usr/bin/python
import time
import sys
from easysnmp import snmp_get, snmp_set, snmp_walk
start = time.time()
host = sys.argv[1]
oid  = '.1.3.6.1.4.1.1286.1.3.18.2.1.1.1.7'
PortUpCount = 0
PortDownCount = 0

# Perform an SNMP walk
status= snmp_walk(oid, hostname=host, community='public', version=2)
#print (status)
portstatus = []
for item in status:
     portstatus.append(item.value)
PortDownCount = portstatus.count('2')
PortUpCount = portstatus.count('1')
print('PortDownCount:{} PortUpCount:{}'.format(PortDownCount,PortUpCount))
print 'It took', time.time()-start, 'seconds.'
this script is taking device ip as argument, post snmpwalk taking values in list and counting down and up port...

python EciPostStatus.py 10.114.1.20
PortDownCount:35 PortUpCount:29
It took 4.49605989456 seconds.
currently its taking 4 to 9 seconds, output is provided to cacti for graph. Is there way to make this more faster.


RE: Help required for faster execution of working code - buran - Feb-08-2018

try
import time
import sys
from easysnmp import snmp_get, snmp_set, snmp_walk
from collections import Counter
start = time.time()
host = sys.argv[1]
oid  = '.1.3.6.1.4.1.1286.1.3.18.2.1.1.1.7'
 
# Perform an SNMP walk
walk = snmp_walk(oid, hostname=host, community='public', version=2)
#print (status)
portstatus = [item.value for item in walk]
cntr = Counter(portstatus)
PortUpCount = cntr['1']
PortDownCount = cntr['2']
print('PortDownCount:{} PortUpCount:{}'.format(PortDownCount, PortUpCount))
print('It took', time.time()-start, 'seconds.')
in any case if there is no other status (i.e. only '1' or '2') in your code you don't need to count both


RE: Help required for faster execution of working code - anna - Feb-09-2018

thanks, there is only status 1 or 2 (up or down), modified code and working fine.