#!/usr/bin/perl -w ###################################################################### # # check_shoutcast - check shoutcast server # # Copyright (C) 2011 Christian Wittmer # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301, USA # or review on web # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html # ###################################################################### use strict; use lib "/usr/lib/nagios/plugins"; use utils qw($TIMEOUT %ERRORS &print_revision &support); use LWP::UserAgent; use HTML::Parser 3.00 (); use Getopt::Long; our ($VERSION, $ME, $opt_h, $opt_H, $opt_p, $URL, $content, $result, @info, %inside); my $info_start = 'Current Stream Information'; my $info_stop = 'Stream Status:'; my $marker = 0; $VERSION = "1.00"; $ME = "check_shoutcast"; $ENV{'PATH'}=''; $ENV{'BASH_ENV'}=''; $ENV{'ENV'}=''; ######################################## sub help(); sub usage(); Getopt::Long::Configure('bundling'); GetOptions ( "h" => \$opt_h, "help" => \$opt_h, "H=s" => \$opt_H, "hostname=s" => \$opt_H, "p=i" => \$opt_p, "port=i" => \$opt_p, ); if ($opt_h) { help(); exit $ERRORS{'OK'}; } sub usage() { my $IN = shift; print "Usage:\n $ME -H [-p ]\n"; print "NOTE: -H must be defined\n\n"; print "Options:\n"; print "\t-h, --help\n"; print "\t Print detailed help screen\n"; print "\t-H, --hostname=ADDRESS\n"; print "\t Host name argument for servers using host headers (virtual host)\n"; print "\t Append a port to include it in the header (eg: example.com:5000)\n"; print "\t-p. --port=INTEGER\n"; print "\t Port number\n"; print "\n"; print "Example:\n"; print "\t check_shoutcast -H localhost -p 8000\n"; print "\t SHOUTCAST OK - Server is currently up and public.\n"; print "\n"; print "Notes:\n"; print " This plugin will attempt to open an HTTP connection with the host.\n"; print " And read in the status page of the shoutcast server (index.html)\n"; print " Successful connects and \"Server is currently up and public.\"\n"; print " return STATE_OK, refusals and timeouts return STATE_CRITICAL other\n"; print " errors return STATE_UNKNOWN. Successful connects, and\n"; print " \"Server is currently down.\" result in STATE_WARNING.\n"; print "\n"; if (defined $IN && $IN eq "OK") { exit $ERRORS{'UNKNOWN'}; } } sub help() { print_revision($ME,'1.4.15'); print 'Copyright (c) 2011 Christian Wittmer '."\n\n"; usage(); support(); } #IN: host port sub get_content() { my $host = shift; my $port = shift; if (defined $port) { $URL = 'http://'.$host.':'.$port.'/'; } else { $URL = 'http://'.$host.'/'; } my $ua = LWP::UserAgent->new; $ua->agent('Mozilla/4.0'); $ua->timeout(10); $ua->env_proxy; my $response = $ua->get($URL); if ($response->is_success) { $content = $response->decoded_content; # or whatever $content =~ s/ / /g; return $content; } else { $result = $response->code; print "SHOUTCAST CRITICAL - Can not reach Server ($result)\n"; exit(2); } } sub get_status() { HTML::Parser->new( api_version => 3, handlers => [start => [\&tag, "tagname, '+1'"], end => [\&tag, "tagname, '-1'"], text => [\&text, "dtext"], ], marked_sections => 1, )->parse($content); } sub tag { my($tag, $num) = @_; $inside{$tag} += $num; } sub text { return if $inside{title} || $inside{script} || $inside{style}; my $text = $_[0]; if ( $text =~ m/^$info_start/) { $marker = 1; return; } elsif ($text =~ m/^$info_stop/){ $marker = 0; } if ($marker == 1) { push(@info, $text); } } &usage('OK') unless ($opt_H); &get_content($opt_H, $opt_p); if (defined $content) { &get_status; $result = $info[1]; if ($result eq "Server is currently up and public.") { print "SHOUTCAST OK - $info[1]\n"; exit(0); } elsif ($result eq "Server is currently down.") { print "SHOUTCAST WARNING - $info[1]\n"; exit(1); } } else { print "SHOUTCAST WARNING - No content to parse :(\n"; exit(1); }