Skip to content

Instantly share code, notes, and snippets.

@tenfyzhong
Created July 6, 2017 07:55
Show Gist options
  • Save tenfyzhong/d7571d2752906ced9c62169b51b16b43 to your computer and use it in GitHub Desktop.
Save tenfyzhong/d7571d2752906ced9c62169b51b16b43 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import commands
import time
def usage():
print('./udpstat.py duration')
def gettime(t):
try:
return int(t)
except:
return 1
def stat():
if len(sys.argv) < 2:
usage()
sys.exit(-1)
packets_received = 0
packets_to_unknown_port_received = 0
packet_receive_errors = 0
packets_send = 0
RcvBufErrors = 0
SndbufErrors = 0
while 1:
(status, output) = commands.getstatusoutput('grep Udp: /proc/net/snmp')
if status != 0:
print('grep error')
sys.exit(-2)
counter_line = output.split("\n")[1]
counters = counter_line.split(' ')[1:-1]
tmp = int(counters[0]) - packets_received
print('packets received: %s, increase: %d' % (counters[0], tmp))
packets_received = tmp
tmp = int(counters[1]) - packets_to_unknown_port_received
print('packets to unknown port received: %s, increase: %d' %
(counters[1], tmp))
packets_to_unknown_port_received = tmp
tmp = int(counters[2]) - packet_receive_errors
print('packet receive errors: %s, increase: %d' % (counters[2], tmp))
packet_receive_errors = tmp
tmp = int(counters[3]) - packets_send
print('packets send: %s, increase: %d' % (counters[3], tmp))
packets_send = tmp
tmp = int(counters[4]) - RcvBufErrors
print('RcvBufErrors: %s, increase: %d' % (counters[4], tmp))
RcvBufErrors = tmp
tmp = int(counters[5]) - SndbufErrors
print('SndbufErrors: %s, increase: %d' % (counters[5], tmp))
SndbufErrors = tmp
time.sleep(gettime(sys.argv[1]))
print('')
if __name__ == '__main__':
stat()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment