root / plugins / nginx / nginx_byprojects / byprojects_bandwidth @ 17f78427
Historique | Voir | Annoter | Télécharger (3,29 ko)
| 1 | 614c22df | Danny Fullerton | #!/usr/bin/perl -w |
|---|---|---|---|
| 2 | 5271859f | Danny Fullerton | use strict; |
| 3 | a1f7808b | Neraud | use JSON qw(decode_json); |
| 4 | 614c22df | Danny Fullerton | # |
| 5 | # byprojects_bandwidth |
||
| 6 | # |
||
| 7 | cf03f9b0 | Danny Fullerton | # Perl script to monitor total bandwidth *byprojects* (e.g. vhost) from multiple |
| 8 | # files and/or regex. |
||
| 9 | 614c22df | Danny Fullerton | # |
| 10 | 17f78427 | Lars Kruse | # Danny Fullerton <northox@mantor.org> |
| 11 | 614c22df | Danny Fullerton | # Mantor Organization <www.mantor.org> |
| 12 | cf03f9b0 | Danny Fullerton | # This work is licensed under a MIT license. |
| 13 | 614c22df | Danny Fullerton | # |
| 14 | 5271859f | Danny Fullerton | # You need logtail (https://www.fourmilab.ch/webtools/logtail/) |
| 15 | 614c22df | Danny Fullerton | # |
| 16 | 17f78427 | Lars Kruse | # Your nginx configuration should look like this (i.e. $request_length |
| 17 | cf03f9b0 | Danny Fullerton | # body_bytes_sent at the end): |
| 18 | 614c22df | Danny Fullerton | # log_format main '$remote_addr - $remote_user $time_local "$request" ' |
| 19 | # '$status $body_bytes_sent "$http_referer" ' |
||
| 20 | # '"$http_user_agent" $request_length $body_bytes_sent'; |
||
| 21 | # |
||
| 22 | 17f78427 | Lars Kruse | # Log can be gathered from multiple sources by simply specifying multiple log |
| 23 | cf03f9b0 | Danny Fullerton | # filename or using wildcards (glob). File content can be selected using regex. |
| 24 | # |
||
| 25 | # - 'prod' => [ {'path' => '/home/prod/log/access.log'} ],
|
||
| 26 | # Prod graph will be using everything in /home/prod/log/access.log |
||
| 27 | # |
||
| 28 | # - 'test' => [ {'path' => '/var/log/access.log', 'regex' => '"[A-Z]+ /test/'},
|
||
| 29 | # {'path' => '/home/test/log/access*.log'} ],
|
||
| 30 | # Test graph will be using everything file matching /home/test/log/access*.log |
||
| 31 | # and stuff that match the expression '"[A-Z] /test/' in /var/log/access.log |
||
| 32 | # such as '"GET /test/' |
||
| 33 | 3a17b22e | Neraud | # |
| 34 | # Configuration |
||
| 35 | # [byprojects_*] |
||
| 36 | # env.logtail_path /usr/local/bin/logtail |
||
| 37 | 69896717 | Neraud | # env.site.prod [{"path":"/home/prod/log/access.log"}]
|
| 38 | # env.site.dev [{"path":"/var/log/httpd/ssl-dev-access.log"}, {"path":"/home/dev/log/access*.log"}]
|
||
| 39 | # env.site.test [{"path":"/var/log/access.log","regex":"\"[A-Z]+ /test/"}, {"path":"/home/test/log/access.log"}]
|
||
| 40 | 614c22df | Danny Fullerton | |
| 41 | 5271859f | Danny Fullerton | my $server = 'Nginx'; |
| 42 | 614c22df | Danny Fullerton | |
| 43 | 4b2fcbf8 | Lars Kruse | my $statepath = $ENV{MUNIN_PLUGSTATE};
|
| 44 | 3a17b22e | Neraud | my $logtail = $ENV{logtail_path} || '/usr/local/bin/logtail';
|
| 45 | 614c22df | Danny Fullerton | |
| 46 | a1f7808b | Neraud | my @loglist = grep {$_ =~ /site\./} keys(%ENV);
|
| 47 | 15d05f30 | Lars Kruse | my %envLogs = %ENV{@loglist};
|
| 48 | a1f7808b | Neraud | my %logs; |
| 49 | while(my($k, $v) = each %envLogs) { @logs{substr($k, 5)} = decode_json($v); }
|
||
| 50 | 5271859f | Danny Fullerton | |
| 51 | 614c22df | Danny Fullerton | ########### |
| 52 | |||
| 53 | if(defined($ARGV[0])) {
|
||
| 54 | if ($ARGV[0] eq 'autoconf') {
|
||
| 55 | print "yes\n"; |
||
| 56 | exit(0); |
||
| 57 | } elsif ($ARGV[0] eq 'config') {
|
||
| 58 | 5271859f | Danny Fullerton | my $order = ''; |
| 59 | while ((my $project, my @files) = each(%logs)) { $order .= $project.' ' }
|
||
| 60 | 614c22df | Danny Fullerton | print "graph_order $order\n"; |
| 61 | print "graph_title $server total bandwidth byprojects\n"; |
||
| 62 | print "graph_total Total\n"; |
||
| 63 | print "graph_vlabel Bits\n"; |
||
| 64 | 62560fae | dipohl | print "graph_category webserver\n"; |
| 65 | 7afe6bfb | Stig Sandbeck Mathisen | print "graph_info This graph show $server total bandwidth used by various " . |
| 66 | cf03f9b0 | Danny Fullerton | "projects.\n"; |
| 67 | 5271859f | Danny Fullerton | while ((my $project, my @files) = each(%logs)) {
|
| 68 | print $project.".label $project\n"; |
||
| 69 | print $project.".type GAUGE\n"; |
||
| 70 | 614c22df | Danny Fullerton | } |
| 71 | exit(0); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | 5271859f | Danny Fullerton | foreach my $project ( keys %logs ) {
|
| 76 | my $i = 0; |
||
| 77 | my $o = 0; |
||
| 78 | my $x = 0; |
||
| 79 | foreach my $log ( @{$logs{$project}} ) {
|
||
| 80 | cf03f9b0 | Danny Fullerton | my @paths = glob $log->{'path'};
|
| 81 | foreach my $path (@paths) {
|
||
| 82 | my $state = $statepath.'/'.$project.$x.'_totalbandwidth.state'; |
||
| 83 | 17f78427 | Lars Kruse | open(LT, "$logtail -f ".$log->{'path'}." -o $state |") or
|
| 84 | cf03f9b0 | Danny Fullerton | die "Can't open $logtail : $!"; |
| 85 | while (<LT>) {
|
||
| 86 | my $buf = $_; |
||
| 87 | if($buf eq '') { next }
|
||
| 88 | if(!defined($log->{'regex'}) || $buf =~ m/$log->{'regex'}/) {
|
||
| 89 | if($buf =~ m/(\d+) (\d+)$/) {
|
||
| 90 | $i += $1; |
||
| 91 | $o += $2; |
||
| 92 | } |
||
| 93 | 614c22df | Danny Fullerton | } |
| 94 | } |
||
| 95 | cf03f9b0 | Danny Fullerton | close(LT); |
| 96 | $x++; |
||
| 97 | 614c22df | Danny Fullerton | } |
| 98 | } |
||
| 99 | $x = $i + $o; |
||
| 100 | 5271859f | Danny Fullerton | print $project.".value $x\n"; |
| 101 | 614c22df | Danny Fullerton | } |
