Projet

Général

Profil

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

root / plugins / nginx / nginx_byprojects / byprojects_access @ df5325d2

Historique | Voir | Annoter | Télécharger (2,66 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 and/or regex.
7
#
8
# Danny Fullerton <northox@mantor.org> 
9
# Mantor Organization <www.mantor.org>
10
# This work is licensed under a Creative Commons Attribution 3.0 Unported License.
11
#
12
# You need logtail (https://www.fourmilab.ch/webtools/logtail/)
13
#
14
# Log can be gathered from multiple sources by simply specifying multiple log filename 
15
# and/or a log filename and a regex.
16
#   - 'prod' => [ {'path' => '/home/prod/log/access.log'} ],
17
#     Prod graph will be using everything in /home/prod/log/access.log
18
#   - 'test' => [ {'path' => '/var/log/access.log', 'regex' => '"[A-Z]+ /test/'},
19
#                 {'path' => '/home/test/log/access.log'} ],
20
#     Test graph will be using everything in /home/test/log/access.log and stuff that match 
21
#     '"[A-Z] /test/' in /var/log/access.log such as '"GET /test/'
22

    
23
my $server = 'Nginx';
24

    
25
my $statepath = '/usr/local/var/munin/plugin-state'; # where logtail will save the state
26
my $logtail = '/usr/local/bin/logtail';
27

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

    
42
###########
43

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

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