Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / ping / multi_tcp_ping @ 1fc177ce

Historique | Voir | Annoter | Télécharger (4,35 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->service_check(1);
109
    $p->{port_num} = $host->[1] || $defaults{port};
110

    
111
    ($ret, $time, $ip) = $p->ping($host->[0]);
112

    
113
    $time = $defaults{unreachable} if !$ret;
114
    print "${addr}.value $time\n";
115
}
116

    
117
sub get_hosts {
118
    # Hosts are defined in the 'hosts' environment variable. It's a list of
119
    # hosts (and optionally ports) - We parse the list and arrange it neatly
120
    # to be easily consumed.
121
    my ($hostsdef, @hosts);
122
    $hostsdef = shift;
123
    return unless $hostsdef;
124

    
125
    for my $host (split(/,/, $hostsdef)) {
126
	$host =~ s/\s//g;
127

    
128
	$host =~ /^(?:([^:]+))
129
	    (?::(\d+))?$/x;
130

    
131
	push @hosts, [$1, $2 || $defaults{port}];
132

    
133
    }
134

    
135
    return @hosts;
136
}
137

    
138
sub config {
139
    my @res = ("graph_title TCP connection times",
140
	       "graph_args --base 1000 -l 0",
141
	       "graph_vlabel seconds",
142
	       "graph_category network",
143
	       "graph_info Shows the time to establish a TCP connection");
144
    for my $host (@hosts) {
145
	my $addr = host_label_for($host);
146
	push @res, "$addr.label $addr";
147
	push @res, "$addr.draw LINE2";
148
	push @res, "$addr.info Time to establish TCP connection to " .
149
	    "$host->[0]:$host->[1]";
150

    
151
    }
152

    
153
    print map {"$_\n"} @res;
154
    exit 0;
155
}
156

    
157
sub autoconf {
158
    print "yes\n";
159
    exit 0;
160
}
161

    
162
sub host_label_for {
163
    my ($ip, $port) = @{$_[0]};
164
    # Periods and colonsare not allowed in variable names
165
    my $addr = "src_${ip}_${port}";
166
    $addr =~ s/\./_/g;
167
    return $addr;
168
}