-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnodes2graphite.py
executable file
·47 lines (40 loc) · 1.41 KB
/
nodes2graphite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
# developed by @fpletz for @freifunkMUC
# https://github.com/freifunkMUC/stats-tools/blob/master/nodes2graphite.py
import sys
import json
import pickle
import struct
from time import time
def get_metrics(timestamp, stats, prefix=''):
for k,v in stats.items():
key = '.'.join([prefix, k])
if type(v) is dict:
for i in get_metrics(timestamp, v, key):
yield i
elif type(v) is not str:
yield (key, (timestamp, v))
def load_metrics(f):
nodes = json.load(f)
ts = int(time())
online_nodes = 0
total_clients = 0
for (node_id,node_data) in nodes['nodes'].items():
for m in get_metrics(ts, node_data['statistics'], 'nodes.' + node_id):
yield m
if 'clients' in node_data['statistics']:
total_clients += node_data['statistics']['clients']
if node_data['flags']['online']: online_nodes += 1
yield ('node-stats.online', (ts, online_nodes))
yield ('node-stats.clients', (ts, total_clients))
def get_pickled_msg(metrics):
payload = pickle.dumps(list(metrics), protocol=2)
header = struct.pack("!L", len(payload))
return header + payload
def main():
with open(sys.argv[1], encoding='utf-8') as data_file:
metrics = load_metrics(data_file)
for (k,(t,v)) in metrics:
print(' '.join(map(str, [k,v,t])))
if __name__ == '__main__':
main()