Projet

Général

Profil

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

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

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

1 614c22df Danny Fullerton
#!/usr/bin/perl -w
2 5271859f Danny Fullerton
use strict;
3 614c22df Danny Fullerton
#
4
# byprojects_access
5
#
6 cf03f9b0 Danny Fullerton
# Perl script to monitor access *byprojects* (e.g. vhost) from multiple files 
7
# and/or regex.
8 614c22df Danny Fullerton
#
9
# Danny Fullerton <northox@mantor.org> 
10
# Mantor Organization <www.mantor.org>
11 cf03f9b0 Danny Fullerton
# This work is licensed under a MIT license.
12 614c22df Danny Fullerton
#
13
# You need logtail (https://www.fourmilab.ch/webtools/logtail/)
14
#
15 cf03f9b0 Danny Fullerton
# 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 614c22df Danny Fullerton
27 5271859f Danny Fullerton
my $server = 'Nginx';
28 614c22df Danny Fullerton
29 4b2fcbf8 Lars Kruse
my $statepath = $ENV{MUNIN_PLUGSTATE};
30 5271859f Danny Fullerton
my $logtail = '/usr/local/bin/logtail';
31 614c22df Danny Fullerton
32 5271859f Danny Fullerton
my %logs = (
33
    'prod' => [
34
                {'path' => '/home/prod/log/access.log'}
35
              ],
36
    'dev'  => [
37
                {'path' => '/var/log/httpd/ssl-dev-access.log'},
38 cf03f9b0 Danny Fullerton
                {'path' => '/home/dev/log/access*.log'} # glob is supported
39 5271859f Danny Fullerton
              ],
40
    'test' => [
41
                {'path' => '/var/log/access.log', 'regex' => '"[A-Z]+ /test/'},
42
                {'path' => '/home/test/log/access.log'}
43
              ],
44 614c22df Danny Fullerton
);
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 5271859f Danny Fullerton
    my $order = '';
54
    while ((my $project, my @files) = each(%logs)) { $order .= $project.' ' }
55 614c22df Danny Fullerton
    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 62560fae dipohl
    print "graph_category webserver\n";
60 614c22df Danny Fullerton
    print "graph_info This graph show $server access by various projects.\n";
61 5271859f Danny Fullerton
    while ((my $project, my @files) = each(%logs)) {
62 cf03f9b0 Danny Fullerton
      print $project.".label $project\n";
63 5271859f Danny Fullerton
      print $project.".type DERIVE\n";
64
      print $project.".min 0\n";
65 614c22df Danny Fullerton
    }
66
    exit(0);
67
  }
68
}
69
70 5271859f Danny Fullerton
foreach my $project ( keys %logs )  {
71
  my $i = 0;
72
  my $x = 0;
73
  foreach my $log ( @{$logs{$project}} ) {
74 cf03f9b0 Danny Fullerton
    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 614c22df Danny Fullerton
      }
86
    }
87 5271859f Danny Fullerton
    close(LT);
88 614c22df Danny Fullerton
    $x++;
89
  }
90 5271859f Danny Fullerton
  print $project.".value $i\n";
91 614c22df Danny Fullerton
}