~ubuntu-branches/ubuntu/gutsy/munin/gutsy

« back to all changes in this revision

Viewing changes to node/node.d/squid_icp.in

  • Committer: Bazaar Package Importer
  • Author(s): Tore Anderson
  • Date: 2004-05-21 20:51:19 UTC
  • Revision ID: james.westby@ubuntu.com-20040521205119-oz8bllbjp9hs80ig
Tags: upstream-0+1.0.0pre5
ImportĀ upstreamĀ versionĀ 0+1.0.0pre5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!@@PERL@@ -w
 
2
#
 
3
# When using squid as a "load balancer" (of sorts), who gets the
 
4
# request?
 
5
#
 
6
# Configuration variables:
 
7
#
 
8
#       squidhost    - host (default "localhost")
 
9
#       squidport    - port (default "3128")
 
10
#       squiduser    - username (default "")
 
11
#       squidpasswd  - password (default "")
 
12
#
 
13
# Parameters:
 
14
#
 
15
#       config    (required)
 
16
#       autoconf  (optional - only used by munin-config)
 
17
#
 
18
# $Log: squid_icp.in,v $
 
19
# Revision 1.2  2004/03/14 11:33:13  auduny
 
20
# Added squidpatch from Jacques Caruso
 
21
#
 
22
# Revision 1.1  2004/01/02 18:50:00  jimmyo
 
23
# Renamed occurrances of lrrd -> munin
 
24
#
 
25
# Revision 1.1.1.1  2004/01/02 15:18:07  jimmyo
 
26
# Import of LRRD CVS tree after renaming to Munin
 
27
#
 
28
# Revision 1.6  2003/12/18 17:26:26  jimmyo
 
29
# Remove use of "use"
 
30
#
 
31
# Revision 1.5  2003/11/10 18:41:33  jimmyo
 
32
# Removed Data::Dumper dependency.
 
33
#
 
34
# Revision 1.4  2003/11/07 21:36:25  jimmyo
 
35
# Fixed typos and mental typos
 
36
#
 
37
# Revision 1.3  2003/11/07 17:43:16  jimmyo
 
38
# Cleanups and log entries
 
39
#
 
40
#
 
41
#
 
42
#%# family=manual
 
43
#%# capabilities=autoconf
 
44
 
 
45
# $Id: squid_icp.in,v 1.2 2004/03/14 11:33:13 auduny Exp $
 
46
 
 
47
my $ret = undef;
 
48
 
 
49
if (! eval "require IO::Socket;")
 
50
{
 
51
        $ret = "IO::Socket not found";
 
52
}
 
53
if (! eval "require MIME::Base64;")
 
54
{
 
55
        $ret = "MIME::Base64 not found";
 
56
}
 
57
if (! eval "require Net::hostent;")
 
58
{
 
59
        $ret = "Net::hostent not found";
 
60
}
 
61
 
 
62
$squid_host = $ENV{squidhost} || "localhost";
 
63
$squid_port = $ENV{squidport} || 3128;
 
64
$user = $ENV{squiduser} || "";
 
65
$passwd = $ENV{squidpasswd} || "";
 
66
 
 
67
if($ARGV[0] and $ARGV[0] eq "autoconf") {
 
68
    &autoconf($squid_host, $squid_port, $user, $passwd);
 
69
}
 
70
 
 
71
sub autoconf {
 
72
    my ($host, $port, $user, $passwd) = @_;
 
73
 
 
74
        if ($ret)
 
75
        {
 
76
                print "no ($ret)\n";
 
77
                exit 1;
 
78
        }
 
79
 
 
80
    my $cachemgr = IO::Socket::INET->new(PeerAddr => $host,
 
81
                                        PeerPort => $port,
 
82
                                        Proto    => 'tcp');
 
83
 
 
84
    if (!$cachemgr)
 
85
    {
 
86
        print "no (could not connect: $!)\n";
 
87
        exit 1;
 
88
    }
 
89
 
 
90
    my $request = "GET cache_object://$host/counters HTTP/1.0\r\n" .
 
91
        "Accept: */*\r\n" .
 
92
        &make_auth_header($user, $passwd) .
 
93
        "\r\n";
 
94
                  
 
95
    $cachemgr->syswrite($request, length($request));
 
96
    my @lines = $cachemgr->getlines();
 
97
 
 
98
    print "yes\n";
 
99
    exit 0;
 
100
}
 
