dhcp.py.j2 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/python
  2. import datetime, collectd, os.path, ipaddress
  3. def parse_leases(lease_file):
  4. validKeys = ['lease', 'starts', 'ends', '}']
  5. leases = {}
  6. lease = {}
  7. for line in lease_file:
  8. tokens = line.split()
  9. if len(tokens) == 0 or tokens[0].startswith('#'):
  10. continue
  11. key = tokens[0].lower()
  12. if key in validKeys:
  13. if key == 'lease':
  14. lease = {'id' : tokens[1]}
  15. elif key == '}':
  16. if lease['id'] in leases:
  17. if leases[lease['id']]['ends'] < lease['ends']:
  18. leases[lease['id']] = lease
  19. else:
  20. leases[lease['id']] = lease
  21. else:
  22. lease[key] = tokens[2]+' '+tokens[3].rstrip(';')
  23. return leases
  24. def parse_dhcpd_conf(config_file):
  25. subnet = {}
  26. subnets = {}
  27. for line in config_file:
  28. line = line.strip().rstrip(';')
  29. tokens = line.split()
  30. if len(tokens) == 0 or tokens[0].startswith('#'):
  31. continue
  32. key = tokens[0].lower()
  33. if key == 'subnet':
  34. net = ipaddress.ip_network(unicode(tokens[1]+'/'+tokens[3]))
  35. subnet = {'netaddr_str' : tokens[1], 'netaddr_int' : int(net.network_address), 'netmask_str' : tokens[3], 'netmask_int' : int(net.netmask)}
  36. elif key == 'interface':
  37. subnet['interface'] = tokens[1]
  38. elif key == '}':
  39. subnet_add(subnets, subnet)
  40. subnet = {}
  41. return subnets
  42. def subnet_add(subnets, subnet):
  43. if 'interface' not in subnet:
  44. subnet['interface'] = 'global'
  45. sub_name = subnet['interface']
  46. if sub_name not in subnets:
  47. subnets[sub_name] = subnet
  48. else:
  49. pass #needs to be implemented
  50. def count_active_leases(all_leases, now, subnets):
  51. count_active = 0
  52. count_all = 0
  53. subnet_counter = {}
  54. for interface in subnets.iterkeys():
  55. subnet_counter[interface] = {'count_active': 0, 'count_all': 0, 'count_touch': 0}
  56. for lease in all_leases.itervalues():
  57. addr = int(ipaddress.ip_address(unicode(lease['id'])))
  58. for subnet in subnets.itervalues():
  59. if (addr & subnet['netmask_int']) == subnet['netaddr_int']:
  60. subnc = subnet_counter[subnet['interface']]
  61. subnc['count_all'] += 1
  62. count_all += 1
  63. if timestamp_inbetween(now, lease['starts'], lease['ends']):
  64. subnc['count_active'] += 1
  65. count_active += 1
  66. break
  67. for subnet in subnet_counter.itervalues():
  68. subnet['count_touch'] = subnet['count_all'] - subnet['count_active']
  69. count_touch = count_all - count_active
  70. return count_active, count_touch, subnet_counter
  71. def timestamp_inbetween(now, start, end):
  72. return start < now < end
  73. def read(data=None):
  74. configfile = '/etc/dhcp/dhcpd.conf'
  75. if os.path.isfile(configfile):
  76. cfile = open(configfile, 'r')
  77. config = parse_dhcpd_conf(cfile)
  78. leasefile = '/var/lib/dhcp/dhcpd.leases'
  79. if os.path.isfile(leasefile):
  80. lfile = open(leasefile, 'r')
  81. all_leases = parse_leases(lfile)
  82. lfile.close()
  83. count_active, count_touch, subnet_counter = count_active_leases(all_leases, datetime.datetime.utcnow().strftime("%Y/%m/%d %H:%M:%S"), config)
  84. vl = collectd.Values(type='dhcp_leases')
  85. vl.plugin='dhcp'
  86. vl.type_instance = 'all_interfaces'
  87. vl.dispatch(values=[count_active, count_touch])
  88. for k, v in subnet_counter.iteritems():
  89. vl = collectd.Values(type='dhcp_leases')
  90. vl.plugin='dhcp'
  91. vl.type_instance = k
  92. vl.dispatch(values=[v['count_active'], v['count_touch']])
  93. vl = collectd.Values(type='dhcp_leases')
  94. def write(vl, data=None):
  95. for i in vl.values:
  96. print "%s (%s): %f" % (vl.plugin, vl.type, i)
  97. collectd.register_read(read)
  98. collectd.register_write(write);