Hawkes Blog
Windows reboot | Linux be root
Hawkes Blog
Monitor your RaspberryPi with Monit

Short How-To: monitor your RaspberryPi with the lightweight tool Monit

Short How-To: Monitor your RaspberryPi with Monit

prerequisite:
  • raspberry pi running raspbian

What is Monit

Monit is a small and lightweight managing and monitoring system for *nix hosts.

It´s possible to monitor multiple local daemons, directories, ... and react with multiple actions if something specified happened.

For example

  • restart nginx/apache + dependencies if port 80 isn´t available
  • check file checksum
  • execute self-made scripts and do something if the returncode is !=0
  • .... and so on

I would recommend to take a look in the great documentation.


1. install Monit

apt-get update
apt-get install monit

2. basic configuration

edit /etc/default/monit

#START=no
START=yes

edit /etc/monit/monitrc

# check services at 2-minute intervals
set daemon 120
# the monit logfile
set logfile /var/log/monit.log
set idfile /var/lib/monit/id
set statefile /var/lib/monit/state
# mailserver used to send the alert mails
set mailserver <<your mailserver>>,
    # set the smtp helo hostname
    using HOSTNAME <<your helo name>>
# activate the eventqueue to store events/alerts if the mailserver is
# currently unavailable
set eventqueue
    # set the base directory where events will be stored
    basedir /var/lib/monit/events
    # optionally limit the queue size
    slots 100
# set the alert recipients
# reminder ->
# http://mmonit.com/monit/documentation/monit.html#setting_an_error_reminder
# please set email address to recive the alert mails
set alert <<emailaddress>> with reminder on 10 cycles
# start the build-in webserver and listen on all ips port 2812
# allow access to admin with password monit
set httpd port 2812 and
    use address 0.0.0.0
    allow admin:monit
# include host/daemons configs
include /etc/monit/conf.d/*

3. configuration

generic Host setup

  • default: /etc/monit/conf.d/localhost
  • basic system checks (load, disk/memory usage ...)
  • include configs for specifc checks

should be self-explaining if not -> documentation :)

# check the local system <<your hostname>>
check system hp.home
    if loadavg (1min) > 5 then alert
    if loadavg (5min) > 3 then alert
    if memory usage > 75% then alert
    if swap usage > 25% then alert
    if cpu usage (user) > 70% then alert
    if cpu usage (system) > 30% then alert
    if cpu usage (wait) > 30% then alert

# local fs system check
check filesystem rootfs with path /
    if space > 90% then alert
    if inodes > 90% then alert

# include config files for specific checks
include /etc/monit/monitrc.d/dnsmasq

specific checks

just a small example how to create a specific check for dnsmasq /etc/monit/monitrc.d/dnsmasq

should be self-explaining too if not -> documentation :)

check process dnsmasq with pidfile /var/run/dnsmasq/dnsmasq.pid
  start program = "/etc/init.d/dnsmasq start" with timeout 60 seconds
  stop program = "/etc/init.d/dnsmasq stop"
  if failed
    host 127.0.0.1
    port 53 use type udp
    protocol dns
    with timeout 60 seconds
  then restart
  if 3 restarts within 5 cycles then timeout
  depends on dnsmasq-bin
  depends on dnsmasq-hosts


check file dnsmasq-bin with path /usr/sbin/dnsmasq
   if failed checksum then unmonitor
   if failed permission 755 then unmonitor
   if failed uid root then unmonitor
   if failed gid root then unmonitor

check file dnsmasq-hosts with path /etc/hosts
   if changed timestamp then restart

4. first start

  • validate the config first
/etc/init.d/monit syntax
  • if no error occurred
/etc/init.d/monit start
  • check the monit logfile /var/log/monit.log
[CEST Apr 20 13:29:50] info     : Starting monit daemon with http interface at [0.0.0.0:2812]
[CEST Apr 20 13:29:50] info     : Starting monit HTTP server at [0.0.0.0:2812]
[CEST Apr 20 13:29:50] info     : monit HTTP server started
[CEST Apr 20 13:29:50] info     : 'raspberryone' Monit started

open http://<<yourraspberryip>>:2812 in your webbrowser and login with your credentials (admin:monit).


5. if something fails

  • monit logfile
[CEST Apr 20 14:34:37] error    : 'dnsmasq' process is not running
[CEST Apr 20 14:34:37] info     : 'dnsmasq' trying to restart
[CEST Apr 20 14:34:37] info     : 'dnsmasq' start: /etc/init.d/dnsmasq
  • the alert mail
Does not exist Service dnsmasq

    Date:        Sun, 20 Apr 2014 14:34:37
    Action:      restart
    Host:        raspberryone
    Description: process is not running

Your faithful employee,
    Monit

MfG Hawkes