Projet

Général

Profil

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

root / plugins / nginx / nginx_byprojects / byprojects_inout_bandwidth @ 614c22df

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

1
#!/usr/bin/perl -w
2
#
3
# byprojects_inout_bandwidth
4
#
5
# Perl script to monitor in/out bandwidth *byprojects* (e.g. vhost) from multiple files and/or regex.
6
#
7
# Danny Fullerton <northox@mantor.org> 
8
# Mantor Organization <www.mantor.org>
9
#
10
# You need logtail (https://www.fourmilab.ch/webtools/logtail/) and 
11
#
12
# Your nginx configuration should look like this (i.e. $request_length $body_bytes_sent at the end):
13
#   log_format main  '$remote_addr - $remote_user $time_local "$request" '
14
#                    '$status $body_bytes_sent "$http_referer" '
15
#                    '"$http_user_agent" $request_length $body_bytes_sent';
16
#
17
# Log can be gather from multiple sources by simply specifying multiple log filename and/or an array with two
18
# elements: log filename and a regex.
19
#   - 'prod' => array('/home/prod/log/access.log'),
20
#     Prod graph will be using everything in /home/prod/log/access.log
21
#   - 'test' => array(array('/var/log/httpd/access.log', '"[A-Z]+ /test/'), '/home/test/log/access.log')
22
#     Test graph will be using eveything in /home/test/log/access.log and stuff that match '"[A-Z] /test/' in 
23
#     /var/log/httpd/access.log such as '"GET /test/'
24

    
25
$server = 'Nginx';
26

    
27
$statepath = '/usr/local/var/munin/plugin-state'; # directory where logtail will save the state
28
$logtail = '/usr/local/bin/logtail';
29

    
30
%logs = (
31
'prod' => ('/home/prod/log/access.log'),
32
'test' => (
33
            ('/var/log/httpd/access.log', '"[A-Z]+ /test/'),
34
            '/home/test/log/access.log'
35
          )
36
);
37

    
38
###########
39

    
40
if(defined($ARGV[0])) {
41
  if ($ARGV[0] eq 'autoconf') {
42
    print "yes\n";
43
    exit(0);
44
  } elsif ($ARGV[0] eq 'config') {
45
    print "graph_title $server in/out bandwidth byprojects\n";
46
    print "graph_args --base 1000\n";
47
    print "graph_vlabel bits per \${graph_period} in (-) / out (+)\n";
48
    print "graph_category $server\n";
49
    print "graph_info This graph show $server in/out bandwidth used by various projects.\n";
50
    while (($client, $files) = each(%logs)) {
51
      print "i".$client.".label $client\n";
52
      print "i".$client.".type GAUGE\n";
53
      print "i".$client.".graph no\n";
54
      print "i".$client.".cdef i".$client.",8,*\n";
55
      print "o".$client.".label $client\n";
56
      print "o".$client.".type GAUGE\n";
57
      print "o".$client.".negative i".$client."\n";
58
      print "o".$client.".cdef o".$client.",8,*\n";
59
    }
60
    exit(0);
61
  }
62
}
63

    
64
while (($client, $files) = each(%logs)) {
65
  $i = $o = $x = 0;
66
  foreach $file ($files) {
67
    $regex = '';
68
    $state = $statepath.'/'.$client.$x.'_inoutbandwidth.state';
69
    if(ref($file) eq 'ARRAY') { ($file, $regex) = @file }
70
    open(my $lt, "$logtail -f $file -o $state |") or die "Can't open $logtail : $!";
71
    while (<$lt>) {
72
      $buf = $_;
73
      if($buf eq '') { next }
74
      if(!defined($regex) || $buf =~ m/$regex/) {
75
        if($buf =~ m/(\d+) (\d+)$/) {
76
          $i += $1;
77
          $o += $2;
78
        }
79
      }
80
    }
81
    close($lt);
82
    $x++;
83
  }
84
  print "i".$client.".value $i\n";
85
  print "o".$client.".value $o\n";
86
}