Skip to content

Instantly share code, notes, and snippets.

@omershtivi1
Created April 20, 2020 11:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omershtivi1/e8cdce6bf9cda51685656f7d0d32be93 to your computer and use it in GitHub Desktop.
Save omershtivi1/e8cdce6bf9cda51685656f7d0d32be93 to your computer and use it in GitHub Desktop.
A python script to get snmp info
import os
from pysnmp.entity.rfc3413.oneliner import cmdgen
import time
import datetime
def snmp_get_info(address:str, community: str, oid:str ) -> list:
cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
cmdgen.CommunityData(community),
cmdgen.UdpTransportTarget((address, 161)),
oid
)
return varBinds
# The router return values in octets per second convert to megabits
def from_octets_snmp_oid_to_megabits(octets_snmp: list) -> float:
octets_per_sec = str(octets_snmp[0]).split("=")[1]
return float(octets_per_sec) / 1000 / 1000 * 8
interface_oid = '1.3.6.1.2.1.31.1.1.1.6.514'
snmp_community = os.environ['SNMP_COMMUNITY']
device_address = '10.200.100.96'
# The router keep counts in how absolute octets and not overtime so get the base octets per seconds for time=0sec
snmp_value = snmp_get_info(device_address, snmp_community, interface_oid)
megabits = from_octets_snmp_oid_to_megabits(snmp_value)
print("Starting test.. time: ", datetime.datetime.fromtimestamp(time.time()).strftime('%H:%M:%S'))
# sample 10 times
for counter in range(0, 10):
time.sleep(3)
snmp_value = snmp_get_info(device_address, snmp_community, interface_oid)
five_sec_value = from_octets_snmp_oid_to_megabits(snmp_value)
# Divide by 3 in order to get a one second resolution
print(datetime.datetime.fromtimestamp(time.time()).strftime('%H:%M:%S - '),
round((five_sec_value - megabits)/3, 2), "mbits/sec")
megabits = five_sec_value
print("Done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment