Debian Codegraph @

scripts/munin/n2n_

raw
1 #!/usr/bin/env perl
2 use warnings;
3 use strict;
4 #
5 # Requires
6 # libjson-perl
7 #
8
9 # Magic Markers
10 #
11 #%# family=auto
12 #%# capabilities=autoconf suggest
13
14 package JsonUDP;
15 use warnings;
16 use strict;
17
18 use IO::Socket::INET;
19 use JSON;
20
21 sub new {
22 my $class = shift;
23 my $port = shift || 5644;
24 my $self = {};
25 bless($self, $class);
26
27 $self->{sock} = IO::Socket::INET->new(
28 PeerAddr => '127.0.0.1',
29 PeerPort => $port,
30 Proto => 'udp',
31 );
32 $self->{json} = JSON->new->utf8->relaxed->pretty->canonical;
33 $self->{tag} = 0;
34 $self->{debug} = 0;
35 return $self;
36 }
37
38 sub _tx {
39 my $self = shift;
40 my $msgline = shift;
41 return $self->{sock}->send($msgline);
42 }
43
44 sub _rx {
45 my $self = shift;
46 my $tag = shift;
47
48 my $db = [];
49 my $error;
50
51 while(1) {
52 my $jsontxt;
53 $self->{sock}->recv($jsontxt,1024);
54 if ($self->{debug}) {
55 print($jsontxt);
56 }
57 my $msg = $self->{json}->decode($jsontxt);
58
59 # ignore packets not for us
60 if ($msg->{_tag} ne $tag) {
61 next;
62 }
63
64 # Save most recent error for return
65 if ($msg->{_type} eq 'error') {
66 $error = $msg;
67 next;
68 }
69
70 if ($msg->{_type} eq 'end') {
71 if ($error) {
72 # TODO: an error channel
73 return undef;
74 }
75 return $db;
76 }
77
78 if ($msg->{_type} eq 'row') {
79 delete $msg->{_tag};
80 delete $msg->{_type};
81 push @$db, $msg;
82 next;
83 }
84
85 # Ignore any unknown _type
86 }
87 }
88
89 sub read {
90 my $self = shift;
91 my $cmdline = shift;
92 my $tag = $self->{tag}++;
93
94 # TODO:
95 # Add a read cache
96
97 $self->_tx(sprintf("r %i %s", $tag, $cmdline));
98 return $self->_rx($tag);
99 }
100
101 1;
102
103 package main;
104 use warnings;
105 use strict;
106
107 my $config = {
108 edge_pkts => {
109 p2p_tx_pkt => {
110 label => 'Peer to Peer tx rate',
111 type => 'DERIVE',
112 min => 0,
113 },
114 p2p_rx_pkt => {
115 label => 'Peer to Peer rx rate',
116 type => 'DERIVE',
117 min => 0,
118 },
119 super_tx_pkt => {
120 label => 'Peer to Supernode tx rate',
121 type => 'DERIVE',
122 min => 0,
123 },
124 super_rx_pkt => {
125 label => 'Peer to Supernode rx rate',
126 type => 'DERIVE',
127 min => 0,
128 },
129 super_broadcast_tx_pkt => {
130 label => 'Broadcast to Supernode tx rate',
131 type => 'DERIVE',
132 min => 0,
133 },
134 super_broadcast_rx_pkt => {
135 label => 'Broadcast to Supernode rx rate',
136 type => 'DERIVE',
137 min => 0,
138 },
139 transop_tx_pkt => {
140 label => 'Transform tx rate',
141 type => 'DERIVE',
142 min => 0,
143 },
144 transop_rx_pkt => {
145 label => 'Transform rx rate',
146 type => 'DERIVE',
147 min => 0,
148 },
149 },
150 edge_counts => {
151 edges => {
152 label => 'Current known peers',
153 type => 'GAUGE',
154 },
155 supernodes => {
156 label => 'Current known supernodes',
157 type => 'GAUGE',
158 },
159 },
160 supernode_pkts => {
161 errors_tx_pkt => {
162 label => 'Error rate',
163 type => 'DERIVE',
164 min => 0,
165 },
166 reg_super_rx_pkt => {
167 label => 'Connect rate',
168 type => 'DERIVE',
169 min => 0,
170 },
171 reg_super_nak => {
172 label => 'Connect error rate',
173 type => 'DERIVE',
174 min => 0,
175 },
176 forward_tx_pkt => {
177 label => 'Packets forwarded rate',
178 type => 'DERIVE',
179 min => 0,
180 },
181 broadcast_tx_pkt => {
182 label => 'Broadcast packet rate',
183 type => 'DERIVE',
184 min => 0,
185 },
186 },
187 supernode_counts => {
188 edges => {
189 label => 'Current known edges',
190 type => 'GAUGE',
191 },
192 communities => {
193 label => 'Current known communities',
194 type => 'GAUGE',
195 },
196 },
197 };
198
199 my $fetchinfo = {
200 edge_pkts => {
201 port => 5644,
202 read => "packetstats",
203 },
204 edge_counts => {
205 port => 5644,
206 count => [
207 "edges",
208 "supernodes",
209 ],
210 },
211 supernode_pkts => {
212 port => 5645,
213 read => "packetstats",
214 },
215 supernode_counts => {
216 port => 5645,
217 count => [
218 "edges",
219 "communities",
220 ],
221 },
222 };
223
224 sub do_config {
225 my $rpc = shift;
226 my $name = shift;
227
228 print("graph_title n2n $name status\n");
229 print("graph_category network\n");
230 my @names;
231 while (my ($fieldname, $field) = each(%{$config->{$name}})) {
232 push @names, $fieldname;
233 while (my ($key, $val) = each(%{$field})) {
234 print($fieldname.'.'.$key," ",$val,"\n");
235 }
236 }
237
238 # Ensure stable order
239 print("graph_order ", join(' ', sort(@names)), "\n");
240 }
241
242 sub do_fetch {
243 my $rpc = shift;
244 my $name = shift;
245 my $db;
246
247 my $read_table = $fetchinfo->{$name}->{read};
248 if (defined($read_table)) {
249 $db = $rpc->read($read_table);
250 for my $row (@$db) {
251 my $type = $row->{type};
252 delete $row->{type};
253 while (my ($key, $val) = each(%{$row})) {
254 my $metricname = $type."_".$key;
255 print($metricname,".value ",$val,"\n");
256 }
257 }
258 }
259
260 my $count_tables = $fetchinfo->{$name}->{count};
261 if (defined($count_tables)) {
262 for my $table (@{$count_tables}) {
263 $db = $rpc->read($table);
264 print($table,".value ", scalar(@$db), "\n");
265 }
266 }
267 }
268
269 sub do_autoconf {
270 # quick check to see if this plugin should be enabled
271 if (`pgrep supernode`) {
272 print("yes\n");
273 } elsif (`pgrep edge`) {
274 print("yes\n");
275 } else {
276 print("no - neither edge nor supernode are running\n");
277 }
278 }
279
280 sub do_suggest {
281 my $ports = {};
282 if (`pgrep supernode`) {
283 $ports->{5645}=1;
284 }
285 if (`pgrep edge`) {
286 $ports->{5644}=1;
287 }
288
289 while (my ($name, $info) = each(%{$fetchinfo})) {
290 my $port = $info->{port};
291 next if (!defined($port)); # this not a real fetchinfo
292 next if (!defined($ports->{$port})); # not linked to a running daemon
293 print($name,"\n");
294 }
295 }
296
297 my $subc = {
298 'fetch' => \&do_fetch,
299 'config' => \&do_config,
300 'autoconf' => \&do_autoconf,
301 'suggest' => \&do_suggest,
302 };
303
304 sub main() {
305 my $name = $ARGV[1] || $0;
306 $name =~ s%^.*/n2n_([^/]+)%$1%;
307
308 my $port = $fetchinfo->{$name}->{port};
309 my $rpc = JsonUDP->new($port);
310
311 my $cmd = $ARGV[0];
312 if (!defined($cmd)) {
313 $cmd = 'fetch';
314 }
315
316 my $func = $subc->{$cmd};
317 if (!defined($func)) {
318 die("bad sub command");
319 }
320
321 return $func->($rpc, $name);
322 }
323 main();
324