Skip to content

Instantly share code, notes, and snippets.

@bpostlethwaite
Last active May 19, 2019 06:03
Show Gist options
  • Save bpostlethwaite/11161941 to your computer and use it in GitHub Desktop.
Save bpostlethwaite/11161941 to your computer and use it in GitHub Desktop.
monit

monitoring shit with monit

get monit

sudo apt-get install monit

monit configuration is in ~/.monitrc

You can think of monit as a wrapper around init.d, so you give monit your daemons you want to wrap and you tell monit how to start and stop them, from then on you can use monit to start and stop these services. monit start mydaemon where the name mydaemon is set in your monitrc file (example using celery shown below)

monitrc

set daemon 10

set alert [email protected] {resource}

check process celery with pidfile /var/run/celery.pid
       start program = "/etc/init.d/celeryd start"
       stop program  = "/etc/init.d/celeryd stop"
       if total memory > 1000.0 MB for 5 cycles then alert
       if total memory > 1500.0 MB for 5 cycles then alert
       if total memory > 2000.0 MB for 5 cycles then restart
  1. tell monit to go into Daemon mode and wake up every n seconds, in the conf set to 10 seconds. This is the monit cycle that is used elsewhere in the config. Change the time if the resource deltas in the process being monitored are much lower or higher frequency.
  2. Set an email alert for email address [email protected], (obviously put your own in there), for the alert types 'resource'.
  3. The next chunk is where we monitor our first process, celery. We define the alias celery which is linked to the process found at pidfile /var/run/celery.pid you will need to make sure celery uses this pidfile, or some other pidfile you can link with monit (doesn't matter the location so long as they match), see how to set a pidfile using celery here

Running Monit

First make sure we got the monitrc right by running a quick self test

monit -t

You should get something telling you everything is OK

Then you start monit with a simple

monit

and can start your process, and start monitoring it with

monit start celery

you can do things like

monit status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment