#!/usr/bin/perl # Script: test-ping.pl # # Description: Send test pings to remote server from Linux and HP-UX servers # Results are saved in CSV- and ASCII-format files # # Last Update: 25 September 2013 # Designed by: Dusan U. Baljevic (dusan.baljevic@ieee.org) # Coded by: Dusan U. Baljevic (dusan.baljevic@ieee.org) # # Copyright 2013-2015 Dusan Baljevic # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # Define important environment variables # $ENV{'PATH'} = "/usr/bin:/usr/sbin:/bin:/sbin"; if ( eval "require Net::Ping" ) { import Net::Ping; } else { warn "WARN: Perl module Net::Ping not found\n"; } if ( eval "require Time::Local" ) { import Time::Local; } else { warn "WARN: Perl module Time::Local not found\n"; } # Display usage if "-h" option is used # sub Prusage { print <&1`; ( $System, $Hostname, $undef ) = split( /\s+/, $VH ); # Enforce strictness # if ( eval "require strict" ) { import strict; use strict; no strict 'refs'; } else { print "WARN: Perl strict not found\n"; } # Make sure strictness is enforced # use vars qw($System $Hostname $COMMAND $PING_ARG @abbr $REMHOST $Sec $Min $Hour $DayOfMonth $Month $Year $DayOfWeek $EPOCHTIME $DayofYear $IsDST $PACKETLOSS @PCKLOSS $RTT @RTTARR $REMSERV); # Remote server to test with ICMP (ping) # if ( ! "$REMHOST" ) { $REMSERV="192.168.55.87"; } else { $REMSERV = "$REMHOST"; } # CVS file to save results every five minutes # my $SAVEFILE = "/tmp/$Hostname-$REMSERV.csv"; # ASCII file to save results every five minutes # my $SAVETXT = "/tmp/$Hostname-$REMSERV.txt"; # How long to sleep between two ping checks # #my $SLEEPTIME = 300; my $SLEEPTIME = 60; # Define Shell # $ENV{'SHELL'} = '/usr/bin/sh' if $ENV{'SHELL'} ne ''; $ENV{'IFS'} = '' if $ENV{'IFS'} ne ''; sub checktime { # Get current local time # ( $Sec, $Min, $Hour, $DayOfMonth, $Month, $Year, $DayOfWeek, $DayofYear, $IsDST ) = localtime; $EPOCHTIME = timelocal( $Sec, $Min, $Hour, $DayOfMonth, $Month, $Year ); @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); # Localtime returns January..December as 0..11 # $Month++; $Year = $Year + 1900; } if ( $System eq "HP-UX" ) { $PING_ARG="-n 5"; $COMMAND="ping $REMSERV $PING_ARG"; } elsif ( $System eq "Linux" ) { $PING_ARG="-c 5 -q"; $COMMAND="ping $PING_ARG $REMSERV"; } else { print "ERROR: This script supports Linux and HP-UX only\n"; exit(1); } sub loop_ping { if ( open( MPSCH, "$COMMAND 2>&1 | " ) ) { while () { next if grep( /PING|not found|statistics/, $_ ); # Get rid of leading and trailing empty spaces # $_ =~ s{\A \s* | \s* \z}{}gxm; chomp($_); if ( grep(/,/, $_) ) { ( undef, undef, $PACKETLOSS, undef ) = split( /,/, $_ ); $PACKETLOSS =~ s/^\s+//g; $PACKETLOSS =~ s/\s+$//g; @PCKLOSS = split( /\s+/, $PACKETLOSS ); $PCKLOSS[0] =~ s/%$//g; } if ( grep(/=/, $_) ) { ( undef, $RTT ) = split( /=/, $_ ); $RTT =~ s/^\s+//g; $RTT =~ s/\s+$//g; @RTTARR = split( /\//, $RTT ); } } } close(MPSCH); if ( ! -s "$SAVEFILE" ) { if ( open( SAVE, "> $SAVEFILE" ) ) { print SAVE "Datestamp,Local_Srv,Remote_Srv,Pct_Lost_Packets,RTT_Min,RTT_Avg,RTT_Max\n"; my $PP = sprintf("%s%s%s-%02d:%02d,%s,%s", $DayOfMonth, $abbr[$Month], $Year, $Hour, $Min, $Hostname, $REMSERV); print SAVE $PP; } } else { if ( open( SAVE, ">> $SAVEFILE" ) ) { my $PP = sprintf("%s%s%s-%02d:%02d,%s,%s", $DayOfMonth, $abbr[$Month], $Year, $Hour, $Min, $Hostname, $REMSERV); print SAVE $PP; } } if ( "@RTTARR" ) { print SAVE ",$PCKLOSS[0],$RTTARR[0],$RTTARR[1],$RTTARR[2]\n"; } else { $APPEND = ",,,"; print SAVE ",$PCKLOSS[0]$APPEND\n"; } close(SAVE); if ( ! -s "$SAVETXT" ) { if ( open( SAVE2, "> $SAVETXT" ) ) { print SAVE2 "#Description of filed in the file\n"; print SAVE2 "#Datestamp;Local_Srv;Remote_Srv;Pct_Lost_Packets;RTT_Min;RTT_Avg;RTT_Max\n"; my $SV = sprintf("%02d%s%04d-%02d:%02d %18s %18s", $DayOfMonth, $abbr[$Month], $Year, $Hour, $Min, $Hostname, $REMSERV); print SAVE2 "$SV"; } } else { if ( open( SAVE2, ">> $SAVETXT" ) ) { my $SV = sprintf("%02d%s%04d-%02d:%02d %18s %18s", $DayOfMonth, $abbr[$Month], $Year, $Hour, $Min, $Hostname, $REMSERV); print SAVE2 "$SV"; } } if ( "@RTTARR" ) { my $SV2 = sprintf("\t%5s\t%.3f\t%.3f\t%.3f\n", $PCKLOSS[0], $RTTARR[0], $RTTARR[1], $RTTARR[2]); print SAVE2 "$SV2"; } else { my $SV2 = sprintf("\t%5s\n", $PCKLOSS[0]); print SAVE2 "$SV2"; } close(SAVE2); } # Run forever unless interrupted... # while (1) { checktime(); loop_ping(); sleep($SLEEPTIME); } exit(0);