#!/usr/bin/perl -w
# 
#  MythTV (http://www.mythtv.org) backend Nagios check plugin. 
#
# Copyright (C) 2008 Steve Cliffe <steve@sjcnet.id.au>
#
# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307
#
# Fixed issues to make it work on Nagios 3
#

if (-e "/usr/local/nagios/libexec/utils.pm") {
use lib "/usr/local/nagios/libexec";
}else{
use lib "/usr/lib/nagios/libexec";
}
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use Getopt::Long;
use LWP::Simple;
use XML::Simple;
my ($opt_p, $opt_H, $port, $hostname);
sub print_usage ();

my @Tuner_States = (
	"Idle",
	"Watching Live TV",
	"Watching Recorded",
	"Watching Recording",
	"Recording");

my $warning_days = 4;
my $critical_days = 2;
my $exit_state = 'UNKNOWN';

# Parse arguments

Getopt::Long::Configure('bundling');
GetOptions(
        "p=i" => \$opt_p,   "port=i"     => \$opt_p,
        "H=s" => \$opt_H,   "hostname=s" => \$opt_H
);

unless ($opt_H) { 
	print_usage(); 
	exit $ERRORS{'UNKNOWN'}; 
}
if (! utils::is_hostname($opt_H)) {
        print("Invalid hostname/IP address\n");
        print_usage();
        exit $ERRORS{'UNKNOWN'};
}

$hostname = $opt_H;

if (defined $opt_p) {
    	$port = $opt_p;
} else {
    	$port = 6544;
}

sub print_usage () {
        print "Usage: \n";
        print " check_mythtv.pl -H host [-p port]\n";
}

# Main code

my $xml = get("http://$hostname:$port/xml");
if (!defined $xml) {
	print "Failed to get http://$hostname:$port/xml\n";
	exit $ERRORS{'CRITICAL'};
}

my $ref = XMLin($xml);

# Check tuner status to report as info

my $tuner_status = "";

foreach my $encoder ( keys( %{ $ref->{Encoders}->{Encoder} } ) ) {
	my $enc = $ref->{Encoders}->{Encoder}->{$encoder};
	my $state = $enc->{state};
	if ($state) {
		$tuner_status .= " Encoder $encoder is $Tuner_States[$state],";
	}
}

# Check program guide

my $guide_days = $ref->{MachineInfo}->{Guide}->{guideDays};

$exit_state = 'OK';
my $result_text = "Ok:";

if ($guide_days <= $warning_days) {
	$exit_state = 'WARNING';
	$result_text = "Warning:";
}
if ($guide_days <= $critical_days) {
        $exit_state = 'CRITICAL';
        $result_text = "Critical:";
}

# Report

if (length($tuner_status)) {
	$result_text .= "$tuner_status $guide_days days program guide available\n";
} else {
	$result_text .= " Tuners idle, $guide_days days program guide available\n";
}

print $result_text;
exit $ERRORS{$exit_state};
