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