Monit
Monit,与 M/Monit 区分开,是一个 AGPL3.0 许可的系统和进程监控工具。Monit 可以自动重启崩溃的服务,显示来自标准硬件的温度(通过 lm_sensors)和来自 smartmontools 的硬盘(例如)。服务告警可以基于广泛的标准发送,包括单个事件或一段时间内的事件。可以通过命令行直接访问,也可以通过其集成的 HTTP(S) 服务器作为 Web 应用程序运行。这允许快速、简化的系统状态快照。
安装
安装 monit 包以及用于可选测试的任何软件,例如 lm_sensors 或 smartmontools。完成配置后,请务必 启用并 启动 monit.service。
配置
Monit 的主配置文件是 /etc/monitrc。您可以选择编辑此文件,但如果您想运行脚本(例如获取硬盘温度或健康状态),则应取消注释 include /etc/monit.d/* 的最后一行指令,保存 /etc/monitrc 并创建 /etc/monit.d/。
/etc/monitrc 文件(以及可能存储在 /etc/monit.d 中的文件)具有 0700 权限。不遵守将导致 Monit 无法启动。配置语法
Monit 使用的配置语法非常易于阅读;本质上是 check WHAT 后跟 if THING condition THEN action 格式。在配置文件中,if、and、with(in)、has、us(ing|e)、on(ly)、then、for、of 的任何出现都仅用于人类可读性,Monit 完全忽略它们。
检查通常以 cycles 执行。这在配置文件开头定义,例如 30 秒轮询定义为
set daemon 30
因此,具有 4 cycles 的检查将在 2 分钟后发生
配置示例
邮件服务器声明
set mailserver smtp.myserver.com port 587
username "MyUser" password "MyPassW0rd"
using tlsv12
邮件通知格式
set mail-format {
from: Monit@MyServer
subject: $SERVICE $EVENT at $DATE
message: Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION.
}
$SERVICE,不是通用的示例,而是 Monit 用告警是什么、在哪个系统上等信息替换的特定变量名。CPU、内存和交换空间利用率
check system $HOST
if loadavg (15min) > 15 for 5 times within 15 cycles then alert
if memory usage > 80% for 4 cycles then alert
if swap usage > 20% for 4 cycles then alert
文件系统使用情况
check filesystem rootfs with path /
if space usage > 90% then alert
check filesystem NFS with path /mnt/nfs_share
if space usage > 90% then alert
进程监控
check process sshd with pidfile /var/run/sshd.pid start program "systemctl start sshd" stop program "systemctl stop sshd" if failed port 22 protocol ssh then restart
check process smbd with pidfile /run/samba/smbd.pid group samba start program = "/etc/init.d/samba start" stop program = "/etc/init.d/samba stop" if failed host 192.168.1.250 port 139 type TCP then restart depends on smbd_bin check file smbd_bin with path /usr/bin/smbd group samba if failed permission 755 then unmonitor if failed uid root then unmonitor if failed gid root then unmonitor
使用脚本监控硬盘健康和温度
温度
创建文件 /etc/monit.d/scripts/hdtemp.sh 以及 /etc/monit.d/scripts 文件夹(如果需要)。
/etc/monit.d/scripts/hdtemp.sh
#!/usr/bin/sh
HDDTP=`/usr/bin/smartctl -A /dev/sd${1} | grep Temp.*Cels | awk -F " " '{printf "%d",$10}'`
#echo $HDDTP # for debug only
exit $HDDTP
monitrc or /etc/monit.d/*.monit file
check program SSD-A-Temp with path "/etc/monit.d/scripts/hdtemp.sh a"
every 5 cycles
if status > 40 then alert
group health
check program HDD-B-Temp with path "/etc/monit.d/scripts/hdtemp.sh b"
every 5 cycles
if status > 40 then alert
group health
在此示例中,/etc/monit.d/scripts/hdtemp.sh 脚本假定您的驱动器路径是 /dev/sdX,其中 X 由 check 声明末尾的字母填充。下一个示例中的 SMART 健康状态也使用类似的方法。
SMART 健康状态
/etc/monit.d/scripts/hdhealth.sh
#!/usr/bin/sh
STATUS=`/usr/bin/smartctl -H /dev/sd${1} | grep overall-health | awk 'match($0,"result:"){print substr($0,RSTART+8,6)}'`
if [ "$STATUS" = "PASSED" ]
then
# 1 implies PASSED
TP=1
else
# 2 implies FAILED
TP=2
fi
#echo $TP # for debug only
exit $TP
monitrc or /etc/monit.d/*.monit file
check program SSD-A-Health with path "/etc/monit.d/scripts/hdhealth.sh a"
every 120 cycles
if status != 1 then alert
group health
check program HDD-B-Health with path "/etc/monit.d/scripts/hdhealth.sh b"
every 120 cycles
if status != 1 then alert
group health
group 声明将导致 Monit 将所有分配给同一组名(在此例中为 health)的检查一起显示。告警接收者:全局或子系统
告警可以全局设置,即给定的用户/电子邮件地址会收到所有 alert 条件的通知;或者您可以为每种检查类型设置一个告警接收者(例如,网络告警发送给接收者 A;进程告警发送给接收者 B)。您可以根据需要设置任意数量的全局或子系统接收者,只需进行多次声明即可。
全局告警
全局告警设置在任何子系统检查之外;为了便于阅读,它们应该与邮件服务器声明设置在同一位置。
SET ALERT email@domain
子系统告警
子系统告警的设置与全局告警非常相似,除了它们缺少 SET 标志。
ALERT email@domain