標籤:開源軟體 監控 nagios
linux下搭建nagios監控
一、什麼是nagios
1.nagios 簡介
Nagios是一個監視系統運行狀態和網路資訊的監視系統。
Nagios能監視所指定的本地或遠程主機以及服務,同時提供異常通知功能等。
Nagios可運行在Linux/Unix平台之上,同時提供一個可選的基於瀏覽器的WEB介面以方便系統管理人員查看網路狀態,
各種系統問題,以及日誌等等。
Nagios是十分受歡迎的、開源且免費的電腦及網路系統監視軟體。
Nagios是“Nagios Ain‘t Gonna Insist On Sainthood”的縮寫。
它最早是在1999年以“NetSaint”發布。Nagios主要應用在Linux和Unix平台環境下的監控,
但通過外掛程式,也可以監控MS Windows系統的主機。Nagios在LinuxCon 2010 Poll並選為最受歡迎的IT營運工具。
它被Infoworld在2009年評為最佳開源軟體,同時也是該年度的SourceForge社區的系統管理工具最佳選擇。
Nagios也被很多知名企業所採用,包括AOL,DHL,At&t,歐萊雅,德州儀器,Siemens COM CZ,時代華納有線,Yahoo等。
2.Nagios的主要功能特點:
- 監視網路服務 (SMTP, POP3, HTTP, NNTP, PING等)
- 監視主機資源 (進程, 磁碟等)
- 簡單的外掛程式設計可以輕鬆擴充Nagios的監視功能
- 服務等監視的並發處理
- 錯誤通知功能 (通過email, pager, 或其他使用者自訂方法)
- 可指定自訂的事件處理控制器
- 可選的基於瀏覽器的WEB介面以方便系統管理人員查看網路狀態,各種系統問題,以及日誌等等
- 可以通過手機查看系統監控資訊
二、nagios 監控環境搭建
1.搭建環境介紹:
hostnameIP系統
服務端webserver192.168.1.20CentOS 6.6
用戶端hpf-linux192.168.1.110CentOS 6.6
2.基礎服務端安裝:
[[email protected] ~]# yum install -y epel-release //若機器安裝的有epel擴充源則省略此步驟[[email protected] ~]# yum install -y httpd nagios nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe //安裝nagios相關包[[email protected] ~]# htpasswd -c /etc/nagios/passwd nagiosadmin //產生登入nagios背景帳號及密碼New password: Re-type new password: Adding password for user nagiosadmin[[email protected] ~]# nagios -v /etc/nagios/nagios.cfg //檢測nagios設定檔是否錯誤Total Warnings: 0Total Errors: 0Things look okay - No serious problems were detected during the pre-flight check
啟動服務端nagios服務及監控的服務:
[[email protected] ~]# /etc/init.d/httpd start[[email protected] ~]# /etc/init.d/nagios start
登入瀏覽器輸入 http://ip/nagios ,查看服務是nagios是否搭建成功
通過輸入剛才產生的密碼進入nagios後台管理;
點擊serviers查看監控,根據監控的服務是否正常進行調試;
剛開始http服務能會有warning,錯誤提示為HTTP WARNING: HTTP/1.1 403 Forbidden - 5152 bytes in 0.001 second response t;
原因為:nagios監控HTTP時,會監控到/var/www/html/下面的index.html檔案,若沒有就會提示錯誤,
建立一個檔案即可!建立後等一會便會將監控狀態變為OK;
3.增加服務端nagios 監控機(增加監控用戶端)
用戶端安裝nagios監控服務及檔案配置:
[[email protected] ~]# yum install -y epel-release //若用戶端已安裝epel擴充源則省略此步驟[[email protected] ~]# yum install -y nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe //安裝nagios監控相關包[[email protected] ~]# vi /etc/nagios/nrpe.cfg 找到“allowed_hosts=127.0.0.1” 改為 “allowed_hosts=127.0.0.1,192.168.1.20” 後面的ip為服務端ip; 找到” dont_blame_nrpe=0” 改為 “dont_blame_nrpe=1” 下面兩個設定檔的更改是根據nagios服務端添加的監控服務(check_hda1)變更的:command[check_sda1]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/sda1command[check_sda2]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/sda2
注意:監控端和被監控端的command選項都需要增加 command[check_sda]的選項,
並且在重啟nrpe和Nagios 後,需要等一段時間,nagios的web頁面才會把原先 check disk標記
為critical的選項給回複成正常。
佈建服務端nagios指令檔:
[[email protected] conf.d]# vi /etc/nagios/objects/commands.cfg //在該設定檔下添加以下內容define command{ command_name check_nrpe command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ }[[email protected] ~]# cd /etc/nagios/conf.d/[[email protected] conf.d]# vi 192.168.1.110.cfg define host{ use linux-server host_name 192.168.1.110 alias 1.110 address 192.168.1.110 } define service{ use generic-service host_name 192.168.1.110 service_description check_ping check_command check_ping!100.0,20%!200.0,50% max_check_attempts 5 normal_check_interval 1 } define service{ use generic-service host_name 192.168.1.110 service_description check_ssh check_command check_ssh max_check_attempts 5 #當nagios檢測到問題時,一共嘗試檢測5次都有問題才會警示,如果該數值為1,那麼檢測到問題立即警示 normal_check_interval 1 #重新檢測的時間間隔,單位是分鐘,預設是3分鐘 notification_interval 60 #在服務出現異常後,故障一直沒有解決,nagios再次對使用者發出通知的時間。單位是分鐘。如果你認為,所有的事件只需要一次通知就夠了,可以把這裡的選項設為0。 }define service{ use generic-service host_name 192.168.1.110 service_description check_http check_command check_http max_check_attempts 5 normal_check_interval 1 }define service{ use generic-service host_name 192.168.1.110 service_description check_load check_command check_nrpe!check_load max_check_attempts 5 normal_check_interval 1}define service{ use generic-service host_name 192.168.1.110 service_description check_disk_sda1 check_command check_nrpe!check_sda1 max_check_attempts 5 normal_check_interval 1}define service{ use generic-service host_name 192.168.1.110 service_description check_disk_sda2 check_command check_nrpe!check_sda2 max_check_attempts 5 normal_check_interval 1}[[email protected] ~]# nagios -v /etc/nagios/nagios.cfg //檢測設定檔是否正確Total Warnings: 0Total Errors: 0Things look okay - No serious problems were detected during the pre-flight check
在用戶端啟動nrpe服務:
[[email protected] ~]# /etc/init.d/nrpe start
在服務端上重啟nagios服務:
[[email protected] ~]# /etc/init.d/nagios restart
在瀏覽器上查看nagios服務的監控是否正常顯示:
4.配置郵件警示:
[[email protected] ~]# vim /etc/nagios/objects/contacts.cfgdefine contact{ contact_name nagios1 use generic-contact alias mail1 email [email protected] }define contact{ contact_name nagios2 use generic-contact alias mail2 email [email protected] }define contactgroup{ contactgroup_name common alias common members nagios1,nagios2 }[[email protected] conf.d]# vi 192.168.1.110.cfg 在上面的192.168.1.110.cfg 設定檔有如下一段內容:define service{ use generic-service host_name 192.168.1.110 service_description check_load check_command check_nrpe!check_load max_check_attempts 5 normal_check_interval 1}將上面一段配置的最後添加以下四個語句: contact_groups common notifications_enabled 1 #是否開啟提醒功能。1為開啟,0為禁用。一般,這個選項會在主設定檔(nagios.cfg)中定義,效果相同。 notification_period 24x7 #發送提醒的時間段。非常重要的主機(服務)我定義為7×24,一般的主機(服務)就定義為上班時間。 #如果不在定義的時間段內,無論什麼問題發生,都不會發送提醒。 notification_options w,u,c,r #這個是service的狀態。w為waning, u為unknown, c為critical, r為recover(恢複了), #類似的還有一個 host對應的狀態:d,u,r d = 狀態為DOWN, u = 狀態為UNREACHABLE , r = 狀態恢複為OK, #需要加入到host的定義配置裡。
[[email protected] ~]# nagios -v /etc/nagios/nagios.cfg //檢測設定檔是否錯誤Total Warnings: 0Total Errors: 0Things look okay - No serious problems were detected during the pre-flight check
5、驗證警示郵件配置是否成功:
開啟虛擬機器郵件服務
[[email protected] ~]# yum install -y sendmail //安裝郵件服務包[[email protected] ~]# /etc/init.d/sendmail start //啟動郵件服務[[email protected] ~]# netstat -lnp |grep sendmail //查看郵件服務開啟的連接埠tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1011/sendmail
在瀏覽器上配置163郵箱的白名單以防止把警示郵件當成垃圾郵件:
wKioL1WCiOWTxQqrAAUfz-fMY94418.jpg
[[email protected] ~]# /etc/init.d/nrpe stop //在用戶端關閉nrpe服務看服務端是否發送警示郵件;Shutting down nrpe: [確定]
警示郵件發送時間會有一段時間的延時,需要耐心等待;
本文出自 “清晰明了” 部落格,請務必保留此出處http://duanyexuanmu.blog.51cto.com/1010786/1750019
centos 搭建 nagios 監控系統.