2016年07月25日 18:03:38 趕路人兒 閱讀數:2034php
版權聲明:本文爲博主原創文章,未經博主容許不得轉載。 https://blog.csdn.net/liuxiao723846/article/details/52022545html
Monit是一款功能很是豐富的進程、文件、目錄和設備的監測軟件,適用於Linux/Unix平臺。官網地址:https://mmonit.com/
在CentOS 6.4上配置Monit的步驟:linux
咱們以服務器IP地址:10.153.126.189,爲例進行配置,監控10.153.110.12, 10.153.75.78這兩臺服務器。nginx
1、安裝Monit:web
# yum install monit –y
這一步可能會報錯:apache
# yum install monit –y
segmentfault
Loaded plugins: fastestmirror, security
api
Determining fastest mirrors
tomcat
Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
服務器
解決方法:
vi /etc/yum.repos.d/epel.repo
編輯[epel]下的baseurl前的#號去掉,mirrorlist前添加#號。正確配置以下:
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
至此Monit安裝完畢,接下來配置monit.conf文件
2、monit配置:
一、文件位置在/etc/monit.conf,修改常見配置:
1)檢測時間、日誌位置:
set daemon 120
with start delay 240
set logfile syslog facility log_daemon
每120s檢測一次;啓動後延時240s開始檢測;
日誌文件位置;
2)id文件
set idfile /var/monit/id
set eventqueue
basedir /var/monit
咱們必須定義「idfile」,Monit守護進程的一個獨一無二的ID文件;
「eventqueue」,當monit的郵件由於SMTP或者網絡故障發不出去,郵件會暫存在這裏;以及確保/var/monit路徑是存在的。而後使用下邊的配置就能夠了;
3)設置web界面:
set httpd port 1966 and
SSL ENABLE
PEMFILE /var/certs/monit.pem
allow monituser:romania
allow localhost
allow 192.168.0.0/16
allow myhost.mydomain.ro
二、監控信息能夠直接寫到/etc/monit.conf文件中,也能夠單首創建一個.cfg後綴的文件,將check... if 語句添加到裏面,而後在monit.conf文件末尾加入include路徑。
set daemon 30 # 每30秒查詢一次服務器狀態
set logfile /data/apps/monit/log/monit.log # 默認日誌位於 /var/log/monit
set idfile /var/.monit.id
set eventqueue
basedir /data/apps/monit/data
slots 10000
set httpd port 2812 and # 監聽2812端口
use address 10.153.126.189 # 本服務器地址,這裏咱們的Monit安裝到了10.153.126.189這臺服務器上。
allow localhost
allow 10.1.0.0/255.255.0.0
allow admin : pin # 在這裏設置用戶名及口令。admin爲用戶名,冒號後爲密碼。
# 接下來設置要監聽的服務器
# address後面的是服務器的IP地址。第二行設置port端口號。exec後爲當異常狀況出現後執行的腳本。能夠添加多個check ... if 語句,同時監聽不少臺服務器
# 這裏 /data/apps/monit/contrib/sms.py這個腳本負責報警
check host read_kajuan_10.153.110.12 with address 10.153.110.12
if failed port 80 with timeout 1 seconds for 2 cycles then exec "/data/apps/monit/contrib/sms.py"
check host read_kajuan_10.153.75.78 with address 10.153.75.78
if failed port 80 with timeout 1 seconds for 2 cycles then exec "/data/apps/monit/contrib/sms.py"
#include /etc/monit.d/*
3、常見監控:
一、根據ip+端口,監控web服務器端口存活:
check host gamecenter_api_10.153.123.2 with address 10.153.123.2
if failed port 8093 with timeout 1 seconds for 2 cycles then exec "/data/apps/monit/contrib/sms.py"
表示:在兩次監控週期內,若是端口超時超過1m則報警。
二、根據pid,監控服務進程:
check process tomcat with pidfile /var/run/catalina.pid # 進程pid
start program = "/etc/init.d/tomcat start" # 設置啓動命令
stop program = "/etc/init.d/tomcat stop" # 設置中止命令
if 9 restarts within 10 cycles then timeout # 設置在10個監視週期內重,啓了9次則超時,再也不監視這個服務。緣由另外說明【3】
if cpu usage > 90% for 5 cycles then alert # 若是在5個週期內該服務的cpu使用率都超過90%則提示
if failed url http://127.0.0.1:4000/ timeout 120 seconds for 5 cycles then restart # 若連續5個週期打開url都失敗(120秒超時,超時也認爲失敗)則重啓服務
設置超時後再也不監視是爲了讓服務不要一直重啓,若是連續重啓屢次不成功,極有可能再重啓下去也不會成功的。而且tomcat的重啓須要佔用大量系統資源,假如一直重啓下去,反而會使其它服務也沒法正常運做。
三、能夠對moint自己服務器進行監控:
# 系統名稱,能夠是IP或域名
check system www.example.com
if loadavg (1min) > 4 then alert
if loadavg (5min) > 2 then alert
if memory usage > 75% then alert
if cpu usage (user) > 70% then alert
if cpu usage (system) > 30% then alert
if cpu usage (wait) > 20% then alert
四、實例:
#
# 監控nginx
#
# 須要提供進程pid文件信息
check process nginx with pidfile /var/run/nginx.pid
# 進程啓動命令行,注:必須是命令全路徑
start program = "/etc/init.d/nginx start"
# 進程關閉命令行
stop program = "/etc/init.d/nginx stop"
# nginx進程狀態測試,監測到nginx連不上了,則自動重啓
if failed host www.example.com port 80 protocol http then restart
# 屢次重啓失敗將再也不嘗試重啓,這種就是系統出現嚴重錯誤的狀況
if 3 restarts within 5 cycles then timeout
# 可選,設置分組信息
group server
# 可選的ssl端口的監控,若是有的話
# if failed port 443 type tcpssl protocol http
# with timeout 15 seconds
# then restart
#
# 監控apache
#
check process apache with pidfile /var/run/apache2.pid
start program = "/etc/init.d/apache2 start"
stop program = "/etc/init.d/apache2 stop"
# apache吃cpu和內存比較厲害,額外添加一些關於這方面的監控設置
if cpu > 50% for 2 cycles then alert
if cpu > 70% for 5 cycles then restart
if totalmem > 1500 MB for 10 cycles then restart
if children > 250 then restart
if loadavg(5min) greater than 10 for 20 cycles then stop
if failed host www.example.com port 8080 protocol http then restart
if 3 restarts within 5 cycles then timeout
group server
# 可選,依賴於nginx
depends on nginx
#
# 監控spawn-fcgi進程(其實就是fast-cgi進程)
#
check process spawn-fcgi with pidfile /var/run/spawn-fcgi.pid
# spawn-fcgi必定要帶-P參數纔會生成pid文件,默認是沒有的
start program = "/usr/bin/spawn-fcgi -a 127.0.0.1 -p 8081 -C 10 -u userxxx -g groupxxx -P /var/run/spawn-fcgi.pid -f /usr/bin/php-cgi"
stop program = "/usr/bin/killall /usr/bin/php-cgi"
# fast-cgi走的不是http協議,monit的protocol參數也沒有cgi對應的設置,這裏去掉protocol http便可。
if failed host 127.0.0.1 port 8081 then restart
if 3 restarts within 5 cycles then timeout
group server
depends on nginx
注意:
參考:
http://www.cnblogs.com/ddr888/archive/2011/03/02/1969087.html
http://feilong.me/2011/02/monitor-core-processes-with-monit
http://www.vpser.net/manage/monit.html
http://itoedr.blog.163.com/blog/static/1202842972014529115715267/
https://www.rails365.net/articles/bu-shu-zhi-shi-yong-monit-lai-jian-kong-fu-wu-si
http://linuxjcq.blog.51cto.com/3042600/717843
https://segmentfault.com/a/1190000002867212