root / plugins / nginx / nginx_byprojects / byprojects_access @ 17f78427
Historique | Voir | Annoter | Télécharger (2,87 ko)
| 1 |
#!/usr/bin/perl -w |
|---|---|
| 2 |
use strict; |
| 3 |
use JSON qw(decode_json); |
| 4 |
# |
| 5 |
# byprojects_access |
| 6 |
# |
| 7 |
# Perl script to monitor access *byprojects* (e.g. vhost) from multiple files |
| 8 |
# and/or regex. |
| 9 |
# |
| 10 |
# Danny Fullerton <northox@mantor.org> |
| 11 |
# Mantor Organization <www.mantor.org> |
| 12 |
# This work is licensed under a MIT license. |
| 13 |
# |
| 14 |
# You need logtail (https://www.fourmilab.ch/webtools/logtail/) |
| 15 |
# |
| 16 |
# Log can be gathered from multiple sources by simply specifying multiple log |
| 17 |
# filename or using wildcards (glob). File content can be selected using regex. |
| 18 |
# |
| 19 |
# - 'prod' => [ {'path' => '/home/prod/log/access.log'} ],
|
| 20 |
# Prod graph will be using everything in /home/prod/log/access.log |
| 21 |
# |
| 22 |
# - 'test' => [ {'path' => '/var/log/access.log', 'regex' => '"[A-Z]+ /test/'},
|
| 23 |
# {'path' => '/home/test/log/access*.log'} ],
|
| 24 |
# Test graph will be using everything file matching /home/test/log/access*.log |
| 25 |
# and stuff that match the expression '"[A-Z] /test/' in /var/log/access.log |
| 26 |
# such as '"GET /test/' |
| 27 |
# |
| 28 |
# Configuration |
| 29 |
# [byprojects_*] |
| 30 |
# env.logtail_path /usr/local/bin/logtail |
| 31 |
# env.site.prod [{"path":"/home/prod/log/access.log"}]
|
| 32 |
# env.site.dev [{"path":"/var/log/httpd/ssl-dev-access.log"}, {"path":"/home/dev/log/access*.log"}]
|
| 33 |
# env.site.test [{"path":"/var/log/access.log","regex":"\"[A-Z]+ /test/"}, {"path":"/home/test/log/access.log"}]
|
| 34 |
|
| 35 |
my $server = 'Nginx'; |
| 36 |
|
| 37 |
my $statepath = $ENV{MUNIN_PLUGSTATE};
|
| 38 |
my $logtail = $ENV{logtail_path} || '/usr/local/bin/logtail';
|
| 39 |
|
| 40 |
my @loglist = grep {$_ =~ /site\./} keys(%ENV);
|
| 41 |
my %envLogs = %ENV{@loglist};
|
| 42 |
my %logs; |
| 43 |
while(my($k, $v) = each %envLogs) { @logs{substr($k, 5)} = decode_json($v); }
|
| 44 |
|
| 45 |
########### |
| 46 |
|
| 47 |
if(defined($ARGV[0])) {
|
| 48 |
if ($ARGV[0] eq 'autoconf') {
|
| 49 |
print "yes\n"; |
| 50 |
exit(0); |
| 51 |
} elsif ($ARGV[0] eq 'config') {
|
| 52 |
my $order = ''; |
| 53 |
while ((my $project, my @files) = each(%logs)) { $order .= $project.' ' }
|
| 54 |
print "graph_order $order\n"; |
| 55 |
print "graph_title $server access byprojects\n"; |
| 56 |
print "graph_total Total\n"; |
| 57 |
print "graph_vlabel Access by \${graph_period}\n";
|
| 58 |
print "graph_category webserver\n"; |
| 59 |
print "graph_info This graph show $server access by various projects.\n"; |
| 60 |
while ((my $project, my @files) = each(%logs)) {
|
| 61 |
print $project.".label $project\n"; |
| 62 |
print $project.".type DERIVE\n"; |
| 63 |
print $project.".min 0\n"; |
| 64 |
} |
| 65 |
exit(0); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
foreach my $project ( keys %logs ) {
|
| 70 |
my $i = 0; |
| 71 |
my $x = 0; |
| 72 |
foreach my $log ( @{$logs{$project}} ) {
|
| 73 |
my @paths = glob $log->{'path'};
|
| 74 |
foreach my $path (@paths) {
|
| 75 |
my $state = $statepath.'/'.$project.$x.'_access.state'; |
| 76 |
open(LT, "$logtail -f ".$log->{'path'}." -o $state |") or
|
| 77 |
die "Can't open $logtail: $!"; |
| 78 |
while (<LT>) {
|
| 79 |
my $buf = $_; |
| 80 |
if($buf eq '') { next }
|
| 81 |
if(!defined($log->{'regex'}) || $buf =~ m/$log->{'regex'}/) {
|
| 82 |
$i++; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
close(LT); |
| 87 |
$x++; |
| 88 |
} |
| 89 |
print $project.".value $i\n"; |
| 90 |
} |
