Repository
Munin (contrib)
Last change
2018-08-02
Graph Categories
Keywords
Language
Perl
License
JSON

byprojects_bandwidth

Sadly there is no documentation for this plugin.

#!/usr/bin/perl -w
use strict;
use JSON qw(decode_json);
#
# byprojects_bandwidth
#
# Perl script to monitor total bandwidth *byprojects* (e.g. vhost) from multiple
# files and/or regex.
#
# Danny Fullerton <northox@mantor.org>
# Mantor Organization <www.mantor.org>
# This work is licensed under a MIT license.
#
# You need logtail (https://www.fourmilab.ch/webtools/logtail/)
#
# Your nginx configuration should look like this (i.e. $request_length
# body_bytes_sent at the end):
#   log_format main  '$remote_addr - $remote_user $time_local "$request" '
#                    '$status $body_bytes_sent "$http_referer" '
#                    '"$http_user_agent" $request_length $body_bytes_sent';
#
# 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/'
#
# Configuration
# [byprojects_*]
# env.logtail_path /usr/local/bin/logtail
# 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"}]

my $server = 'Nginx';

my $statepath = $ENV{MUNIN_PLUGSTATE};
my $logtail = $ENV{logtail_path} || '/usr/local/bin/logtail';

my @loglist = grep {$_ =~ /site\./} keys(%ENV);
my %envLogs = %ENV{@loglist};
my %logs;
while(my($k, $v) = each %envLogs) { @logs{substr($k, 5)} = decode_json($v); }

###########

if(defined($ARGV[0])) {
  if ($ARGV[0] eq 'autoconf') {
    print "yes\n";
    exit(0);
  } elsif ($ARGV[0] eq 'config') {
    my $order = '';
    while ((my $project, my @files) = each(%logs)) { $order .= $project.' ' }
    print "graph_order $order\n";
    print "graph_title $server total bandwidth byprojects\n";
    print "graph_total Total\n";
    print "graph_vlabel Bits\n";
    print "graph_category webserver\n";
    print "graph_info This graph show $server total bandwidth used by various " .
      "projects.\n";
    while ((my $project, my @files) = each(%logs)) {
      print $project.".label $project\n";
      print $project.".type GAUGE\n";
    }
    exit(0);
  }
}

foreach my $project ( keys %logs )  {
  my $i = 0;
  my $o = 0;
  my $x = 0;
  foreach my $log ( @{$logs{$project}} ) {
    my @paths = glob $log->{'path'};
    foreach my $path (@paths) {
      my $state = $statepath.'/'.$project.$x.'_totalbandwidth.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;
          }
        }
      }
      close(LT);
      $x++;
    }
  }
  $x = $i + $o;
  print $project.".value $x\n";
}