2
0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00
contrib-munin/plugins/apache/apache_byprojects/byprojects_access
Steve Schnepp 760b07cdb6 Merge pull request #407 from northox/glob_support
Add file expansion (glob) and switch to MIT license
2013-12-04 03:26:00 -08:00

92 lines
2.8 KiB
Perl

#!/usr/bin/perl -w
use strict;
#
# byprojects_access
#
# Perl script to monitor access *byprojects* (e.g. vhost) from multiple files
# and/or regex.
#
# Danny Fullerton <northox@mantor.org>
# Mantor Organization <www.mantor.org>
# This work is licensed under a MIT license.
#
# You need logtail (https://www.fourmilab.ch/webtools/logtail/)
#
# Log can be gathered from multiple sources by simply specifying multiple log
# 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/'
my $server = 'Apache';
my $statepath = '/usr/local/var/munin/plugin-state';
my $logtail = '/usr/local/bin/logtail';
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'}
],
);
###########
if(defined($ARGV[0])) {
if ($ARGV[0] eq 'autoconf') {
print "yes\n";
exit(0);
} elsif ($ARGV[0] eq 'config') {
my $order = '';
while ((my $project, my @files) = each(%logs)) { $order .= $project.' ' }
print "graph_order $order\n";
print "graph_title $server access byprojects\n";
print "graph_total Total\n";
print "graph_vlabel Access by \${graph_period}\n";
print "graph_category $server\n";
print "graph_info This graph show $server access by various projects.\n";
while ((my $project, my @files) = each(%logs)) {
print $project.".label $project\n";
print $project.".type DERIVE\n";
print $project.".min 0\n";
}
exit(0);
}
}
foreach my $project ( keys %logs ) {
my $i = 0;
my $x = 0;
foreach my $log ( @{$logs{$project}} ) {
my @paths = glob $log->{'path'};
foreach my $path (@paths) {
my $state = $statepath.'/'.$project.$x.'_access.state';
open(LT, "$logtail -f ".$log->{'path'}." -o $state |") or
die "Can't open $logtail : $!";
while (<LT>) {
my $buf = $_;
if($buf eq '') { next }
if(!defined($log->{'regex'}) || $buf =~ m/$log->{'regex'}/) {
$i++;
}
}
close(LT);
$x++;
}
}
print $project.".value $i\n";
}