root / plugins / nginx / nginx_byprojects / byprojects_bandwidth @ 4b2fcbf8
Historique | Voir | Annoter | Télécharger (3,16 ko)
| 1 | 614c22df | Danny Fullerton | #!/usr/bin/perl -w |
|---|---|---|---|
| 2 | 5271859f | Danny Fullerton | use strict; |
| 3 | 614c22df | Danny Fullerton | # |
| 4 | # byprojects_bandwidth |
||
| 5 | # |
||
| 6 | cf03f9b0 | Danny Fullerton | # Perl script to monitor total bandwidth *byprojects* (e.g. vhost) from multiple |
| 7 | # files and/or regex. |
||
| 8 | 614c22df | Danny Fullerton | # |
| 9 | # Danny Fullerton <northox@mantor.org> |
||
| 10 | # 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 | cf03f9b0 | Danny Fullerton | # Your nginx configuration should look like this (i.e. $request_length |
| 16 | # body_bytes_sent at the end): |
||
| 17 | 614c22df | Danny Fullerton | # log_format main '$remote_addr - $remote_user $time_local "$request" ' |
| 18 | # '$status $body_bytes_sent "$http_referer" ' |
||
| 19 | # '"$http_user_agent" $request_length $body_bytes_sent'; |
||
| 20 | # |
||
| 21 | cf03f9b0 | Danny Fullerton | # Log can be gathered from multiple sources by simply specifying multiple log |
| 22 | # filename or using wildcards (glob). File content can be selected using regex. |
||
| 23 | # |
||
| 24 | # - 'prod' => [ {'path' => '/home/prod/log/access.log'} ],
|
||
| 25 | # Prod graph will be using everything in /home/prod/log/access.log |
||
| 26 | # |
||
| 27 | # - 'test' => [ {'path' => '/var/log/access.log', 'regex' => '"[A-Z]+ /test/'},
|
||
| 28 | # {'path' => '/home/test/log/access*.log'} ],
|
||
| 29 | # Test graph will be using everything file matching /home/test/log/access*.log |
||
| 30 | # and stuff that match the expression '"[A-Z] /test/' in /var/log/access.log |
||
| 31 | # such as '"GET /test/' |
||
| 32 | 614c22df | Danny Fullerton | |
| 33 | 5271859f | Danny Fullerton | my $server = 'Nginx'; |
| 34 | 614c22df | Danny Fullerton | |
| 35 | 4b2fcbf8 | Lars Kruse | my $statepath = $ENV{MUNIN_PLUGSTATE};
|
| 36 | 5271859f | Danny Fullerton | my $logtail = '/usr/local/bin/logtail'; |
| 37 | 614c22df | Danny Fullerton | |
| 38 | 5271859f | Danny Fullerton | my %logs = ( |
| 39 | 'prod' => [ |
||
| 40 | {'path' => '/home/prod/log/access.log'}
|
||
| 41 | ], |
||
| 42 | 'dev' => [ |
||
| 43 | {'path' => '/var/log/httpd/ssl-dev-access.log'},
|
||
| 44 | {'path' => '/home/dev/log/access.log'}
|
||
| 45 | ], |
||
| 46 | 'test' => [ |
||
| 47 | {'path' => '/var/log/access.log', 'regex' => '"[A-Z]+ /test/'},
|
||
| 48 | {'path' => '/home/test/log/access.log'}
|
||
| 49 | ], |
||
| 50 | 614c22df | Danny Fullerton | ); |
| 51 | |||
| 52 | 5271859f | Danny Fullerton | |
| 53 | 614c22df | Danny Fullerton | ########### |
| 54 | |||
| 55 | if(defined($ARGV[0])) {
|
||
| 56 | if ($ARGV[0] eq 'autoconf') {
|
||
| 57 | print "yes\n"; |
||
| 58 | exit(0); |
||
| 59 | } elsif ($ARGV[0] eq 'config') {
|
||
| 60 | 5271859f | Danny Fullerton | my $order = ''; |
| 61 | while ((my $project, my @files) = each(%logs)) { $order .= $project.' ' }
|
||
| 62 | 614c22df | Danny Fullerton | print "graph_order $order\n"; |
| 63 | print "graph_title $server total bandwidth byprojects\n"; |
||
| 64 | print "graph_total Total\n"; |
||
| 65 | print "graph_vlabel Bits\n"; |
||
| 66 | 62560fae | dipohl | print "graph_category webserver\n"; |
| 67 | 7afe6bfb | Stig Sandbeck Mathisen | print "graph_info This graph show $server total bandwidth used by various " . |
| 68 | cf03f9b0 | Danny Fullerton | "projects.\n"; |
| 69 | 5271859f | Danny Fullerton | while ((my $project, my @files) = each(%logs)) {
|
| 70 | print $project.".label $project\n"; |
||
| 71 | print $project.".type GAUGE\n"; |
||
| 72 | 614c22df | Danny Fullerton | } |
| 73 | exit(0); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | 5271859f | Danny Fullerton | foreach my $project ( keys %logs ) {
|
| 78 | my $i = 0; |
||
| 79 | my $o = 0; |
||
| 80 | my $x = 0; |
||
| 81 | foreach my $log ( @{$logs{$project}} ) {
|
||
| 82 | cf03f9b0 | Danny Fullerton | my @paths = glob $log->{'path'};
|
| 83 | foreach my $path (@paths) {
|
||
| 84 | my $state = $statepath.'/'.$project.$x.'_totalbandwidth.state'; |
||
| 85 | open(LT, "$logtail -f ".$log->{'path'}." -o $state |") or
|
||
| 86 | die "Can't open $logtail : $!"; |
||
| 87 | while (<LT>) {
|
||
| 88 | my $buf = $_; |
||
| 89 | if($buf eq '') { next }
|
||
| 90 | if(!defined($log->{'regex'}) || $buf =~ m/$log->{'regex'}/) {
|
||
| 91 | if($buf =~ m/(\d+) (\d+)$/) {
|
||
| 92 | $i += $1; |
||
| 93 | $o += $2; |
||
| 94 | } |
||
| 95 | 614c22df | Danny Fullerton | } |
| 96 | } |
||
| 97 | cf03f9b0 | Danny Fullerton | close(LT); |
| 98 | $x++; |
||
| 99 | 614c22df | Danny Fullerton | } |
| 100 | } |
||
| 101 | $x = $i + $o; |
||
| 102 | 5271859f | Danny Fullerton | print $project.".value $x\n"; |
| 103 | 614c22df | Danny Fullerton | } |
