-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathffmap
executable file
·433 lines (370 loc) · 13.1 KB
/
ffmap
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#!/usr/bin/perl -w
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2014,2015 Alexander Dahl <[email protected]>
use strict;
use warnings;
use utf8; # this script is written in utf8
use lib $ENV{'MUNIN_LIBDIR'};
use Munin::Plugin;
use Carp;
use Data::Dumper;
use Encode qw(decode_utf8 encode);
use Log::Log4perl qw(:easy);
use Time::Piece;
use Time::Seconds;
use JSON;
use LWP::Simple;
Log::Log4perl->easy_init($WARN);
# some munin init
need_multigraph();
# data
my $nodes_ref;
my @state_vector = restore_state();
# all nodes and clients
my $sum_clientcount = 0;
my $node_count = 0;
my $sum_online_nodes = 0;
# firmware versions
my %firmware_count = ( 'zundef' => 0 );
my %firmware_name = ( 'zundef' => 'undef' );
my %last_seen_v;
# models
my %model_count = ( 'zundef' => 0 );
my %model_name = ( 'zundef' => 'undef' );
my %last_seen_h;
# clients on specific nodes
my @personal_nodes;
sub get_node_data {
my $logger = get_logger();
if ( defined $ENV{nodesurl} ) {
my $content = get( $ENV{nodesurl} );
croak 'Could not get it!' unless defined $content;
$nodes_ref = decode_json($content);
} else {
croak 'URL to get nodes.json not configured!';
}
my $nodes_age
= gmtime()
- Time::Piece->strptime( $nodes_ref->{'meta'}->{'timestamp'},
'%Y-%m-%dT%H:%M:%S' );
$logger->debug( "timestamp ", $nodes_ref->{'meta'}->{'timestamp'} );
if ( $nodes_age->minutes > 5 ) {
croak 'nodes.json data too old (' . $nodes_age->pretty . ')';
}
}
sub proc_node_data {
my $logger = get_logger();
foreach my $node ( @{ $nodes_ref->{'nodes'} } ) {
$logger->trace( "processing node id ", $node->{'id'} );
if (!( $node->{'flags'}->{'gateway'}
|| $node->{'flags'}->{'client'}
)
)
{
# number of online/offline nodes and client count
$node_count++;
$sum_clientcount += $node->{'clientcount'};
if ( $node->{'flags'}->{'online'} ) { $sum_online_nodes++ }
# firmware versions (not wanted for gateways and clients)
if ( defined $node->{'firmware'} ) {
my $fieldname
= clean_fieldname( 'v' . $node->{'firmware'} );
$logger->trace( "fw: ", $node->{'firmware'}, " - ",
$fieldname );
if ( exists( $firmware_count{$fieldname} ) ) {
$firmware_count{$fieldname}++;
} else {
$firmware_count{$fieldname} = 1;
}
if ( not exists $firmware_name{$fieldname} ) {
$firmware_name{$fieldname} = $node->{'firmware'};
}
} else {
$firmware_count{'zundef'}++;
}
# models (this is not wanted for gateways and clients)
if ( defined $node->{'hardware'} ) {
my $fieldname
= clean_fieldname( 'h' . $node->{'hardware'} );
$logger->trace( "hw: ", $node->{'hardware'}, " - ",
$fieldname );
if ( exists( $model_count{$fieldname} ) ) {
$model_count{$fieldname}++;
} else {
$model_count{$fieldname} = 1;
}
if ( not exists $model_name{$fieldname} ) {
$model_name{$fieldname} = $node->{'hardware'};
}
} else {
$model_count{'zundef'}++;
}
}
}
return;
}
sub count_personal_nodes_clients {
foreach my $node ( @{ $nodes_ref->{'nodes'} } ) {
foreach my $pers_coll_ref (@personal_nodes) {
foreach my $key ( keys %{$pers_coll_ref} ) {
if ( "$key" eq 'cname' ) { next; }
foreach my $id ( @{ $pers_coll_ref->{$key}->{ids} } ) {
if ( $node->{'id'} eq $id ) {
# print {*STDERR} "found " . $id . "\n";
$pers_coll_ref->{$key}->{clientcount}
= $node->{'clientcount'};
}
}
}
}
}
# print {*STDERR} Dumper( @personal_nodes );
return;
}
sub deserialize_state {
foreach my $element (@state_vector) {
# split line in variables
my ( $category, $timestamp, $fieldname, $value )
= split( /:/xms, $element, 4 );
# skip this entry if too old, it will not be written again later
my $entry_age
= gmtime() - Time::Piece->strptime( $timestamp, "%s" );
if ( $entry_age->years > 1 ) { next; }
# distinguish different categories and handle them
if ( $category eq 'last_seen_h' ) {
$last_seen_h{$fieldname} = $timestamp;
if ($value) {
$model_name{$fieldname} = $value;
}
$model_count{$fieldname} = 0;
} elsif ( $category eq 'last_seen_v' ) {
$last_seen_v{$fieldname} = $timestamp;
if ($value) {
$firmware_name{$fieldname} = $value;
}
$firmware_count{$fieldname} = 0;
} else {
next;
}
}
return;
}
sub update_last_seen_times {
foreach my $key ( keys %model_count ) {
if ( $model_count{$key} > 0 ) {
$last_seen_h{$key} = time();
}
}
foreach my $key ( keys %firmware_count ) {
if ( $firmware_count{$key} > 0 ) {
$last_seen_v{$key} = time();
}
}
return;
}
sub serialize_state {
my @new_state;
foreach my $key ( keys %last_seen_h ) {
if ( $key eq 'zundef' ) { next; }
push @new_state, join q{:}, 'last_seen_h', $last_seen_h{$key},
$key, $model_name{$key};
}
foreach my $key ( keys %last_seen_v ) {
if ( $key eq 'zundef' ) { next; }
push @new_state, join q{:}, 'last_seen_v', $last_seen_v{$key},
$key, $firmware_name{$key};
}
# print {*STDERR} Dumper(@new_state);
save_state(@new_state);
return;
}
sub read_personal_node_config {
if ( not defined $ENV{pers_collection_n} ) {
return;
}
for my $i ( 1 .. $ENV{pers_collection_n} ) {
my $node_n_key = 'pers_collection_' . $i . '_node_n';
if ( not defined $ENV{$node_n_key} ) {
print {*STDERR} "$node_n_key not set in config\n";
next;
}
my $name_key = 'pers_collection_' . $i . '_name';
if ( not defined $ENV{$name_key} ) {
print {*STDERR} "$name_key not set in config\n";
next;
}
$personal_nodes[ $i - 1 ]{'cname'}
= decode_utf8( $ENV{$name_key}, Encode::FB_CROAK );
# print {*STDERR} "node_n_key: $node_n_key\n";
for my $j ( 1 .. $ENV{$node_n_key} ) {
my $node_x_name_key
= 'pers_collection_' . $i . '_node_' . $j . '_name';
my $node_x_ids_key
= 'pers_collection_' . $i . '_node_' . $j . '_ids';
if ( not defined $ENV{$node_x_name_key} ) {
print {*STDERR} "$node_x_name_key not set in config\n";
next;
}
if ( not defined $ENV{$node_x_ids_key} ) {
print {*STDERR} "$node_x_ids_key not set in config\n";
next;
}
my $fieldname
= clean_fieldname( 'n' . $ENV{$node_x_name_key} );
$personal_nodes[ $i - 1 ]{$fieldname} = {
'name' => decode_utf8(
$ENV{$node_x_name_key}, Encode::FB_CROAK
),
'ids' => [ split( /,/xms, $ENV{$node_x_ids_key} ) ],
'clientcount' => 0,
};
}
}
# print {*STDERR} Dumper( @personal_nodes );
return;
}
### main
read_personal_node_config();
# process data
deserialize_state();
if ( $ARGV[0] and $ARGV[0] eq "config" ) {
# Nodes and Clients
print "multigraph ffmap_all_nodes\n";
print "graph_title Freifunk Nodes and Clients\n";
print "graph_args --base 1000 --lower-limit 0\n";
print "graph_scale no\n";
print "graph_category freifunk\n";
print "graph_vlabel #\n";
print "online.label online\n";
print "online.type GAUGE\n";
print "online.draw AREASTACK\n";
print "online.min 0\n";
print "online.colour COLOUR0\n";
print "online.info Number of online nodes.\n";
print "offline.label offline\n";
print "offline.type GAUGE\n";
print "offline.draw AREASTACK\n";
print "offline.min 0\n";
print "offline.colour COLOUR8\n";
print "offline.info Number of offline nodes.\n";
print "clients.label clients\n";
print "clients.type GAUGE\n";
print "clients.min 0\n";
print "clients.colour COLOUR1\n";
print "clients.info Sum of clients over all nodes.\n";
# Personal Nodes
foreach my $pers_coll_ref (@personal_nodes) {
my $clean_name = clean_fieldname( $pers_coll_ref->{'cname'} );
print "multigraph ffmap_personal_nodes_$clean_name\n";
print encode(
'iso-8859-1',
"graph_title Personal Freifunk Nodes -- $pers_coll_ref->{cname}\n",
Encode::FB_CROAK
);
print "graph_args --base 1000 --lower-limit 0\n";
print "graph_scale no\n";
print "graph_category freifunk\n";
print "graph_vlabel Clients\n";
foreach my $key ( sort keys %{$pers_coll_ref} ) {
if ( "$key" eq 'cname' ) { next; }
print encode(
'iso-8859-1',
"${key}.label " . $pers_coll_ref->{$key}->{name} . "\n",
Encode::FB_CROAK
);
print "${key}.draw AREASTACK\n";
print "${key}.min 0\n";
}
my $fieldname = 'sum_' . $clean_name;
print "${fieldname}.label sum\n";
print "${fieldname}.type GAUGE\n";
print "${fieldname}.min 0\n";
print encode(
'iso-8859-1',
"${fieldname}.info Sum of all clients on personal nodes collection \""
. $pers_coll_ref->{cname} . "\".\n",
Encode::FB_CROAK
);
print "${fieldname}.colour COLOUR16\n";
}
# Node Models
print "multigraph ffmap_hardware\n";
print "graph_title Freifunk Node Hardware\n";
print "graph_args --base 1000 --lower-limit 0\n";
print "graph_scale no\n";
print "graph_category freifunk\n";
print "graph_vlabel Nodes\n";
foreach my $key ( sort keys %model_name ) {
print "${key}.label " . $model_name{$key} . "\n";
print "${key}.draw AREASTACK\n";
print "${key}.min 0\n";
if ( $key eq 'zundef' ) {
print "${key}.colour DDDDDD\n";
}
}
# Node Firmware Versions
print "multigraph ffmap_firmware_versions\n";
print "graph_title Freifunk Node Firmware Versions\n";
print "graph_args --base 1000 --lower-limit 0\n";
print "graph_scale no\n";
print "graph_category freifunk\n";
print "graph_vlabel Nodes\n";
foreach my $key ( sort keys %firmware_name ) {
print "${key}.label " . $firmware_name{$key} . "\n";
print "${key}.draw AREASTACK\n";
print "${key}.min 0\n";
if ( $key eq 'zundef' ) {
print "${key}.colour DDDDDD\n";
}
}
exit 0;
}
get_node_data();
proc_node_data();
update_last_seen_times();
serialize_state();
# all nodes graph values
my $offline_nodes = $node_count - $sum_online_nodes;
print "multigraph ffmap_all_nodes\n";
print "online.value $sum_online_nodes\n";
print "offline.value $offline_nodes\n";
print "clients.value $sum_clientcount\n";
# process personal nodes/clients graph
count_personal_nodes_clients();
foreach my $pers_coll_ref (@personal_nodes) {
my $clean_name = clean_fieldname( $pers_coll_ref->{'cname'} );
my $sum = 0;
print "multigraph ffmap_personal_nodes_$clean_name\n";
foreach my $key ( sort keys %{$pers_coll_ref} ) {
if ( "$key" eq 'cname' ) { next; }
print $key
. '.value '
. $pers_coll_ref->{$key}->{clientcount} . "\n";
$sum += $pers_coll_ref->{$key}->{clientcount};
}
my $fieldname = clean_fieldname( 'sum_' . $pers_coll_ref->{cname} );
print "${fieldname}.value ${sum}\n";
}
# models
print "multigraph ffmap_hardware\n";
foreach my $key ( sort keys %model_count ) {
print "${key}.value " . $model_count{$key} . "\n";
}
# firmware
print "multigraph ffmap_firmware_versions\n";
foreach my $key ( sort keys %firmware_count ) {
print "${key}.value " . $firmware_count{$key} . "\n";
}
# vim: set ft=perl et sts=0 ts=4 sw=4 sr: