#!/usr/bin/perl -w
###############################################################################
# Webmin Sysstats - ping.pl
#
# Webmin Sysstats Module
# Copyright (C) 2002 by Eric Gerbier
# Bug reports to: gerbier@users.sourceforge.net
# $Id$
#
# 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 2 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.
#
###############################################################################
use strict;
# to be used alone or from sysstat.pl without warning
# because require verify the path
require '../../gen-lib.pl' if ( !exists $ENV{WEBMINSTAT_TEMP} );
use vars qw( $chrono_start $chrono_stop );
use RRDs;
use Net::Ping;
# ping default timeout
my $timeout = 2;
# available network ping connexion
my @ping_protocols = ( 'icmp', 'udp', 'tcp' );
my %config;
read_file_cached( 'config', \%config );
my $param = 'ping';
my $ping_num = 1;
while ( exists( $config{ $param . $ping_num } ) ) {
my @tab = split( /,/, $config{ $param . $ping_num } );
my $real_ping_num = $tab[0]; # database number
my $ping_proto = $tab[1]; # tcp, udp, icmp
my $ping_name = $tab[2]; # display name
my $ping_host = $tab[3]; # hostname or ip adresse
my $runstop = $tab[5]; # control parameter aquisistion
#debug( "ping = $ping");
my $total = 'U';
if ( is_param_runstop($runstop) ) {
# shell code : can have problem with time-outs
# if ( open( PING, "ping -c1 -q -w $timeout $ping_host | " ) ) {
# while (<PING>) {
# if (m/^rtt min\/avg\/max\/mdev = (\d+\.\d*)\/(\d+\.\d*)/) {
# $total = $1;
# last;
# }
# }
# close(PING);
# }
# else {
# warning( "can not execute ping : $!");
# }
# perl code with Net::Ping module and my chrono subroutines
# control network
my $proto;
foreach my $t (@ping_protocols) {
if ( $ping_proto eq $t ) {
$proto = $ping_proto;
last;
}
}
if ( !$proto ) {
$proto = 'icmp';
debug('use default protocol : icmp');
}
my $p = Net::Ping->new($proto);
my $debut = &$chrono_start();
my $status = $p->ping( $ping_host, $timeout );
my $delta = &$chrono_stop($debut);
$total = $delta if ($status);
}
else {
debug("ping $real_ping_num disable");
}
debug(
"ping ($ping_name $real_ping_num $ping_host $ping_proto): total=$total"
);
my $rrdbase = $real_ping_num . '.rrd';
RRDs::update( $rrdbase, "N:$total" );
my $ERR = RRDs::error();
warning("ERROR while updating $rrdbase: $ERR") if $ERR;
$ping_num++;
}