Monit常见问题集

HTTP interface is not enabled

$ vim /etc/monit/monitrc
解注释或添加

1
2
3
4
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'

$ monit reload
$ monit
$ monit start all

自定义监控配置

可在这两个文件下新建文件
/etc/monit/conf.d/
/etc/monit/conf-enabled/

模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
check process nginx with pidfile /usr/local/nginx/logs/nginx.pid
start program = "/usr/local/nginx/sbin/nginx"
stop program = "/usr/local/nginx/sbin/nginx -s stop"
check process php-fpm with pidfile /var/run/php-fpm/php-fpm.pid
start program = "/etc/init.d/php-fpm start"
stop program = "/etc/init.d/php-fpm stop"
check process mysqld with pidfile "/letv/mysql2/data/cdn.oss.letv.com.pid"
start program = "/etc/init.d/mysqld start"
stop program = "/etc/init.d/mysqld stop"
```

.pid 文件位置 /var/run/
例: Chrome 位置 /opt/google/chrome/chrome

## How to find the .pid file for a given process
https://serverfault.com/questions/417892/how-to-find-the-pid-file-for-a-given-processmo

## 不用.pid 的方法

check process chrome
matching “chrome”

1
2
3
4
5
6
7
8
9
10
11
12
来源:https://serverfault.com/questions/383058/how-to-monitor-select-processes-on-centos-5-x-host/383393#383393



## 使用脚本来创建pid文件
来源:https://mmonit.com/wiki/Monit/FAQ
例子:监控一个java程序
可在monitrc添加配置
```sh
check process xyz with pidfile /var/run/xyz.pid
start = "/bin/xyz start"
stop = "/bin/xyz stop"

脚本路径名为/bin/xyz

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
export JAVA_HOME=/usr/local/java/
CLASSPATH=ajarfile.jar:.

case $1 in
start)
echo $$ > /var/run/xyz.pid;
exec 2>&1 java -cp ${CLASSPATH} org.something.with.main 1>/tmp/xyz.out
;;
stop)
kill `cat /var/run/xyz.pid` ;;
*)
echo "usage: xyz {start|stop}" ;;
esac
exit 0

综上,要监控一个进程可以关联.pid 文件或采用match进行关键字匹配或创建脚本

常用命令

monit statue

Centos中monit的配置文件位置

/etc/monit.conf
/etc/monit.d/

如何配置使远端访问2812端口(web监控界面)

修改/etc/monit.conf 文件

1
2
3
set httpd port 2812 
allow 192.168.174.0/255.255.255.0 # 配置网关,同一局域网下主机均可访问
allow admin:monit # require user 'admin' with password 'monit'

centos安装GUI

1
2
3
# yum groupinstall "GNOME Desktop" "Graphical Administration Tools"
# ln -sf /lib/systemd/system/runlevel5.target /etc/systemd/system/default.target
# reboot

centos7默认log文件保存位置

/var/log/monit

centos7 monit相关文件位置

monit控制文件位置: /etc/monit.conf
自定义脚本位置: /etc/monit.d/ *.conf
packetfence 监控控制文件位置: /etc/monit.d/00_packetfence.conf/

如何去掉服务

  1. 在/etc/monit.d/00_packetfence.conf/ 中删除不需要监控的服务脚本,此操作仅取消监控功能,不会杀掉相应进程

添加服务

在/etc/monit.d/下新建文件添加相应的监控

写log

默认log在var/log/monit,可以对其进行筛选,找出相应log
例如: cat /var/log/monit | grep packetfence-pfdhcp

实时显示log信息

tail -f /var/log/monit

写log的一篇很好的总结

https://stackoverflow.com/questions/3356476/debugging-monit

官方shell 脚本实例

1
2
3
4
check program hwtest with path /usr/local/bin/hwtest.sh
with timeout 500 seconds
if status = 1 then alert
if status = 3 for 5 cycles then exec "/usr/local/bin/emergency.sh"

monit 行为

1
2
3
4
5
6
7
8
9
ALERT: 发报警
RESTART: 重启并发报警(注册的 restart 方法, 如果没有, 则先 stop 再 start)
START: 启动并发报警(注册的 start 方法)
STOP: 关闭服务并发报警, 关闭之后不会再被 Monit 检查, 重启 Monit 也不会监控这个服务, 只能从网页或者控制台再次开启 (注册的 stop 方法)
EXEC: 执行指定的脚本并报警, 可以指定用户(需要以 root 权限启动), 可以设定多次检查周期作为一个周期
if failed <test> then exec "/usr/local/bin/sms.sh"
as uid nobody and gid nobody
repeat every 5 cycles
UNMONITOR: 不再监控并发报警, 关闭之后不会再被 Monit 检查, 重启 Monit 也不会监控这个服务, 只能从网页或者控制台再次开启

Service checks

Each service entry consists of the keywords check, followed by the service type. Each entry requires a unique descriptive name, which may be freely chosen. This name is used by Monit to refer to the service internally and in all interactions with the user.

Currently, nine types of check statements are supported:

Process

1
CHECK PROCESS <unique name> <PIDFILE <path> | MATCHING <regex>>

is the absolute path to the program’s pid-file. A pid-file is a file, containing a Process’s unique ID. If the pid-file does not exist or does not contain the PID number of a running process, Monit will call the entry’s start method if defined.
//原文写道:若pidfile不存在,会调用start方法。但实际操作起来,若pidfile不存在在会直接报错
//更新:报错原因分析可能是无法识别pid引起start方法的重复调用,未能找到解决方法,实际运用中建议采用matching 加路径的方法

is an alternative to using PID files and uses process name pattern matching to find the process to monitor. The top-most matching parent with highest uptime is selected, so this form of check is most useful if the process name is unique. Pid-file should be used where possible as it defines expected PID exactly. You can test if a process match a pattern from the command-line using monit procmatch “regex-pattern”. This will lists all processes matching or not, the regex-pattern.

status

status

1
2
check program myscript with path /usr/local/bin/myscript.sh
if status != 0 then alert

shell 脚本的status failed 问题

shell脚本的前两个字符必须为#!,前面不能有空格,与终端下执行命令不同,即使前面有空格在终端下也可以正常运行,而monit下必须严格按照要求才能识别

开放2812端口

1
2
3
vim /usr/local/pf/conf/iptables.conf
-A INPUT -p tcp --dport 2812 -j ACCEPT
#重启

或者
命令行输入:iptables -A INPUT -p tcp —dport 2812 -j ACCEPT