A Few Words about Monit

By Jimmy Bonney | October 13, 2013

Sony Remote Controller

In order to monitor a few other important services on our servers, we are relying on Monit. This is an excellent utility tool that ensures that everything is running properly – and should anything happen, it will attempt to fix the problem “on its own” (based on some configuration options that have been defined).

We are probably only scratching the surface of what Monit can do for us so the configuration options that I’ll discuss below might really be some basic settings. But as usual, it will act as a reminder for myself and hopefully it might help someone else in the process.

Monit Homepage

Interact with Monit

In order to be able to issue commands to Monit, it is necessary to set it up to use the embedded web server. In your configuration file (either /etc/monit/monitrc or your own local file), ensure that the web server configuration is active. Note that the code below only allows a local access to Monit web server (no external ports are open).

1
2
3
4
5
6
7
8
# Monit has an embedded web server which can be used to view status of
# services monitored and manage services from a web interface. See the
# Monit Wiki if you want to enable SSL for the web server.

set httpd port 2812 and
    use address localhost  # only accept connection from localhost
    allow localhost        # allow localhost to connect to the server and
    allow admin:monit      # require user 'admin' with password 'monit'

Stopping a Process Based on its PID

In order to stop a process based on its pid, it is necessary to include the kill command in a script. This is discussed in a number of articles (see sources below) and relies on the fact that Monit runs the command given in the conf file through an exec() command.

1
2
# monitrc or any monit.conf file
stop program  = "/bin/bash -c '/bin/kill -9 `cat /shared/pids/myprocess.pid`'"

Since Monit expects a script to be executed, you can also decide to write the kill command in a separate file as such:

1
2
3
4
5
#!/bin/bash
# kill_process_x.sh

# Kill process X
/bin/kill -9 `cat /shared/pids/myprocess.pid`

And then from the monit conf file:

1
2
# monitrc or any monit.conf file
stop program  = "/path/to/kill_process_x.sh"

For the time being, comments are managed by Disqus, a third-party library. I will eventually replace it with another solution, but the timeline is unclear. Considering the amount of data being loaded, if you would like to view comments or post a comment, click on the button below. For more information about why you see this button, take a look at the following article.