Projet

Général

Profil

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

root / plugins / nginx / nginx_byprojects / byprojects_bandwidth @ 4b2fcbf8

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

1
#!/usr/bin/perl -w
2
use strict;
3
#
4
# byprojects_bandwidth
5
#
6
# Perl script to monitor total bandwidth *byprojects* (e.g. vhost) from multiple
7
# files and/or regex.
8
#
9
# Danny Fullerton <northox@mantor.org> 
10
# Mantor Organization <www.mantor.org>
11
# This work is licensed under a MIT license.
12
#
13
# You need logtail (https://www.fourmilab.ch/webtools/logtail/)
14
#
15
# Your nginx configuration should look like this (i.e. $request_length 
16
# body_bytes_sent at the end):
17
#   log_format main  '$remote_addr - $remote_user $time_local "$request" '
18
#                    '$status $body_bytes_sent "$http_referer" '
19
#                    '"$http_user_agent" $request_length $body_bytes_sent';
20
#
21
# Log can be gathered from multiple sources by simply specifying multiple log 
22
# filename or using wildcards (glob). File content can be selected using regex.
23
#
24
# - 'prod' => [ {'path' => '/home/prod/log/access.log'} ],
25
#   Prod graph will be using everything in /home/prod/log/access.log
26
#
27
# - 'test' => [ {'path' => '/var/log/access.log', 'regex' => '"[A-Z]+ /test/'},
28
#               {'path' => '/home/test/log/access*.log'} ],
29
#   Test graph will be using everything file matching /home/test/log/access*.log
30
#   and stuff that match the expression '"[A-Z] /test/' in /var/log/access.log
31
#   such as '"GET /test/'
32

    
33
my $server = 'Nginx';
34

    
35
my $statepath = $ENV{MUNIN_PLUGSTATE};
36
my $logtail = '/usr/local/bin/logtail';
37

    
38
my %logs = (
39
    'prod' => [
40
                {'path' => '/home/prod/log/access.log'}
41
              ],
42
    'dev'  => [
43
                {'path' => '/var/log/httpd/ssl-dev-access.log'},
44
                {'path' => '/home/dev/log/access.log'}
45
              ],
46
    'test' => [
47
                {'path' => '/var/log/access.log', 'regex' => '"[A-Z]+ /test/'},
48
                {'path' => '/home/test/log/access.log'}
49
              ],
50
);
51

    
52

    
53
###########
54

    
55
if(defined($ARGV[0])) {
56
  if ($ARGV[0] eq 'autoconf') {
57
    print "yes\n";
58
    exit(0);
59
  } elsif ($ARGV[0] eq 'config') {
60
    my $order = '';
61
    while ((my $project, my @files) = each(%logs)) { $order .= $project.' ' }
62
    print "graph_order $order\n";
63
    print "graph_title $server total bandwidth byprojects\n";
64
    print "graph_total Total\n";
65
    print "graph_vlabel Bits\n";
66
    print "graph_category webserver\n";
67
    print "graph_info This graph show $server total bandwidth used by various " .
68
      "projects.\n";
69
    while ((my $project, my @files) = each(%logs)) {
70
      print $project.".label $project\n";
71
      print $project.".type GAUGE\n";
72
    }
73
    exit(0);
74
  }
75
}
76

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