Projet

Général

Profil

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

root / plugins / nginx / nginx_byprojects / byprojects_bandwidth @ 69896717

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

1
#!/usr/bin/perl -w
2
use strict;
3
use JSON qw(decode_json);
4
#
5
# byprojects_bandwidth
6
#
7
# Perl script to monitor total bandwidth *byprojects* (e.g. vhost) from multiple
8
# 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
    my $order = '';
59
    while ((my $project, my @files) = each(%logs)) { $order .= $project.' ' }
60
    print "graph_order $order\n";
61
    print "graph_title $server total bandwidth byprojects\n";
62
    print "graph_total Total\n";
63
    print "graph_vlabel Bits\n";
64
    print "graph_category webserver\n";
65
    print "graph_info This graph show $server total bandwidth used by various " .
66
      "projects.\n";
67
    while ((my $project, my @files) = each(%logs)) {
68
      print $project.".label $project\n";
69
      print $project.".type GAUGE\n";
70
    }
71
    exit(0);
72
  }
73
}
74

    
75
foreach my $project ( keys %logs )  {
76
  my $i = 0;
77
  my $o = 0;
78
  my $x = 0;
79
  foreach my $log ( @{$logs{$project}} ) {
80
    my @paths = glob $log->{'path'};
81
    foreach my $path (@paths) {
82
      my $state = $statepath.'/'.$project.$x.'_totalbandwidth.state';
83
      open(LT, "$logtail -f ".$log->{'path'}." -o $state |") or 
84
        die "Can't open $logtail : $!";
85
      while (<LT>) {
86
        my $buf = $_;
87
        if($buf eq '') { next }
88
        if(!defined($log->{'regex'}) || $buf =~ m/$log->{'regex'}/) {
89
          if($buf =~ m/(\d+) (\d+)$/) {
90
            $i += $1;
91
            $o += $2;
92
          }
93
        }
94
      }
95
      close(LT);
96
      $x++;
97
    }
98
  }
99
  $x = $i + $o;
100
  print $project.".value $x\n";
101
}