2 # vim: shiftwidth=4 tabstop=4
4 # This CGI program is a Prometheus exporter for pppd's lcp-rtt-file feature.
6 # Copyright (C) Marco d'Itri <md@linux.it>
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.
17 use List::Util qw(sum max min);
20 my $data = read_data('/run/ppp-rtt.data');
21 my $stats = compute_statistics($data, 60);
23 my $s = metrics($stats);
25 print "Content-type: text/plain\n\n$s";
33 # TYPE lcp_rtt_status gauge
35 lcp_rtt_status $stats->{status}
37 foreach (qw(average min max loss)) {
38 next if not exists $stats->{$_};
40 # TYPE lcp_rtt_$_ gauge
42 lcp_rtt_$_ $stats->{$_}
49 sub compute_statistics {
50 my ($data, $length) = @_;
52 my $cutoff = time() - $length;
53 my @e = grep { $_->[0] >= $cutoff } @{ $data->{data} };
54 return { status => -1 } if not @e; # no data
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;
62 status => $data->{status},
74 open(my $fh, '<', $file);
78 $bytes_read = sysread($fh, $data, 8192, length($data));
79 } while ($bytes_read == 8192);
82 my ($magic, $status, $position, $echo_interval, $rest)
83 = unpack('NNNN a*', $data);
84 return undef if $magic != 0x19450425;
86 # the position is relative to the C array, not to the logical entries
89 my @rawdata = unpack('(N C a3)*', $rest);
91 while (my ($time, $loss, $rtt) = splice(@rawdata, 0, 3)) {
92 push(@data, [ $time, unpack('N', "\000$rtt"), $loss ]);
96 # skip any "empty" (null) entries
98 # rearrange the list in chronological order
99 (@data[$position+1 .. $#data], @data[0 .. $position]);
103 echo_interval => $echo_interval,
104 position => $position,