Monit監控軟件安裝

Monit監控軟件安裝

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

 
  1. # yum install monit –ysegmentfault

  2. Loaded plugins: fastestmirror, securityapi

  3. Determining fastest mirrorstomcat

  4. Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again服務器

解決方法:

vi /etc/yum.repos.d/epel.repo

編輯[epel]下的baseurl前的#號去掉,mirrorlist前添加#號。正確配置以下:

 
  1. [epel]

  2. name=Extra Packages for Enterprise Linux 6 - $basearch

  3. baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch

  4. #mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch

  5. failovermethod=priority

  6. enabled=1

  7. gpgcheck=1

  8. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

至此Monit安裝完畢,接下來配置monit.conf文件

2、monit配置:

一、文件位置在/etc/monit.conf,修改常見配置:

1)檢測時間、日誌位置:

 
  1. set daemon 120

  2. with start delay 240

  3. set logfile syslog facility log_daemon

每120s檢測一次;啓動後延時240s開始檢測;

日誌文件位置;

2)id文件

 
  1. set idfile /var/monit/id

  2. set eventqueue

  3. basedir /var/monit

咱們必須定義「idfile」,Monit守護進程的一個獨一無二的ID文件;
「eventqueue」,當monit的郵件由於SMTP或者網絡故障發不出去,郵件會暫存在這裏;以及確保/var/monit路徑是存在的。而後使用下邊的配置就能夠了;

3)設置web界面:

 
  1. set httpd port 1966 and

  2. SSL ENABLE

  3. PEMFILE /var/certs/monit.pem

  4. allow monituser:romania

  5. allow localhost

  6. allow 192.168.0.0/16

  7. allow myhost.mydomain.ro

二、監控信息能夠直接寫到/etc/monit.conf文件中,也能夠單首創建一個.cfg後綴的文件,將check... if 語句添加到裏面,而後在monit.conf文件末尾加入include路徑。

 
  1. set daemon 30 # 每30秒查詢一次服務器狀態

  2.  
  3. set logfile /data/apps/monit/log/monit.log # 默認日誌位於 /var/log/monit

  4.  
  5. set idfile /var/.monit.id

  6.  
  7. set eventqueue

  8. basedir /data/apps/monit/data

  9. slots 10000

  10.  
  11. set httpd port 2812 and # 監聽2812端口

  12. use address 10.153.126.189 # 本服務器地址,這裏咱們的Monit安裝到了10.153.126.189這臺服務器上。

  13. allow localhost

  14. allow 10.1.0.0/255.255.0.0

  15. allow admin : pin # 在這裏設置用戶名及口令。admin爲用戶名,冒號後爲密碼。

  16.  
  17. # 接下來設置要監聽的服務器

  18.  
  19. # address後面的是服務器的IP地址。第二行設置port端口號。exec後爲當異常狀況出現後執行的腳本。能夠添加多個check ... if 語句,同時監聽不少臺服務器

  20.  
  21. # 這裏 /data/apps/monit/contrib/sms.py這個腳本負責報警

  22.  
  23. check host read_kajuan_10.153.110.12 with address 10.153.110.12

  24. if failed port 80 with timeout 1 seconds for 2 cycles then exec "/data/apps/monit/contrib/sms.py"

  25.  
  26. check host read_kajuan_10.153.75.78 with address 10.153.75.78

  27. if failed port 80 with timeout 1 seconds for 2 cycles then exec "/data/apps/monit/contrib/sms.py"

  28.  
  29. #include /etc/monit.d/*

3、常見監控:

一、根據ip+端口,監控web服務器端口存活:

 
  1. check host gamecenter_api_10.153.123.2 with address 10.153.123.2

  2. if failed port 8093 with timeout 1 seconds for 2 cycles then exec "/data/apps/monit/contrib/sms.py"

表示:在兩次監控週期內,若是端口超時超過1m則報警。

二、根據pid,監控服務進程:

 
  1. check process tomcat with pidfile /var/run/catalina.pid # 進程pid

  2. start program = "/etc/init.d/tomcat start" # 設置啓動命令

  3. stop program = "/etc/init.d/tomcat stop" # 設置中止命令

  4. if 9 restarts within 10 cycles then timeout # 設置在10個監視週期內重,啓了9次則超時,再也不監視這個服務。緣由另外說明【3】

  5. if cpu usage > 90% for 5 cycles then alert # 若是在5個週期內該服務的cpu使用率都超過90%則提示

  6. if failed url http://127.0.0.1:4000/ timeout 120 seconds for 5 cycles then restart # 若連續5個週期打開url都失敗(120秒超時,超時也認爲失敗)則重啓服務

