root / plugins / nginx / nginx_byprojects / byprojects_access @ 17f78427
Historique | Voir | Annoter | Télécharger (2,87 ko)
| 1 | 614c22df | Danny Fullerton | #!/usr/bin/perl -w |
|---|---|---|---|
| 2 | 5271859f | Danny Fullerton | use strict; |
| 3 | a1f7808b | Neraud | use JSON qw(decode_json); |
| 4 | 614c22df | Danny Fullerton | # |
| 5 | # byprojects_access |
||
| 6 | # |
||
| 7 | 17f78427 | Lars Kruse | # Perl script to monitor access *byprojects* (e.g. vhost) from multiple files |
| 8 | cf03f9b0 | Danny Fullerton | # and/or regex. |
| 9 | 614c22df | Danny Fullerton | # |
| 10 | 17f78427 | Lars Kruse | # Danny Fullerton <northox@mantor.org> |
| 11 | 614c22df | Danny Fullerton | # Mantor Organization <www.mantor.org> |
| 12 | cf03f9b0 | Danny Fullerton | # This work is licensed under a MIT license. |
| 13 | 614c22df | Danny Fullerton | # |
| 14 | # You need logtail (https://www.fourmilab.ch/webtools/logtail/) |
||
| 15 | # |
||
| 16 | 17f78427 | Lars Kruse | # Log can be gathered from multiple sources by simply specifying multiple log |
| 17 | cf03f9b0 | Danny Fullerton | # 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 | 3a17b22e | Neraud | # |
| 28 | # Configuration |
||
| 29 | # [byprojects_*] |
||
| 30 | # env.logtail_path /usr/local/bin/logtail |
||
| 31 | 69896717 | Neraud | # 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 | 614c22df | Danny Fullerton | |
| 35 | 5271859f | Danny Fullerton | my $server = 'Nginx'; |
| 36 | 614c22df | Danny Fullerton | |
| 37 | 4b2fcbf8 | Lars Kruse | my $statepath = $ENV{MUNIN_PLUGSTATE};
|
| 38 | 3a17b22e | Neraud | my $logtail = $ENV{logtail_path} || '/usr/local/bin/logtail';
|
| 39 | 614c22df | Danny Fullerton | |
| 40 | a1f7808b | Neraud | my @loglist = grep {$_ =~ /site\./} keys(%ENV);
|
| 41 | 15d05f30 | Lars Kruse | my %envLogs = %ENV{@loglist};
|
| 42 | a1f7808b | Neraud | my %logs; |
| 43 | while(my($k, $v) = each %envLogs) { @logs{substr($k, 5)} = decode_json($v); }
|
||
| 44 | 614c22df | Danny Fullerton | |
| 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 | 5271859f | Danny Fullerton | my $order = ''; |
| 53 | while ((my $project, my @files) = each(%logs)) { $order .= $project.' ' }
|
||
| 54 | 614c22df | Danny Fullerton | 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 | 62560fae | dipohl | print "graph_category webserver\n"; |
| 59 | 614c22df | Danny Fullerton | print "graph_info This graph show $server access by various projects.\n"; |
| 60 | 5271859f | Danny Fullerton | while ((my $project, my @files) = each(%logs)) {
|
| 61 | cf03f9b0 | Danny Fullerton | print $project.".label $project\n"; |
| 62 | 5271859f | Danny Fullerton | print $project.".type DERIVE\n"; |
| 63 | print $project.".min 0\n"; |
||
| 64 | 614c22df | Danny Fullerton | } |
| 65 | exit(0); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | 5271859f | Danny Fullerton | foreach my $project ( keys %logs ) {
|
| 70 | my $i = 0; |
||
| 71 | my $x = 0; |
||
| 72 | foreach my $log ( @{$logs{$project}} ) {
|
||
| 73 | cf03f9b0 | Danny Fullerton | my @paths = glob $log->{'path'};
|
| 74 | foreach my $path (@paths) {
|
||
| 75 | my $state = $statepath.'/'.$project.$x.'_access.state'; |
||
| 76 | 17f78427 | Lars Kruse | open(LT, "$logtail -f ".$log->{'path'}." -o $state |") or
|
| 77 | cf03f9b0 | Danny Fullerton | 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 | 614c22df | Danny Fullerton | } |
| 85 | } |
||
| 86 | 5271859f | Danny Fullerton | close(LT); |
| 87 | 614c22df | Danny Fullerton | $x++; |
| 88 | } |
||
| 89 | 5271859f | Danny Fullerton | print $project.".value $i\n"; |
| 90 | 614c22df | Danny Fullerton | } |
