Getting Started with Monit for Server Monitoring


Monit is a server monitoring program that may verify the processes operating in your system to make sure that they’re at all times on-line and working inside affordable CPU and reminiscence limits. It additionally comes with an internet interface to watch your system.

What Does Monit Do?

Essentially, Monit will verify in on a given course of each two minutes (by default) to see the way it’s doing. It retains a log of useful resource utilization, and might take motion if an error happens. This consists of relaunching crashed processes, and restarting processes utilizing too many assets.

Monit defines its conduct solely via config recordsdata. Here’s the default config for MySQL for example:

verify course of mysql
  with pidfile /var/run/mysqld/mysqld.pid
  begin program = "/usr/sbin/service mysql start" with timeout 60 seconds
  cease program = "/usr/sbin/service mysql stop" with timeout 60 seconds
  if totalmem > 400 MB for 5 cycles then alert
  if totalmem > 600 MB for 5 cycles then restart
  if cpu > 50% for 5 cycles then alert
  if cpu > 90% for 5 cycles then restart
  if 3 restarts inside 5 cycles then timeout

Monit is configured to watch the operating MySQL course of. If it goes down, it will probably begin it once more utilizing the begin program = config. If MySQL begins utilizing too many assets, it may be restarted routinely, although you’ll be warned by e mail first.

It may also be configured to watch your system as a complete, and might warn you in case your server is experiencing uncommon load.

verify system wp01
  if loadavg(5min) > 1 then alert
  if reminiscence utilization > 90% for 5 cycles then alert
  if cpu utilization (person) > 90% for 5 cycles then alert

Everything Monit displays is viewable from the net interface, which runs over port 2812 by default.

Additionally, you’ll be able to view information for a number of hosts multi function place with the M/Monit utility, which does require a license however is free to attempt.

RELATED: How to Setup Slack Notifications for Low Disk Space On Your Server

Installing and Configuring Monit

Install Monit from your distro’s package manager; for Debian based mostly techniques like Ubuntu, that will be:

sudo apt-get set up monit

Monit ought to come with default config, normally positioned at ~/.monitrc. If it didn’t (as was the case with a macOS set up) you could find the default config recordsdata here.

Monit’s net interface is configured to run on port 2812. This is ok, nevertheless it’s solely open by default with a default password. We’ll wish to lock it right down to a certified IP handle and

set httpd port 2812
    enable your_ip
    enable admin:password

Change this password, in fact. There’s a few more configuration options for the net interface, however that is good for now.

Monit can then be configured to watch particular person processes. Usually, that is completed with the processes PID file (typically present in /var/run/) which shops the present PID of the operating occasion of that course of, because the PID will change each time the method restarts.

verify course of nginx with pidfile /var/run/nginx.pid
    begin program = "/etc/init.d/nginx start"
    cease program = "/etc/init.d/nginx stop"

You’ll must go Monit a command to start out and cease your course of. It ought to routinely restart if the method goes down, with no extra config, however if you wish to restart based mostly on useful resource utilization, you’ll must specify that your self with a couple of if statements:

verify course of nginx with pidfile /var/run/nginx.pid
    begin program = "/etc/init.d/nginx start"
    cease program = "/etc/init.d/nginx stop"
    if totalmem > 600 MB for 5 cycles then restart
    if cpu > 90% for 5 cycles then restart
    if 3 restarts inside 5 cycles then timeout

Monit will ship out alerts each time one thing restarts, so that you’ll be notified when one thing is unsuitable.

Once you’re completed with the config, reload the monit daemon with:

monit reload

And begin monitoring with:

monit begin all

This ought to begin the net interface as nicely, which you should utilize to confirm that Monit is working correctly, normally operating on port 2812.

This is the fundamental config to get Monit operating out of the field, however Monit has much more syntax. You can read the full manual on every part Monit can do to be taught extra.

Configuring Alerts

Monit could be configured to ship e mail alerts each time main points happen, or when processes are restarted. You’ll want so as to add configuration for your mail server:

set mailserver smtp.gmail.com
  port 587
  username "[email protected]"
  password "secret"
  with timeout 60 seconds

set eventqueue
  basedir /var/lib/monit/occasions
  slots 100

The first block defines the mail server to make use of. The simplest way can be to only use smtp.gmail.com with a Gmail account, although you can set up Postfix in your server to run domestically, or use an enterprise e mail service.

The second line tells Monit to queue alerts, if the mail server isn’t reachable for no matter cause.

Obviously, you’ll additionally must set the e-mail handle it sends to:

set alert [email protected]

Additionally, since Monit has the flexibility to run scripts with the exec command, you’ll be able to arrange customized alerts nonetheless you’d like. For instance, you can arrange a script to send you Slack notifications, retailer it as /usr/native/bin/slack-webhook.sh, and run it each time nginx adjustments PID or is restarted by Monit:

verify course of nginx with pidfile /var/run/nginx.pid
  begin program = "/etc/init.d/nginx start"
  cease program = "/etc/init.d/nginx stop"
  if modified pid then exec "/bin/bash -c 'PROCESS=Nginx /usr/local/bin/slack-webhook.sh'"
  if 1 restart inside 1 cycle then exec "/bin/bash -c 'PROCESS=Nginx /usr/local/bin/slack-webhook.sh'"

Note that each one paths should be absolutely certified, together with /bin/bash and paths to executables in your native bin. And after you’re completed modifying the config recordsdata, you’ll must reload Monit once more.



Source link

This Web site is affiliated with Amazon associates, Clickbank, JVZoo, Sovrn //Commerce, Warrior Plus etc.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *