Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / mythtv / mythtv_programs @ 17f78427

Historique | Voir | Annoter | Télécharger (4,75 ko)

1 bc735e2c Ian Dobson
#!/usr/bin/perl -w
2
#
3
# Munin plugin for MythTV
4 1c9538f5 ian dobson
# This plugin can graph:- EPG programs per channel
5 17f78427 Lars Kruse
#
6
# NOTE: This plugin needs to run as root so add the following to your munin-node config file
7 bc735e2c Ian Dobson
#  [mythtv_status*]
8
#  user=root
9
#
10
# $Log$
11
# Revision 0.1  2008/03/27  idobson
12 1c9538f5 ian dobson
#
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 bc735e2c Ian Dobson
#
17
# Magic markers (optional - used by munin-config and installation scripts):
18
#
19
#%# family=auto
20
#%# capabilities=autoconf
21 1c9538f5 ian dobson
use strict;
22
use warnings;
23 bc735e2c Ian Dobson
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 1c9538f5 ian dobson
my @result="";
33 bc735e2c Ian Dobson
my $result="";
34
my $gata="";
35
my $VideoInput=1;
36
my $Channel="";
37
38 1c9538f5 ian dobson
#Setup SQL access
39
 PrepSQLRead();
40
41 bc735e2c Ian Dobson
#Auto config options
42
 if ($ARGV[0] and $ARGV[0] eq "autoconf" ) {
43 1c9538f5 ian dobson
    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 17f78427 Lars Kruse
 }
52
53 bc735e2c Ian Dobson
#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 1c9538f5 ian dobson
     print "graph_args --base 1000 --upper-limit 12 --lower-limit 0 --rigid\n";
59 212768ed dipohl
     print "graph_category tv\n";
60 bc735e2c Ian Dobson
     print "graph_vlabel EPG days\n";
61 1c9538f5 ian dobson
     @result=SQLQuery("SELECT  `chanid` , `name` FROM `channel` WHERE `visible` = '1' order by `chanid` ");
62 bc735e2c Ian Dobson
     my $Ptr=0;
63
     foreach $gata  (@result) {
64
      if ($Ptr == 0) {
65
       $Channel = $gata;
66 1c9538f5 ian dobson
       print "Channel" . $Channel . "EPG.draw LINE1\n";
67
       print "Channel" . $Channel . "EPG.min -1\n";
68
       print "Channel" . $Channel . "EPG.max 12\n";
69 bc735e2c Ian Dobson
       $Ptr=1;
70
      } else {
71
       print "Channel" . $Channel . "EPG.label EPG days for channel $gata\n";
72
       $Ptr=0;
73
      }
74 17f78427 Lars Kruse
     }
75 bc735e2c Ian Dobson
   exit 0;
76
 }
77
78
#Actually dump data to Munin
79 17f78427 Lars Kruse
   @result=SQLQuery("SELECT o.chanid, (UNIX_TIMESTAMP(MAX(c.endtime))
80
                     - UNIX_TIMESTAMP(NOW()))/86400
81
                     FROM  channel o, program c
82 1c9538f5 ian dobson
                     WHERE o.chanid = c.chanid
83 17f78427 Lars Kruse
                     AND o.visible = '1'
84 1c9538f5 ian dobson
                     GROUP BY o.chanid");
85 bc735e2c Ian Dobson
     my $Ptr=0;
86
     foreach $gata (@result) {
87
      if ($Ptr == 0) {
88
       $Channel = $gata;
89
       $Ptr=1;
90
      } else {
91 1c9538f5 ian dobson
       if ( $gata > 12 ) {
92 17f78427 Lars Kruse
         print "Channel" . $Channel . "EPG.value 12.0\n";
93
       } else {
94 1c9538f5 ian dobson
         print "Channel" . $Channel . "EPG.value $gata\n";
95 17f78427 Lars Kruse
       }
96 bc735e2c Ian Dobson
       $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 17f78427 Lars Kruse
108 bc735e2c Ian Dobson
# 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 1c9538f5 ian dobson
   my $ref;
156 17f78427 Lars Kruse
   my $dbh = DBI->connect("DBI:mysql:$SQLDBName:$SQLServer", $SQLUser, $SQLPassword)
157 bc735e2c Ian Dobson
         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 17f78427 Lars Kruse
   }
169 bc735e2c Ian Dobson
}