Projet

Général

Profil

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

root / plugins / apache / apache_byprojects / byprojects_inout_bandwidth @ 17f78427

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

1
#!/usr/bin/perl -w
2
use strict;
3
#
4
# byprojects_inout_bandwidth
5
#
6
# Perl script to monitor in/out bandwidth *byprojects* (e.g. vhost) from
7
# multiple 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
# mod_logio apache module (https://httpd.apache.org/docs/2.0/mod/mod_logio.html)
16
# Your logformat should look like this:
17
# "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O"
18
# where %I is input and %O is output.
19
#
20
# Log can be gathered from multiple sources by simply specifying multiple log
21
# filename or using wildcards (glob). File content can be selected using regex.
22
#
23
# - 'prod' => [ {'path' => '/home/prod/log/access.log'} ],
24
#   Prod graph will be using everything in /home/prod/log/access.log
25
#
26
# - 'test' => [ {'path' => '/var/log/access.log', 'regex' => '"[A-Z]+ /test/'},
27
#               {'path' => '/home/test/log/access*.log'} ],
28
#   Test graph will be using everything file matching /home/test/log/access*.log
29
#   and stuff that match the expression '"[A-Z] /test/' in /var/log/access.log
30
#   such as '"GET /test/'
31

    
32
my $server = 'Apache';
33

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

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

    
51

    
52
###########
53

    
54
if(defined($ARGV[0])) {
55
  if ($ARGV[0] eq 'autoconf') {
56
    print "yes\n";
57
    exit(0);
58
  } elsif ($ARGV[0] eq 'config') {
59
    print "graph_title $server in/out bandwidth byprojects\n";
60
    print "graph_args --base 1000\n";
61
    print "graph_vlabel bits per \${graph_period} in (-) / out (+)\n";
62
    print "graph_category webserver\n";
63
    print "graph_info This graph show $server in/out bandwidth used by various" .
64
     " projects.\n";
65
    while ((my $project, my @files) = each(%logs)) {
66
      print "i".$project.".label $project\n";
67
      print "i".$project.".type GAUGE\n";
68
      print "i".$project.".graph no\n";
69
      print "i".$project.".cdef i".$project.",8,*\n";
70
      print "o".$project.".label $project\n";
71
      print "o".$project.".type GAUGE\n";
72
      print "o".$project.".negative i".$project."\n";
73
      print "o".$project.".cdef o".$project.",8,*\n";
74
    }
75
    exit(0);
76
  }
77
}
78

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