Domoticz: monitoring

Monit is a helpful program that automatically monitors and manages server programs to ensure that they not only stay online consistently, but that the file size, checksum, or permissions are always correct. Additionally Monit comes with a basic web interface through which all of the processes can be set up.

Monit is very easy to use nearly out of the box. By default, it is set up to check that services are running every 2 minutes and stores its log file in “/var/log/monit.log”.

sudo apt-get install monit
 
sudo nano /etc/monit/monitrc
  • set daemon 300 -> Check services at 300 seconds (5 minutes)
  • with start delay 300 -> Start checking the services after 300 seconds (this avoids bootloops if Domoticz has not started yet, give it some time to start fully)

You can setup Monit to inform you about every status update by e-mail.

set mailserver smtp.gmail.com port 587 
username "yourmailaddress@gmail.com" password "MYPASSWORD"
using tlsv1
with timeout 30 seconds
 
# Somewhere at the bottom
# You can put any email address here
set alert email@gmail.com

Monit comes with its own webserver, running on port 2812. To configure the webinterface, find and uncomment the section that begins with set httpd port 2812.

set httpd
    port 2812
    allow username:password
 
sudo service monit reload

Once the web services are set up, you can begin to input the programs that you want monitored and protected into the “/etc/monit/monitrc” configuration file. To simply ensure that programs stay online, you can use the /etc/init.d commands to stop or start a program.

# Domoticz check
check process domoticz with pidfile /var/run/domoticz.pid
start program = "/etc/init.d/domoticz.sh start"
stop  program = "/etc/init.d/domoticz.sh stop"
if failed
  url http://127.0.0.1:8080/json.htm?type=command&param=getversion
      and content = '"status" : "OK"'
  for 2 cycles
  then restart
if 5 restarts within 5 cycles then exec "/sbin/reboot"
 
sudo monit -t
sudo monit start all

Send messages to a Telegram Bot
Instead of triggering email alerts we will trigger a shell script to send a message to a Telegram Bot. Each bot is given a unique authentication token when it is created. The token looks something like 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11. The id (12345678) of your username you will use to send the message to your new bot.

sudo touch /etc/monit/start_domoticz.sh
sudo chmod +x /etc/monit/start_domoticz.sh
sudo nano /etc/monit/start_domoticz.sh
 
#!/bin/sh
/usr/bin/curl -s "https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/sendMessage?chat_id=12345678&text=$MONIT_SERVICE - $MONIT_DESCRIPTION" > /dev/null
exec "/etc/init.d/domoticz.sh start"
 
sudo touch /etc/monit/telegram_bot_notifications.sh
sudo chmod +x /etc/monit/telegram_bot_notifications.sh
sudo nano /etc/monit/telegram_bot_notifications.sh
 
#!/bin/sh
/usr/bin/curl -s "https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/sendMessage?chat_id=12345678&text=$MONIT_SERVICE - $MONIT_DESCRIPTION" > /dev/null
 
// Change monit service
check process domoticz with pidfile /var/run/domoticz.pid
start program = "/etc/monit/start_domoticz.sh"
stop  program = "/etc/init.d/domoticz.sh stop"
if failed
  url http://127.0.0.1:8080/json.htm?type=command&param=getversion
      and content = '"status" : "OK"'
  for 2 cycles
  then exec "/etc/monit/start_domoticz.sh" else if succeeded then exec "/etc/monit/telegram_bot_notifications.sh"
if 5 restarts within 5 cycles then exec "/sbin/reboot"