2012-04-17 23:49:02 +02:00
|
|
|
#!/usr/bin/perl -w
|
2012-12-13 04:56:38 +01:00
|
|
|
use strict;
|
2017-07-08 10:36:46 +02:00
|
|
|
use JSON qw(decode_json);
|
2012-04-17 23:49:02 +02:00
|
|
|
#
|
|
|
|
# byprojects_bandwidth
|
|
|
|
#
|
2013-07-02 04:42:28 +02:00
|
|
|
# Perl script to monitor total bandwidth *byprojects* (e.g. vhost) from multiple
|
|
|
|
# files and/or regex.
|
2012-04-17 23:49:02 +02:00
|
|
|
#
|
2018-08-02 02:03:42 +02:00
|
|
|
# Danny Fullerton <northox@mantor.org>
|
2012-04-17 23:49:02 +02:00
|
|
|
# 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
|
|
|
#
|
2018-08-02 02:03:42 +02:00
|
|
|
# Your nginx configuration should look like this (i.e. $request_length
|
2013-07-02 04:42:28 +02:00
|
|
|
# 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';
|
|
|
|
#
|
2018-08-02 02:03:42 +02:00
|
|
|
# Log can be gathered from multiple sources by simply specifying multiple log
|
2013-07-02 04:42:28 +02:00
|
|
|
# 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/'
|
2017-07-08 09:16:07 +02:00
|
|
|
#
|
|
|
|
# Configuration
|
|
|
|
# [byprojects_*]
|
|
|
|
# env.logtail_path /usr/local/bin/logtail
|
2017-07-13 12:59:05 +02:00
|
|
|
# env.site.prod [{"path":"/home/prod/log/access.log"}]
|
|
|
|
# env.site.dev [{"path":"/var/log/httpd/ssl-dev-access.log"}, {"path":"/home/dev/log/access*.log"}]
|
|
|
|
# env.site.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
|
|
|
my $server = 'Nginx';
|
2012-04-17 23:49:02 +02:00
|
|
|
|
2017-04-18 23:32:55 +02:00
|
|
|
my $statepath = $ENV{MUNIN_PLUGSTATE};
|
2017-07-08 09:16:07 +02:00
|
|
|
my $logtail = $ENV{logtail_path} || '/usr/local/bin/logtail';
|
2012-04-17 23:49:02 +02:00
|
|
|
|
2017-07-08 10:36:46 +02:00
|
|
|
my @loglist = grep {$_ =~ /site\./} keys(%ENV);
|
2018-07-23 02:35:54 +02:00
|
|
|
my %envLogs = %ENV{@loglist};
|
2017-07-08 10:36:46 +02:00
|
|
|
my %logs;
|
|
|
|
while(my($k, $v) = each %envLogs) { @logs{substr($k, 5)} = decode_json($v); }
|
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') {
|
2012-12-13 04:56:38 +01:00
|
|
|
my $order = '';
|
|
|
|
while ((my $project, my @files) = each(%logs)) { $order .= $project.' ' }
|
2012-04-17 23:49:02 +02:00
|
|
|
print "graph_order $order\n";
|
|
|
|
print "graph_title $server total bandwidth byprojects\n";
|
|
|
|
print "graph_total Total\n";
|
|
|
|
print "graph_vlabel Bits\n";
|
2017-02-21 15:09:54 +01:00
|
|
|
print "graph_category webserver\n";
|
2014-10-04 19:48:14 +02:00
|
|
|
print "graph_info This graph show $server total 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 $project.".label $project\n";
|
|
|
|
print $project.".type GAUGE\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.'_totalbandwidth.state';
|
2018-08-02 02:03:42 +02:00
|
|
|
open(LT, "$logtail -f ".$log->{'path'}." -o $state |") or
|
2013-07-02 04:42:28 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
$x = $i + $o;
|
2012-12-13 04:56:38 +01:00
|
|
|
print $project.".value $x\n";
|
2012-04-17 23:49:02 +02:00
|
|
|
}
|