System and service monitoring with monit


Webmin has some built in tools for system and service monitoring, however some may not want to use webmin with Ubuntu. Monit is super simple and has a built in web interface to see the status of all the items being monitored. It will restart services that die, and you can add temperature monitoring and notices as well. I will use lm-sensors for the CPU monitoring and smartmontools for HDD monitoring.

Let’s get it installed:

sudo apt-get install monit lm-sensors smartmontools

To configure lm-sensors, you must first run sensors-detect as root.

sudo sensors-detect

It will run through lots of scans to identify your hardware. Once done, running “sensors” will display the data.

There are several sample monit configuration files provided with the package. I want to use the apache, mysql, and openssh default scripts, and then add my custom scripts.
The directory /etc/monit/conf.d is included in the default configuration file, /etc/monit/monitrc. So I put my custom info there.

First to edit the configuration file:

sudo nano /etc/monit/monitrc

The monit wiki has good examples: http://mmonit.com/wiki/Monit/HowTo.

The only changes I made were to the mail notice settings and the http server port and access.
The Monit configuration for Gmail mailserver:

set mailserver smtp.gmail.com port 587 
    username "MYUSER" password "MYPASSWORD"
    using tlsv1

After getting the http access set up, I copied the default monit config files for apache, mysqp, and openssh to the conf.d directory and then made my new file.

cd /etc/monit/conf.d
sudo cp ../monitrc.d/apache2 .
sudo cp ../monitrc.d/mysql .
sudo cp ../monitrc.d/openssh-server .
sudo nano custom

Here is the contents of my custom monitors:

check process squeezelite with pidfile /var/run/squeezelite.pid
    start program = "/etc/init.d/squeezelite start"
    stop program = "/etc/init.d/squeezelite stop"

check process logitechmediaserver with pidfile /var/run/logitechmediaserver.pid
    start program = "/etc/init.d/logitechmediaserver start"
    stop program = "/etc/init.d/logitechmediaserver stop"

check process motion with pidfile /var/run/motion.pid
    start program = "/etc/init.d/motion start"
    stop program = "/etc/init.d/motion stop"

check process stunnel4 with pidfile /var/run/stunnel4.pid
    start program = "/etc/init.d/stunnel4 start"
    stop program = "/etc/init.d/stunnel4 stop"

check process qbittorrent-nox with pidfile /var/run/qbittorrent-nox.pid
    start program = "/etc/init.d/qbittorrent start"
    stop program = "/etc/init.d/qbittorrent stop"

check program CPU with path "/opt/scripts/temp_cpu.sh"
    if status > 55 then alert
    group temperature

check program SSD with path "/opt/scripts/temp_sda.sh"
    every 5 cycles
    if status > 45 then alert
    group temperature

check program HDD-POOL-RED3TB with path "/opt/scripts/temp_sdb.sh"
    every 60 cycles
    if status > 45 then alert
    group temperature

check program HDD-POOL-HIT2TB with path "/opt/scripts/temp_sdc.sh"
    every 60 cycles
    if status > 45 then alert
    group temperature

check program HDD-POOL-GREEN2TB with path "/opt/scripts/temp_sde.sh"
    every 60 cycles
    if status > 45 then alert
    group temperature

check program HDD-WD750GB with path "/opt/scripts/temp_sdd.sh"
    every 60 cycles
    if status > 45 then alert
    group temperature

The CPU monitoring script contents, it may need to be tweaked based on your hardware:

#!/bin/sh
TP=`sensors | egrep CPUTIN | cut -c18-19`
echo $TP # for debug only
exit $TP

The HDD monitoring script contents:

#!/bin/sh
TP=`/usr/sbin/smartctl -a /dev/sda | grep Temp | awk -F " " '{printf "%d",$10}'`
echo $TP # for debug only
exit $TP

Leave a Reply

Your email address will not be published.