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