#!/usr/bin/perl -w
#
# Copyright 2008 Bjorn Ruberg
# Based on and shamelessly copied from the other squid_ Munin plugins
#
# 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.
#
#
# 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)
#
#%# family=auto
#%# capabilities=autoconf

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

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/info 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/info HTTP/1.0\r\n" .
	"Accept: */*\r\n" .
	&make_auth_header ($user, $passwd) .
	"\r\n";
    
    $cachemgr->syswrite ($request, length($request));
    my @lines = grep (/Mean Object Size/, $cachemgr->getlines());
    my $line = pop @lines;
    if ($line =~ /^.*?\:\s+(\d+(?:\.\d+)?)\s+(M|K)?B$/i) {
        $val  = $1;
        $unit = $2;
        if ($unit eq "K") { $val *= 1024; } elsif ($unit eq "M") { $val *= 1048576; }
        print "objectsize.value $val\n";
    }
}

if($ARGV[0] and $ARGV[0] eq "config") {
    print "graph_title Squid object size\n";
    print "graph_vlabel bytes\n";
    print "graph_args -l 0\n";
    print "graph_category squid\n";
    print "objectsize.label Object size\n";
    exit 0;
}

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

