Repository
Munin (2.0)
Last change
2018-09-18
Graph Categories
Family
auto
Capabilities
Language
Perl
Authors

squid_traffic

Name

squid_traffic - Plugin to monitor squid cache traffic

Configuration

Configuration variables:

squidhost   - host (default "localhost")
squidport   - port (default "3128")
squiduser   - username (default "")
squidpasswd - password (default "")

Author

Copyright (C) 2004 Jimmy Olsen, Audun Ytterdal, Tore Anderson

License

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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Magic Markers

#%# family=auto
#%# capabilities=autoconf
#!@@PERL@@ -w
# -*- perl -*-

=head1 NAME

squid_traffic - Plugin to monitor squid cache traffic

=head1 CONFIGURATION

Configuration variables:

 squidhost   - host (default "localhost")
 squidport   - port (default "3128")
 squiduser   - username (default "")
 squidpasswd - password (default "")

=head1 AUTHOR

Copyright (C) 2004 Jimmy Olsen, Audun Ytterdal, Tore Anderson

=head1 LICENSE

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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

=head1 MAGIC MARKERS

 #%# family=auto
 #%# capabilities=autoconf

=cut

my $ret = undef;

if (! eval "require IO::Socket;")
{
	$ret = "IO::Socket not found";
}
if (! eval "require MIME::Base64;")
{
	$ret = "MIME::Base64 not found";
}

$squid_host = $ENV{squidhost} || "localhost";
$squid_port = $ENV{squidport} || 3128;
$user = $ENV{squiduser} || "";
$passwd = $ENV{squidpasswd} || "";

$target = "client_http\.(kbytes_in|kbytes_out|hit_kbytes_out) =";

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 0;
    }

    my $cachemgr = IO::Socket::INET->new(PeerAddr => $host,
					PeerPort => $port,
					Proto    => 'tcp',
					Timeout  => 5);

    if (!$cachemgr)
    {
	print "no (could not connect: $!)\n";
	exit 0;
    }

    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 $cachemgr = IO::Socket::INET->new(PeerAddr => $host,
					PeerPort => $port,
					Proto    => 'tcp') or die($!);



    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();
    for(my $i = 0; $i <= $#lines; $i++) {
	if($lines[$i] =~ /$target\s+(-?\d+)/) {
	    print "$1.value $2\n";
	}
    }
}

if($ARGV[0] and $ARGV[0] eq "config") {
    print "graph_title Squid traffic status\n";
    print "graph_args --base 1000\n";
    print "graph_vlabel bits per \${graph_period}\n";
    print "graph_order kbytes_in kbytes_out hit_kbytes_out\n";
    print "graph_category squid\n";
    print "kbytes_in.label received\n";
    print "kbytes_in.cdef kbytes_in,8096,*\n";
    print "kbytes_in.type DERIVE\n";
    print "kbytes_in.min 0\n";
    print "kbytes_in.max 500000\n";
    print "kbytes_out.label sent\n";
    print "kbytes_out.cdef kbytes_out,8096,*\n";
    print "kbytes_out.type DERIVE\n";
    print "kbytes_out.min 0\n";
    print "kbytes_out.max 500000\n";
    print "hit_kbytes_out.label from cache\n";
    print "hit_kbytes_out.cdef hit_kbytes_out,8096,*\n";
    print "hit_kbytes_out.type DERIVE\n";
    print "hit_kbytes_out.min 0\n";
    print "hit_kbytes_out.max 500000\n";
    exit 0;
}

&query_squid($squid_host, $squid_port, $user, $passwd, $target);



# vim:syntax=perl