2012-04-17 23:49:02 +02:00
|
|
|
#!/usr/bin/perl -w
|
2012-12-13 04:56:38 +01:00
|
|
|
use strict;
|
2012-04-17 23:49:02 +02:00
|
|
|
#
|
|
|
|
# byprojects_bandwidth
|
|
|
|
#
|
2018-08-02 02:03:42 +02:00
|
|
|
# Perl script to monitor total bandwidth *byprojects* (e.g. vhost) from multiple
|
2013-07-02 04:42:28 +02:00
|
|
|
# files and/or regex.
|
2012-04-17 23:49:02 +02:00
|
|
|
#
|
2018-08-02 02:03:42 +02:00
|
|
|
# Danny Fullerton <northox@mantor.org>
|
2012-04-17 23:49:02 +02:00
|
|
|
# Mantor Organization <www.mantor.org>
|
2013-07-02 04:42:28 +02:00
|
|
|
# This work is licensed under a MIT license.
|
2012-04-17 23:49:02 +02:00
|
|
|
#
|
2012-12-13 04:56:38 +01:00
|
|
|
# You need logtail (https://www.fourmilab.ch/webtools/logtail/)
|
2012-04-17 23:49:02 +02:00
|
|
|
#
|
|
|
|
# mod_logio apache module (https://httpd.apache.org/docs/2.0/mod/mod_logio.html).
|
2013-07-02 04:42:28 +02:00
|
|
|
# Your logformat should look like this:
|
|
|
|
# "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O"
|
2012-04-17 23:49:02 +02:00
|
|
|
# where %I is input and %O is output.
|
|
|
|
#
|
2018-08-02 02:03:42 +02:00
|
|
|
# Log can be gathered from multiple sources by simply specifying multiple log
|
2013-07-02 04:42:28 +02:00
|
|
|
# filename or using wildcards (glob). File content can be selected using regex.
|
|
|
|
#
|
|
|
|
# - 'prod' => [ {'path' => '/home/prod/log/access.log'} ],
|
|
|
|
# Prod graph will be using everything in /home/prod/log/access.log
|
|
|
|
#
|
|
|
|
# - 'test' => [ {'path' => '/var/log/access.log', 'regex' => '"[A-Z]+ /test/'},
|
|
|
|
# {'path' => '/home/test/log/access*.log'} ],
|
|
|
|
# Test graph will be using everything file matching /home/test/log/access*.log
|
|
|
|
# and stuff that match the expression '"[A-Z] /test/' in /var/log/access.log
|
|
|
|
# such as '"GET /test/'
|
2012-04-17 23:49:02 +02:00
|
|
|
|
2012-12-13 04:56:38 +01:00
|
|
|
my $server = 'Apache';
|
2012-04-17 23:49:02 +02:00
|
|
|
|
2017-04-18 23:32:55 +02:00
|
|
|
my $statepath = $ENV{MUNIN_PLUGSTATE};
|
2012-12-13 04:56:38 +01:00
|
|
|
my $logtail = '/usr/local/bin/logtail';
|
2012-04-17 23:49:02 +02:00
|
|
|
|
2012-12-13 04:56:38 +01:00
|
|
|
my %logs = (
|
|
|
|
'prod' => [
|
|
|
|
{'path' => '/home/prod/log/access.log'}
|
|
|
|
],
|
|
|
|
'dev' => [
|
|
|
|
{'path' => '/var/log/httpd/ssl-dev-access.log'},
|
|
|
|
{'path' => '/home/dev/log/access.log'}
|
|
|
|
],
|
|
|
|
'test' => [
|
|
|
|
{'path' => '/var/log/access.log', 'regex' => '"[A-Z]+ /test/'},
|
|
|
|
{'path' => '/home/test/log/access.log'}
|
|
|
|
],
|
2012-04-17 23:49:02 +02:00
|
|
|
);
|
|
|
|
|
2012-12-13 04:56:38 +01:00
|
|
|
|
2012-04-17 23:49:02 +02:00
|
|
|
###########
|
|
|
|
|
|
|
|
if(defined($ARGV[0])) {
|
|
|
|
if ($ARGV[0] eq 'autoconf') {
|
|
|
|
print "yes\n";
|
|
|
|
exit(0);
|
|
|
|
} elsif ($ARGV[0] eq 'config') {
|
2012-12-13 04:56:38 +01:00
|
|
|
my $order = '';
|
|
|
|
while ((my $project, my @files) = each(%logs)) { $order .= $project.' ' }
|
2012-04-17 23:49:02 +02:00
|
|
|
print "graph_order $order\n";
|
|
|
|
print "graph_title $server total bandwidth byprojects\n";
|
|
|
|
print "graph_total Total\n";
|
|
|
|
print "graph_vlabel Bits\n";
|
2017-02-21 18:24:41 +01:00
|
|
|
print "graph_category webserver\n";
|
2014-10-04 20:30:29 +02:00
|
|
|
print "graph_info This graph show $server total bandwidth used by various " .
|
2013-07-02 04:42:28 +02:00
|
|
|
"projects.\n";
|
2012-12-13 04:56:38 +01:00
|
|
|
while ((my $project, my @files) = each(%logs)) {
|
|
|
|
print $project.".label $project\n";
|
|
|
|
print $project.".type GAUGE\n";
|
2012-04-17 23:49:02 +02:00
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-13 04:56:38 +01:00
|
|
|
foreach my $project ( keys %logs ) {
|
|
|
|
my $i = 0;
|
|
|
|
my $o = 0;
|
|
|
|
my $x = 0;
|
|
|
|
foreach my $log ( @{$logs{$project}} ) {
|
2013-07-02 04:42:28 +02:00
|
|
|
my @paths = glob $log->{'path'};
|
|
|
|
foreach my $path (@paths) {
|
|
|
|
my $state = $statepath.'/'.$project.$x.'_totalbandwidth.state';
|
2018-08-02 02:03:42 +02:00
|
|
|
open(LT, "$logtail -f ".$log->{'path'}." -o $state |") or
|
2013-07-02 04:42:28 +02:00
|
|
|
die "Can't open $logtail : $!";
|
|
|
|
while (<LT>) {
|
|
|
|
my $buf = $_;
|
|
|
|
if($buf eq '') { next }
|
|
|
|
if(!defined($log->{'regex'}) || $buf =~ m/$log->{'regex'}/) {
|
|
|
|
if($buf =~ m/(\d+) (\d+)$/) {
|
|
|
|
$i += $1;
|
|
|
|
$o += $2;
|
|
|
|
}
|
2012-04-17 23:49:02 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-02 04:42:28 +02:00
|
|
|
close(LT);
|
|
|
|
$x++;
|
2012-04-17 23:49:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$x = $i + $o;
|
2012-12-13 04:56:38 +01:00
|
|
|
print $project.".value $x\n";
|
2012-04-17 23:49:02 +02:00
|
|
|
}
|