Sensor CPU check in bash

This script is checking the CPU Temp sensor; If one reach the threshold then a notification or shutdown (if needed) is done.

A few variables required to be set; then you are done

 

#!/bin/bash


## FEW VARIABLES ########################

NOW=$(date +"%d-%m-%Y-%T")

LOGFILE="/var/log/sensor_temp.log"


## THRESHOLDFOR NOTIFICATIONS ##
## UPON THE SENSORS COMMAND OUTPUT ##
## WARNING=HIGH 
## CRITICAL=CRIT

HIGH="60"
CRIT="100"

## SENDER AND RECIPIENTS

MAILFROM="sender@mydomain.com"
MAILTO="recipient@mydomain.com"

## IF HELO SET TO 0 ##
EHLO="1"

#######################################

## CHECK IF SENSOR IS PRESENT

        hash sensors 2>/dev/null || { echo >&2 "It requires sensor but it's not installed.  Aborting now."; exit 1; }



## CREATE FUNCTION EMAIL #############
email () {

                ## AUTOMATIC HOST TO CONNECT
                DOMAINNAME=$(echo "${MAILTO}" | grep -o '@[[:alnum:][:graph:]]*' | sed -e 's/@//g')


                if hash dig 2>/dev/null;
                then
                MAILSERVER=$(dig +short ${DOMAINNAME} mx | sort -n | awk '{print $2; exit}' | dig +short -f -)
                else
                MAILSERVER=$(nslookup -query=mx $DOMAINNAME | grep ${DOMAINNAME} | head -1)
                fi


        PORT="25"

                #### SEND MAIL via RAW TCP #######
                echo
                echo "Connecting to $MAILSERVER on Port $PORT";
                echo "Please wait ... "
                echo
                exec 3<>/dev/tcp/$MAILSERVER/$PORT

                if [ $? -ne 0 ] ; then
                        echo
                        echo "ERROR: Cannot connect to the Mail Server";
                        echo "Please check the servername and/or the port number"
                        exit
                fi
                if [ "${EHLO}" == '1' ] ; then

                echo -en "EHLO ${DOMAINAME}\r\n"  >&3
                echo -en "MAIL FROM:<${MAILFROM}>\r\n" >&3
                echo -en "RCPT TO:<${MAILTO}>\r\n" >&3
                echo -en "DATA\r\n" >&3
                echo -en "FROM: ${MAILFROM}\r\nTO:${MAILTO}\r\n" >&3
                echo -en "Subject: ${SUBJECT}\r\n\r\n" >&3
                echo -en "${DATA}\r\n" >&3
                echo -en ".\r\n" >&3
                echo -en "QUIT\r\n" >&3
                cat <&3

                else

                echo -en "HELO ${DOMAINNAME}\r\n"  >&3
                echo -en "MAIL FROM:<${MAILFROM}>\r\n" >&3
                echo -en "RCPT TO:<${MAILTO}>\r\n" >&3
                echo -en "DATA\r\n" >&3
                echo -en "FROM: ${MAILFROM}\r\nTO:${MAILTO}\r\n" >&3
                echo -en "Subject: ${SUBJECT}\r\n\r\n" >&3
                echo -en "${DATA}\r\n" >&3
                echo -en ".\r\n" >&3
                echo -en "QUIT\r\n" >&3
                cat <&3

                fi

                echo
                echo
                echo "Check the above output for errors"
                echo
}

## READ CPU TEMP ##

        CORE_0=$(sensors | grep 'Core 0' | awk '{print int($3)}')
        CORE_1=$(sensors | grep 'Core 1' | awk '{print int($3)}')

## ECHO VALUE

        echo "TIME: $NOW"
        echo "###########################" >> ${LOGFILE}
        echo "TIME: $NOW" >> ${LOGFILE}
        echo "CPU Core 0: ${CORE_0} C"
        echo "CPU Core 0: ${CORE_0} C" >> ${LOGFILE}
        echo "CPU Core 1: ${CORE_1} C"
        echo "CPU Core 1: ${CORE_1} C" >> ${LOGFILE}



## CHECK THE RETURN VALUE FROM EACH CORE; IF NEEDED SEND NOTIFICATION

        if [ "${CORE_0}" -gt ${HIGH} ] || [ ${CORE_1} -gt ${HIGH} ]; 
                then
        ## SET VARIABLE FOR EMAIL
                SUBJECT="***WARNING*** FOR HIGH TEMP FROM ${HOSTNAME} NUC"
                DATA="Dear User,\r\n\r\nThe temperature for CPU is HIGH: ${CORE_0}C & ${CORE_1}C\r\nPlease Monitor this Machine closer.\r\n\r\nNB Team"
                email
                echo "THE TEMPERATURE IS HIGH" >> ${LOGFILE}
                echo "NOTIFICATION SENT" >> ${LOGFILE}
                echo "THE TEMPERATURE IS HIGH" 
        fi

        if [ "${CORE_0}" -gt ${CRIT} ] || [ ${CORE_1} -gt ${CRIT} ]; 
                then
        ## SET VARIABLE FOR EMAIL
                SUBJECT="***CRITICAL*** FOR TEMP FROM ${HOSTNAME} NUC"
                DATA="Dear User,\r\n\r\nThe temperature for CPU is HIGH: ${CORE_0}C & ${CORE_1}C\r\nPlease do something on this Machine.\r\n\r\n"
                email
                echo "THE TEMPERATURE IS CRITICAL" >> ${LOGFILE}
                echo "NOTIFICATION SENT" >> ${LOGFILE}
                echo "THE TEMPERATURE IS NOW CRITICAL"
        fi

Leave a Reply

Your email address will not be published. Required fields are marked *