From 305d6b35ccfd9feb28ac1dfa4f9ac9b2e30fd31b Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Fri, 28 Aug 2020 12:25:02 +0200 Subject: [PATCH] system: popular temperature topic changes * Use priv-sep script to fetch values via configd * Use grep on variable names only to avoid false positives * Switch the RRD script read to unify temperature reads * Use "=" in the script output to match sysctl input --- plist | 1 + src/etc/inc/rrd.inc | 2 +- src/opnsense/scripts/system/temperature.sh | 35 +++++++++++++++++++ .../conf/actions.d/actions_system.conf | 6 ++++ src/www/widgets/api/plugins/temperature.inc | 9 ++--- 5 files changed, 48 insertions(+), 5 deletions(-) create mode 100755 src/opnsense/scripts/system/temperature.sh diff --git a/plist b/plist index 05a01bcfa..75ae9f19b 100644 --- a/plist +++ b/plist @@ -769,6 +769,7 @@ /usr/local/opnsense/scripts/system/list_interrupts.py /usr/local/opnsense/scripts/system/rfc5246_cipher_suites.csv /usr/local/opnsense/scripts/system/ssl_ciphers.py +/usr/local/opnsense/scripts/system/temperature.sh /usr/local/opnsense/scripts/systemhealth/activity.py /usr/local/opnsense/scripts/systemhealth/clearlog /usr/local/opnsense/scripts/systemhealth/definitions/gateway-quality.xml diff --git a/src/etc/inc/rrd.inc b/src/etc/inc/rrd.inc index cf21a8230..3b2d882cc 100644 --- a/src/etc/inc/rrd.inc +++ b/src/etc/inc/rrd.inc @@ -518,7 +518,7 @@ function rrd_configure($verbose = false) } /* the CPU Temperature gathering function */ - $rrdupdatesh .= "CPUTEMP=`$sysctl -n dev.cpu.0.temperature | sed 's/C//g'`\n"; + $rrdupdatesh .= "CPUTEMP=\$(/usr/local/opnsense/scripts/system/temperature.sh rrd)\n"; $rrdupdatesh .= "$rrdtool update $rrddbpath$ifname$cputemp N:\${CPUTEMP}\n"; /* end CPU Temp gathering */ diff --git a/src/opnsense/scripts/system/temperature.sh b/src/opnsense/scripts/system/temperature.sh new file mode 100755 index 000000000..6daad78ac --- /dev/null +++ b/src/opnsense/scripts/system/temperature.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +# Copyright (C) 2020 Franco Fichtner +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, +# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY +# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, +# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +CMD=${1} + +if [ "${CMD}" = 'rrd' ]; then + sysctl -n dev.cpu.0.temperature | sed 's/C//g' +else + # The grep is opportunistic, but at least we only grep the + # variable names and not their content at the same time. + sysctl -e $(sysctl -aN | grep temperature) | sort +fi diff --git a/src/opnsense/service/conf/actions.d/actions_system.conf b/src/opnsense/service/conf/actions.d/actions_system.conf index d059d6396..f0277b68e 100644 --- a/src/opnsense/service/conf/actions.d/actions_system.conf +++ b/src/opnsense/service/conf/actions.d/actions_system.conf @@ -54,3 +54,9 @@ command:/usr/local/etc/rc.halt parameters: type:script message:Halt system + +[temp] +command:/usr/local/opnsense/scripts/system/temperature.sh +parameters:%s +type:script_output +message:Reading system temperature values diff --git a/src/www/widgets/api/plugins/temperature.inc b/src/www/widgets/api/plugins/temperature.inc index d3d7a8750..af32ce51e 100644 --- a/src/www/widgets/api/plugins/temperature.inc +++ b/src/www/widgets/api/plugins/temperature.inc @@ -32,9 +32,9 @@ function temperature_api() { $result = array(); - exec("/sbin/sysctl -a | grep temperature", $sysctlOutput); - foreach ($sysctlOutput as $sysctl) { - $parts = explode(':', $sysctl); + + foreach (explode("\n", configd_run('system temp')) as $sysctl) { + $parts = explode('=', $sysctl); if (count($parts) >= 2) { $tempItem = array(); $tempItem['device'] = $parts[0]; @@ -45,8 +45,9 @@ function temperature_api() $result[] = $tempItem; } } + usort($result, function ($item1, $item2) { - return strcmp(strtolower($item1['device']), strtolower($item2['device'])); + return strcmp(strtolower($item1['device']), strtolower($item2['device'])); }); return $result;