Personal tools
You are here: Home Linux MythTV images check_mythtv.pl v1.1 (modified)
Navigation
 

check_mythtv.pl v1.1 (modified)

A modified version of check_mythtv.pl v1.1 from http://www.sjcnet.id.au/computers/nagios-check-plugin-for-mythtv that doesn't enable http proxy support

check_mythtv.pl — text/x-perl, 3Kb

File contents

#!/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


use strict;
use lib "/usr/lib64/nagios/plugins";
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use Getopt::Long;
use LWP::UserAgent;
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 $ua = LWP::UserAgent->new;
my $response = $ua->get("http://$hostname:$port/xml");
if (!$response->is_success) {
	print "Failed to get http://$hostname:$port/xml\n";
	exit $ERRORS{'CRITICAL'};
}

my $xml = $response->decoded_content;
if (!defined $xml) {
	print "Failed to decode response from  http://$hostname:$port/xml\n";
	exit $ERRORS{'CRITICAL'};
}

my $ref = XMLin($xml);

# Check tuner status to report as info

my $tuner_status = "";

if ($ref->{Encoders}->{count} > 1) {
	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],";
		}
	}
} else {
	my $id = $ref->{Encoders}->{Encoder}->{id};
	my $state = $ref->{Encoders}->{Encoder}->{state};
	if ($state) {
		$tuner_status .= " Encoder $id 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};

Document Actions