Skip to content

Instantly share code, notes, and snippets.

@descilla
Created January 14, 2017 19:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save descilla/1541f295aef05e4cec433804a04b5818 to your computer and use it in GitHub Desktop.
Save descilla/1541f295aef05e4cec433804a04b5818 to your computer and use it in GitHub Desktop.
Script for getting isc-kea stats from control-socket
#!/usr/bin/python
import socket,os,json
BUFF_SIZE = 1024
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect("/var/kea/control.sock")
s.send('{"command":"statistic-get-all","arguments":{}}')
result = ""
part = s.recv(BUFF_SIZE)
while len(part) == BUFF_SIZE:
result += part
part = s.recv(1024)
result += part
json = json.loads(result)
resDict = {}
resDict[u'all_interfaces'] = dict([('total-addresses',0),('assigned-addresses',0),('declined-addresses',0),('declined-reclaimed-addresses',0),('reclaimed-leases',0),('reclaimed-declined-addresses',0)])
for k,v in json["arguments"].iteritems():
k = k.split('.')
interface = k[0].replace('subnet[','bat').replace(']','')
if len(k) != 2:
continue
if interface not in resDict:
resDict[interface] = dict()
resDict[interface][k[1]] = v[0][0]
resDict['all_interfaces'][k[1]] += v[0][0]
for k,v in resDict.iteritems():
print(k,v)
s.close()
@tmalkowski
Copy link

tmalkowski commented Feb 9, 2018

Thanks! This saved me a ton of time trying to get a proof of concept to work from.

I created a quick&dirty PHP script to convert statistics to graphite format, maybe others will find it useful. Doesn't reset counters or actually send to graphite, but that's easy enough to add on.

https://gist.github.com/tmalkowski/4f2d8f37dd5ab40fd4c6fcab75dbc2d0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment