Python Forum
Help required for faster execution of working code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help required for faster execution of working code
#1
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.
Reply
#2
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
Reply
#3
thanks, there is only status 1 or 2 (up or down), modified code and working fine.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Different code execution times Wirbelwind94 4 738 Oct-06-2023, 12:30 PM
Last Post: snippsat
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 953 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 872 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 1,001 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,066 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  faster code for my code kucingkembar 19 3,186 Aug-09-2022, 09:48 AM
Last Post: DPaul
  In consistency in code execution Led_Zeppelin 1 1,107 Jun-27-2022, 03:00 AM
Last Post: deanhystad
  My Code isn't working... End3r 4 1,914 Mar-21-2022, 10:12 AM
Last Post: End3r
  I don't undestand why my code isn't working. RuyCab 2 1,978 Jun-17-2021, 03:06 PM
Last Post: RuyCab
  code is not working , can anybody help? RandomPerson69 4 2,891 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