]> git.ozlabs.org Git - ppp.git/blob - scripts/lcp_rtt_dump
CI: Updated the 'checkout' actions that were using Node.js 16 to Node.js 20. (#489)
[ppp.git] / scripts / lcp_rtt_dump
1 #!/usr/bin/perl
2 # vim: shiftwidth=4 tabstop=4
3 #
4 # This program dumps to standard output the content of the file written
5 # by pppd's lcp-rtt-file configuration option.
6 #
7 # Copyright (C) Marco d'Itri <md@linux.it>
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13
14 use v5.14;
15 use warnings;
16 use autodie;
17
18 use POSIX qw(strftime);
19
20 {
21         my $data = read_data($ARGV[0] || '/run/ppp-rtt.data');
22         die "The data file is invalid!\n" if not $data;
23         dump_data($data);
24 }
25
26 sub dump_data {
27         my ($s) = @_;
28
29         say "status:   $s->{status}";
30         say "interval: $s->{echo_interval}";
31         say "position: $s->{position}";
32         say 'elements: ' . scalar(@{ $s->{data} });
33         say '';
34
35         foreach (my $i= 0; $i < @{ $s->{data} }; $i++) {
36                 my $date = strftime('%F %T', localtime($s->{data}->[$i]->[0]));
37                 print "$i\t$date\t$s->{data}->[$i]->[1]\t$s->{data}->[$i]->[2]\n";
38         }
39 }
40
41 sub read_data {
42         my ($file) = @_;
43
44         my $data;
45         open(my $fh, '<', $file);
46         binmode($fh);
47         my $bytes_read;
48         do {
49                 $bytes_read = sysread($fh, $data, 8192, length($data));
50         } while ($bytes_read == 8192);
51         close($fh);
52
53         my ($magic, $status, $position, $echo_interval, $rest)
54                 = unpack('NNNN a*', $data);
55         return undef if $magic != 0x19450425;
56
57         # the position is relative to the C array, not to the logical entries
58         $position /= 2;
59
60         my @rawdata = unpack('(N C a3)*', $rest);
61         my @data;
62         while (my ($time, $loss, $rtt) = splice(@rawdata, 0, 3)) {
63                 push(@data, [ $time, unpack('N', "\000$rtt"), $loss ]);
64         }
65
66         if (0) {
67         @data =
68                 # skip any "empty" (null) entries
69                 grep { $_->[0] }
70                 # rearrange the list in chronological order
71                 (@data[$position+1 .. $#data], @data[0 .. $position]);
72         }
73
74         return {
75                 status                  => $status,
76                 echo_interval   => $echo_interval,
77                 position                => $position,
78                 data                    => \@data,
79         };
80 }
81