root / plugins / nginx / nginx_byprojects / byprojects_bandwidth @ df5325d2
Historique | Voir | Annoter | Télécharger (3,06 ko)
| 1 |
#!/usr/bin/perl -w |
|---|---|
| 2 |
use strict; |
| 3 |
# |
| 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 |
# 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 |
# 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 |
# 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 |
# Prod graph will be using everything in /home/prod/log/access.log |
| 23 |
# - '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 |
|
| 28 |
my $server = 'Nginx'; |
| 29 |
|
| 30 |
my $statepath = '/usr/local/var/munin/plugin-state'; # where logtail will save the state |
| 31 |
my $logtail = '/usr/local/bin/logtail'; |
| 32 |
|
| 33 |
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 |
); |
| 46 |
|
| 47 |
|
| 48 |
########### |
| 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 |
my $order = ''; |
| 56 |
while ((my $project, my @files) = each(%logs)) { $order .= $project.' ' }
|
| 57 |
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 |
while ((my $project, my @files) = each(%logs)) {
|
| 64 |
print $project.".label $project\n"; |
| 65 |
print $project.".type GAUGE\n"; |
| 66 |
} |
| 67 |
exit(0); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
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 |
if($buf eq '') { next }
|
| 81 |
if(!defined($log->{'regex'}) || $buf =~ m/$log->{'regex'}/) {
|
| 82 |
if($buf =~ m/(\d+) (\d+)$/) {
|
| 83 |
$i += $1; |
| 84 |
$o += $2; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
close(LT); |
| 89 |
$x++; |
| 90 |
} |
| 91 |
$x = $i + $o; |
| 92 |
print $project.".value $x\n"; |
| 93 |
} |
