root / plugins / ping / multi_tcp_ping @ d2161137
Historique | Voir | Annoter | Télécharger (4,33 ko)
| 1 |
#!/usr/bin/perl |
|---|---|
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
multi_tcp_ping - Graphs together the TCP ping results for several hosts |
| 6 |
|
| 7 |
=head1 SYNOPSIS |
| 8 |
|
| 9 |
This plugin is meant to be called from Munin. You should set the |
| 10 |
'hosts' environment variable from Munin's configuration (i.e. |
| 11 |
/etc/munin/munin.conf) to specify which hosts and ports to query. |
| 12 |
|
| 13 |
=head1 DESCRIPTION |
| 14 |
|
| 15 |
This plugin expects to receive the following environment variables: |
| 16 |
|
| 17 |
=over 4 |
| 18 |
|
| 19 |
=item hosts (REQUIRED!) |
| 20 |
|
| 21 |
Comma-separated list of hosts to query. You can specify the TCP port |
| 22 |
to connect to on each of the hosts by listing them as host:port - The |
| 23 |
port defaults to 80. The following is a valid hosts declaration: |
| 24 |
|
| 25 |
hosts='192.168.0.15, 192.168.0.18:22' |
| 26 |
|
| 27 |
It will query host 192.168.0.15 on the default port (80), as well as |
| 28 |
host 192.168.0.18 on port 22. |
| 29 |
|
| 30 |
=back |
| 31 |
|
| 32 |
If the connection was opened successfully, it gives as the return |
| 33 |
value the time it took to establish the connection. If the requested |
| 34 |
host is not reachable, a hard-wired '-0.01' will be returned. Why |
| 35 |
-0.01? Because giving a negative value is the best way to easily get |
| 36 |
-visually- that something failed. Connection establishment times are |
| 37 |
usually in the 5-500ms range. 100ms will be not too little (and thus |
| 38 |
invisible), not too much (and thus killing the details in our graphs). |
| 39 |
|
| 40 |
=head1 DEPENDS ON |
| 41 |
|
| 42 |
L<Net::Ping> |
| 43 |
|
| 44 |
=head1 SEE ALSO |
| 45 |
|
| 46 |
L<munin>, L<munin-node> |
| 47 |
|
| 48 |
=head1 AUTHOR |
| 49 |
|
| 50 |
Gunnar Wolf <gwolf@gwolf.org> |
| 51 |
|
| 52 |
=head1 COPYRIGHT |
| 53 |
|
| 54 |
|
| 55 |
Copyright 2008 Gunnar Wolf, Instituto de Investigaciones |
| 56 |
Economicas, UNAM. This plugin is Free Software; you can |
| 57 |
redistribute it and/or modify it under the terms of the GNU General |
| 58 |
Public License as published by the Free Software Foundation; version 2 |
| 59 |
dated June, 1991, or any later version (at your choice). |
| 60 |
|
| 61 |
This program is distributed in the hope that it will be useful, but |
| 62 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 63 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 64 |
General Public License for more details. |
| 65 |
|
| 66 |
You should have received a copy of the GNU General Public License |
| 67 |
along with this program; if not, write to the Free Software |
| 68 |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 69 |
USA. |
| 70 |
|
| 71 |
=cut |
| 72 |
|
| 73 |
use strict; |
| 74 |
use warnings; |
| 75 |
|
| 76 |
# This evil "eval" is to make Travis CI able to test the plugin syntax |
| 77 |
# without having a perl built with threads. |
| 78 |
# |
| 79 |
# Also: The use of interpreter-based threads in perl is officially |
| 80 |
# discouraged. |
| 81 |
eval 'use threads; 1;' or die 'Could not use threads'; |
| 82 |
|
| 83 |
use Net::Ping; |
| 84 |
my (%defaults, @hosts, $cmd_arg); |
| 85 |
|
| 86 |
%defaults = (port => 80, timeout => 2, unreachable => -0.01); |
| 87 |
@hosts = get_hosts($ENV{hosts});
|
| 88 |
die "Hosts not set - cannot continue\n" unless @hosts; |
| 89 |
|
| 90 |
$cmd_arg = $ARGV[0] || ''; |
| 91 |
config() if($cmd_arg eq "config"); |
| 92 |
autoconf() if ($cmd_arg eq 'autoconf'); |
| 93 |
|
| 94 |
for my $host (@hosts) {
|
| 95 |
threads->new(\&ping_host, $host) |
| 96 |
} |
| 97 |
|
| 98 |
map {$_->join} threads->list;
|
| 99 |
exit 0; |
| 100 |
|
| 101 |
sub ping_host {
|
| 102 |
my ($host, $addr, $p, $ret, $time, $ip); |
| 103 |
$host = shift; |
| 104 |
$addr = host_label_for($host); |
| 105 |
|
| 106 |
$p=Net::Ping->new("tcp", $defaults{timeout});
|
| 107 |
$p->hires(); |
| 108 |
$p->{port_num} = $host->[1] || $defaults{port};
|
| 109 |
|
| 110 |
($ret, $time, $ip) = $p->ping($host->[0]); |
| 111 |
|
| 112 |
$time = $defaults{unreachable} if !$ret;
|
| 113 |
print "${addr}.value $time\n";
|
| 114 |
} |
| 115 |
|
| 116 |
sub get_hosts {
|
| 117 |
# Hosts are defined in the 'hosts' environment variable. It's a list of |
| 118 |
# hosts (and optionally ports) - We parse the list and arrange it neatly |
| 119 |
# to be easily consumed. |
| 120 |
my ($hostsdef, @hosts); |
| 121 |
$hostsdef = shift; |
| 122 |
return unless $hostsdef; |
| 123 |
|
| 124 |
for my $host (split(/,/, $hostsdef)) {
|
| 125 |
$host =~ s/\s//g; |
| 126 |
|
| 127 |
$host =~ /^(?:([^:]+)) |
| 128 |
(?::(\d+))?$/x; |
| 129 |
|
| 130 |
push @hosts, [$1, $2 || $defaults{port}];
|
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
return @hosts; |
| 135 |
} |
| 136 |
|
| 137 |
sub config {
|
| 138 |
my @res = ("graph_title TCP connection times",
|
| 139 |
"graph_args --base 1000 -l 0", |
| 140 |
"graph_vlabel seconds", |
| 141 |
"graph_category network", |
| 142 |
"graph_info Shows the time to establish a TCP connection"); |
| 143 |
for my $host (@hosts) {
|
| 144 |
my $addr = host_label_for($host); |
| 145 |
push @res, "$addr.label $addr"; |
| 146 |
push @res, "$addr.draw LINE2"; |
| 147 |
push @res, "$addr.info Time to establish TCP connection to " . |
| 148 |
"$host->[0]:$host->[1]"; |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
print map {"$_\n"} @res;
|
| 153 |
exit 0; |
| 154 |
} |
| 155 |
|
| 156 |
sub autoconf {
|
| 157 |
print "yes\n"; |
| 158 |
exit 0; |
| 159 |
} |
| 160 |
|
| 161 |
sub host_label_for {
|
| 162 |
my ($ip, $port) = @{$_[0]};
|
| 163 |
# Periods and colonsare not allowed in variable names |
| 164 |
my $addr = "src_${ip}_${port}";
|
| 165 |
$addr =~ s/\./_/g; |
| 166 |
return $addr; |
| 167 |
} |
