root / plugins / apache / apache_byprojects / byprojects_bandwidth @ 17f78427
Historique | Voir | Annoter | Télécharger (3,07 ko)
| 1 | 614c22df | Danny Fullerton | #!/usr/bin/perl -w |
|---|---|---|---|
| 2 | 5271859f | Danny Fullerton | use strict; |
| 3 | 614c22df | Danny Fullerton | # |
| 4 | # byprojects_bandwidth |
||
| 5 | # |
||
| 6 | 17f78427 | Lars Kruse | # Perl script to monitor total bandwidth *byprojects* (e.g. vhost) from multiple |
| 7 | cf03f9b0 | Danny Fullerton | # files and/or regex. |
| 8 | 614c22df | Danny Fullerton | # |
| 9 | 17f78427 | Lars Kruse | # Danny Fullerton <northox@mantor.org> |
| 10 | 614c22df | Danny Fullerton | # Mantor Organization <www.mantor.org> |
| 11 | cf03f9b0 | Danny Fullerton | # This work is licensed under a MIT license. |
| 12 | 614c22df | Danny Fullerton | # |
| 13 | 5271859f | Danny Fullerton | # You need logtail (https://www.fourmilab.ch/webtools/logtail/) |
| 14 | 614c22df | Danny Fullerton | # |
| 15 | # mod_logio apache module (https://httpd.apache.org/docs/2.0/mod/mod_logio.html). |
||
| 16 | cf03f9b0 | Danny Fullerton | # Your logformat should look like this: |
| 17 | # "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O"
|
||
| 18 | 614c22df | Danny Fullerton | # where %I is input and %O is output. |
| 19 | # |
||
| 20 | 17f78427 | Lars Kruse | # Log can be gathered from multiple sources by simply specifying multiple log |
| 21 | cf03f9b0 | Danny Fullerton | # filename or using wildcards (glob). File content can be selected using regex. |
| 22 | # |
||
| 23 | # - 'prod' => [ {'path' => '/home/prod/log/access.log'} ],
|
||
| 24 | # Prod graph will be using everything in /home/prod/log/access.log |
||
| 25 | # |
||
| 26 | # - 'test' => [ {'path' => '/var/log/access.log', 'regex' => '"[A-Z]+ /test/'},
|
||
| 27 | # {'path' => '/home/test/log/access*.log'} ],
|
||
| 28 | # Test graph will be using everything file matching /home/test/log/access*.log |
||
| 29 | # and stuff that match the expression '"[A-Z] /test/' in /var/log/access.log |
||
| 30 | # such as '"GET /test/' |
||
| 31 | 614c22df | Danny Fullerton | |
| 32 | 5271859f | Danny Fullerton | my $server = 'Apache'; |
| 33 | 614c22df | Danny Fullerton | |
| 34 | 4b2fcbf8 | Lars Kruse | my $statepath = $ENV{MUNIN_PLUGSTATE};
|
| 35 | 5271859f | Danny Fullerton | my $logtail = '/usr/local/bin/logtail'; |
| 36 | 614c22df | Danny Fullerton | |
| 37 | 5271859f | Danny Fullerton | my %logs = ( |
| 38 | 'prod' => [ |
||
| 39 | {'path' => '/home/prod/log/access.log'}
|
||
| 40 | ], |
||
| 41 | 'dev' => [ |
||
| 42 | {'path' => '/var/log/httpd/ssl-dev-access.log'},
|
||
| 43 | {'path' => '/home/dev/log/access.log'}
|
||
| 44 | ], |
||
| 45 | 'test' => [ |
||
| 46 | {'path' => '/var/log/access.log', 'regex' => '"[A-Z]+ /test/'},
|
||
| 47 | {'path' => '/home/test/log/access.log'}
|
||
| 48 | ], |
||
| 49 | 614c22df | Danny Fullerton | ); |
| 50 | |||
| 51 | 5271859f | Danny Fullerton | |
| 52 | 614c22df | Danny Fullerton | ########### |
| 53 | |||
| 54 | if(defined($ARGV[0])) {
|
||
| 55 | if ($ARGV[0] eq 'autoconf') {
|
||
| 56 | print "yes\n"; |
||
| 57 | exit(0); |
||
| 58 | } elsif ($ARGV[0] eq 'config') {
|
||
| 59 | 5271859f | Danny Fullerton | my $order = ''; |
| 60 | while ((my $project, my @files) = each(%logs)) { $order .= $project.' ' }
|
||
| 61 | 614c22df | Danny Fullerton | print "graph_order $order\n"; |
| 62 | print "graph_title $server total bandwidth byprojects\n"; |
||
| 63 | print "graph_total Total\n"; |
||
| 64 | print "graph_vlabel Bits\n"; |
||
| 65 | b19b3a81 | dipohl | print "graph_category webserver\n"; |
| 66 | a2bedd79 | Stig Sandbeck Mathisen | print "graph_info This graph show $server total bandwidth used by various " . |
| 67 | cf03f9b0 | Danny Fullerton | "projects.\n"; |
| 68 | 5271859f | Danny Fullerton | while ((my $project, my @files) = each(%logs)) {
|
| 69 | print $project.".label $project\n"; |
||
| 70 | print $project.".type GAUGE\n"; |
||
| 71 | 614c22df | Danny Fullerton | } |
| 72 | exit(0); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | 5271859f | Danny Fullerton | foreach my $project ( keys %logs ) {
|
| 77 | my $i = 0; |
||
| 78 | my $o = 0; |
||
| 79 | my $x = 0; |
||
| 80 | foreach my $log ( @{$logs{$project}} ) {
|
||
| 81 | cf03f9b0 | Danny Fullerton | my @paths = glob $log->{'path'};
|
| 82 | foreach my $path (@paths) {
|
||
| 83 | my $state = $statepath.'/'.$project.$x.'_totalbandwidth.state'; |
||
| 84 | 17f78427 | Lars Kruse | open(LT, "$logtail -f ".$log->{'path'}." -o $state |") or
|
| 85 | cf03f9b0 | Danny Fullerton | die "Can't open $logtail : $!"; |
| 86 | while (<LT>) {
|
||
| 87 | my $buf = $_; |
||
| 88 | if($buf eq '') { next }
|
||
| 89 | if(!defined($log->{'regex'}) || $buf =~ m/$log->{'regex'}/) {
|
||
| 90 | if($buf =~ m/(\d+) (\d+)$/) {
|
||
| 91 | $i += $1; |
||
| 92 | $o += $2; |
||
| 93 | } |
||
| 94 | 614c22df | Danny Fullerton | } |
| 95 | } |
||
| 96 | cf03f9b0 | Danny Fullerton | close(LT); |
| 97 | $x++; |
||
| 98 | 614c22df | Danny Fullerton | } |
| 99 | } |
||
| 100 | $x = $i + $o; |
||
| 101 | 5271859f | Danny Fullerton | print $project.".value $x\n"; |
| 102 | 614c22df | Danny Fullerton | } |
