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
#!/usr/bin/perl -w
2
use strict;
3
use JSON qw(decode_json);
4
#
5
# byprojects_inout_bandwidth
6
#
7
# Perl script to monitor in/out bandwidth *byprojects* (e.g. vhost) from
8
# multiple files and/or regex.
9
#
10
# Danny Fullerton <northox@mantor.org>
11
# Mantor Organization <www.mantor.org>
12
# This work is licensed under a MIT license.
13
#
14
# You need logtail (https://www.fourmilab.ch/webtools/logtail/)
15
#
16
# Your nginx configuration should look like this (i.e. $request_length
17
# body_bytes_sent at the end):
18
#   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
# Log can be gathered from multiple sources by simply specifying multiple log
23
# 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
#
34
# Configuration
35
# [byprojects_*]
36
# env.logtail_path /usr/local/bin/logtail
37
# 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

    
41
my $server = 'Nginx';
42

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

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

    
51
###########
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
    print "graph_category webserver\n";
62
    print "graph_info This graph show $server in/out bandwidth used by various" .
63
      " projects.\n";
64
    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
    }
74
    exit(0);
75
  }
76
}
77

    
78
foreach my $project ( keys %logs )  {
79
  my $i = 0;
80
  my $o = 0;
81
  my $x = 0;
82
  foreach my $log ( @{$logs{$project}} ) {
83
    my @paths = glob $log->{'path'};
84
    foreach my $path (@paths) {
85
      my $state = $statepath.'/'.$project.$x.'_inoutbandwidth.state';
86
      open(LT, "$logtail -f ".$log->{'path'}." -o $state |") or
87
        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
        }
97
      }
98
      close(LT);
99
      $x++;
100
    }
101
  }
102
  print "i".$project.".value $i\n";
103
  print "o".$project.".value $o\n";
104
}