Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / nginx / nginx_byprojects / byprojects_inout_bandwidth @ 17f78427

Historique | Voir | Annoter | Télécharger (3,52 ko)

1 614c22df Danny Fullerton
#!/usr/bin/perl -w
2 5271859f Danny Fullerton
use strict;
3 a1f7808b Neraud
use JSON qw(decode_json);
4 614c22df Danny Fullerton
#
5
# byprojects_inout_bandwidth
6
#
7 17f78427 Lars Kruse
# Perl script to monitor in/out bandwidth *byprojects* (e.g. vhost) from
8 cf03f9b0 Danny Fullerton
# multiple files and/or regex.
9 614c22df Danny Fullerton
#
10 17f78427 Lars Kruse
# Danny Fullerton <northox@mantor.org>
11 614c22df Danny Fullerton
# Mantor Organization <www.mantor.org>
12 cf03f9b0 Danny Fullerton
# This work is licensed under a MIT license.
13 614c22df Danny Fullerton
#
14 5271859f Danny Fullerton
# You need logtail (https://www.fourmilab.ch/webtools/logtail/)
15 614c22df Danny Fullerton
#
16 17f78427 Lars Kruse
# Your nginx configuration should look like this (i.e. $request_length
17 cf03f9b0 Danny Fullerton
# body_bytes_sent at the end):
18 614c22df Danny Fullerton
#   log_format main  '$remote_addr - $remote_user $time_local "$request" '
19
#                    '$status $body_bytes_sent "$http_referer" '
20
#                    '"$http_user_agent" $request_length $body_bytes_sent';
21
#
22 17f78427 Lars Kruse
# Log can be gathered from multiple sources by simply specifying multiple log
23 cf03f9b0 Danny Fullerton
# filename or using wildcards (glob). File content can be selected using regex.
24
#
25
# - 'prod' => [ {'path' => '/home/prod/log/access.log'} ],
26
#   Prod graph will be using everything in /home/prod/log/access.log
27
#
28
# - 'test' => [ {'path' => '/var/log/access.log', 'regex' => '"[A-Z]+ /test/'},
29
#               {'path' => '/home/test/log/access*.log'} ],
30
#   Test graph will be using everything file matching /home/test/log/access*.log
31
#   and stuff that match the expression '"[A-Z] /test/' in /var/log/access.log
32
#   such as '"GET /test/'
33 3a17b22e Neraud
#
34
# Configuration
35
# [byprojects_*]
36
# env.logtail_path /usr/local/bin/logtail
37 69896717 Neraud
# env.site.prod  [{"path":"/home/prod/log/access.log"}]
38
# env.site.dev   [{"path":"/var/log/httpd/ssl-dev-access.log"}, {"path":"/home/dev/log/access*.log"}]
39
# env.site.test  [{"path":"/var/log/access.log","regex":"\"[A-Z]+ /test/"}, {"path":"/home/test/log/access.log"}]
40 614c22df Danny Fullerton
41 5271859f Danny Fullerton
my $server = 'Nginx';
42 614c22df Danny Fullerton
43 4b2fcbf8 Lars Kruse
my $statepath = $ENV{MUNIN_PLUGSTATE};
44 3a17b22e Neraud
my $logtail = $ENV{logtail_path} || '/usr/local/bin/logtail';
45 614c22df Danny Fullerton
46 a1f7808b Neraud
my @loglist = grep {$_ =~ /site\./} keys(%ENV);
47 15d05f30 Lars Kruse
my %envLogs = %ENV{@loglist};
48 a1f7808b Neraud
my %logs;
49
while(my($k, $v) = each %envLogs) { @logs{substr($k, 5)} = decode_json($v); }
50 5271859f Danny Fullerton
51 614c22df Danny Fullerton
###########
52
53
if(defined($ARGV[0])) {
54
  if ($ARGV[0] eq 'autoconf') {
55
    print "yes\n";
56
    exit(0);
57
  } elsif ($ARGV[0] eq 'config') {
58
    print "graph_title $server in/out bandwidth byprojects\n";
59
    print "graph_args --base 1000\n";
60
    print "graph_vlabel bits per \${graph_period} in (-) / out (+)\n";
61 62560fae dipohl
    print "graph_category webserver\n";
62 de4ba4b5 Stig Sandbeck Mathisen
    print "graph_info This graph show $server in/out bandwidth used by various" .
63 cf03f9b0 Danny Fullerton
      " projects.\n";
64 5271859f Danny Fullerton
    while ((my $project, my @files) = each(%logs)) {
65
      print "i".$project.".label $project\n";
66
      print "i".$project.".type GAUGE\n";
67
      print "i".$project.".graph no\n";
68
      print "i".$project.".cdef i".$project.",8,*\n";
69
      print "o".$project.".label $project\n";
70
      print "o".$project.".type GAUGE\n";
71
      print "o".$project.".negative i".$project."\n";
72
      print "o".$project.".cdef o".$project.",8,*\n";
73 614c22df Danny Fullerton
    }
74
    exit(0);
75
  }
76
}
77
78 5271859f Danny Fullerton
foreach my $project ( keys %logs )  {
79
  my $i = 0;
80
  my $o = 0;
81
  my $x = 0;
82
  foreach my $log ( @{$logs{$project}} ) {
83 cf03f9b0 Danny Fullerton
    my @paths = glob $log->{'path'};
84
    foreach my $path (@paths) {
85
      my $state = $statepath.'/'.$project.$x.'_inoutbandwidth.state';
86 17f78427 Lars Kruse
      open(LT, "$logtail -f ".$log->{'path'}." -o $state |") or
87 cf03f9b0 Danny Fullerton
        die "Can't open $logtail : $!";
88
      while (<LT>) {
89
        my $buf = $_;
90
        if($buf eq '') { next }
91
        if(!defined($log->{'regex'}) || $buf =~ m/$log->{'regex'}/) {
92
          if($buf =~ m/(\d+) (\d+)$/) {
93
            $i += $1;
94
            $o += $2;
95
          }
96 614c22df Danny Fullerton
        }
97
      }
98 cf03f9b0 Danny Fullerton
      close(LT);
99
      $x++;
100 614c22df Danny Fullerton
    }
101
  }
102 5271859f Danny Fullerton
  print "i".$project.".value $i\n";
103
  print "o".$project.".value $o\n";
104 614c22df Danny Fullerton
}