1.什么是Monit?
Monit是一个实用程序,用于管理和监视类Unix系统上的进程,程序,文件,目录和文件系统。 Monit会进行自动维护和修复,并且可以在错误情况下执行有意义的因果操作。例如。如果不运行,Monit可以启动进程,如果不响应,则可以重新启动进程,如果使用过多的资源,则可以停止进程。您可以使用Monit监视文件,目录和文件系统的更改,例如时间戳更改,校验和更改或大小更改。
Monit通过基于自由格式,面向令牌的语法的易于配置的控制文件(/ etc / monit / monitrc)进行控制。监控日志 syslog或它自己的日志文件(/var/log/monit.log),并通过可自定义的警报消息通知您有关错误情况的信息。
2.安装
[email protected]:~$ sudo apt install monit Reading package lists... Done Building dependency tree Reading state information... Done The following package was automatically installed and is no longer required: grub-pc-bin Use 'sudo apt autoremove' to remove it. Suggested packages: exim4 | postfix | mail-transport-agent sysvinit-core The following NEW packages will be installed: monit 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 318 kB of archives. After this operation, 946 kB of additional disk space will be used. Get:1 http://us-central1.gce.archive.ubuntu.com/ubuntu bionic/universe amd64 monit amd64 1:5.25.1-1build1 [318 kB] Fetched 318 kB in 0s (9343 kB/s) debconf: delaying package configuration, since apt-utils is not installed Selecting previously unselected package monit. (Reading database ... 45688 files and directories currently installed.) Preparing to unpack .../monit_1%3a5.25.1-1build1_amd64.deb ... Unpacking monit (1:5.25.1-1build1) ... Setting up monit (1:5.25.1-1build1) ... Processing triggers for systemd (237-3ubuntu10.28) ... [email protected]:~$
3.服务控制和显示服务状态
[email protected]:~$ sudo monit -t Control file syntax OK [email protected]:~$ sudo service monit restart
[email protected]:~$ sudo service monit status
● monit.service - LSB: service and resource monitoring daemon
Loaded: loaded (/etc/init.d/monit; generated)
Active: active (running) since Thu 2019-09-05 18:20:09 UTC; 26s ago
Docs: man:systemd-sysv-generator(8)
Process: 27892 ExecStop=/etc/init.d/monit stop (code=exited, status=0/SUCCESS)
Process: 27896 ExecStart=/etc/init.d/monit start (code=exited, status=0/SUCCESS)
Tasks: 1 (limit: 690)
CGroup: /system.slice/monit.service
└─27911 /usr/bin/monit -c /etc/monit/monitrc
Sep 05 18:20:09 ubuntu-min-nginx systemd[1]: Stopped LSB: service and resource monitoring daemon.
Sep 05 18:20:09 ubuntu-min-nginx systemd[1]: Starting LSB: service and resource monitoring daemon...
Sep 05 18:20:09 ubuntu-min-nginx monit[27896]: * Starting daemon monitor monit
Sep 05 18:20:09 ubuntu-min-nginx monit[27896]: ...done.
Sep 05 18:20:09 ubuntu-min-nginx systemd[1]: Started LSB: service and resource monitoring daemon.
[email protected]:~$
4.监视系统CPU,内存,负载
配置monit以监视系统负载,cpu使用率和内存使用率。一旦超出规格,monit将使用命令“ / sbin / shutdown -r now”触发重启。
[email protected]:~$ sudo vi /etc/monit/monitrc
check system $HOST
if loadavg (1min) > 1 then alert
if loadavg (5min) > 1.05 then exec "/sbin/shutdown -r now"
if cpu usage > 50% for 5 cycles then alert
if cpu usage > 75% for 3 cycles then exec "/sbin/shutdown -r now"
if memory usage > 85% then alert
# if memory usage > 95% then reboot
if swap usage > 25% then alert
当达到系统资源阈值并触发监视服务时,以下是一些日志:
[email protected]:~$ cat /var/log/monit.log [EDT Sep 4 15:46:17] info : Starting Monit 5.25.1 daemon [EDT Sep 4 15:46:17] info : 'ubuntu18niginxmariadb2' Monit 5.25.1 started [EDT Sep 4 20:02:19] warning : 'ubuntu18niginxmariadb2' cpu usage of 98.7% matches resource limit [cpu usage > 75.0%] [EDT Sep 4 20:02:19] warning : 'ubuntu18niginxmariadb2' cpu usage of 98.7% matches resource limit [cpu usage > 50.0%] [EDT Sep 4 20:02:19] error : 'ubuntu18niginxmariadb2' loadavg(5min) of 1.6 matches resource limit [loadavg(5min) > 1.0] [EDT Sep 4 20:02:19] info : 'ubuntu18niginxmariadb2' exec: '/sbin/shutdown -r now' [EDT Sep 4 20:02:19] error : 'ubuntu18niginxmariadb2' loadavg(1min) of 4.1 matches resource limit [loadavg(1min) > 1.0] [EDT Sep 4 20:02:24] info : Monit daemon with pid [1121] stopped [EDT Sep 4 20:02:24] info : 'ubuntu18niginxmariadb2' Monit 5.25.1 stopped [EDT Sep 4 20:02:48] info : Starting Monit 5.25.1 daemon [EDT Sep 4 20:02:48] info : 'ubuntu18niginxmariadb2' Monit 5.25.1 started [EDT Sep 5 01:32:50] error : 'ubuntu18niginxmariadb2' loadavg(1min) of 1.0 matches resource limit [loadavg(1min) > 1.0] [EDT Sep 5 01:34:51] warning : 'ubuntu18niginxmariadb2' cpu usage of 87.7% matches resource limit [cpu usage > 75.0%] [EDT Sep 5 01:34:51] warning : 'ubuntu18niginxmariadb2' cpu usage of 87.7% matches resource limit [cpu usage > 50.0%] [EDT Sep 5 01:34:51] error : 'ubuntu18niginxmariadb2' loadavg(5min) of 1.8 matches resource limit [loadavg(5min) > 1.0] [EDT Sep 5 01:34:51] info : 'ubuntu18niginxmariadb2' exec: '/sbin/shutdown -r now' [EDT Sep 5 01:34:51] error : 'ubuntu18niginxmariadb2' loadavg(1min) of 4.0 matches resource limit [loadavg(1min) > 1.0] [EDT Sep 5 01:34:55] info : Monit daemon with pid [977] stopped [EDT Sep 5 01:34:55] info : 'ubuntu18niginxmariadb2' Monit 5.25.1 stopped [EDT Sep 5 01:35:18] info : Starting Monit 5.25.1 daemon [EDT Sep 5 01:35:18] info : 'ubuntu18niginxmariadb2' Monit 5.25.1 started [EDT Sep 5 02:23:19] error : 'ubuntu18niginxmariadb2' loadavg(1min) of 1.3 matches resource limit [loadavg(1min) > 1.0] [EDT Sep 5 02:25:19] warning : 'ubuntu18niginxmariadb2' cpu usage of 50.7% matches resource limit [cpu usage > 50.0%] [EDT Sep 5 02:25:19] error : 'ubuntu18niginxmariadb2' loadavg(1min) of 2.0 matches resource limit [loadavg(1min) > 1.0] [EDT Sep 5 02:27:19] warning : 'ubuntu18niginxmariadb2' cpu usage of 99.5% matches resource limit [cpu usage > 75.0%] [EDT Sep 5 02:27:19] warning : 'ubuntu18niginxmariadb2' cpu usage of 99.5% matches resource limit [cpu usage > 50.0%] [EDT Sep 5 02:27:19] error : 'ubuntu18niginxmariadb2' loadavg(5min) of 2.1 matches resource limit [loadavg(5min) > 1.0] [EDT Sep 5 02:27:19] info : 'ubuntu18niginxmariadb2' exec: '/sbin/shutdown -r now' [EDT Sep 5 02:27:19] error : 'ubuntu18niginxmariadb2' loadavg(1min) of 4.3 matches resource limit [loadavg(1min) > 1.0] [EDT Sep 5 02:27:22] info : Monit daemon with pid [1003] stopped [EDT Sep 5 02:27:22] info : 'ubuntu18niginxmariadb2' Monit 5.25.1 stopped [EDT Sep 5 02:27:47] info : Starting Monit 5.25.1 daemon [EDT Sep 5 02:27:47] info : 'ubuntu18niginxmariadb2' Monit 5.25.1 started [EDT Sep 5 02:41:47] error : 'ubuntu18niginxmariadb2' loadavg(1min) of 2.1 matches resource limit [loadavg(1min) > 1.0] [EDT Sep 5 02:43:48] warning : 'ubuntu18niginxmariadb2' cpu usage of 99.5% matches resource limit [cpu usage > 75.0%] [EDT Sep 5 02:43:48] warning : 'ubuntu18niginxmariadb2' cpu usage of 99.5% matches resource limit [cpu usage > 50.0%] [EDT Sep 5 02:43:48] error : 'ubuntu18niginxmariadb2' loadavg(5min) of 1.9 matches resource limit [loadavg(5min) > 1.0] [EDT Sep 5 02:43:48] info : 'ubuntu18niginxmariadb2' exec: '/sbin/shutdown -r now' [EDT Sep 5 02:43:48] error : 'ubuntu18niginxmariadb2' loadavg(1min) of 4.1 matches resource limit [loadavg(1min) > 1.0] [EDT Sep 5 02:43:52] info : Monit daemon with pid [1041] stopped [EDT Sep 5 02:43:52] info : 'ubuntu18niginxmariadb2' Monit 5.25.1 stopped [EDT Sep 5 02:44:16] info : Starting Monit 5.25.1 daemon [EDT Sep 5 02:44:16] info : 'ubuntu18niginxmariadb2' Monit 5.25.1 started
5.启用Monit Web服务
[email protected]:~$ sudo nano /etc/monit/monitrc
[email protected]:~$ sudo service monit restart[email protected]:~$ netstat -na Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:2812 0.0.0.0:* LISTEN tcp 0 0 10.128.0.3:59048 169.254.169.254:80 ESTABLISHED tcp 0 0 10.128.0.3:80 52.53.175.20:61496 TIME_WAIT tcp 0 0 10.128.0.3:59058 169.254.169.254:80 ESTABLISHED tcp 0 0 10.128.0.3:80 3.83.192.114:46676 TIME_WAIT tcp 0 0 10.128.0.3:80 54.193.33.178:52530 TIME_WAIT tcp 0 0 10.128.0.3:80 3.90.252.110:24496 TIME_WAIT tcp 0 0 10.128.0.3:59044 169.254.169.254:80 CLOSE_WAIT tcp 0 384 10.128.0.3:22 173.194.90.33:40188 ESTABLISHED tcp 0 0 10.128.0.3:59052 169.254.169.254:80 ESTABLISHED tcp6 0 0 :::22 :::* LISTEN tcp6 0 0 :::2812 :::* LISTEN udp 0 0 127.0.0.1:323 0.0.0.0:* udp 0 0 127.0.0.53:53 0.0.0.0:* udp 0 0 10.128.0.3:68 0.0.0.0:* udp6 0 0 ::1:323 :::* raw6 0 0 :::58 :::*
浏览到您的Monit服务器的 http:// yourserverip:2821 页