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