Monit is a free open source utility for managing and monitoring, processes, files, directories and filesystems on a UNIX system. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations.
Why Monit?
Monit is light weight application runs on UNIX systems. You can run it daemonized in the background to watch all the processes you specify in the Monit configuration file. Also, it has a good web Graphical User Interface, that let you watch the tasks you specify for Monit. you can start service(s), stop service(s), restart service(s) via the web interface.
What Monit can do?
Monit can start a process if it does not run, restart a process if it does not respond and stop a process if it uses too much resources. You can use Monit to monitor files, directories and filesystems for changes, such as timestamp changes, checksum changes or size changes. You can also monitor remote hosts; Monit can ping a remote host and can check TCP/IP port connections and server protocols. Monit is controlled via an easy to use control file based on a free-format, token-oriented syntax. Monit logs to syslog or to its own log file and notifies you about error conditions and recovery status via customizable alert.
Example problem
Let’s say you have Apache or Nginx web server that runs on your server. Suddenly, your website is down, because the used memory exceeded the limits, lets say that! The solution is to restart your web server. You have to login your server via ssh and restart the server! This would take time. So what about restarting it via a simple web page? What about make the server restart automatically when some conditions happen? Cool yeah?
Up and running in 15 minutes
Installing on Ubuntu
1 | sudo apt-get install monit |
Configurations
Default configurations are located at /etc/monit/monitrc you can edit it. Or if you find that it includes some other files in the bottom of /etc/monit/monitrc it’s recommended to edit the included files!
In my case all lines of /etc/monit/monitrc are commented out, and in the bottom of the file there’s this snippet:
1 2 3 4 5 6 7 8 9 10 | ############################################################################### ## Includes ############################################################################### ## ## It is possible to include additional configuration parts from other files or ## directories. # include /etc/monit.d/* # # |
So, now we have to make edits in the folder of /etc/monit.d/. By default you’ll find localhost file in /etc/monit.d/ and that seems that this is the only file that be loaded for Monit. Note that maybe you have get sudo permission to edit this file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | set daemon 20 set logfile syslog facility log_daemon set mailserver smtp.gmail.com port 587 # if you want to use Gmail/Google app mail as a sender username "_EMAIL_" password "_PASSWORD_" using tlsv1 with timeout 30 seconds set alert MY_PERSONAL_EMAIL@domain.com # set this to recive alerts and notifications set httpd port 2812 and # Monit will run on port 2812 use address localhost allow localhost allow 192.168.0.0/255.255.255.0 #set your network ip range allow USERNAME:PASSWORD #set your username and password to loin to your Monit |
Now run Monit with the new configs with sudo monit -d 60 -c /etc/monit/monitrc
Try to curl localhost:2812 by curl localhost:2812 and you’ll get this response, Unauthorized response as expected.
1 2 3 4 5 6 7 8 9 10 | <html> <head><title>401 Unauthorized</title></head> <body bgcolor=#FFFFFF> <h2>Unauthorized</h2> You are <b>not</b> authorized to access <i>monit</i>. Either you supplied the wrong credentials (e.g. bad password), or your browser doesn't understand how to supply the credentials required <p><hr><a href='http://mmonit.com/monit/'><font size=-1>monit 5.0.3</font></a> </body> </html> |
Congratulations! Your Monit is running well!
I wanna see what I’ve done!!
If you are running Nginx on your server, and you want to access Monit on some domain, you can get this behavior using a proxy pass.
1 2 3 4 5 6 7 8 | server { listen 80; server_name services.DOMAIN.com; location / { proxy_pass http://127.0.0.1:2812; proxy_set_header Host $host; } } |
Or on Apache
1 2 3 4 5 | <VirtualHost *:80> ServerName services.DOMAIN.com ProxyPass / http://127.0.0.1:2812 ProxyPassReverse / http://127.0.0.1:2812 </VirtualHost> |
Restart you Apache or Nginx server and go to services.DOMAIN.com. You’ll get a prompt to put your username and password. Enter what we already set in /etc/monit.d/localhost. And here we go!
Adding services
You can add services to monitor as much as you want, just add some lines in the bottom of /etc/monit.d/localhost.
Monit service
1 2 3 4 | check process monit with pidfile /var/run/monit.pid start program "/etc/init.d/monit start" stop program "/etc/init.d/monit stop" |
Nginx service
1 2 3 4 5 6 7 8 9 10 | check process nginx with pidfile /opt/nginx/logs/nginx.pid #Pid file for nginx in my case it located in /opt/nginx/logs/ start program = "/etc/init.d/nginx start" stop program = "/etc/init.d/nginx stop" if failed host IP_ADDRESS port 80 protocol HTTP then restart #set your server IP that runs nginx if 5 restarts with 5 cycles then timeout if cpu is greater than 30% for 2 cycles then alert if cpu is greater than 50% for 5 cycles then restart if totalmem is greater than 50.0 MB for 5 cycles then restart if children is greater than 10 then restart |
I will update and add more services later.
Good cases to use Monit
- - Monitoring Web Applications that run as deamons, like: thin, mongrel, passenger standalone, …
- - Monitoring MongoDB database folder’s permissions.
- - Monitoring Node.js applications.
Conclusion
Monit is a great tool to monitor your server. It has lots of feature that I didn’t mention in this blog post. You have to give a try. Also don’t forget to take a look at Monit Official Documentation and there’s a screencast by FOSSCasts.