[LINUX] Automation of server monitoring etc. with Monit

What is Monit

With the integrated monitoring daemon, you can perform email notification, automatic recovery, etc., and monitoring above the set threshold. -Http response, process, port monitoring Example) Apache, MySQL, SSL, Postfix, fluentd ・ CPU, memory, load average, disk capacity, etc.

1. 1. Initial setting (when newly installing on the server)

① Install and enable the EPEL rpm package

$ sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

② Install monit

$ sudo yum -y install monit

③ Check if there are related files

$ rpm -ql monit
/etc/logrotate.d/monit
/etc/monit.d
/etc/monit.d/logging
/etc/monitrc
/usr/bin/monit
/usr/lib/systemd/system/monit.service
/usr/share/doc/monit-5.14
/usr/share/doc/monit-5.14/COPYING
/usr/share/doc/monit-5.14/README
/usr/share/man/man1/monit.1.gz
/var/log/monit.log

④ Backup of configuration file

$ sudo cp -av /etc/monitrc{,.bk}
`/etc/monitrc' -> `/etc/monitrc.bak'
// -a Copy with permissions as much as possible
// -v Show file name before copying
// {,.bk}With a technique called brace deployment.Copy with file name bk

⑤ Edit the setting file

$ vi /etc/monitrc
…
set daemon  30              //The default check interval is 30 seconds, so change it arbitrarily.
…
include /etc/monit.d/*.conf // .Edit to read only conf

⑤'monit also has a WWW server function, which allows you to refer to settings and control services via HTTP.

set httpd port 2812 and    #Web interface port(Default:2812)
    allow localhost        #IP to allow access(localhost)
    allow XXX.XXX.XXX.XXX/XX  #IP to allow access(Any IP)
    allow admin:monit      #Management console user='admin' password='monit'
#Comment out because SSL is not used below
    #with ssl {            # enable SSL/TLS and set path to server certificate
    #    pemfile: /etc/ssl/certs/monit.pem
    #}

⑥ Creation of configuration file

$ vi /etc/monit.d/XXXX.conf //Create new for each setting
$ mv /etc/monit.d/logging /etc/monit.d/logging.conf //Rename log configuration file

2. Common configuration file

The following is the main configuration file.

/etc/monitrc
backup:/etc/monitrc.bk

Include and read the following individual configuration file.
/etc/monit.d/*.conf

3. 3. Individual setting file (example)

/etc/monit.d/test.conf
//Monitoring process settings
check process test matching "test"
//Startup settings
start program "/usr/local/test/bin/start_test.sh"
//Stop setting
stop program "/usr/local/test/bin/stop_test.sh"
//If there is no process, restart
if does not exist then restart
//Notify slack *
if does not exist then exec "/usr/local/test/bin/slacknotice.sh"
//If you do not wake up after restarting 5 times during 5 monitoring, stop
if 5 restarts within 5 cycles then unmonitor

It is recommended to include the following cron as a monit best practice.

echo "0 */1 * * * /usr/bin/monit monitor all" >> /var/spool/cron/root

https://hogem.hatenablog.com/entry/20090723/1248358467

4. Execution command

Start command $ systemctl start monit Automatically starts when the OS starts $ systemctl enable monit Stop command $ systemctl stop monit status $ monit status Reload when setting is changed (Important !!) $ monit reload Test command when changing settings $ monit -t Know the monitoring status $ monit summary When monit is skipped (when Not monitored) $ monit monitor all Command list $ monit -h

5. Management screen (Monit Service Manager)

IP: IP of monit server PORT: PORT set in ⑤' http://XXX.XXX.XXX.XXX:2812/

6. About paid M / Monit

The following sites were described in detail. https://inokara.hateblo.jp/category/monit

Recommended Posts

Automation of server monitoring etc. with Monit
Automation of remote operations with Fabric
Winning with Monitoring
That of / etc / shadow
Proxy server with Docker
Local server with python
Put numpy scipy etc. in virtualenv of Ubuntu 12.04 LTS Server
I tried starting Django's server with VScode instead of Pycharm
Check the memory status of the server with the Linux free command
Check the operating status of the server with the Linux top command
Build a speed of light web API server with Falcon
The true value of Terraform automation starting with Oracle Cloud