]> git.ozlabs.org Git - ppp.git/blob - scripts/lcp_rtt_exporter
6fab745f2bffb0fc0483b34c4000097fbf08dbeb
[ppp.git] / scripts / lcp_rtt_exporter
1 #!/usr/bin/perl
2 # vim: shiftwidth=4 tabstop=4
3 #
4 # This CGI program is a Prometheus exporter for pppd's lcp-rtt-file feature.
5 #
6 # Copyright (C) Marco d'Itri <md@linux.it>
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12
13 use v5.14;
14 use warnings;
15 use autodie;
16
17 use List::Util qw(sum max min);
18
19 {
20         my $data = read_data('/run/ppp-rtt.data');
21         my $stats = compute_statistics($data, 60);
22
23         my $s = metrics($stats);
24
25         print "Content-type: text/plain\n\n$s";
26         exit;
27 }
28
29 sub metrics {
30         my ($stats) = @_;
31
32         my $s = <<END;
33 # TYPE lcp_rtt_status gauge
34 # HELP LCP RTT status
35 lcp_rtt_status $stats->{status}
36 END
37         foreach (qw(average min max loss)) {
38                 next if not exists $stats->{$_};
39                 $s .= <<END;
40 # TYPE lcp_rtt_$_ gauge
41 # HELP LCP RTT $_
42 lcp_rtt_$_ $stats->{$_}
43 END
44         }
45
46         return $s;
47 }
48
49 sub compute_statistics {
50         my ($data, $length) = @_;
51
52         my $cutoff = time() - $length;
53         my @e = grep { $_->[0] >= $cutoff } @{ $data->{data} };
54         return { status => -1 } if not @e; # no data
55
56         my $average = (sum map { $_->[1] } @e) / scalar(@e);
57         my $min = min map { $_->[1] } @e;
58         my $max = max map { $_->[1] } @e;
59         my $loss = sum map { $_->[2] } @e;
60
61         return {
62                 status  => $data->{status},
63                 average => $average,
64                 min             => $min,
65                 max             => $max,
66                 loss    => $loss,
67         };
68 }
69
70 sub read_data {
71         my ($file) = @_;
72
73         my $data;
74         open(my $fh, '<', $file);
75         binmode($fh);
76         my $bytes_read;
77         do {
78                 $bytes_read = sysread($fh, $data, 8192, length($data));
79         } while ($bytes_read == 8192);
80         close($fh);
81
82         my ($magic, $status, $position, $echo_interval, $rest)
83                 = unpack('NNNN a*', $data);
84         return undef if $magic != 0x19450425;
85
86         # the position is relative to the C array, not to the logical entries
87         $position /= 2;
88
89         my @rawdata = unpack('(N C a3)*', $rest);
90         my @data;
91         while (my ($time, $loss, $rtt) = splice(@rawdata, 0, 3)) {
92                 push(@data, [ $time, unpack('N', "\000$rtt"), $loss ]);
93         }
94
95         @data =
96                 # skip any "empty" (null) entries
97                 grep { $_->[0] }
98                 # rearrange the list in chronological order
99                 (@data[$position+1 .. $#data], @data[0 .. $position]);
100
101         return {
102                 status                  => $status,
103                 echo_interval   => $echo_interval,
104                 position                => $position,
105                 data                    => \@data,
106         };
107 }
108