]> git.openstreetmap.org Git - chef.git/blob - cookbooks/munin/files/default/plugins/squid_icp
Merge remote-tracking branch 'github/pull/165'
[chef.git] / cookbooks / munin / files / default / plugins / squid_icp
1 #!/usr/bin/perl -w
2 # -*- perl -*-
3
4 =head1 NAME
5
6 squid_icp - Plugin to graph traffic to the ICP peers
7
8 =head1 CONFIGURATION
9
10 The following configuration variables are used by this plugin:
11
12  [squid_icp]
13   env.squidhost    - host (default "localhost")
14   env.squidport    - port (default "3128")
15   env.squiduser    - username (default "")
16   env.squidpasswd  - password (default "")
17
18 =head1 ABOUT
19
20 When using squid as a "load balancer" (of sorts), who gets the
21 request?
22
23 =head1 AUTHORS
24
25 Copyright (C) 2004 Jimmy Olsen
26
27 =head1 LICENSE
28
29 Gnu GPLv2
30
31 =begin comment
32
33 This program is free software; you can redistribute it and/or modify
34 it under the terms of the GNU General Public License as published by
35 the Free Software Foundation; version 2 dated June, 1991.
36
37 This program is distributed in the hope that it will be useful, but
38 WITHOUT ANY WARRANTY; without even the implied warranty of
39 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
40 General Public License for more details.
41
42 You should have received a copy of the GNU General Public License
43 along with this program; if not, write to the Free Software
44 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
45 02110-1301 USA.
46
47 =end comment
48
49 =head1 MAGIC MARKERS
50
51  #%# family=manual
52  #%# capabilities=autoconf
53
54 =cut
55
56 my $ret = undef;
57
58 if (! eval "require IO::Socket;")
59 {
60         $ret = "IO::Socket not found";
61 }
62 if (! eval "require MIME::Base64;")
63 {
64         $ret = "MIME::Base64 not found";
65 }
66 if (! eval "require Net::hostent;")
67 {
68         $ret = "Net::hostent not found";
69 }
70
71 $squid_host = $ENV{squidhost} || "localhost";
72 $squid_port = $ENV{squidport} || 3128;
73 $user = $ENV{squiduser} || "";
74 $passwd = $ENV{squidpasswd} || "";
75
76 if($ARGV[0] and $ARGV[0] eq "autoconf") {
77     &autoconf($squid_host, $squid_port, $user, $passwd);
78 }
79
80 sub autoconf {
81     my ($host, $port, $user, $passwd) = @_;
82
83         if ($ret)
84         {
85                 print "no ($ret)\n";
86                 exit 0;
87         }
88
89     my $cachemgr = IO::Socket::INET->new(PeerAddr => $host,
90                                         PeerPort => $port,
91                                         Proto    => 'tcp',
92                                         Timeout  => 5);
93
94     if (!$cachemgr)
95     {
96         print "no (could not connect: $!)\n";
97         exit 0;
98     }
99
100     my $request = "GET cache_object://$host/counters HTTP/1.0\r\n" .
101         "Accept: */*\r\n" .
102         &make_auth_header($user, $passwd) .
103         "\r\n";
104                   
105     $cachemgr->syswrite($request, length($request));
106     my @lines = $cachemgr->getlines();
107
108     print "yes\n";
109     exit 0;
110 }
111
112 sub make_auth_header {
113     my ($user, $passwd) = @_;
114
115     if(!defined $passwd || $passwd eq "") {
116         return "";
117     } else {
118         my $auth = MIME::Base64::encode_base64(($user ? $user : "") . ":$passwd", "");
119         return "Authorization: Basic $auth\r\n" .
120             "Proxy-Authorization: Basic $auth\r\n";
121     }
122 }
123
124
125 sub query_squid {
126     my ($host, $port, $user, $passwd) = @_;
127     my $ret;
128
129     my $cachemgr = IO::Socket::INET->new(PeerAddr => $host,
130                                         PeerPort => $port,
131                                         Proto    => 'tcp') or die($!);
132
133     
134
135     my $request = "GET cache_object://$host/server_list HTTP/1.0\r\n" .
136         "Accept: */*\r\n" .
137         &make_auth_header($user, $passwd) .
138         "\r\n";
139                   
140     $cachemgr->syswrite($request, length($request));
141     my @lines = $cachemgr->getlines();
142     my $id = "";
143     for(my $i = 0; $i <= $#lines; $i++) {
144         chomp $lines[$i];
145         if($lines[$i] =~ /Host[^:]+:\s*(\S+)\/\d+\/\d+\s*$/) {
146             my $host = $1;
147             $id = $host;
148             $id =~ s/\./_/g;
149
150             unless(exists($ret->{$id})) {
151                 $ret->{$id}->{host} = $host;
152                 $ret->{$id}->{fetches} = 0;
153             }
154         }
155         elsif($lines[$i] =~ /FETCHES\s*:\s*(\d+)/) {
156             $ret->{$id}->{fetches} += $1;
157         }
158     }
159     return $ret;
160 }
161
162 my $hosts = &query_squid($squid_host, $squid_port, $user, $passwd);
163
164 if($ARGV[0] and $ARGV[0] eq "config") {
165     my $first = 1;
166     print "graph_title Squid relay statistics\n";
167     print "graph_vlabel requests / \${graph_period}\n";
168     print "graph_args -l 0 --base 1000\n";
169     print "graph_total total\n";
170         print "graph_category squid\n";
171     foreach my $i (sort keys %{$hosts}) {
172         print "$i.label ", $hosts->{$i}->{host}, "\n";
173         print "$i.type DERIVE\n";
174         print "$i.max 500000\n";
175         print "$i.min 0\n";
176         if ($first) {
177             print "$i.draw AREA\n";
178             $first = 0;
179         } else {
180             print "$i.draw STACK\n";
181         }
182     }
183     exit 0;
184 }
185
186 foreach my $i (keys %{$hosts}) {
187     print "$i.value ", $hosts->{$i}->{fetches}, "\n";
188 }
189
190 # vim:syntax=perl