2012-04-17 23:49:02 +02:00
|
|
|
#!/usr/bin/perl -w
|
2012-12-13 04:56:38 +01:00
|
|
|
use strict;
|
2012-04-17 23:49:02 +02:00
|
|
|
#
|
|
|
|
# byprojects_inout_bandwidth
|
|
|
|
#
|
2013-07-02 04:42:28 +02:00
|
|
|
# Perl script to monitor in/out bandwidth *byprojects* (e.g. vhost) from
|
|
|
|
# multiple files and/or regex.
|
2012-04-17 23:49:02 +02:00
|
|
|
#
|
|
|
|
# Danny Fullerton <northox@mantor.org>
|
|
|
|
# Mantor Organization <www.mantor.org>
|
2013-07-02 04:42:28 +02:00
|
|
|
# This work is licensed under a MIT license.
|
2012-04-17 23:49:02 +02:00
|
|
|
#
|
2012-12-13 04:56:38 +01:00
|
|
|
# You need logtail (https://www.fourmilab.ch/webtools/logtail/)
|
2012-04-17 23:49:02 +02:00
|
|
|
#
|
2013-07-02 04:42:28 +02:00
|
|
|
# Your nginx configuration should look like this (i.e. $request_length
|
|
|
|
# body_bytes_sent at the end):
|
2012-04-17 23:49:02 +02:00
|
|
|
# log_format main '$remote_addr - $remote_user $time_local "$request" '
|
|
|
|
# '$status $body_bytes_sent "$http_referer" '
|
|
|
|
# '"$http_user_agent" $request_length $body_bytes_sent';
|
|
|
|
#
|
2013-07-02 04:42:28 +02:00
|
|
|
# Log can be gathered from multiple sources by simply specifying multiple log
|
|
|
|
# filename or using wildcards (glob). File content can be selected using regex.
|
|
|
|
#
|
|
|
|
# - 'prod' => [ {'path' => '/home/prod/log/access.log'} ],
|
|
|
|
# Prod graph will be using everything in /home/prod/log/access.log
|
|
|
|
#
|
|
|
|
# - 'test' => [ {'path' => '/var/log/access.log', 'regex' => '"[A-Z]+ /test/'},
|
|
|
|
# {'path' => '/home/test/log/access*.log'} ],
|
|
|
|
# Test graph will be using everything file matching /home/test/log/access*.log
|
|
|
|
# and stuff that match the expression '"[A-Z] /test/' in /var/log/access.log
|
|
|
|
# such as '"GET /test/'
|
2012-04-17 23:49:02 +02:00
|
|
|
|
2012-12-13 04:56:38 +01:00
|
|
|
my $server = 'Nginx';
|
2012-04-17 23:49:02 +02:00
|
|
|
|
2013-07-02 04:42:28 +02:00
|
|
|
my $statepath = '/usr/local/var/munin/plugin-state';
|
2012-12-13 04:56:38 +01:00
|
|
|
my $logtail = '/usr/local/bin/logtail';
|
2012-04-17 23:49:02 +02:00
|
|
|
|
2012-12-13 04:56:38 +01:00
|
|
|
my %logs = (
|
|
|
|
'prod' => [
|
|
|
|
{'path' => '/home/prod/log/access.log'}
|
|
|
|
],
|
|
|
|
'dev' => [
|
|
|
|
{'path' => '/var/log/httpd/ssl-dev-access.log'},
|
|
|
|
{'path' => '/home/dev/log/access.log'}
|
|
|
|
],
|
|
|
|
'test' => [
|
|
|
|
{'path' => '/var/log/access.log', 'regex' => '"[A-Z]+ /test/'},
|
|
|
|
{'path' => '/home/test/log/access.log'}
|
|
|
|
],
|
2012-04-17 23:49:02 +02:00
|
|
|
);
|
|
|
|
|
2012-12-13 04:56:38 +01:00
|
|
|
|
2012-04-17 23:49:02 +02:00
|
|
|
###########
|
|
|
|
|
|
|
|
if(defined($ARGV[0])) {
|
|
|
|
if ($ARGV[0] eq 'autoconf') {
|
|
|
|
print "yes\n";
|
|
|
|
exit(0);
|
|
|
|
} elsif ($ARGV[0] eq 'config') {
|
|
|
|
print "graph_title $server in/out bandwidth byprojects\n";
|
|
|
|
print "graph_args --base 1000\n";
|
|
|
|
print "graph_vlabel bits per \${graph_period} in (-) / out (+)\n";
|
|
|
|
print "graph_category $server\n";
|
2014-10-04 19:45:24 +02:00
|
|
|
print "graph_info This graph show $server in/out bandwidth used by various" .
|
2013-07-02 04:42:28 +02:00
|
|
|
" projects.\n";
|
2012-12-13 04:56:38 +01:00
|
|
|
while ((my $project, my @files) = each(%logs)) {
|
|
|
|
print "i".$project.".label $project\n";
|
|
|
|
print "i".$project.".type GAUGE\n";
|
|
|
|
print "i".$project.".graph no\n";
|
|
|
|
print "i".$project.".cdef i".$project.",8,*\n";
|
|
|
|
print "o".$project.".label $project\n";
|
|
|
|
print "o".$project.".type GAUGE\n";
|
|
|
|
print "o".$project.".negative i".$project."\n";
|
|
|
|
print "o".$project.".cdef o".$project.",8,*\n";
|
2012-04-17 23:49:02 +02:00
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-13 04:56:38 +01:00
|
|
|
foreach my $project ( keys %logs ) {
|
|
|
|
my $i = 0;
|
|
|
|
my $o = 0;
|
|
|
|
my $x = 0;
|
|
|
|
foreach my $log ( @{$logs{$project}} ) {
|
2013-07-02 04:42:28 +02:00
|
|
|
my @paths = glob $log->{'path'};
|
|
|
|
foreach my $path (@paths) {
|
|
|
|
my $state = $statepath.'/'.$project.$x.'_inoutbandwidth.state';
|
|
|
|
open(LT, "$logtail -f ".$log->{'path'}." -o $state |") or
|
|
|
|
die "Can't open $logtail : $!";
|
|
|
|
while (<LT>) {
|
|
|
|
my $buf = $_;
|
|
|
|
if($buf eq '') { next }
|
|
|
|
if(!defined($log->{'regex'}) || $buf =~ m/$log->{'regex'}/) {
|
|
|
|
if($buf =~ m/(\d+) (\d+)$/) {
|
|
|
|
$i += $1;
|
|
|
|
$o += $2;
|
|
|
|
}
|
2012-04-17 23:49:02 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-02 04:42:28 +02:00
|
|
|
close(LT);
|
|
|
|
$x++;
|
2012-04-17 23:49:02 +02:00
|
|
|
}
|
|
|
|
}
|
2012-12-13 04:56:38 +01:00
|
|
|
print "i".$project.".value $i\n";
|
|
|
|
print "o".$project.".value $o\n";
|
2012-04-17 23:49:02 +02:00
|
|
|
}
|