Check ptp.pl

From Federal Burro of Information
Jump to navigationJump to search
#!/usr/bin/perl -w

use strict;

# tail the last line of the log file to this script:
# /var/log/ptpd2/stats.log

#log file format expected:
# Timestamp                 , State                        /Clock ID, One Way Dly, Off f Mstr ,  Slv to Mstr, Master to Slave, Drift, Last packet Received
# 2013-02-03 04:18:47.312826, slv f4ce46fffea688da(unknown)/01,  0.000000000,  0.000000000,  0.000000000,  0.000000000, 0, I
# 0    1  2  3  4  5  6           7                         8    9             10            11            12           13 14

# get the newest log line

my $DEBUG = 0 ;
my $error ; # store $?
my $newest = 600;
my $nm = 0 ; # number of matching lines.
my $maxage = 300;
my $offset; # in seconds
my $maxoffset = 0.001;
my $logfile = "/var/log/ptpd2/stats.log";

my ( $day  , $mon , $date , $time , $year) = split ( " " , scalar localtime());
my ( $hour , $min , $sec )  = split ( ":", $time );
my $sod = ( $hour * 3600 ) + ( $min * 60 ) + $sec ; # Second of day.

if ( ! -e $logfile ) {
        print "CRITICAL: Can\'t find file: $logfile : $!\n";
        exit 2;
}

if ( ! -r $logfile ) {
        print "CRITICAL: Can\'t read file: $logfile : $!\n";
        exit 2;
}

my @data = `tail -10 $logfile`;
$error = $?;

if ( $error ) {
        print "CRITICAL: Failed to tail file $logfile via backticks\n";
        exit 2;
}

# while (<>){
foreach ( @data ) {
    my @arr;
    #                   0          1      2       3      4      5       6          7     8       9         10        11        12        13     14
    if ( @arr = $_ =~ /(\d\d\d\d)-(\d\d)-(\d\d)\s(\d\d):(\d\d):(\d\d)\.(\d+), slv (.*)\/(.*),\s+(-*.+),\s+(-*.+),\s+(-*.+),\s+(-*.+),\s+(.*),\s+(\w)/ ) {
        $nm++;
        if ( $DEBUG ) {
        print $_;
        print "###########\n";
        print "Year:   ".$arr[0]."\n";
        print "month:  ".$arr[1]."\n";
        print "day:    ".$arr[2]."\n";
        print "hour:   ".$arr[3]."\n";
        print "minutes:".$arr[4]."\n";
        print "second: ".$arr[5]."\n";
        print "fract:  ".$arr[6]."\n";
        print "id:     ".$arr[7]."\n";
        print "clock:  ".$arr[8]."\n";
        print "One Way Dly:         ".$arr[9] ."\n";
        print "Off f Mstr:          ".$arr[10]."\n";
        print "Slv to Mstr:         ".$arr[11]."\n";
        print "Master to Slave:     ".$arr[12]."\n";
        print "Drift:               ".$arr[13]."\n";
        print "Last packet Received ".$arr[14]."\n";
        }
        my $lastlogtime = ( $arr[3] * 3600 ) + ( $arr[4] * 60 ) + $arr[5];
        #print "Now $sod\n";
        #print "Log line $lastlogtime\n";
        my $thisage = $sod - $lastlogtime ;
        # print "Age: $thisage\n";
        if ( $thisage < $newest ) {
            $newest = $thisage;
            $offset  =  $arr[10];
        }
    }
}

if ( $DEBUG ) {
    print "Age : $newest\n";
    print "Offset $offset\n";
    print "Number of matching lines $nm\n";
}

if ( $nm < 1 ) {
    print "CRITICAL: number of log lines that have stats less than 1\n";
    exit 2;
}

if ( $newest > $maxage ) {
    print "CRITICAL: newest log line is too old $newest seconds \> $maxage\n";
    exit 2;
}

if ( abs($offset) > $maxoffset ) {
    print "CRITICAL: Offset greater than maxoffset $offset \> $maxoffset \n";
    exit 2;
}


print "OK: Offset within bounds | 'offset'=${offset}s;${maxoffset};${maxoffset};0;1\n";
exit 0;