Difference between revisions of "Myth sensors.pl"

From MythTV Official Wiki
Jump to: navigation, search
(Move myth_sensors.sh from contrib)
 
m (add version info, admit to authoring hack)
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
Retrieves sensors output and formats it for inclusion in the MythTV backend status page
+
{{Wrongtitle|myth_sensors.pl}}
 +
{{Script info
 +
|author=Michael T. Dean
 +
|short=Adds temp/fan info to the backend status page
 +
|long=Retrieves sensors output (thermal/fan information) and formats it for inclusion in the MythTV backend status page,
 +
|category=Miscellaneous Status Information Scripts
 +
|file=myth_sensors.pl
 +
|S21=yes|S22=yes|S23=yes|S24=yes}}
  
{{Code box|myth_sensors.sh|
+
'''myth_sensors.pl''' retrieves sensors output and formats it for inclusion in the MythTV backend status page
 +
 
 +
 
 +
{{Code box|myth_sensors.pl|
 
<pre>
 
<pre>
#!/bin/bash
+
#!/usr/bin/perl -w
 +
# myth_sensors.pl
 
#
 
#
# myth_sensors.sh
 
 
# Retrieves sensors output and formats it for inclusion in the MythTV backend
 
# Retrieves sensors output and formats it for inclusion in the MythTV backend
 
# status page.
 
# 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.
  
# The following line outputs information to include the CPU temperature
 
# information in the backend status page.  Though the sensors output includes
 
# the °C unit marker, it is stripped out of the data before being placed in the
 
# output page, so the display value includes a proper HTML entity.
 
  
#sensors | awk '/CPU Temp/ { printf "Current CPU Temperature: %s &#8451;.[]:[]temperature-CPU[]:[]%s\n", $3, $3 };'
+
# 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';
  
# The following lines output CPU and Motherboard temperature and CPU and Case
+
# Specify the data to retrieve and the output information in the format:
# fan speed information.  Modify the patterns (i.e. /CPU Temp/) to match the
+
#
# sensors output lines containing the data you want to grab (i.e. /Core0
+
# [ "<sensors label>", "<display output>", "<XML output name>" ]
# Temp/)Continue adding lines to the awk program as desired to include
+
#
# additional information.
+
# 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 endThe <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 &amp;#8451;", "temperature-CPU" ],
 +
                  [ "CPU Fan", "Current CPU Fan Speed:", "fan-CPU" ],
 +
                  [ "M/B Temp", "Current Motherboard Temperature: %v &amp;#8451;", "temperature-MB" ],
 +
                  [ "Case Fan", "Current Case Fan Speed:", "fan-case" ],
 +
    );
  
sensors | awk '
+
# Editing the following code should not be necessary.
/CPU Temp/ {
+
    my $line = 0;
  printf "Current CPU Temperature: %s &#8451;.[]:[]temperature-CPU[]:[]%s\n", \
+
    my %data = ();
        $3, $3
+
     my ($label, $display, $value, $name);
};
 
/M\/B Temp/ {
 
  printf \
 
  "Current Motherboard Temperature: %s &#8451;.[]:[]temperature-MB[]:[]%s\n", \
 
     $3, $3
 
};
 
/CPU Fan/ {
 
  printf "Current CPU Fan Speed: %s.[]:[]fan-CPU[]:[]%s\n", $3, $3
 
};
 
/Case Fan/ {
 
  printf "Current Case Fan Speed: %s.[]:[]fan-case[]:[]%s\n", $3, $3
 
};
 
' | sort
 
  
# With appropriate authentication in place (i.e. using SSH Certificate
+
    my @sensors_data = `$sensors_command_line`;
# Authentication), you may also retrieve sensors data from remote system (i.e.
 
# slave backends, remote frontends, etc.).  Do so with commands of the format
 
# below (provide an appropriate hostname in place of "slave" (3 places) and
 
# replace the awk script with one providing the desired information as shown
 
# above):
 
  
#ssh -q slave sensors | awk '/CPU Temp/ { printf "Current CPU Temperature (slave): %s &#8451;.[]:[]temperature-CPU-slave[]:[]%s\n", $3, $3 };'
+
    while ($sensors_data[$line])
 
+
    {
# Alternatively, this information could be retrieved from an appropriate
+
        $sensors_data[$line] =~ /^(.+):\s+(?:\+|-)?(\d+\.?\d*)/;
# monitoring program (monit, mrtg) or by using SNMP, depending on the system
+
        if ($1 && $2)
# and network configuration.
+
        {
 +
            $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");
 +
        }
 +
    }
 
</pre>
 
</pre>
 
}}
 
}}
 +
 +
== 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 <code>remote</code> and either specify a full path for <code>myth_sensors.pl</code> 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.
 +
  
 
[[Category:Miscellaneous_Status_Information_Scripts]]
 
[[Category:Miscellaneous_Status_Information_Scripts]]

Latest revision as of 02:02, 12 November 2010

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


Author Michael T. Dean
Description Retrieves sensors output (thermal/fan information) and formats it for inclusion in the MythTV backend status page,
Supports Version21.png  Version22.png  Version23.png  Version24.png  


myth_sensors.pl 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.