標籤:
最近一段時間在研究Nagios監控系統,然後網上搜尋查看很多的資料,借鑒與各位大蝦的經驗,整理記錄一下自己安裝記錄。
Nagios 全名為(Nagios Ain’t Goona Insist on Saintood),最初項目名字是 NetSaint。它是一款免費的開源 IT 基礎設施監控系統,其功能強大,靈活性強,能有效監控 Windows 、Linux、VMware 和 Unix 主機狀態,交換器、路由器等網路設定等。一旦主機或服務狀態出現異常時,會發出郵件或簡訊警示第一時間通知 IT 運營人員,在狀態恢複後發出正常的郵件或簡訊通知。Nagios 結構簡單,可維護性強,越來越受中小企業青睞,以及營運和管理員的使用。同時提供一個可選的基於瀏覽器的 Web 介面,方便管理員查看系統的運行狀態,網路狀態、服務狀態、日誌資訊,以及其他異常現象.
Nagios 結構說明
Nagios 結構上來說, 可分為核心和外掛程式兩個部分。Nagios 的核心部分只提供了很少的監控功能,因此要搭建一個完善的 IT 監控管理系統,使用者還需要在 Nagios 伺服器安裝相應的外掛程式,外掛程式可以從 Nagios 官方網站下載 http://www.nagios.org/,也可以根據實際要求自己編寫所需的外掛程式。
Nagios 可實現的功能特性
- 監控網路服務(SMTP、POP3、HTTP、FTP、PING 等);
- 監控本機及遠程主機資源(CPU 負荷、磁碟利用率、進程 等);
- 允許使用者編寫自己的外掛程式來監控特定的服務,方便地擴充自己服務的檢測方法,支援多種開發語言(Shell、Perl、Python、PHP 等)
- 具備定義網路分層結構的能力,用"parent"主機定義來表達網路主機間的關係,這種關係可被用來發現和明晰主機宕機或不可達狀態;
- 當服務或主機問題產生與解決時將警示發送給連絡人(通過 EMail、簡訊、使用者定義方式);
- 可以支援並實現對主機的冗餘監控;
- 可用 WEB 介面用於查看當前的網路狀態、通知和故障曆史、記錄檔等;
Nagios 監控實現原理
Nagios 軟體需安裝在一台獨立的伺服器上運行,這台伺服器稱為監控中心,監控中心伺服器可以採用 Linux 或 Unix 作業系統;每一台被監視的硬體主機或服務都運行一個與監控中心伺服器進行通訊的 Nagios 軟體背景程式,也可以理解為 Agent 或外掛程式均可。監控中心伺服器讀取設定檔中的指令與遠端精靈進行通訊,並且指示遠端精靈進行必要的檢查。雖然 Nagios 軟體必須在 Linux 或 Unix 作業系統上運行,但是遠程被監控的機器可以是任何能夠與其進行通訊的主機,根據遠程主機返回的應答,Naigos 將依據配置進行回應;接著 Nagios 將通過本地的機器進行測試,如果檢測傳回值不正確,Nagios 將通過一種或多種方式警示;具體原理如所示:
圖 1. Nagios 監控原理圖
Nagios 安裝
# wget ftp://rpmfind.net/linux/dag/redhat/el7/en/x86_64/dag/RPMS/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm# rpm --import http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt# rpm -Uvh rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
#yum install gd libjpeg-devel gd-devel perl* openssl-devel gcc gcc-c++ httpd php
[[email protected] ~]# groupadd -g 6000 nagios[[email protected] ~]# groupadd -g 6001 nagcmd[[email protected] ~]# useradd -u 6000 -g nagios -G nagcmd -d /home/nagios nagios
[[email protected] opt]# mkdir /usr/local/nagios[[email protected] opt]# tar zxvf nagios-4.0.8.tar.gz[[email protected] opt]# cd nagios-4.0.8[[email protected] nagios-4.0.8]# ./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios --with-command-user=nagios --with-command-group=nagcmd
[[email protected] nagios-4.0.8]# make all[[email protected] nagios-4.0.8]# make install[[email protected] nagios-4.0.8]# make install-init[[email protected] nagios-4.0.8]# make install-commandmode[[email protected] nagios-4.0.8]# make install-config[[email protected] nagios-4.0.8]# make install-webconf
[[email protected] nagios-4.0.8]# vim /etc/httpd/conf/httpd.conf
DirectoryIndex index.html index.html.var改為:DirectoryIndex index.html index.php
再添加一行:AddType application/x-httpd-php .php
以上兩處主要用於增加 php 格式的支援。
同時為了安全,需要經過授權才能訪問 Nagios 的 Web 監控介面,所以需要在設定檔/etc/httpd/conf/httpd.conf 或 /etc/httpd/conf.d/nagios.conf 增加存取控制配置,若定義在 httpd.conf 檔案中,將的語句加入到 httpd.conf 檔案最後面即可.
ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin<Directory "/usr/local/nagios/sbin"> Options ExecCGI AllowOverride None Order allow,deny Allow from all AuthName "Nagios Access" AuthType BasicAuthUserFile /usr/local/nagios/etc/htpasswd.user//用於此目錄訪問身分識別驗證的檔案 Require valid-user</Directory>Alias /nagios /usr/local/nagios/share<Directory "/usr/local/nagios/share"> Options None AllowOverride None Order allow,deny Allow from all AuthName "Nagios Access" AuthType BasicAuthUserFile /usr/local/nagios/etc/htpasswd.user//用於此目錄訪問身分識別驗證的檔案 Require valid-user</Directory>
[[email protected] nagios-4.0.8]# htpasswd -c /usr/local/nagios/etc/htpasswd.user nagiosadmin //Enter之後,輸入設定一個密碼
[[email protected] ~]# chown nagios.nagcmd /usr/local/nagios/etc/htpasswd.user[[email protected] ~]# usermod -a -G nagios,nagcmd apache[[email protected] ~]# /etc/init.d/httpd restart
Naigos 提供的各種監控功能基本上是通過外掛程式來完成的,而 Nagios 的核心指提供了很少的功能,因此安裝外掛程式是非常有必要的。Nagios 官網提供了很多不同版塊的外掛程式應用,同時外掛程式版本與 Nagios 版本也沒有什麼關聯,如果支援漢化功能,則需要找到與之匹配的漢化軟體包,否則會導致部分功能不能完成運行,這裡保持預設英文,如下面的安裝細節:
[[email protected] opt]# tar zxvf nagios-plugins-2.0.3.tar.gz[[email protected] opt]# cd nagios-plugins-2.0.3[[email protected] nagios-plugins-2.0.3]# ./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios --with-command-user=nagios --with-command-group=nagcmd[[email protected] nagios-plugins-2.0.3]# make all[[email protected] nagios-plugins-2.0.3]# make install
[[email protected] nagios-plugins-2.0.3]# chmod 755 /usr/local/nagios
這樣 Nagios 的外掛程式就安裝完成了,通過查看 Nagios 主程式目錄,/usr/local/nagios/linexec 目錄下可以看到很多的外部外掛程式執行檔案,如:
[[email protected] ~]# /etc/init.d/httpd restart[[email protected] ~]# /etc/init.d/nagios restart[[email protected] ~]# chkconfig nagios on[[email protected] ~]# chkconfig httpd on
[[email protected] ~]# setenforce 0 //臨時關閉//或者修改/etc/selinux/config檔案,將 SELINUX=enforcing 改為 SELINUX=disabled 重啟機器OK
[[email protected] ~]# /etc/init.d/iptables stop //關閉iptables或[[email protected] ~]# service iptables stop[[email protected] ~]# chkconfig iptables off //永久關閉,重啟生效.
通過瀏覽器,在地址欄輸入: http://IPAddress/nagios, 輸入使用者名稱及密碼即可訪問 Naigos 登入介面。
Nagios 監控系統架設全攻略(一)