#!/usr/bin/perl -w

=encoding utf8

=head1 NAME

apache - 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.

=head1 APPLICABLE SYSTEMS

Apache HTTP servers with C</server-status> 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]
     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<different> Apaches configured on
your host.

If you need authenticated access to the URL you can either specify the
username and password in the URL, or use the C<http_username> and
C<http_password> variables.  For example:

  [apache]
     env.url http://munin:spamalot@localhost/server-status?auto

or

  [apache]
     env.http_username munin
     env.http_password spamalot

This will provide for HTTP basic authentication.

=head2 WILDCARD CONFIGURATION

It's possible to use the plugin in a virtual-node capacity, in which
case the URL will automatically default to the hostname defined after
the second underscore in the plugin name:

  [apache_someserver]
     env.url http://someserver:%d/server-status?auto

=head1 INTERPRETATION

=head2 APACHE ACCESSES

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<MaxClients> 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<MaxClients> 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 AUTHOR

Rewritten by Diego Elio Pettenò <flameeyes@flameeyes.eu> 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::HTTP;
use Munin::Plugin::Framework;

my $plugin = Munin::Plugin::Framework->new;

$plugin->{hostname} = $1 if $Munin::Plugin::me =~ /_([^_]+)$/;

if ( exists $ENV{'hostname'} ) {
  $plugin->{hostname} = $ENV{'hostname'};
}

my $URL = "http://127.0.0.1:%d/server-status?auto";
if ( exists $ENV{'url'} ) {
  $URL = $ENV{'url'};
} elsif ( defined $plugin->{hostname} ) {
  $URL = "http://$plugin->{hostname}:%d/server-status?auto"
}

my @PORTS = exists $ENV{'ports'} ? split(' ', $ENV{'ports'}) : (80);
my $UA = Munin::Plugin::HTTP->new;
my %responses;

foreach my $port (@PORTS) {
  my $url = sprintf $URL, $port;
  $responses{$port} = $UA->get($url);
}

while (my ($port, $response) = each(%responses)) {
  if ($response->is_success) {
    if ($response->content =~ /^Total Accesses:/im ) {
      next;
    } else {
      $plugin->{autoconf} = "no (ExtendedStatus option for Apache mod_status is missing on port $port)";
    }
  } elsif ($response->code == 404) {
    $plugin->{autoconf} = "no (Apache server-status not found. check if mod_status is enabled)\n";
  } else {
    $plugin->{autoconf} = "no (Port $port: ". $response->message .")\n";
  }
}

$plugin->{graphs} =
  {
   apache_accesses =>
   {
    title    => "Apache accesses",
    args     => "--base 1000 -l 0",
    vlabel   => "accesses per \${graph_period}",
    category => "apache",
    info     => "The number of accesses (pages and other items served) globally on the Apache server",
    order    => "",
    fields   => { }
   },
   apache_processes =>
   {
    title    => "Apache processes",
    args     => "--base 1000 -l 0",
    vlabel   => "processes",
    total    => "total",
    category => "apache",
   },
   apache_volume =>
   {
    title    => "Apache volume",
    args     => "--base 1024 -l 0",
    vlabel   => "KiB per \${graph_period}",
    scale    => "no", # these are already expressed in KiB, avoid kKiB
    category => "apache",
   },
  };

foreach my $port (@PORTS) {
  my ($accesses, $busy, $idle, $free, $volume, $threads);

  if ( $responses{$port}->is_success ) {
    my $content = $responses{$port}->content;

    if ($content =~ /^Total Accesses:\s+(.+)$/im) {
      $accesses = $1;
    }

    if ($content =~ /^Busy(?:Servers|Workers):\s+(.+)$/im) {
      $busy = $1;
    }

    if ($content =~ /^Idle(?:Servers|Workers):\s+(.+)$/im) {
      $idle = $1;
    }

    if ($content =~ /^(Scoreboard: .*)$/m) {
      $free = () = $1 =~ /\./g;
    }

    if ($content =~ /^Total kBytes:\s+(.+)$/im) {
      $volume = $1;
    }

	my $pid = `pidof -s apache2`;
	chomp($pid);
	$threads = `awk '/^Threads:/ {print \$2}' /proc/$pid/status`;
	chomp($threads);
  }

  $plugin->{graphs}->{apache_accesses}->{fields}->{"accesses$port"} =
    {
     label => "port $port",
     type  => "DERIVE",
     min   => 0,
     value => $accesses,
    };

  $plugin->{graphs}->{apache_processes}->{order} .= "busy$port idle$port free$port threads$port ";
  $plugin->{graphs}->{apache_processes}->{fields}->{"busy$port"} =
    {
     label => "busy servers on port $port",
     draw  => "LINE2",
     min   => 0,
     value => $busy,
    };
  $plugin->{graphs}->{apache_processes}->{fields}->{"idle$port"} =
    {
     label => "idle servers on port $port",
     draw  => "STACK",
     min   => 0,
     value => $idle,
    };
  $plugin->{graphs}->{apache_processes}->{fields}->{"free$port"} =
    {
     label => "free slots on port $port",
     draw  => "STACK",
     min   => 0,
     value => $free,
    };
  $plugin->{graphs}->{apache_processes}->{fields}->{"threads$port"} =
    {
     label => "threads on port $port",
     draw  => "STACK",
     min   => 0,
     value => $threads,
    };

  $plugin->{graphs}->{apache_volume}->{fields}->{"volume$port"} =
    {
     label => "port $port",
     type  => "DERIVE",
     min   => 0,
     value => $volume
    };
}

$plugin->run;
