Home > Back-end >  Raspberry Pi temperature sensor email alerts
Raspberry Pi temperature sensor email alerts

Time:02-01

I want to monitor the temperature in the server room. I connected the temperature sensor to the Raspberry Pi and everything works. I found a script that allows to send an alarm via email. Script below:

    #!/bin/bash

threshold=80
alert_file=/tmp/temp_alert_status
alert_status=`cat $alert_file`
current_temp=`/usr/local/bin/get_temp.py | head -1 | awk -F. '{print $1}'`
engineers="[email protected], [email protected], [email protected]"
subj_alert="HQ Server Room: Current temperature [$current_temp] is greater than threshold [$threshold].  Investigate immediately!"
subj_norm="HQ Server Room: Current temperature [$current_temp] is now lower than threshold [$threshold].  All Clear."
msg_file=/tmp/email_message.txt

#echo "Current Temp [$current_temp]"
#echo "Alert status [$alert_status]"
#echo "Threshold [$threshold]"

function send_mail(){
        ##
        ## Clear out the message file first
        ##
        > $msg_file

        ##
        ## Create the email message that we're going to pass to ssmtp.  $1 is the Subject line.
        ##
        echo "To: ${engineers}" >> $msg_file
        echo "From: [email protected]" >> $msg_file
        echo "Subject: $1" >> $msg_file
        echo " " >> $msg_file

        ##
        ## Send the email
        ##
        /usr/sbin/ssmtp $engineers < $msg_file
}

if [ $current_temp -gt $threshold ]
then
        if [ $alert_status -eq 0 ]
        then
                ##
                ## The current temperature is above the threshold for the first time.  Set the alert flag and
                ## notify via email.
                ##
                echo "Current temperature [$current_temp] is greater than threshold [$threshold].  Setting alert state."
                echo 1 > $alert_file
                send_mail "$subj_alert"
                exit
        else
                ##
                ## The current temperature is still above the threshold. Continue to monitor but do not
                ## notify via email.
                ##
                echo "Current temperature [$current_temp] is still greater than threshold [$threshold]."
                exit
        fi
else
        if [ $alert_status -eq 1 ]
        then
                ##
                ## The current temperature is below the threshold and the alert state was set.  Revert to non
                ## alert state and notify via email.
                ##
                echo "Temperature [$current_temp] is now below the threshold of [$threshold].  Resetting alert state."
                echo 0 > $alert_file
                send_mail "$subj_norm"
                exit
        fi
        ##
        ## If we got here, the temperature is below the threshold and the alert status was not set so don't do anything.
        ##
fi

The script works, but the alert is only sent in the subject of the message, and I would like to change that.

How does the script know that "$1" is the message subject field? Where is it defined? So how to define the message content field?

CodePudding user response:

afaiq

$1 is the first paramter given to the function

        echo "To: ${engineers}" >> $msg_file
        echo "From: [email protected]" >> $msg_file
        echo "Subject: $1" >> $msg_file # So here it takes the 1st parameter as subject
        echo "$2" >> $msg_file # here $2 is passed as message body. 

So now the second parameter that is passed contains the message body. You might want to try that.

CodePudding user response:

@b3z it doesn't work. I modified the script as follows:

    threshold=80
alert_file=/tmp/temp_alert_status
alert_status=`cat $alert_file`
current_temp=`/usr/local/bin/get_temp.py | head -1 | awk -F. '{print $1}'`
engineers="[email protected], [email protected], [email protected]"
subj_alert="HQ Server Room: Current temperature [$current_temp] is greater than threshold [$threshold].  Investigate immediately!"
body_alert="Test_1"
subj_norm="HQ Server Room: Current temperature [$current_temp] is now lower than threshold [$threshold].  All Clear."
body_norm="Test_2"
msg_file=/tmp/email_message.txt

#echo "Current Temp [$current_temp]"
#echo "Alert status [$alert_status]"
#echo "Threshold [$threshold]"

function send_mail(){
        ##
        ## Clear out the message file first
        ##
        > $msg_file

        ##
        ## Create the email message that we're going to pass to ssmtp.  $1 is the Subject line.
        ##
        echo "To: ${engineers}" >> $msg_file
        echo "From: [email protected]" >> $msg_file
        echo "Subject: $1" >> $msg_file
        echo "$2" >> $msg_file 

And I get an emails without any content - only subject...

  •  Tags:  
  • Related