#!/usr/local/bin/perl -w
# -*- cperl -*-

=head1 NAME

shoutcast online - Munin plugin to show online for shoutcast v1.9.8

=head1 CONFIGURATION

This shows the default configuration of this plugin.  You can override
the status URL.

  [shoutcast*]
      env.url http://localhost:8000/7.html

=head1 MAGIC MARKERS

  #%# family=auto
  #%# capabilities=autoconf

=head1 VERSION

  1.0

=head1 AUTHOR

Stanislav Rudenko aka Sandel

=head1 LICENSE

none

=cut

my $ret = undef;

if (! eval "require LWP::UserAgent;"){
	$ret = "LWP::UserAgent not found";
}

my $URL = exists $ENV{'url'} ? $ENV{'url'} : "http://localhost:8000/7.html";

if ( exists $ARGV[0] and $ARGV[0] eq "autoconf" ) {
    if ($ret){
	print "no ($ret)\n";
	exit 0;
    }

    my $ua = LWP::UserAgent->new(timeout => 30);
    $ua->agent('XML Getter (Mozilla Compatible)');

    my $request = HTTP::Request->new( 'GET' => $URL );
    $request->protocol('HTTP/1.0');

    my $response = $ua->request($request);

    unless ($response->is_success and $response->content =~ /<HTML><meta http-equiv="Pragma" content="no-cache"><\/head><body>/im) {
	print "no (no shoutcast status on $URL)\n";
	exit 0;
    } else {
	print "yes\n";
	exit 0;
    }
}

if ( exists $ARGV[0] and $ARGV[0] eq "config" ) {

    # Default Settings
    print "graph_title SHOUTcast Online\n";
    print "graph_args --base 1000\n";
    print "graph_category streaming\n";
    print "graph_vlabel Connections per \${graph_period}\n";

    # Max Listeners Allowed to Connect to Server
    print "max_connections.draw AREA\n";
    print "max_connections.colour cdcfc4\n";
    print "max_connections.min 0\n";
    print "max_connections.label Max Slots\n";
    print "max_connections.type GAUGE\n";

    # Peak Listeners
    print "ax_used_connections.draw AREA\n";
    print "ax_used_connections.colour ffd660\n";
    print "ax_used_connections.min 0\n";
    print "ax_used_connections.label Peak Listeners\n";
    print "ax_used_connections.type GAUGE\n";

    # DJ-Online tag must be eq to Peak Listeners 
    print "djonline_tag.draw AREA\n";
    print "djonline_tag.colour e5ff60\n";
    print "djonline_tag.min 0\n";
    print "djonline_tag.label DJ-Online Tag\n";
    print "djonline_tag.type GAUGE\n";

    # Max Listeners Connected to Server
    print "all_connections.draw LINE1\n";
    print "all_connections.colour a00e95\n";
    print "all_connections.min 0\n";
    print "all_connections.label Listeners\n";
    print "all_connections.type GAUGE\n";

    # Max Unique Listeners Connected to Server
    print "unique_connections.draw LINE1\n";
    print "unique_connections.colour 330099\n";
    print "unique_connections.min 0\n";
    print "unique_connections.label Unique Listeners\n";
    print "unique_connections.type GAUGE\n";

    exit 0;
}

#my $ua = LWP::UserAgent->new(timeout => 30);
#my $response = $ua->request(HTTP::Request->new('GET',$URL));

my $ua = LWP::UserAgent->new(timeout => 30);
$ua->agent('XML Getter (Mozilla Compatible)');

my $request = HTTP::Request->new( 'GET' => $URL );
$request->protocol('HTTP/1.0');

my $response = $ua->request($request);

#<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>89,1,453,800,87,128,Kitadani Hiroshi - Bushi</body>
#print $response->content . "\n\n";

if ( $response->content =~ /<HTML><meta http-equiv="Pragma" content="no-cache"><\/head><body>(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(.*)<\/body>/s ) {

    my $djonline = ( $7 =~ m/Dj-Online/i ) ? $3 : '0';

    print "max_connections.value $4\n";
    print "ax_used_connections.value $3\n";
    print "djonline_tag.value $djonline\n";
    print "all_connections.value $1\n";
    print "unique_connections.value $5\n";
} else {
    print "\n";
}
