How Can Monit Help You Manage Your Linux Server?
Monit is a very popular and lightweight Linux server monitoring tool used by the system administrators all over. Besides monitoring, it can be used to restart programs that are misbehaving, as well as check files and directories for changes, and monitor network connections to remote servers. Monit also allows user scripts, so you can monitor whatever you ask it to (nicely)! Today we'll demonstrate a simple, but practical, script to have Monit check a MySQL process running and restart it automatically if it’s stopped or not responding! Monit can usually be found in CentOS' official repository, so the easiest way to install Monit on your CentOS 6 server is to use yum package manager:
ServerSuit ~# yum install monit -y
Once Monit is installed, you’ll need to configure it. The configuration file is called monitrc and you can find it in /etc/monitrc directory, and we'll use it to create a monitoring config for our MySQL server, which checks if MySQL service is running every 2 minutes, and restart it if it's down. There are three types of commands Monit accepts:
1. Set statements – these are for basic Monit settings like used ports, checks interval and log files location.
set daemon 120 # Check services ar 2-minute intervals set logfile /var/log/monit.log # set log file path set mailserver mail.ServerSuit.com # set mailserver for alert delivery set httpd port 2812 # Run web server on port 2812 allow localhost # Allow localhost to connect allow user:pass # require Username(user)and password(pass)
2. Include statements – it’s a good idea to keep the main files more compact and use separate config file for every service monitored, but for this exercise we have only one service, so let’s ignore these for now.
3. Service entry statements - here you can specify the programs you want to be managed by Monit.
check process mysqld with pidfile /var/run/mysqld/mysqld.pid start program = "/etc/init.d/mysql start" stop program = "/etc/init.d/mysql stop" if failed host 127.0.0.1 port 3306 then restart if 5 restarts within 5 cycles then timeout
Our Monit configuration is ready, but, of course, we gotta check the configuration file for possible syntax errors:
ServerSuit ~# monit -t
If there aren't any errors, you're good to go!
ServerSuit ~# monit start all
Done! Now if you shutdown MySQL server by using ‘service mysql stop’ command you'll see that in about 2 minutes, Monit will restart the service itself! You can check service status using the console with command ‘monit status’, or you can configure Monit web interface access in a config file. Currently, Monit is set up to allow access only from the server itself for security reasons, so you’ll need to change the config file, or use Nginx as reverse proxy server as described in our previous article.
This setup isn't really that complicated, but you can still save yourself a lot of time by using ServerSuit to set up Monit! You'll get our convenient software list when you sign up:
Then all you have to do is click that Monit button, and ServerSuit will push a preconfigured version of Monit to your server.
ServerSuit is free for 30-days when you first sign up, so there's literally never a bad time to try it out!
Until next time!











