#!/usr/bin/perl -w
#
# Cache traffic
#
# 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: squid_traffic.in,v $
# 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.7  2003/12/18 17:26:26  jimmyo
# Remove use of "use"
#
# Revision 1.6  2003/11/10 18:41:33  jimmyo
# Removed Data::Dumper dependency.
#
# Revision 1.5  2003/11/07 21:36:25  jimmyo
# Fixed typos and mental typos
#
# Revision 1.4  2003/11/07 17:43:16  jimmyo
# Cleanups and log entries
#
#
#
#%# family=auto
#%# capabilities=autoconf

# $Id: squid_traffic.in,v 1.2 2004/03/14 11:33:13 auduny Exp $

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 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 $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 bps\n";
    print "graph_order kbytes_in kbytes_out hit_kbytes_out\n";
    print "kbytes_in.label received\n";
    print "kbytes_in.cdef kbytes_in,8096,*\n";
    print "kbytes_in.type COUNTER\n";
    print "kbytes_out.label sent\n";
    print "kbytes_out.cdef kbytes_out,8096,*\n";
    print "kbytes_out.type COUNTER\n";
    print "hit_kbytes_out.label from cache\n";	
    print "hit_kbytes_out.type COUNTER\n";	
    exit 0;
}

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



# vim:syntax=perl
