Projet

Général

Profil

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

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

Historique | Voir | Annoter | Télécharger (2,74 ko)

1
#!/usr/bin/perl -w
2
use strict;
3
#
4
# byprojects_access
5
#
6
# Perl script to monitor access *byprojects* (e.g. vhost) from multiple files
7
# 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
# Log can be gathered from multiple sources by simply specifying multiple log
16
# filename or using wildcards (glob). File content can be selected using regex.
17
#
18
# - 'prod' => [ {'path' => '/home/prod/log/access.log'} ],
19
#   Prod graph will be using everything in /home/prod/log/access.log
20
#
21
# - 'test' => [ {'path' => '/var/log/access.log', 'regex' => '"[A-Z]+ /test/'},
22
#               {'path' => '/home/test/log/access*.log'} ],
23
#   Test graph will be using everything file matching /home/test/log/access*.log
24
#   and stuff that match the expression '"[A-Z] /test/' in /var/log/access.log
25
#   such as '"GET /test/'
26

    
27
my $server = 'Apache';
28

    
29
my $statepath = $ENV{MUNIN_PLUGSTATE};
30
my $logtail = '/usr/local/bin/logtail';
31

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

    
46
###########
47

    
48
if(defined($ARGV[0])) {
49
  if ($ARGV[0] eq 'autoconf') {
50
    print "yes\n";
51
    exit(0);
52
  } elsif ($ARGV[0] eq 'config') {
53
    my $order = '';
54
    while ((my $project, my @files) = each(%logs)) { $order .= $project.' ' }
55
    print "graph_order $order\n";
56
    print "graph_title $server access byprojects\n";
57
    print "graph_total Total\n";
58
    print "graph_vlabel Access by \${graph_period}\n";
59
    print "graph_category webserver\n";
60
    print "graph_info This graph show $server access by various projects.\n";
61
    while ((my $project, my @files) = each(%logs)) {
62
      print $project.".label $project\n";
63
      print $project.".type DERIVE\n";
64
      print $project.".min 0\n";
65
    }
66
    exit(0);
67
  }
68
}
69

    
70
foreach my $project ( keys %logs )  {
71
  my $i = 0;
72
  my $x = 0;
73
  foreach my $log ( @{$logs{$project}} ) {
74
    my @paths = glob $log->{'path'};
75
    foreach my $path (@paths) {
76
      my $state = $statepath.'/'.$project.$x.'_access.state';
77
      open(LT, "$logtail -f ".$log->{'path'}." -o $state |") or
78
        die "Can't open $logtail : $!";
79
      while (<LT>) {
80
        my $buf = $_;
81
        if($buf eq '') { next }
82
        if(!defined($log->{'regex'}) || $buf =~ m/$log->{'regex'}/) {
83
          $i++;
84
        }
85
      }
86
      close(LT);
87
      $x++;
88
    }
89
  }
90
  print $project.".value $i\n";
91
}