Difference between revisions of "Myth sensors.pl"

From MythTV Official Wiki
Jump to: navigation, search
(OK, since I made a typo on the page name, it's easier to actually do the TODO to make this a real Perl script than to rename the page.)
m (HTML entities...)
Line 32: Line 32:
 
# machine parsing easier.
 
# machine parsing easier.
 
     my @output = (
 
     my @output = (
                   [ "CPU Temp", "Current CPU Temperature: %v ℃", "temperature-CPU" ],
+
                   [ "CPU Temp", "Current CPU Temperature: %v ℃", "temperature-CPU" ],
 
                   [ "CPU Fan", "Current CPU Fan Speed:", "fan-CPU" ],
 
                   [ "CPU Fan", "Current CPU Fan Speed:", "fan-CPU" ],
                   [ "M/B Temp", "Current Motherboard Temperature: %v ℃", "temperature-MB" ],
+
                   [ "M/B Temp", "Current Motherboard Temperature: %v ℃", "temperature-MB" ],
 
                   [ "Case Fan", "Current Case Fan Speed:", "fan-case" ],
 
                   [ "Case Fan", "Current Case Fan Speed:", "fan-case" ],
 
     );
 
     );

Revision as of 05:03, 11 April 2010

Important.png Note: The correct title of this article is myth_sensors.pl. It appears incorrectly here due to technical restrictions.


Retrieves sensors output and formats it for inclusion in the MythTV backend status page


Script.png myth_sensors.pl

#!/usr/bin/perl -w
# myth_sensors.pl
#
# Retrieves sensors output and formats it for inclusion in the MythTV backend
# status page.
#
# You may specify the sensors_command_line as well as the data to retrieve and
# the output information as described in the comments, below.


# Change the command line required to execute the sensors program, below.
# The default value should work as long as sensors is in the PATH for the user
# running mythbackend
    my $sensors_command_line = 'sensors';

# Specify the data to retrieve and the output information in the format:
#
# [ "<sensors label>", "<display output>", "<XML output name>" ]
#
# where <sensors label> is the label used by the sensors program for the
# value you wish to include and <display output> may contain "%v" at the
# location where the value should be placed.  If <display output> does not
# contain "%v", the value will be appended to the end.  The <XML output name>
# is an optional value for the "name" attribute, which can be used to make
# machine parsing easier.
    my @output = (
                  [ "CPU Temp", "Current CPU Temperature: %v &#8451;", "temperature-CPU" ],
                  [ "CPU Fan", "Current CPU Fan Speed:", "fan-CPU" ],
                  [ "M/B Temp", "Current Motherboard Temperature: %v &#8451;", "temperature-MB" ],
                  [ "Case Fan", "Current Case Fan Speed:", "fan-case" ],
    );

# Editing the following code should not be necessary.
    my $line = 0;
    my %data = ();
    my ($label, $display, $value, $name);

    my @sensors_data = `$sensors_command_line`;

    while ($sensors_data[$line])
    {
        $sensors_data[$line] =~ /^(.+):\s+(?:\+|-)?(\d+\.?\d*)/;
        if ($1 && $2)
        {
            $data{ $1 } = $2;
        }
        $line++;
    }
    for $i (0 .. $#output) {
        $label = $output[$i][0];
        $value = $data{$label}; 
        if ($value)
        {
            $display = $output[$i][1];
            $name = $output[$i][2];
            $display = "$display %v" unless ($display =~ /%v/);
            $display =~ s/%v/$value/;
            print("${display}");
            print("[]:[]${name}") if ($name);
            print("[]:[]${value}") if ($name && $value);
            print("\n");
        }
    }

Getting sensors Data from Remote Systems

With appropriate authentication in place (i.e. using SSH Certificate Authentication, Kerberos Authentication, ...), you may also retrieve sensors data from remote systems (i.e. remote backends, remote frontends, etc.). Do so with commands of the format below (provide an appropriate host name in place of remote and either specify a full path for myth_sensors.pl or ensure the script is located in the user's PATH):

ssh -q remote myth_sensors.pl

Alternatively, this information could be retrieved from an appropriate monitoring program (monit, mrtg) or by using SNMP, depending on the system and network configuration.