From 1b2574a43a0761cacb913b454004655803fe8db4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Mon, 5 Nov 2012 12:23:46 -0800 Subject: [PATCH] apache_status: add a new multigraph plugin to replace the old apache_* from the main repository. This plugin uses a single requests to parse all the statistics instead of making three of them, reducing the overhead, and uses the multigraph capabilities to generate the same graphs. The naming is compatible so that it should be a clean update from the previous plugins. This also merges in apache_average_requests (and apache_request_rate) which, albeit not as relevant, also parsed the same file. --- plugins/apache/apache_average_requests | 128 ----------- plugins/apache/apache_request_rate | 41 ---- plugins/apache/apache_status | 289 +++++++++++++++++++++++++ 3 files changed, 289 insertions(+), 169 deletions(-) delete mode 100755 plugins/apache/apache_average_requests delete mode 100755 plugins/apache/apache_request_rate create mode 100755 plugins/apache/apache_status diff --git a/plugins/apache/apache_average_requests b/plugins/apache/apache_average_requests deleted file mode 100755 index e61eb6f9..00000000 --- a/plugins/apache/apache_average_requests +++ /dev/null @@ -1,128 +0,0 @@ -#!/usr/bin/perl -# -*- cperl -*- - -=head1 NAME - -apache_average_request - Munin plugin to monitor the average request to -Apache servers. It handles a list of ports passed in from a plugin -configuration file. - -=head1 APPLICABLE SYSTEMS - -Apache HTTP servers with C enabled. - -=head1 CONFIGURATION - -Enable stats in apache first!: - - - - SetHandler server-status - AuthUserFile /route/to/auth.file - AuthName Login-Stats - AuthType Basic - require valid-user - - - -And now in munin conf: - -[apache_*] -env.url http://USER:PASS@127.0.0.1/server-status?auto -env.ports 80 - -=head1 AUTHOR - -Ricardo Fraile -Unknown - -=head1 LICENSE - -GPLv2 - -=head1 MAGIC MARKERS - - #%# family=auto - #%# capabilities=autoconf - -=cut - -use strict; -use warnings; -use Munin::Plugin; - -my $ret = undef; - -if (! eval "require LWP::UserAgent;") -{ - $ret = "LWP::UserAgent not found"; - if ( ! defined $ARGV[0] ) { - die $ret; - } -} - -my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://127.0.0.1:%d/server-status?auto"; -my @PORTS = exists $ENV{'ports'} ? split(' ', $ENV{'ports'}) : (80); - -if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) -{ - if ($ret) - { - print "no ($ret)\n"; - exit 0; - } - - my $ua = LWP::UserAgent->new(timeout => 30); - - foreach my $port (@PORTS) { - my $url = sprintf $URL, $port; - my $response = $ua->request(HTTP::Request->new('GET',$url)); - if ($response->is_success) { - if ($response->content =~ /^Total Accesses:/im ){ - next; - } - else { - print "no (ExtendedStatus option for apache" - . " mod_status is missing on port $port)\n"; - exit 0; - } - } - elsif ($response->code == 404) { - print "no (apache server-status not found. check if mod_status is enabled)\n"; - exit 0; - } - else { - print "no (Port $port: ". $response->message .")\n"; - exit 0; - } - } - print "yes\n"; - exit 0; -} - -if ( defined $ARGV[0] and $ARGV[0] eq "config" ) -{ - - print "graph_title Average apache request\n"; - print "graph_title Average apache Requests per second\n"; - print "graph_vlabel average reqs/sec\n"; - print "graph_scale no\n"; - print "graph_category apache\n"; - print "request.label Average apache reqs/sec\n"; - exit 0; - -} - -my $ua = LWP::UserAgent->new(timeout => 30); - -foreach my $port (@PORTS) { - my $url = sprintf $URL, $port; - my $response = $ua->request(HTTP::Request->new('GET',$url)); - if ($response->content =~ /^ReqPerSec:\s+(.+)$/im) { - print "request.value $1\n"; - } else { - print "request.value U\n"; - } -} - -# vim:syntax=perl diff --git a/plugins/apache/apache_request_rate b/plugins/apache/apache_request_rate deleted file mode 100755 index 11ec7343..00000000 --- a/plugins/apache/apache_request_rate +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env ruby -require 'rubygems' -require 'json' -require 'open-uri' - -if ARGV.first == 'config' - puts 'graph_category Apache' - puts 'graph_title Requests per second' - puts 'graph_vlabel reqs/sec' - puts 'graph_scale no' - puts 'requests_per_second.label Apache reqs/sec' - exit 0 -end - -STATUS_URL = 'http://localhost/server-status?auto' -TMPFILE = '/tmp/apache-status-monitor' -SECONDS_BEFORE_STALE = 900 - -begin - previous_sample = File.exists?(TMPFILE) ? JSON.parse(File.open(TMPFILE, 'r').read) : {} - output = {} - - apache_status = open(STATUS_URL).read - new_total_requests = apache_status.match(/Total Accesses: (\d+)$/)[1].to_i #subtract one to account for request we just made! - sample_duration = previous_sample['updated_at'] ? Time.now - Time.at(previous_sample['updated_at'].to_i) : nil - - if sample_duration && sample_duration < SECONDS_BEFORE_STALE - output['requests_per_second'] = "%0.2f" % ((new_total_requests - 1 - previous_sample['total_requests'].to_i) / sample_duration.to_f) - else - output = {} - end - - # Write back to tmpfile - new_tmp_data = {'total_requests' => new_total_requests, 'updated_at' => Time.now.to_i} - File.open(TMPFILE, 'w') { |f| f.puts(new_tmp_data.to_json)} - - puts "requests_per_second.value #{output['requests_per_second']}" -rescue Exception => e - puts "Exception: #{e.message}" - exit -1 -end \ No newline at end of file diff --git a/plugins/apache/apache_status b/plugins/apache/apache_status new file mode 100755 index 00000000..b310f0e1 --- /dev/null +++ b/plugins/apache/apache_status @@ -0,0 +1,289 @@ +#!/usr/bin/env perl +# -*- cperl -*- + +=head1 NAME + +apache_status - Munin multigraph plugin to monitor Apache statistics. + +=head1 NOTES + +This plugin will produce multiple graphs showing: + + - the number of accesses to Apache servers; + - the number of apache processes running on the machine; + - the volume of data sent from Apache servers; + - the average requests per second to Apache servers. + +=head1 APPLICABLE SYSTEMS + +Apache HTTP servers with C enabled. + +=head1 CONFIGURATION + +The plugin needs access to http://localhost/server-status?auto (or +modify the URL for another host). See your Apache documentation on +how to set up this URL in your httpd.conf. Apache needs ExtendedStatus +enabled for this plugin to work. + +Tip: To see if it's already set up correctly, just run this plugin +with the parameter "autoconf". If you get a "yes", everything should +work like a charm already. + +This configuration section shows the defaults of the plugin: + + [apache_status] + env.url http://127.0.0.1:%d/server-status?auto + env.ports 80 + +The %d in the url will be replaced with the port. The default port is +80 as shown. + +The port list is a space separated list of ports. NOTE that one +single Apache can have several open ports, and the plugin needs only +to contact one to get the servers global status. The list of ports is +only needed if you have several B Apaches configured on +your host. + +If you need authenticated access to the URL you can specify the +username and password in the URL. For example: + + [apache_status] + env.url http://munin:spamalot@localhost/server-status?auto + +This will provide for HTTP basic authentication. + +=head1 INTERPRETATION + +=head2 APACHE ACCESES + +The graph shows the number of accesses (pages and other items served) +globally on the Apache server. + +=head2 APACHE PROCESSES + +The graph shows the number of Apache processes running on the +machine, and in addition separate "busy" and "idle" servers count. + +If there is a flat ceiling effect on the graph where the number of +servers does not increase any more, in spite of no idle servers, then +the server has probably reached its C setting. In this +case it's very likely that some clients are getting connection refused +or some other problem when connecting to your server. In this case +increase the C setting. Unless there is also no more free +memory. + +=head2 APACHE VOLUME + +The graph shows the Apache HTTP servers global data volume in +bytes. I.e. how many bytes the server has served. + +If there is a flat ceiling effect on the graph you may have reached +some kind of bandwidth limit on your outgoing connection. + +=head1 MAGIC MARKERS + + #%# family=auto + #%# capabilities=autoconf + +=head1 BUGS + +Does not support digest authentication. + +=head1 AUTHOR + +Rewritten by Diego Elio Pettenò based upon +original apache_accesses, apache_processes and apache_volume plugins +of unknown origin. + +=head1 LICENSE + +GPLv2 + +=cut + +use strict; +use warnings; +use Munin::Plugin; + +my $ret = undef; + +if (! eval "require LWP::UserAgent;") { + $ret = "LWP::UserAgent not found"; + if ( ! defined $ARGV[0] ) { + die $ret; + } +} + +my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://127.0.0.1:%d/server-status?auto"; +my @PORTS = exists $ENV{'ports'} ? split(' ', $ENV{'ports'}) : (80); +my $UA = LWP::UserAgent->new(timeout => 30, + agent => sprintf("munin/%s (libwww-perl/%s)", $Munin::Common::Defaults::MUNIN_VERSION, $LWP::VERSION)); + +if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) { + if ($ret) { + print "no ($ret)\n"; + exit 0; + } + + foreach my $port (@PORTS) { + my $url = sprintf $URL, $port; + my $response = $UA->request(HTTP::Request->new('GET',$url)); + if ($response->is_success) { + if ($response->content =~ /^Total Accesses:/im ) { + next; + } else { + print "no (ExtendedStatus option for apache" + . " mod_status is missing on port $port)\n"; + exit 0; + } + } elsif ($response->code == 404) { + print "no (apache server-status not found. check if mod_status is enabled)\n"; + exit 0; + } else { + print "no (Port $port: ". $response->message .")\n"; + exit 0; + } + } + print "yes\n"; + exit 0; +} + +if ( defined $ARGV[0] and $ARGV[0] eq "config" ) { + print < "multigraph apache_accesses\n", + processes => "multigraph apache_processes\n", + volume => "multigraph apache_volume\n", + average => "multigraph apache_average_requests\n" + ); + +foreach my $port (@PORTS) { + my $url = sprintf $URL, $port; + my $response = $UA->request(HTTP::Request->new('GET',$url)); + + if ($response->content =~ /^Total Accesses:\s+(.+)$/im) { + $multigraphs{accesses} .= "accesses$port.value $1\n"; + } else { + $multigraphs{accesses} .= "accesses$port.value U\n"; + } + + if ($response->content =~ /^Busy(?:Servers|Workers):\s+(.+)$/im) { + $multigraphs{processes} .= "busy$port.value $1\n"; + } else { + $multigraphs{processes} .= "busy$port.value U\n"; + } + if ($response->content =~ /^Idle(?:Servers|Workers):\s+(.+)$/im) { + $multigraphs{processes} .= "idle$port.value $1\n"; + } else { + $multigraphs{processes} .= "idle$port.value U\n"; + } + if ($response->content =~ /^(Scoreboard: .*)$/m) { + my $count = () = $1 =~ /\./g; + $multigraphs{processes} .= "free$port.value $count\n"; + } else { + $multigraphs{processes} .= "free$port.value U\n"; + } + + if ($response->content =~ /^Total kBytes:\s+(.+)$/im) { + $multigraphs{volume} .= "volume$port.value $1\n"; + } else { + $multigraphs{volume} .= "volume$port.value U\n"; + } + + if ($response->content =~ /^ReqPerSec:\s+(.+)$/im) { + $multigraphs{average} .= "average$port.value $1\n"; + } else { + $multigraphs{average} .= "average$port.value U\n"; + } +} + +foreach my $graph (keys %multigraphs) { + print $multigraphs{$graph}; +} + +# vim:syntax=perl