設置超時後再也不監視是爲了讓服務不要一直重啓,若是連續重啓屢次不成功,極有可能再重啓下去也不會成功的。而且tomcat的重啓須要佔用大量系統資源,假如一直重啓下去,反而會使其它服務也沒法正常運做。

三、能夠對moint自己服務器進行監控:

 
  1. # 系統名稱,能夠是IP或域名

  2. check system www.example.com

  3. if loadavg (1min) > 4 then alert

  4. if loadavg (5min) > 2 then alert

  5. if memory usage > 75% then alert

  6. if cpu usage (user) > 70% then alert

  7. if cpu usage (system) > 30% then alert

  8. if cpu usage (wait) > 20% then alert

四、實例:

 
  1. #

  2. # 監控nginx

  3. #

  4. # 須要提供進程pid文件信息

  5. check process nginx with pidfile /var/run/nginx.pid

  6. # 進程啓動命令行,注:必須是命令全路徑

  7. start program = "/etc/init.d/nginx start"

  8. # 進程關閉命令行

  9. stop program = "/etc/init.d/nginx stop"

  10. # nginx進程狀態測試,監測到nginx連不上了,則自動重啓

  11. if failed host www.example.com port 80 protocol http then restart

  12. # 屢次重啓失敗將再也不嘗試重啓,這種就是系統出現嚴重錯誤的狀況

  13. if 3 restarts within 5 cycles then timeout

  14. # 可選,設置分組信息

  15. group server

  16.  
  17. # 可選的ssl端口的監控,若是有的話

  18. # if failed port 443 type tcpssl protocol http

  19. # with timeout 15 seconds

  20. # then restart

  21.  
  22. #

  23. # 監控apache

  24. #

  25. check process apache with pidfile /var/run/apache2.pid

  26. start program = "/etc/init.d/apache2 start"

  27. stop program = "/etc/init.d/apache2 stop"

  28. # apache吃cpu和內存比較厲害,額外添加一些關於這方面的監控設置

  29. if cpu > 50% for 2 cycles then alert

  30. if cpu > 70% for 5 cycles then restart

  31. if totalmem > 1500 MB for 10 cycles then restart

  32. if children > 250 then restart

  33. if loadavg(5min) greater than 10 for 20 cycles then stop

  34. if failed host www.example.com port 8080 protocol http then restart

  35. if 3 restarts within 5 cycles then timeout

  36. group server

  37. # 可選,依賴於nginx

  38. depends on nginx

  39.  
  40. #

  41. # 監控spawn-fcgi進程(其實就是fast-cgi進程)

  42. #

  43. check process spawn-fcgi with pidfile /var/run/spawn-fcgi.pid

  44. # spawn-fcgi必定要帶-P參數纔會生成pid文件,默認是沒有的

  45. 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"

  46. stop program = "/usr/bin/killall /usr/bin/php-cgi"

  47. # fast-cgi走的不是http協議,monit的protocol參數也沒有cgi對應的設置,這裏去掉protocol http便可。

  48. if failed host 127.0.0.1 port 8081 then restart

  49. if 3 restarts within 5 cycles then timeout

  50. group server

  51. depends on nginx

注意:

  1. start和stop的program參數裏的命令必須是全路徑,不然monit不能正常啓動,好比killall應該是/usr/bin/killall。
  2. 對於spawn-fcgi,不少人會用它來管理PHP的fast-cgi進程,但spawn-fcgi自己也是有可能掛掉的,因此仍是須要用monit來監控spawn-fcgi。spawn-fcgi必須帶-P參數纔會有pid文件,並且fast-cgi走的不是http協議,monit的protocol參數也沒有cgi對應的設置,必定要去掉protocol http這項設置才管用。
  3. 進程屢次重啓失敗monit將再也不嘗試重啓,收到這樣的通知郵件代表系統出現了嚴重的問題,要引發足夠的重視,須要趕忙人工處理。
  4. 固然monit除了管理進程以外,還能夠監控文件、目錄、設備等,本文不作討論,具體配置方式能夠去參考monit的官方文檔

參考:

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

相關文章
相關標籤/搜索