1. 程式人生 > 實用技巧 >程序監控-monit

程序監控-monit

一、monit安裝
1 yum -y install pam* openssl flex openssl-devel
2 tar -xf monit.tar.gz && cd monit && ./configure && make && make install
二、用法參考
2.1、監控程序
#以程序PID方式監控
check process DD_CRON with pidfile /var/run/crond.pid
        start program = "/etc/init.d/crond start"
        stop  program 
= "/etc/init.d/crond stop" #以程序名字方式監控 check process OPENSIPS matching "/data/opensips/sbin/opensips -P /var/run/opensips.pid -m 2048 -M 16" start program = "/data/opensips/sbin/opensipsctl start" stop program = "/data/opensips/sbin/opensipsctl stop"

2.2、監控埠

#監控埠,並以指定使用者啟動
#TCP埠
check host tomcat_service with address 
127.0.0.1 start program = "/etc/init.d/tomcat start" as uid "user" and gid "user" and with timeout 60 seconds stop program = "/etc/init.d/tomcat stop" as uid "user" and gid "user" and with timeout 60 seconds if failed port 8088 with timeout 60 seconds then restart
if 2 restarts within 2 cycles then exec "/usr/bin/python /home/shell-scripts/send_messages/send_meassage.py tomcat_service" #UDP埠 check host devhost-udp with address XXX.XXX.XXX.XXX if failed port 3001 type udp then exec "/etc/init.d/app restart"

2.3、監控記憶體

#如果單程序記憶體使用超過 200M 則告警
check process NXLOG with pidfile /var/run/graylog/collector-sidecar/nxlog.pid
        start program = "/etc/init.d/collector-sidecar start"
        stop  program = "/etc/init.d/collector-sidecar stop"
        if totalmem > 200 MB then restart

2.4、監控磁碟

#監控磁碟使用率大於 65% 並告警
check filesystem ROOT with path /dev/vda1
        if space usage > 65% then alert

2.5、監控CPU和記憶體

#監控ping或icmp協議判斷網路是否異常
check host example with address 0.0.0.0
       if failed icmp type echo count 3 with timeout 20 seconds then alert
       if failed ping then alert
       if failed port 5060 type udp protocol sip then alert

2.7、按時間段監控

#在星期1至星期天 凌晨0點至2點不做監控
check program PROC with path "/root/check_sip/check_status.sh"
        if status = 0 for 3 times within 5 cycles then exec "/bin/sh /root/call_mobile.sh"
        repeat every 4 cycles
        alert 648813099@qq.com with reminder on 4 cycles
        not every "* 0-2 * * 1-7"

2.8、自定義監控指令碼

#檢測web埠狀態:
check program webstatus with path "/home/check_http_status.sh"
    if status = 0 then exec "/home/check_http_status.sh start"
    repeat every 5 cycles #(如果問題依然存在,在5次迴圈後,執行/home/check_http_status.sh start)

2.9、檔案監控

check file http_log with path /var/log/httpd/error_log
    ignore match "doorduweb"
    ignore match "% Total"
    ignore match "Dload  Upload"
    ignore match "Permission denied"
    ignore match "favicon.ico"
    if match "[error]" then alert
    alert [email protected]
    alert [email protected]

四、參考文件

https://mmonit.com/monit/documentation/monit.html

https://mmonit.com/wiki/Monit/ConfigurationExamples#syslogd