ipv4ipv6.py.j2 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/python
  2. import datetime, collectd, os.path
  3. def getStats():
  4. stats = {
  5. 'v4' : {},
  6. 'v6' : {}
  7. }
  8. prc = open('/proc/net/netstat', 'r')
  9. for line in prc.readlines():
  10. if 'IpExt' in line and 'InOctets' not in line:
  11. line = line.split()
  12. stats['v4']['rx'] = line[7]
  13. stats['v4']['tx'] = line[8]
  14. break
  15. prc.close()
  16. prc = open('/proc/net/snmp6', 'r')
  17. for line in prc.readlines():
  18. if 'Ip6InOctets' in line:
  19. stats['v6']['rx'] = line.split()[1]
  20. elif 'Ip6OutOctets' in line:
  21. stats['v6']['tx'] = line.split()[1]
  22. prc.close()
  23. return stats
  24. def read(data=None):
  25. stats = getStats()
  26. vl = collectd.Values(type='if_octets')
  27. vl.plugin='ip_traffic'
  28. vl.type_instance = 'v4'
  29. vl.dispatch(values=[stats['v4']['rx'], stats['v4']['tx']])
  30. vl = collectd.Values(type='if_octets')
  31. vl.plugin='ip_traffic'
  32. vl.type_instance = 'v6'
  33. vl.dispatch(values=[stats['v6']['rx'], stats['v6']['tx']])
  34. def write(vl, data=None):
  35. for i in vl.values:
  36. print "%s (%s): %f" % (vl.plugin, vl.type, i)
  37. collectd.register_read(read)
  38. collectd.register_write(write);