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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
#!@@PERL@@ -w
#
# Copyright (C) 2004 Jimmy Olsen
#
# 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; version 2 dated June,
# 1991.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#
# When using squid as a "load balancer" (of sorts), who gets the
# request?
#
# Configuration variables:
#
# squidhost - host (default "localhost")
# squidport - port (default "3128")
# squiduser - username (default "")
# squidpasswd - password (default "")
#
# Parameters:
#
# config (required)
# autoconf (optional - only used by munin-config)
#
# $Log$
# Revision 1.8 2004/12/10 18:51:43 jimmyo
# linux/apt* has been forced to LANG=C, to get predictable output.
#
# Revision 1.7 2004/12/10 10:47:47 jimmyo
# Change name from ${scale} to ${graph_period}, to be more consistent.
#
# Revision 1.6 2004/12/09 22:12:55 jimmyo
# Added "graph_period" option, to make "graph_sums" usable.
#
# Revision 1.5 2004/11/16 20:18:09 jimmyo
# License cleanups.
#
# Revision 1.4 2004/09/26 22:03:52 jimmyo
# Changd COUNTER -> DERIVE for some plugins. Set min/max values.
#
# Revision 1.3 2004/05/20 13:57:12 jimmyo
# Set categories to some of the plugins.
#
# Revision 1.2 2004/03/14 11:33:13 auduny
# Added squidpatch from Jacques Caruso
#
# Revision 1.1 2004/01/02 18:50:00 jimmyo
# Renamed occurrances of lrrd -> munin
#
# Revision 1.1.1.1 2004/01/02 15:18:07 jimmyo
# Import of LRRD CVS tree after renaming to Munin
#
# Revision 1.6 2003/12/18 17:26:26 jimmyo
# Remove use of "use"
#
# Revision 1.5 2003/11/10 18:41:33 jimmyo
# Removed Data::Dumper dependency.
#
# Revision 1.4 2003/11/07 21:36:25 jimmyo
# Fixed typos and mental typos
#
# Revision 1.3 2003/11/07 17:43:16 jimmyo
# Cleanups and log entries
#
#
#
#%# family=manual
#%# capabilities=autoconf
# $Id: squid_icp.in 860 2005-03-29 20:32:59Z ilmari $
my $ret = undef;
if (! eval "require IO::Socket;")
{
$ret = "IO::Socket not found";
}
if (! eval "require MIME::Base64;")
{
$ret = "MIME::Base64 not found";
}
if (! eval "require Net::hostent;")
{
$ret = "Net::hostent not found";
}
$squid_host = $ENV{squidhost} || "localhost";
$squid_port = $ENV{squidport} || 3128;
$user = $ENV{squiduser} || "";
$passwd = $ENV{squidpasswd} || "";
if($ARGV[0] and $ARGV[0] eq "autoconf") {
&autoconf($squid_host, $squid_port, $user, $passwd);
}
sub autoconf {
my ($host, $port, $user, $passwd) = @_;
if ($ret)
{
print "no ($ret)\n";
exit 1;
}
my $cachemgr = IO::Socket::INET->new(PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp');
if (!$cachemgr)
{
print "no (could not connect: $!)\n";
exit 1;
}
my $request = "GET cache_object://$host/counters HTTP/1.0\r\n" .
"Accept: */*\r\n" .
&make_auth_header($user, $passwd) .
"\r\n";
$cachemgr->syswrite($request, length($request));
my @lines = $cachemgr->getlines();
print "yes\n";
exit 0;
}
sub make_auth_header {
my ($user, $passwd) = @_;
if(!defined $passwd || $passwd eq "") {
return "";
} else {
my $auth = MIME::Base64::encode_base64(($user ? $user : "") . ":$passwd", "");
return "Authorization: Basic $auth\r\n" .
"Proxy-Authorization: Basic $auth\r\n";
}
}
sub query_squid {
my ($host, $port, $user, $passwd) = @_;
my $ret;
my $cachemgr = IO::Socket::INET->new(PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp') or die($!);
my $request = "GET cache_object://$host/server_list HTTP/1.0\r\n" .
"Accept: */*\r\n" .
&make_auth_header($user, $passwd) .
"\r\n";
$cachemgr->syswrite($request, length($request));
my @lines = $cachemgr->getlines();
my $id = "";
for(my $i = 0; $i <= $#lines; $i++) {
chomp $lines[$i];
if($lines[$i] =~ /Address[^:]+:\s*([\d\.]+)\s*$/) {
my $host = $1;
$id = "h" . $host;
$id =~ s/\.//g;
my $h;
if ($h = Net::hostent::gethost ($host)) {
$ret->{$id}->{host} = lc $h->name;
} else {
$ret->{$id}->{host} = $host;
}
}
elsif($lines[$i] =~ /FETCHES\s*:\s*(\d+)/) {
$ret->{$id}->{fetches} = $1;
}
}
return $ret;
}
my $hosts = &query_squid($squid_host, $squid_port, $user, $passwd);
if($ARGV[0] and $ARGV[0] eq "config") {
my $first = 1;
print "graph_title Squid relay statistics\n";
print "graph_vlabel requests / \${graph_period}\n";
print "graph_args -l 0 --base 1000\n";
print "graph_total total\n";
print "graph_category squid\n";
foreach my $i (sort keys %{$hosts}) {
print "$i.label ", $hosts->{$i}->{host}, "\n";
print "$i.type DERIVE\n";
print "$i.max 500000\n";
print "$i.min 0\n";
if ($first) {
print "$i.draw AREA\n";
$first = 0;
} else {
print "$i.draw STACK\n";
}
}
exit 0;
}
foreach my $i (keys %{$hosts}) {
print "$i.value ", $hosts->{$i}->{fetches}, "\n";
}
# vim:syntax=perl
|