101
 
 
102
sub make_auth_header {
 
103
    my ($user, $passwd) = @_;
 
104
 
 
105
    if(!defined $passwd || $passwd eq "") {
 
106
        return "";
 
107
    } else {
 
108
        my $auth = MIME::Base64::encode_base64(($user ? $user : "") . ":$passwd", "");
 
109
        return "Authorization: Basic $auth\r\n" .
 
110
            "Proxy-Authorization: Basic $auth\r\n";
 
111
    }
 
112
}
 
113
 
 
114
 
 
115
sub query_squid {
 
116
    my ($host, $port, $user, $passwd) = @_;
 
117
    my $ret;
 
118
 
 
119
    my $cachemgr = IO::Socket::INET->new(PeerAddr => $host,
 
120
                                        PeerPort => $port,
 
121
                                        Proto    => 'tcp') or die($!);
 
122
 
 
123
    
 
124
 
 
125
    my $request = "GET cache_object://$host/server_list HTTP/1.0\r\n" .
 
126
        "Accept: */*\r\n" .
 
127
        &make_auth_header($user, $passwd) .
 
128
        "\r\n";
 
129
                  
 
130
    $cachemgr->syswrite($request, length($request));
 
131
    my @lines = $cachemgr->getlines();
 
132
    my $id = "";
 
133
    for(my $i = 0; $i <= $#lines; $i++) {
 
134
        chomp $lines[$i];
 
135
        if($lines[$i] =~ /Address[^:]+:\s*([\d\.]+)\s*$/) {
 
136
            my $host = $1;
 
137
            $id = "h" . $host;
 
138
            $id =~ s/\.//g;
 
139
 
 
140
            my $h;
 
141
            if ($h = Net::hostent::gethost ($host)) {
 
142
                $ret->{$id}->{host} = lc $h->name;
 
143
            } else {
 
144
                $ret->{$id}->{host} = $host;
 
145
            }
 
146
        }
 
147
        elsif($lines[$i] =~ /FETCHES\s*:\s*(\d+)/) {
 
148
            $ret->{$id}->{fetches} = $1;
 
149
        }
 
150
    }
 
151
    return $ret;
 
152
}
 
153
 
 
154
my $hosts = &query_squid($squid_host, $squid_port, $user, $passwd);
 
155
 
 
156
if($ARGV[0] and $ARGV[0] eq "config") {
 
157
    my $first = 1;
 
158
    print "graph_title Squid relay statistics\n";
 
159
    print "graph_vlabel requests / min\n";
 
160
    print "graph_args -l 0 --base 1000\n";
 
161
    print "graph_total total\n";
 
162
    foreach my $i (sort keys %{$hosts}) {
 
163
        print "$i.label ", $hosts->{$i}->{host}, "\n";
 
164
        print "$i.cdef $i,60,*\n";
 
165
        print "$i.type DERIVE\n";
 
166
        print "$i.min 0\n";
 
167
        if ($first) {
 
168
            print "$i.draw AREA\n";
 
169
            $first = 0;
 
170
        } else {
 
171
            print "$i.draw STACK\n";
 
172
        }
 
173
    }
 
174
    exit 0;
 
175
}
 
176
 
 
177
foreach my $i (keys %{$hosts}) {
 
178
    print "$i.value ", $hosts->{$i}->{fetches}, "\n";
 
179
}
 
180
 
 
181
# vim:syntax=perl