Projet

Général

Profil

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

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

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

1
#!/usr/bin/perl -w
2
#
3
# byprojects_bandwidth
4
#
5
# Perl script to monitor total 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
$statepath = '/usr/local/var/munin/plugin-state'; # directory where logtail will save the state
26
$logtail = '/usr/local/bin/logtail';
27

    
28
$server = 'Nginx';
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
    $order = '';
46
    while (($client, $files) = each(%logs)) { $order .= $client.' ' }
47
    print "graph_order $order\n";
48
    print "graph_title $server total bandwidth byprojects\n";
49
    print "graph_total Total\n";
50
    print "graph_vlabel Bits\n";
51
    print "graph_category $server\n";
52
    print "graph_info This graph show $server total bandwidth used by various projects.\n";
53
    while (($client, $files) = each(%logs)) {
54
      print $client.".label $client\n";
55
      print $client.".type GAUGE\n";
56
    }
57
    exit(0);
58
  }
59
}
60

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