[go: up one dir, main page]

Menu

[8f8cab]: / ping.pl  Maximize  Restore  History

Download this file

109 lines (89 with data), 2.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
#!/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++;
}