#!/bin/bash
#
# example sensors output:
# ------------------------------------------------------------
#nct6779-isa-0290
#Vcore:                 888.00 mV (min =  +0.00 V, max =  +1.74 V)
#in1:                     1.03 V  (min =  +0.00 V, max =  +0.00 V)  ALARM
#AVCC:                    3.39 V  (min =  +2.98 V, max =  +3.63 V)
#+3.3V:                   3.38 V  (min =  +2.98 V, max =  +3.63 V)
#fan1:                   752 RPM  (min =    0 RPM)
#fan2:                   416 RPM  (min =    0 RPM)
#CPUTIN:                 +31.0°C  (high = +80.0°C, hyst = +75.0°C)  sensor = thermistor
#AUXTIN0:                +97.0°C    sensor = thermistor
#AUXTIN1:               +101.0°C    sensor = thermistor
#PECI Agent 0:           +28.5°C
#PCH_CPU_TEMP:            +0.0°C
#intrusion0:            ALARM
#beep_enable:           disabled
#
#acpitz-acpi-0
#temp1:        +27.8°C  (crit = +106.0°C)
#temp2:        +29.8°C  (crit = +106.0°C)
#
#coretemp-isa-0000
#Package id 0:  +36.0°C  (high = +85.0°C, crit = +105.0°C)
#Core 0:        +36.0°C  (high = +85.0°C, crit = +105.0°C)
#Core 1:        +36.0°C  (high = +85.0°C, crit = +105.0°C)
# ------------------------------------------------------------


# If CHIPS is not empty, we ONLY read values for these chips
# example:
#CHIPS=("i5k_amb-isa-0000" "coretemp-isa-0000" "coretemp-isa-0001")
CHIPS=()

# You can put this into the configfile:
MK_CONFDIR=${MK_CONFDIR:-/etc/check_mk}
LMSENSORSCONFIG="$MK_CONFDIR/lmsensors2.cfg"
# shellcheck disable=SC1090
[ -r "$LMSENSORSCONFIG" ] && . "$LMSENSORSCONFIG" 2>/dev/null

if [ -x /usr/bin/sensors ]; then
    echo '<<<lmsensors2>>>'
    if [ "${#CHIPS[@]}" -eq 0 ]; then
        # we read all we can get.
        # we start with empty CHIPNAME
        CHIPNAME=""
        export CHIPNAME
        while read -r LINE; do
            # we get CHIPNAME at runtime - because of that we can't put
            # the perl-part out of the loop (like below)
            OUT=$(echo "${LINE}"  | perl -pane 'if (m/^\s*([^:]+)\s*:\s*(.+?)?\s*$/){$k=$1;$v=$2;$chip=$ENV{CHIPNAME};$k=~s/\s/_/g;$k=~s/\-/_/g;$k=~s/[^a-z0-9_]//ig;$v=~s/[\(\),=]//g;if($v=~/[+][0-9\.]+.{0,2}([CF]).*/i && $chip!~/temp/ && $k!~/temp/){$chip="temp_".$chip};$v=~s/°/ /g;$_=$chip."_".$k." ".$v.$/;}')
            if [[ ! "$OUT" =~ " " ]]; then
                CHIPNAME=${OUT//-/_}
                export CHIPNAME
            else
                if ! [[ "$OUT" =~ ^"(" ]]; then
                    echo "$OUT"
                fi
            fi
        done < <(/usr/bin/sensors -A | sed -e ':a;N;$!ba;s/\n  */ /g')
        # we use seed to remove unwanted newlines like this:
        # temp1:        +56.5°C  (high = +85.0°C, hyst = +80.0°C)
        #                        (crit = +100.0°C, hyst = +95.0°C)  sensor = thermal diode
    else
        # we read only the given chips from CHIPS
        for CHIP in "${CHIPS[@]}"
        do
            CHIPNAME=${CHIP//-/_}
            export CHIPNAME
            while read -r LINE; do
                if [[ "$LINE" =~ " " ]] && ! [[ "$LINE" =~ ^"(" ]]; then
                    echo "$LINE"
                fi
            done < <(/usr/bin/sensors -A "${CHIP}" | sed -e ':a;N;$!ba;s/\n  */ /g' | perl -pane 'if (m/^\s*([^:]+)\s*:\s*(.+?)?\s*$/){$k=$1;$v=$2;$chip=$ENV{CHIPNAME};$k=~s/\s/_/g;$k=~s/\-/_/g;$k=~s/[^a-z0-9_]//ig;$v=~s/[\(\),=]//g;if($v=~/[+][0-9\.]+.{0,2}([CF]).*/i && $chip!~/temp/ && $k!~/temp/){$chip="temp_".$chip};$v=~s/°/ /g;$_=$chip."_".$k." ".$v.$/;}')
        done
    fi
fi
