From bc735e2cc6484235f435b672ccfe308f3619101f Mon Sep 17 00:00:00 2001 From: Ian Dobson Date: Sat, 5 Jul 2008 11:13:51 +0200 Subject: [PATCH] Initial version --- plugins/other/mythtv_programs | 157 ++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100755 plugins/other/mythtv_programs diff --git a/plugins/other/mythtv_programs b/plugins/other/mythtv_programs new file mode 100755 index 00000000..b5eb1745 --- /dev/null +++ b/plugins/other/mythtv_programs @@ -0,0 +1,157 @@ +#!/usr/bin/perl -w +# +# Munin plugin for MythTV +# This plugin can graph:- EPG programs per channels +# +# NOTE: This plugin needs to run as root so add the following to your munin-node config file +# [mythtv_status*] +# user=root +# The http/xml status page must be enabled in the mythtv backend. +# +# $Log$ +# Revision 0.1 2008/03/27 idobson +# Code for all options except recorded implemented +# +# Magic markers (optional - used by munin-config and installation scripts): +# +#%# family=auto +#%# capabilities=autoconf +use LWP::Simple; +#use XML::Parser; +use DBI; + +# Parameters for Backend xml feed +my $backendHostname = "192.168.0.2"; +my $backendStatusPort = "6544"; + +# SQL parameters are read from mysql.txt +# There should be no need to change anything after this point +my $SQLServer = ""; +my $SQLUser = ""; +my $SQLPassword = ""; +my $SQLDBName = ""; + +my $result=""; +my $gata=""; +my $VideoInput=1; +my $Channel=""; + +#Auto config options + if ($ARGV[0] and $ARGV[0] eq "autoconf" ) { + print "yes\n"; + exit 0; + } + +#Setup SQL access + PrepSQLRead(); + +#Config Options +##Configuration for encoder, no config data needs to read from anywhere + if ($ARGV[0] and $ARGV[0] eq "config"){ + print "graph_scale off\n"; + print "graph_title MythTV EPG per channel\n"; + print "graph_args --base 1000\n"; + print "graph_category MythTV\n"; + print "graph_vlabel EPG days\n"; + @result=SQLQuery("SELECT `chanid` , `name` FROM `channel` order by `chanid` "); + my $Ptr=0; + foreach $gata (@result) { + if ($Ptr == 0) { + $Channel = $gata; + $Ptr=1; + } else { + print "Channel" . $Channel . "EPG.label EPG days for channel $gata\n"; + $Ptr=0; + } + } + exit 0; + } + + +#Actually dump data to Munin + @result=SQLQuery("SELECT c.chanid, (UNIX_TIMESTAMP(MAX(c.starttime)) + - UNIX_TIMESTAMP(NOW()))/86400 AS calc + FROM program c, channel o + WHERE o.chanid = c.chanid GROUP BY o.chanid + ORDER BY calc "); + my $Ptr=0; + foreach $gata (@result) { + if ($Ptr == 0) { + $Channel = $gata; + $Ptr=1; + } else { + print "Channel" . $Channel . "EPG.value $gata\n"; + $Ptr=0; + } + } + +exit 0; + + +#Try and read MythTV configuration parameters from mysql.txt (This could be in several places) +sub PrepSQLRead { + my $hostname = `hostname`; + chomp($hostname); + +# Read the mysql.txt file in use by MythTV. Could be in a couple places, so try the usual suspects + my $found = 0; + my @mysql = ('/usr/local/share/mythtv/mysql.txt', + '/usr/share/mythtv/mysql.txt', + '/etc/mythtv/mysql.txt', + '/usr/local/etc/mythtv/mysql.txt', + "$ENV{HOME}/.mythtv/mysql.txt", + 'mysql.txt' + ); + foreach my $file (@mysql) { + next unless (-e $file); + $found = 1; + open(CONF, $file) or die "Unable to open $file: $!\n\n"; + while (my $line = ) { + # Cleanup + next if ($line =~ /^\s*#/); + $line =~ s/^str //; + chomp($line); + # Split off the var=val pairs + my ($var, $val) = split(/\=/, $line, 2); + next unless ($var && $var =~ /\w/); + if ($var eq 'DBHostName') { + $SQLServer = $val; + } + elsif ($var eq 'DBUserName') { + $SQLUser = $val; + } + elsif ($var eq 'DBName') { + $SQLDBName = $val; + } + elsif ($var eq 'DBPassword') { + $SQLPassword = $val; + } + # Hostname override + elsif ($var eq 'LocalHostName') { + $hostname = $val; + } + } + close CONF; + } + die "Unable to locate mysql.txt: $!\n\n" unless ($found && $SQLServer); + return 0; +} + +#Perform SQL query +sub SQLQuery { + my ($QUERY) = @_; + my @data; + my $dbh = DBI->connect("DBI:mysql:$SQLDBName:$SQLServer", $SQLUser, $SQLPassword) + or die "Couldn't connect to database: " . DBI->errstr; + my $table_data = $dbh->prepare($QUERY) or die "Couldn't prepare statement: " . $dbh->errstr; + $table_data->execute or die "Couldn't execute statement: " . $table_data->errstr; + + while ( $ref = $table_data->fetchrow_arrayref() ) { + push (@data,@{$ref}) + } + if ($data[0]) { + return @data; + } else { + return 0; + } +}