Monit - 监视Linux守护程序,文件系统,CPU,文件和网络

时间:2020-03-05 15:28:58  来源:igfitidea点击:

Monit是一个小型Linux实用程序,旨在管理和监视进程,程序,文件系统,目录和文件。
我们可以拥有自动维护和修复,并可以在错误情况下执行有意义的因果操作。
我们可以使用Monit监视文件,目录和文件系统进行更改,例如时间戳更改,校验和更改或者大小更改。
Monit登录Syslog或者其自己的日志文件,并通过可自定义的警报消息通知我们错误情况。
它还可以执行各种TCP/IP网络检查,协议检查,并可利用SSL进行此类检查。

可以通过我们喜欢的Web浏览器通过Web界面使用Monit。

如何安装monit

要在Debian/Ubuntu分发上安装Monit,我们可以使用apt-met:

# apt-get install monit

在Fedora上,我们可以使用yum从存储库安装它:

# yum install monit

要在CentOS/RHEL上安装它,我们必须使用DAG RPMForge,然后使用相同的yum命令安装它。

配置文件

通过名为monitrc的控制文件配置和控制Monit。

此文件的默认位置是~/.monitc如果不可用,它将使用/etc/monit/monitrc。
/etc/init.d/monit中的执行脚本也将使用/etc/monit/monitrc。
要保护控制文件和密码的安全性,控制文件必须具有不超过0700的权限;典型将申请并退出。

目前,支持八种类型的检查语句:

检查过程<唯一名称> <pidfile <path> |匹配<regex >> <path>是程序PIDFile的绝对路径。

检查文件<唯一名称>路径<path> <path>是文件的绝对路径。

检查FIFO <唯一名称>路径<path> <path>是FIFO的绝对路径。

检查文件系统<唯一名称>路径<path> <path>是文件系统块特殊设备,装入点,文件或者文件的一部分的路径。

检查目录<唯一名称>路径<path> <path>是目录的绝对路径。

检查主机<唯一名称>地址<主机地址>主机地址可以指定为主机名字符串,也可以是虚线十进制格式的IP地址字符串。

检查系统<唯一名称>系统名称通常是主机名,但可以使用任何描述性名称。
此测试允许人们检查诸如CPU使用情况(在用户,系统等待的时间百分比),总内存使用或者加载平均值的常规系统资源。

检查程序<唯一名称>路径<可执行文件> [超时<number>秒] <path>是可执行程序或者脚本的绝对路径。
状态测试允许其中检查程序的退出状态。

使用Monit Web界面

Monit附带易于使用的Web界面,我们可以在浏览器中访问,以启用它,我们必须将以下行添加到MONITRC文件中:

set httpd port 2812
allow myuser:mypassword

然后,我们可以使用服务器的IP访问它,它应该如下所示:

示例:监视守护程序,文件系统,CPU,文件和网络

1.要监视守护程序,我们可以将以下行添加到MONITRC文件中:

check process apache with pidfile /var/run/apache2/apache2.pid
start program = "/etc/init.d/apache2 start" with timeout 60 seconds
stop program = "/etc/init.d/apache2 stop"

2.要在高CPU使用情况下发送警报,我们可以在MONITRC文件中使用它:

check process apache with pidfile /var/run/apache2/apache2.pid
start program = "/etc/init.d/apache2 start" with timeout 60 seconds
stop program = "/etc/init.d/apache2 stop"
if cpu > 60% for 2 cycles then alert
if cpu > 80% for 5 cycles then restart

3.在高内存使用情况下重新启动:

check process apache with pidfile /var/run/apache2/apache2.pid
start program = "/etc/init.d/apache2 start" with timeout 60 seconds
stop program = "/etc/init.d/apache2 stop"
if totalmem > 200.0 MB for 5 cycles then restart

4.要检查文件系统:

check filesystem datafs with path /dev/sda1
start program = "/bin/mount /data"
stop program = "/bin/umount /data"

5.要检查目录:

check directory bin with path /bin
if failed permission 755 then alert

6.检查网络上的主机

check host server2 with address 192.168.1.2
if failed icmp type echo count 3 with timeout 3 seconds then alert