root / plugins / network / multi_tcp_ping @ dd4afac8
Historique | Voir | Annoter | Télécharger (4,09 ko)
| 1 | 2969fd43 | Gunnar Wolf | #!/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 | use threads; |
||
| 76 | use Net::Ping; |
||
| 77 | my (%defaults, @hosts, $cmd_arg); |
||
| 78 | |||
| 79 | %defaults = (port => 80, timeout => 2, unreachable => -0.01); |
||
| 80 | @hosts = get_hosts($ENV{hosts});
|
||
| 81 | die "Hosts not set - cannot continue\n" unless @hosts; |
||
| 82 | |||
| 83 | $cmd_arg = $ARGV[0] || ''; |
||
| 84 | config() if($cmd_arg eq "config"); |
||
| 85 | autoconf() if ($cmd_arg eq 'autoconf'); |
||
| 86 | |||
| 87 | for my $host (@hosts) {
|
||
| 88 | threads->new(\&ping_host, $host) |
||
| 89 | } |
||
| 90 | |||
| 91 | map {$_->join} threads->list;
|
||
| 92 | exit 0; |
||
| 93 | |||
| 94 | sub ping_host {
|
||
| 95 | my ($host, $addr, $p, $ret, $time, $ip); |
||
| 96 | $host = shift; |
||
| 97 | $addr = host_label_for($host); |
||
| 98 | |||
| 99 | $p=Net::Ping->new("tcp", $defaults{timeout});
|
||
| 100 | $p->hires(); |
||
| 101 | $p->{port_num} = $host->[1] || $defaults{port};
|
||
| 102 | |||
| 103 | ($ret, $time, $ip) = $p->ping($host->[0]); |
||
| 104 | |||
| 105 | $time = $defaults{unreachable} if !$ret;
|
||
| 106 | print "${addr}.value $time\n";
|
||
| 107 | } |
||
| 108 | |||
| 109 | sub get_hosts {
|
||
| 110 | # Hosts are defined in the 'hosts' environment variable. It's a list of |
||
| 111 | # hosts (and optionally ports) - We parse the list and arrange it neatly |
||
| 112 | # to be easily consumed. |
||
| 113 | my ($hostsdef, @hosts); |
||
| 114 | $hostsdef = shift; |
||
| 115 | return unless $hostsdef; |
||
| 116 | |||
| 117 | for my $host (split(/,/, $hostsdef)) {
|
||
| 118 | $host =~ s/\s//g; |
||
| 119 | |||
| 120 | $host =~ /^(?:([^:]+)) |
||
| 121 | (?::(\d+))?$/x; |
||
| 122 | |||
| 123 | push @hosts, [$1, $2 || $defaults{port}];
|
||
| 124 | |||
| 125 | } |
||
| 126 | |||
| 127 | return @hosts; |
||
| 128 | } |
||
| 129 | |||
| 130 | sub config {
|
||
| 131 | my @res = ("graph_title TCP connection times",
|
||
| 132 | "graph_args --base 1000 -l 0", |
||
| 133 | "graph_vlabel seconds", |
||
| 134 | "graph_category network", |
||
| 135 | "graph_info Shows the time to establish a TCP connection"); |
||
| 136 | for my $host (@hosts) {
|
||
| 137 | my $addr = host_label_for($host); |
||
| 138 | push @res, "$addr.label $addr"; |
||
| 139 | push @res, "$addr.draw LINE2"; |
||
| 140 | push @res, "$addr.info Time to establish TCP connection to " . |
||
| 141 | "$host->[0]:$host->[1]"; |
||
| 142 | |||
| 143 | } |
||
| 144 | |||
| 145 | print map {"$_\n"} @res;
|
||
| 146 | exit 0; |
||
| 147 | } |
||
| 148 | |||
| 149 | sub autoconf {
|
||
| 150 | print "yes\n"; |
||
| 151 | exit 0; |
||
| 152 | } |
||
| 153 | |||
| 154 | sub host_label_for {
|
||
| 155 | my ($ip, $port) = @{$_[0]};
|
||
| 156 | # Periods and colonsare not allowed in variable names |
||
| 157 | my $addr = "src_${ip}_${port}";
|
||
| 158 | $addr =~ s/\./_/g; |
||
| 159 | return $addr; |
||
| 160 | } |
