Skip to content

Instantly share code, notes, and snippets.

@tomasinouk
Created May 4, 2016 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomasinouk/11f75651fd8ea95d0db16cac642aa66f to your computer and use it in GitHub Desktop.
Save tomasinouk/11f75651fd8ea95d0db16cac642aa66f to your computer and use it in GitHub Desktop.
Retrieve Tx, Rx metrics from cellular interface on Cybertec routers via SNMP and print out data.
from pysnmp.hlapi import *
import time
import datetime
# seconds between polls
interval = 60
# IP address of the Cybertec router
ip_address = '10.10.10.10'
# while True:
def readSnmp():
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpEngine(),
CommunityData('public'),
UdpTransportTarget((ip_address, 161)),
ContextData(),
# ObjectType(ObjectIdentity('1.3.6.1.2.1.1.6.0')),
# ObjectType(ObjectIdentity('1.3.6.1.2.1.1.8.0')),
ObjectType(ObjectIdentity('1.3.6.1.2.1.31.1.1.1.6.7')), # cellular IN Bytes IF-MIB::ifHCInOctets.7
ObjectType(ObjectIdentity('1.3.6.1.2.1.31.1.1.1.7.7')), # IN unicast packets IF-MIB::ifHCInUcastPkts.7
ObjectType(ObjectIdentity('1.3.6.1.2.1.31.1.1.1.10.7')), # OUT cellular bytes IF-MIB::ifHCOutOctets.7
ObjectType(ObjectIdentity('1.3.6.1.2.1.31.1.1.1.11.7')) # # OUT unicast packets IF-MIB::ifHCOutUcastPkts.7
)
)
router_metrics_reading = []
if errorIndication:
print(errorIndication)
return False
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
return False
else:
for varBind in varBinds:
# print(' = '.join([x.prettyPrint() for x in varBind]))
# for x in varBind:
# print x.prettyPrint()
# print "value: " + str(varBind[1])
router_metrics_reading.append(int(varBind[1]))
# i += 1
# save in dictionary for later use with JSON or CSV file
# to keep metrics during restarts, etc.
# router_metrics["CellBytesRx"] = router_metrics_reading[0]
# router_metrics["CellPacketsRx"] = router_metrics_reading[1]
# router_metrics["CellBytesTx"] = router_metrics_reading[2]
# router_metrics["CellPacketsTx"] = router_metrics_reading[3]
return router_metrics_reading
if __name__ == '__main__':
while True:
router_metrics = [0,0,0,0]
router_metrics_reading = readSnmp()
if(router_metrics_reading != False):
if(router_metrics_reading[1] < router_metrics[1]):
# add new reads to current metrics
print("Adding to current values")
router_metrics[0] += router_metrics_reading[0]
router_metrics[1] += router_metrics_reading[1]
router_metrics[2] += router_metrics_reading[2]
router_metrics[3] += router_metrics_reading[3]
else:
# initial state
router_metrics = router_metrics_reading
print("Session has not restarted")
print router_metrics
x = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
print x
print("Cellar Bytes Recived : " + str(float(router_metrics[0])/1024) + " kB")
print("Cellar Packet Recived: " + str(router_metrics[1]) + " ")
print("Cellar Bytes Transmit : " + str(float(router_metrics[2])/1024) + " kB")
print("Cellar Packet Transmit: " + str(router_metrics[3]) + " ")
# sleep for 60 sec
time.sleep(interval)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment