Page 1 of 1

Monitor Raspberry Pi CPU Temperature in Monit / MMonit

Posted: Wednesday 11 December 2019 15:36
by ben53252642
This is a script I wrote for monitoring CPU temperature in Monit / MMonit.

Features:
1) Display current temperature in MMonit (checked at each cycle defined in Monit).
2) Alert if set CPU temperature threshold is exceeded.

Requires: apt-get install bc

/etc/monit/customscripts/picputemp.sh

Code: Select all

#!/bin/bash

# Configuration
templimit="80" # CPU temperature must be less than this value

# Get output
output=$(vcgencmd measure_temp | egrep -o '[0-9]*\.[0-9]*')

# If CPU Temperature is below templimit
if (( $(echo "$output < $templimit" |bc) -l )); then
  echo "CPU Temperature: $output°C"
  exit 0
fi

# If CPU temperature is at or above temp limit
if (( $(echo "$output >= $templimit" |bc) -l )); then
  echo "CPU temperature is above limit threshold of $templimit°C, current temp is: $output°C" >&2
  exit 1
fi
Definition to put inside /etc/monit/monitrc

Code: Select all

# Check Raspberry Pi CPU Temperature
check program cpu_temperature with path "/etc/monit/customscripts/picputemp.sh"
 with timeout 5 seconds
 if status != 0 then alert

cpu1.JPG
cpu1.JPG (218.04 KiB) Viewed 3176 times

I had to temporarily set the threshold to 20 degrees to trigger the alert (so I could show how the MMonit log looks when its triggered):

cpu2.jpg
cpu2.jpg (160.92 KiB) Viewed 3173 times

Re: Monitor Raspberry Pi CPU Temperature in Monit / MMonit

Posted: Wednesday 11 December 2019 16:37
by FireWizard
Hi,

Nice script

The script below I use for a long time to monitor the temperature of a Raspberry Pi 3+, which has no Domoticz installed, and send it to Domoticz.

You have to set the IP address of your Domoticz server and the IDX of the virtual Temperature Sensor, you have to create.
(The script has not been made by myself. I found it somewhere, so the credits should go to the original creator)

Code: Select all

#!/usr/bin/python
import subprocess
import time
import os
import urllib2

serverIP = "192.168.10.50:8080"
deviceId = 282

currentTemp = subprocess.check_output(["/usr/bin/vcgencmd","measure_temp"])
currentTemp = float(currentTemp.split("=")[1][:-3])
urllib2.urlopen("http://" + serverIP + "/json.htm?type=command&param=udevice&idx=" + str(deviceId) + "&nvalue=0&svalue=" + str(currentTemp))
You can run it by crontab, e.g every 5 minutes: */5 * * * * python /home/pi/temp2domoticz.py

Regards

Re: Monitor Raspberry Pi CPU Temperature in Monit / MMonit

Posted: Saturday 11 April 2020 9:20
by mattx38
@FireWizard

Hi !
This script is excatly what I was looking for.
So I follow your instructions, but I meet some issues.
When I execute the Python script on the Raspberry I want to monitor, I got these errors :

Code: Select all

pi@PhotoBooth:~ $ sudo bash Metrics2Domoticz.py
import-im6.q16: impossible de se connecter au serveur X `' @ error/import.c/ImportImageCommand/360.
import-im6.q16: impossible de se connecter au serveur X `' @ error/import.c/ImportImageCommand/360.
import-im6.q16: impossible de se connecter au serveur X `' @ error/import.c/ImportImageCommand/360.
import-im6.q16: impossible de se connecter au serveur X `' @ error/import.c/ImportImageCommand/360.
Metrics2Domoticz.py: ligne 6: $'\r' : commande introuvable
Metrics2Domoticz.py: ligne 7: serverIP : commande introuvable
Metrics2Domoticz.py: ligne 8: deviceId : commande introuvable
Metrics2Domoticz.py: ligne 9: $'\r' : commande introuvable
Metrics2Domoticz.py: ligne 10: erreur de syntaxe près du symbole inattendu « ( »
'etrics2Domoticz.py: ligne 10: `currentTemp = subprocess.check_output(["/usr/bin/vcgencmd","measure_temp"])
Could you help me fixing it ?

EDIT : Even thoug, the temperature is well sent to Domoticz and I can see it. Thank you

Re: Monitor Raspberry Pi CPU Temperature in Monit / MMonit

Posted: Saturday 11 April 2020 15:36
by FireWizard
Hello,

@mattx38

The script provided is a Python script.
You try to run it as a bash script. (See your command: sudo bash Metrics2Domoticz.py)

As the bash shell interpreter does not understand the python commands, you will get an error for almost every line in the python script.
You should not run it as a bash script!

Take the following steps:

1. Go to the directory, where you have created the file Metrics2Domoticz.py (e.g. cd /home/pi / your_directory)
2. Check the permissions of your file (ls -l Metrics2Domoticz.py)
You should see some thing like: -rwx------ 1 pi pi 391 Jun 10 2019 Metrics2Domoticz.py
Check the user. In this example it is the user pi. And check that you see a x. In this example you see rwx. That is ok.
If you don't see the eXecutable bit set, give the following command as pi user: chmod +x Metrics2Domoticz.py.
3. Run the script from the directory, where you have installed the script with the command: ./Metrics2Domoticz.py
You should not see any error message and the value should be send to Domoticz.

If you plan to run the script periodically by a cronjob, take into account that the cron daemon run in its own environment and so is not aware about any variables set, such as e.g. $PATH. You have to use the full path in you crontab.

It will look like: */5 * * * * python /home/pi/Metrics2Domoticz.py
This cronjob will run every 5 minutes and in this example your script is installed in the pi home directory (/home/pi).

Regards

Re: Monitor Raspberry Pi CPU Temperature in Monit / MMonit

Posted: Saturday 23 May 2020 14:32
by thomasbaetge
just out of curiosity, why do you not just use the PI-Monitor plugin in Domoticz?

Re: Monitor Raspberry Pi CPU Temperature in Monit / MMonit

Posted: Sunday 24 May 2020 14:51
by FireWizard
Hi,

@ thomasbaetge

You wrote:
just out of curiosity, why do you not just use the PI-Monitor plugin in Domoticz?
I used to use this small script on Raspberry Pi's, which hasn't Domoticz installed.

I have some other Pi's in use, that do other things then Domoticz.
With this small script I monitor the temperature.

In the mean time I have replaced this script with another one, that sends also CPU_Usage, Disk_Usage, Memory_Usage and Swap_Usage.
(via MQTT and Node Red).

More or less the same what the motherboard sensors are doing in Domoticz.

Regards

Re: Monitor Raspberry Pi CPU Temperature in Monit / MMonit

Posted: Monday 25 May 2020 10:41
by thomasbaetge
Cool, I didn't consider more than one device.
Since I have more than one RPIs, I will adopt your solution :)