root / plugins / mythtv / mythtv_programs @ 17f78427
Historique | Voir | Annoter | Télécharger (4,75 ko)
| 1 |
#!/usr/bin/perl -w |
|---|---|
| 2 |
# |
| 3 |
# Munin plugin for MythTV |
| 4 |
# This plugin can graph:- EPG programs per channel |
| 5 |
# |
| 6 |
# NOTE: This plugin needs to run as root so add the following to your munin-node config file |
| 7 |
# [mythtv_status*] |
| 8 |
# user=root |
| 9 |
# |
| 10 |
# $Log$ |
| 11 |
# Revision 0.1 2008/03/27 idobson |
| 12 |
# |
| 13 |
# Revision 0.2 2010/10/07 idobson |
| 14 |
# Fixed up autoconf option, it actually checks if MythTV/mysql Database options are configured |
| 15 |
# Code now strict safe, cleaned up the sql queries |
| 16 |
# |
| 17 |
# Magic markers (optional - used by munin-config and installation scripts): |
| 18 |
# |
| 19 |
#%# family=auto |
| 20 |
#%# capabilities=autoconf |
| 21 |
use strict; |
| 22 |
use warnings; |
| 23 |
use DBI; |
| 24 |
|
| 25 |
# SQL parameters are read from mysql.txt |
| 26 |
# There should be no need to change anything after this point |
| 27 |
my $SQLServer = ""; |
| 28 |
my $SQLUser = ""; |
| 29 |
my $SQLPassword = ""; |
| 30 |
my $SQLDBName = ""; |
| 31 |
|
| 32 |
my @result=""; |
| 33 |
my $result=""; |
| 34 |
my $gata=""; |
| 35 |
my $VideoInput=1; |
| 36 |
my $Channel=""; |
| 37 |
|
| 38 |
#Setup SQL access |
| 39 |
PrepSQLRead(); |
| 40 |
|
| 41 |
#Auto config options |
| 42 |
if ($ARGV[0] and $ARGV[0] eq "autoconf" ) {
|
| 43 |
if ( $SQLDBName ne "" ) {
|
| 44 |
print "yes\n"; |
| 45 |
exit 0; |
| 46 |
} else {
|
| 47 |
print "no\n"; |
| 48 |
print "cannot find MythTV configuration file my.txt\n"; |
| 49 |
exit 1; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
#Config Options |
| 54 |
##Configuration for encoder, no config data needs to read from anywhere |
| 55 |
if ($ARGV[0] and $ARGV[0] eq "config"){
|
| 56 |
print "graph_scale off\n"; |
| 57 |
print "graph_title MythTV EPG per channel\n"; |
| 58 |
print "graph_args --base 1000 --upper-limit 12 --lower-limit 0 --rigid\n"; |
| 59 |
print "graph_category tv\n"; |
| 60 |
print "graph_vlabel EPG days\n"; |
| 61 |
@result=SQLQuery("SELECT `chanid` , `name` FROM `channel` WHERE `visible` = '1' order by `chanid` ");
|
| 62 |
my $Ptr=0; |
| 63 |
foreach $gata (@result) {
|
| 64 |
if ($Ptr == 0) {
|
| 65 |
$Channel = $gata; |
| 66 |
print "Channel" . $Channel . "EPG.draw LINE1\n"; |
| 67 |
print "Channel" . $Channel . "EPG.min -1\n"; |
| 68 |
print "Channel" . $Channel . "EPG.max 12\n"; |
| 69 |
$Ptr=1; |
| 70 |
} else {
|
| 71 |
print "Channel" . $Channel . "EPG.label EPG days for channel $gata\n"; |
| 72 |
$Ptr=0; |
| 73 |
} |
| 74 |
} |
| 75 |
exit 0; |
| 76 |
} |
| 77 |
|
| 78 |
#Actually dump data to Munin |
| 79 |
@result=SQLQuery("SELECT o.chanid, (UNIX_TIMESTAMP(MAX(c.endtime))
|
| 80 |
- UNIX_TIMESTAMP(NOW()))/86400 |
| 81 |
FROM channel o, program c |
| 82 |
WHERE o.chanid = c.chanid |
| 83 |
AND o.visible = '1' |
| 84 |
GROUP BY o.chanid"); |
| 85 |
my $Ptr=0; |
| 86 |
foreach $gata (@result) {
|
| 87 |
if ($Ptr == 0) {
|
| 88 |
$Channel = $gata; |
| 89 |
$Ptr=1; |
| 90 |
} else {
|
| 91 |
if ( $gata > 12 ) {
|
| 92 |
print "Channel" . $Channel . "EPG.value 12.0\n"; |
| 93 |
} else {
|
| 94 |
print "Channel" . $Channel . "EPG.value $gata\n"; |
| 95 |
} |
| 96 |
$Ptr=0; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
exit 0; |
| 101 |
|
| 102 |
|
| 103 |
#Try and read MythTV configuration parameters from mysql.txt (This could be in several places) |
| 104 |
sub PrepSQLRead {
|
| 105 |
my $hostname = `hostname`; |
| 106 |
chomp($hostname); |
| 107 |
|
| 108 |
# Read the mysql.txt file in use by MythTV. Could be in a couple places, so try the usual suspects |
| 109 |
my $found = 0; |
| 110 |
my @mysql = ('/usr/local/share/mythtv/mysql.txt',
|
| 111 |
'/usr/share/mythtv/mysql.txt', |
| 112 |
'/etc/mythtv/mysql.txt', |
| 113 |
'/usr/local/etc/mythtv/mysql.txt', |
| 114 |
'mysql.txt' |
| 115 |
); |
| 116 |
foreach my $file (@mysql) {
|
| 117 |
next unless (-e $file); |
| 118 |
$found = 1; |
| 119 |
open(CONF, $file) or die "Unable to open $file: $!\n\n"; |
| 120 |
while (my $line = <CONF>) {
|
| 121 |
# Cleanup |
| 122 |
next if ($line =~ /^\s*#/); |
| 123 |
$line =~ s/^str //; |
| 124 |
chomp($line); |
| 125 |
# Split off the var=val pairs |
| 126 |
my ($var, $val) = split(/\=/, $line, 2); |
| 127 |
next unless ($var && $var =~ /\w/); |
| 128 |
if ($var eq 'DBHostName') {
|
| 129 |
$SQLServer = $val; |
| 130 |
} |
| 131 |
elsif ($var eq 'DBUserName') {
|
| 132 |
$SQLUser = $val; |
| 133 |
} |
| 134 |
elsif ($var eq 'DBName') {
|
| 135 |
$SQLDBName = $val; |
| 136 |
} |
| 137 |
elsif ($var eq 'DBPassword') {
|
| 138 |
$SQLPassword = $val; |
| 139 |
} |
| 140 |
# Hostname override |
| 141 |
elsif ($var eq 'LocalHostName') {
|
| 142 |
$hostname = $val; |
| 143 |
} |
| 144 |
} |
| 145 |
close CONF; |
| 146 |
} |
| 147 |
die "Unable to locate mysql.txt: $!\n\n" unless ($found && $SQLServer); |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
#Perform SQL query |
| 152 |
sub SQLQuery {
|
| 153 |
my ($QUERY) = @_; |
| 154 |
my @data; |
| 155 |
my $ref; |
| 156 |
my $dbh = DBI->connect("DBI:mysql:$SQLDBName:$SQLServer", $SQLUser, $SQLPassword)
|
| 157 |
or die "Couldn't connect to database: " . DBI->errstr; |
| 158 |
my $table_data = $dbh->prepare($QUERY) or die "Couldn't prepare statement: " . $dbh->errstr; |
| 159 |
$table_data->execute or die "Couldn't execute statement: " . $table_data->errstr; |
| 160 |
|
| 161 |
while ( $ref = $table_data->fetchrow_arrayref() ) {
|
| 162 |
push (@data,@{$ref})
|
| 163 |
} |
| 164 |
if ($data[0]) {
|
| 165 |
return @data; |
| 166 |
} else {
|
| 167 |
return 0; |
| 168 |
} |
| 169 |
} |
