root / plugins / apache / apache_byprojects / byprojects_bandwidth @ 614c22df
Historique | Voir | Annoter | Télécharger (2,62 ko)
| 1 |
#!/usr/bin/perl -w |
|---|---|
| 2 |
# |
| 3 |
# byprojects_bandwidth |
| 4 |
# |
| 5 |
# Perl script to monitor total bandwidth *byprojects* (e.g. vhost) from multiple files and/or regex. |
| 6 |
# |
| 7 |
# Danny Fullerton <northox@mantor.org> |
| 8 |
# Mantor Organization <www.mantor.org> |
| 9 |
# |
| 10 |
# You need logtail (https://www.fourmilab.ch/webtools/logtail/) and |
| 11 |
# |
| 12 |
# mod_logio apache module (https://httpd.apache.org/docs/2.0/mod/mod_logio.html). |
| 13 |
# Your logformat should look like this "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O"
|
| 14 |
# where %I is input and %O is output. |
| 15 |
# |
| 16 |
# Log can be gather from multiple sources by simply specifying multiple log filename and/or an array with two |
| 17 |
# elements: log filename and a regex. |
| 18 |
# - 'prod' => array('/home/prod/log/access.log'),
|
| 19 |
# Prod graph will be using everything in /home/prod/log/access.log |
| 20 |
# - 'test' => array(array('/var/log/httpd/access.log', '"[A-Z]+ /test/'), '/home/test/log/access.log')
|
| 21 |
# Test graph will be using eveything in /home/test/log/access.log and stuff that match '"[A-Z] /test/' in |
| 22 |
# /var/log/httpd/access.log such as '"GET /test/' |
| 23 |
|
| 24 |
$statepath = '/usr/local/var/munin/plugin-state'; # directory where logtail will save the state |
| 25 |
$logtail = '/usr/local/bin/logtail'; |
| 26 |
|
| 27 |
$server = 'Apache'; |
| 28 |
|
| 29 |
%logs = ( |
| 30 |
'prod' => ('/home/prod/log/access.log'),
|
| 31 |
'test' => ( |
| 32 |
('/var/log/httpd/access.log', '"[A-Z]+ /test/'),
|
| 33 |
'/home/test/log/access.log' |
| 34 |
) |
| 35 |
); |
| 36 |
|
| 37 |
########### |
| 38 |
|
| 39 |
if(defined($ARGV[0])) {
|
| 40 |
if ($ARGV[0] eq 'autoconf') {
|
| 41 |
print "yes\n"; |
| 42 |
exit(0); |
| 43 |
} elsif ($ARGV[0] eq 'config') {
|
| 44 |
$order = ''; |
| 45 |
while (($client, $files) = each(%logs)) { $order .= $client.' ' }
|
| 46 |
print "graph_order $order\n"; |
| 47 |
print "graph_title $server total bandwidth byprojects\n"; |
| 48 |
print "graph_total Total\n"; |
| 49 |
print "graph_vlabel Bits\n"; |
| 50 |
print "graph_category $server\n"; |
| 51 |
print "graph_info This graph show $server total bandwidth used by various projects.\n"; |
| 52 |
while (($client, $files) = each(%logs)) {
|
| 53 |
print $client.".label $client\n"; |
| 54 |
print $client.".type GAUGE\n"; |
| 55 |
} |
| 56 |
exit(0); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
while (($client, $files) = each(%logs)) {
|
| 61 |
$i = $o = $x = 0; |
| 62 |
foreach $file ($files) {
|
| 63 |
$regex = ''; |
| 64 |
$state = $statepath.'/'.$client.$x.'_totalbandwidth.state'; |
| 65 |
if(ref($file) eq 'ARRAY') { ($file, $regex) = @file }
|
| 66 |
open(my $lt, "$logtail -f $file -o $state |") or die "Can't open $logtail : $!"; |
| 67 |
while (<$lt>) {
|
| 68 |
$buf = $_; |
| 69 |
if($buf eq '') { next }
|
| 70 |
if(!defined($regex) || $buf =~ m/$regex/) {
|
| 71 |
if($buf =~ m/(\d+) (\d+)$/) {
|
| 72 |
$i += $1; |
| 73 |
$o += $2; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
close($lt); |
| 78 |
$x++; |
| 79 |
} |
| 80 |
$x = $i + $o; |
| 81 |
print $client.".value $x\n"; |
| 82 |
} |
| 83 |
|
