-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.py
More file actions
executable file
·126 lines (95 loc) · 3.36 KB
/
update.py
File metadata and controls
executable file
·126 lines (95 loc) · 3.36 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
#
# ffmonitor - a monitoring tool for freifunk networks
# Copyright (C) 2013 Leo Krueger
#
# This file is part of ffmonitor.
#
# ffmonitor is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ffmonitor is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with ffmonitor. If not, see <http://www.gnu.org/licenses/>.
#
from NameDB import WriteableNameDB
import data
import rrd
import log
logger = log.init_custom_logger('monitor')
namedb = WriteableNameDB()
# get (new) json data from ff server
data, date = data.get_ff_data()
links = data['links']
nodes = data['nodes']
# init connection counters for each node
for node in nodes:
node['vpn'] = 0
node['wifilink'] = 0
node['client'] = 0
# count connections for each node
for link in links:
# sanity check
if not link['type'] in ('vpn', 'client', None):
# something is wrong, but maybe not critical
logger.error('unexpected link type: %s', link['type'])
continue
if link['type'] is None:
link['type'] = 'wifilink'
# increase link target and source counters
nodes[link['source']][link['type']] += 1
nodes[link['target']][link['type']] += 1
# init
num_clients = 0
num_gateways = 0
num_gateways_off = 0
num_nodes = 0
num_nodes_off = 0
num_nodes_geo = 0
num_nodes_geo_off = 0
def get_clean_id(id):
# removes colons from node id
return str(id.replace(':', ''))
for node in nodes:
flags = node['flags']
# clients are always online, so we do not need to check that
if flags['client']:
num_clients += 1
else:
# gateways and nodes are treated equally
if flags['online']:
clean_id = get_clean_id(node['id'])
clean_id = namedb.get_id(clean_id)
namedb.save_name(node['name'], clean_id, node['macs'])
# is a gateway
if flags['gateway']:
num_gateways += 1
# is a node
else:
num_nodes += 1
# decrease number of clients by one,
# as the node itself is also counted as its client
if node['client'] > 0:
node['client'] -= 1
if not node['geo'] is None:
num_nodes_geo += 1
rrd.update_node(clean_id, date, node['client'], node['wifilink'], node['vpn'])
else:
# delete rrd files for nodes offline > 1 month?
# create rrd file for nodes never seen before?
# rrd.check_offline_node(clean_id)
if flags['gateway']:
num_gateways_off += 1
else:
num_nodes_off += 1
if not node['geo'] is None:
num_nodes_geo_off += 1
# nodes are no clients
num_clients -= num_nodes
rrd.update_total(date, num_nodes, num_clients, num_gateways, num_nodes_off, num_nodes_geo, num_nodes_geo_off)