一,Nagios監控簡介
- 生活中大家應該對監控已司空見慣了,例如:餐館門前的監控探頭,小區里的視頻監控,城市道路告訴監控探頭等,這些監控的目的大家都很清楚,無須多說。那么,企業工作中為什么要部署監控系統呢?
- 我們都知道軍隊里,哨兵的角色很重要,我們在殺敵前,基本都要先把敵人站崗的哨兵給解決了,這樣敵人就相當於眼睛瞎了,耳朵聾了,然后再進攻就能輕松搞定這些敵人。在互聯網企業大型網站架構里,服務器,業務系統相當多,可達上萬甚至十萬級別,而且還很復雜,如果沒有監控系統這個“哨兵”,網絡業務出現什么問題,我們就很難知曉,因此,大型網站中監控系統的重要性就不言而喻了。
監控系統需要監控的數據有哪些呢?
- 系統本地資源:負載(uptime),CPU(top,sar),磁盤(df -hi),內存(free),I/O(iostat),Raid內磁盤故障,CPU溫度,passwd文件的變化,本地所有文件改動。
- 網絡服務:端口,Web(URL),DB,ping包,進程,IDC帶寬網絡流量。
- 其他設備:路由器,交換機(端口,光衰,日志),打印機,windows等。
- 業務數據:用戶登錄失敗次數,用戶登陸網站次數,輸入驗證碼失敗的次數,某個API接口流量並發,網絡連接數,IP,PV數,電商網站訂單,支付交易的數量等。
如何獲取到這么多的數據呢?
Nagios監控軟件本身僅僅是一個監控平台,理論上想監控的具體內容只要在服務器中執行命令行就可以獲取,並納入Nagios監控體系里。
二,Nagios監控工具及原理介紹
2.1 Nagios介紹
- Nagios是一款開源的網絡及服務的監控工具,其功能強大,靈活性強。能有效監控Windows,Linux和UNIX等系統的主機各種狀態信息,以及交換機,路由器等網絡設備和主機端口及URL服務等,它會根據不同業務故障級別發出告警信息(郵件,微信,短信,語音報警,飛信),當故障恢復時也會發出對應的恢復消息給管理員。
- Nagios服務器端可以在Linux系統和類UNIX系統上運行,但目前無法在Windows上運行。Windows可以作為被監控的主機運行Nagios客戶端軟件。
- Nagios監控軟件本身僅僅是一個監控平台,我們想監控的具體內容,理論上只要通過服務器命令就可以獲取並納入Nagios監控體系里,所以,可以說Nagios強大到了無所不能的地步。
- Nagios官方網站地址為http://www.nagios.org/.
2.2 Nagios的特點
Nagios可以支持非常多的功能特性,這里把一些常見的功能特性簡單介紹如下:
- 監控網絡服務(HTTP,TCP,PING,SMTP,POP3等)
- 監控主機資源(CPU,負載,I/O狀況,虛擬及正式內存及磁盤利用率等)
- 簡單的插件設計模式使得用戶可以很方便地定制符合自己服務的檢測方法
- 並行服務檢查機制
- 具備定義網絡分層結構的能力,用“parent”主機定義來表達網絡主機間的關系,這種關系可被用來發現和明晰主機宕機或不可達的狀態。
- 當服務或主機問題產生與解決后將及時通報聯系人(mail/im/sms/sound/語音)
- 具備定義事件句柄功能,它可以在主機或服務的事件發生時獲取更多問題定位
- 自動的日志回滾
- 可以支持並實現對主機的冗余監控(支持分布式監控)
- 可選的Web界面用於查看當前的網絡狀態,通知和故障歷史,日志文件等。
2.3 Nagios監控系統家族成員的構成
- Nagios監控一般由一個主程序(Nagios),一個插件程序(Nagios-plugins)和一些可選的附加程序(NRPE,NSClient++,NSCA和NDOUtils)等組成。
- Nagios本身只是一個監控的平台而已,其具體的監控工作都是通過各類插件(例如:Nagios-plugins)來實現的,也可以自己編寫插件。因此,Nagios主程序和Nagios-plugins插件都是Nagios服務器端必須要安裝的程序組件,不過,一般Nagios-plugins也要安裝於被監控端,用來獲取相應的數據。Nagios可選的附加組件描述如下:
(1)NRPE組件(重點)
- 存在的位置:工作於被監控端,操作系統為Linux/UNIX系統
- 作用:用於在被監控的遠程Linux/UNIX主機上執行腳本插件,獲取數據回傳給服務器端,以實現對這些主機資源和服務的監控。
- 存在形式:守護進程(agent)模式,開啟的端口為5666
- 監控的資源:主要用於監控本地資源,包括負載(uptime),CPU(top,sar),磁盤(df -hi),內存(free),I/O(iostat),Raid內磁盤故障,CPU溫度,passwd文件的變化,以及本地所有文件的指紋識別監控,當然也可以監控進程,端口,URL等。
下圖為NRPE組件的運行原理圖:
- 工作原理:通常由Nagios服務器端發起獲取數據請求,由check_nrpe插件攜帶要獲取的命令,傳給被監控端的nrpe守護進程(默認5666端口),nrpe進程讀取nrpe.cfg里對應服務器端發送的命令信息,調用本地插件獲取數據,然后返回給Naios服務器端check_nrpe,進而傳給Nagios展示到Web界面中,嚴格講可以稱之為半被動工作模式,本文主要講解這個NRPE組件的功能,后文將稱其為被動模式。
(2)NSClient++組件(僅了解)
NSClient++組件用於被監控端為Windows系統的服務器。
- 作用:相當於Linux下的nrpe,監控安裝在Windows主機上的agent組件。
- 監控的資源:主要監控Windows系統的本地資源,比如CPU,磁盤,內存,I/O等。
- 工作形式:通常由服務器端發起獲取數據請求,由check_nt傳給被監控端的NSClient++進程,獲取數據后返回給服務器端。
下圖為NSClient++運行原理圖:
- 工作原理:NSClient++的工作原理和NRPE基本相同,只不過適合於Windows被監控端的監控,且服務器端的插件為check_nt,客戶端的進程為NSClient++。通常由服務器端發起獲取數據請求,由check_nt傳給被監控端的NSClient++進程,獲取數據后返回給服務器端,本節內容不會涉及Windows被監控端,感興趣的同學們自行百度研究解決。
(3)NDOUtils組件(不推薦用)
NDOUtils組件工作於Nagios服務器端
- 作用:將Nagios的配置信息和各event產生的數據存入數據庫,以實現對這些數據的檢索和處理,對於中小企業,不推薦使用NDOUtils,直接使用文件記錄數據就很好。
下圖為NDOUtils的運行原理簡圖:
(4)NSCA組件(僅了解)
- 相對於nrpe,這個可以說是純被動模式的監控組件,目前被應用的場景較少,這里只是作為知識點進行介紹,對於中小企業,並不推薦使用。
NSCA需要同時安裝在Nagios的服務器端和客戶端(被監控端)
- 作用:讓被監控的遠程Linux/UNIX主機主動將監控到的信息發送給Nagios服務器,可以用在大規模分布式監控集群模式中,中小企業無需使用。
分布式監控NSCA外部構件簡介:
為完成從遠程主機主動提交強制檢測結果,於是就開發了NSCA外部構件。該外部構件包括兩部分,第一部分是客戶端程序(send_nsca),運行於遠程主機上,並負責將強制檢測結果發送到指定的Nagios服務器端,另一部分是NSCA守護進程(nsca),它既可以作為守護進程獨立運行,也可以注冊到inetd里作為一個inetd客戶程序來提供監聽。從客戶端收到服務檢測結果信息之后,守護進程將結果提交給在中心服務器的Nagios,方式是在外部命令文件里插入一條PROCESS_SVC_CHECK_RESULT命令,之后跟上檢測結果。在Nagios服務器端下一次處理外部命令時將會找到這條由分布式服務器送來的強制監測信息並處理它。
下圖為NSCA分布式監控運行原理簡圖:
2.4 Nagios監控系統完整圖解
下圖為Nagios監控工具綜合系統簡單原理圖。該系統在中小企業用不到,也沒必要搞得這么復雜,這里會對將要講解的Nagios系統做了一個剪裁,只講解企業最常見的功能應用,對一般的企業已經足夠了。
運維思想:化繁為簡是優秀的運維工程師必須掌握的重要技術思想。
下圖是接下來要詳細講解的Nagios監控工具實際原理簡圖。這張原理圖符合生產運維的原則:簡單,易用,高效。
三,Nagios服務器端安裝
3.1 Nagios安裝准備
(1)准備3台服務器或VM虛擬機
下表為Nagios服務器及客戶端服務器列表
管理IP地址 | 角色 | 備注 |
---|---|---|
192.168.0.200 | nagios-server | Nagios服務器端 |
192.168.0.223 | web01 | 被監控的客戶端服務器 |
192.168.0.224 | web02 | 被監控的客戶端服務器 |
(2)設置yum公網安裝源
- 默認情況執行yum會從國外的站點下載,速度慢。因此要換成國內的提供yum源的站點,這樣安裝軟件時更快。yum是一個非常方便的RPM軟件包安裝命令,一般安裝基礎的軟件都會用到。命令語法為:yum install軟件包名或關鍵字-y。
#阿里雲yum源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
(3)解決Perl軟件編譯問題
在安裝好Nagios監控服務后,還要安裝Perl插件程序,因此要提前設置相關環境變量,批量執行命令如下:
[root@Nagios ~]# echo 'export LC_ALL=C' >> /etc/profile
[root@Nagios ~]# tail -1 /etc/profile
export LC_ALL=C
[root@Nagios ~]# source /etc/profile
[root@Nagios ~]# echo $LC_ALL
C
(4)關閉Nagios server端防火牆及SELinux
在測試環境下為了調試方便,最好關掉iptalbes防火牆及SELinux,如果是生產環境中,因為有外部IP,所以在調試完畢后需要開啟防火牆。一般允許服務通過的方法是整個局域網IP段都通過,SELinux是一個可關可開的軟件,企業可以根據需求選擇,大部分企業還是會選擇關閉SELinux,使用其他保護措施,因此,這里也關閉SELinux。
(5)解決系統時間同步問題
如果不解決服務器的時間同步問題,很可能會導致Nagios整個服務配置異常甚至失敗,這個是很多同學初學時不太注意的地方,Nagios一度被稱為“難夠死”,意思是太難配置了,不過只要同學們認真聽,認真學,相信你會感覺很輕松。
[root@Nagios ~]# which ntpdate
/usr/sbin/ntpdate
[root@Nagios ~]# /usr/sbin/ntpdate pool.ntp.org
12 Aug 11:29:43 ntpdate[1434]: step time server 51.174.131.248 offset -28704.196738 sec
#讓Nagios server的系統時間和當前的標准時間保持一致。虛擬機要能上網才行
[root@Nagios ~]# date +%F
2017-08-12
[root@Nagios ~]# echo '#time sync by Mr.chen at 2017-08-12' >> /var/spool/cron/root
[root@Nagios ~]# echo '*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1' >> /var/spool/cron/root
[root@Nagios ~]# crontab -l
#time sync by Mr.chen at 2017-08-12
*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1
#<==執行命令將同步時間加入定時任務隊列。
(6)安裝Nagios服務器端所需軟件包
- Nagios服務器端需要有Web界面展示監控效果,界面的展示主要使用PHP程序,因此,需要LAMP環境
- 特別強調:有些網友總想安裝LNMP環境,這完全自找麻煩,yum安裝的LAMP環境是配合Nagios服務器端展示界面的最佳環境。
[root@Nagios ~]# yum -y install gcc glibc glibc-common
#編譯軟件升級
[root@Nagios ~]# yum -y install gd gd-devel
#用於后面PHP出圖的包
[root@Nagios ~]# yum -y install mysql-server
#非必須,如果有監控數據庫,那么需要先安裝MySQL,否則,MySQL的相關插件不會被安裝
yum -y install httpd php php-gd
#Apache,PHP環境
[root@Nagios ~]# rpm -qa mysql httpd php
httpd-2.2.15-60.el6.centos.4.x86_64
mysql-5.1.73-8.el6_8.x86_64
php-5.3.3-49.el6.x86_64
#檢查LAMP環境的版本
提示:
1)通過yum工具安裝上述所有軟件包,且這些環境一般不需要在Nagios客戶端安裝
2)上述軟件包裝好后的版本為:Apache2.2.15,PHP5.3.3,MySQL5.1.73
(7)創建Nagios服務器端需要的用戶及組
批量執行命令如下:
[root@Nagios ~]# useradd nagios #創建用戶(需要加目錄,否則,啟動Nagios會提醒沒家目錄)
[root@Nagios ~]# groupadd nagcmd #添加用戶組
[root@Nagios ~]# usermod -a -G nagcmd nagios #將nagios加入組
[root@Nagios ~]# usermod -a -G nagcmd apache #將apache加入組
[root@Nagios ~]# id -n -G nagios #查看用戶及用戶組
nagios nagcmd
[root@Nagios ~]# id -n -G apache #查看用戶及用戶組
apache nagcmd
(8)上傳軟件包到指定目錄或通過URL下載
相關軟件包可以去下載。
地址:http://sourceforge.net/projects/nagios/files/
http://www.nagios-plugins.org/download/nagios-plugins-1.4.16.tar.gz
為了方便同學們學習安裝Nagios,已經收集好了相關軟件包,內容如下:
[root@Nagios ~]# tree nagios/
nagios/
|-- Class-Accessor-0.31.tar.gz #iostat插件需要
|-- Config-Tiny-2.12.tar.gz #iostat插件需要
|-- Math-Calc-Units-1.07.tar.gz #iostat插件需要
|-- Nagios-Plugin-0.34.tar.gz #iostat插件需要
|-- Params-Validate-0.91.tar.gz #iostat插件需要
|-- Regexp-Common-2010010201.tar.gz #iostat插件需要
|-- check_iostat #這就是上面提到iostat插件程序
|-- check_memory.pl #檢測內存的插件程序
|-- check_mysql #檢測MySQL插件程序
|-- libart_lgpl-2.3.17.tar.gz #繪圖相關依賴庫
|-- nagios-3.5.1.tar.gz #Nagios主程序
|-- nagios-plugins-1.4.16.tar.gz #Nagios插件包
|-- nrpe-2.12.tar.gz #客戶端的守護進程,agent
|-- pnp-0.4.14.tar.gz #繪圖的Web界面軟件
|-- rrdtool-1.2.14.tar.gz #實際繪圖軟件,PNP調用
0 directories, 15 files
現在啟動LAMP環境的HTTP服務:
[root@Nagios ~]# /etc/init.d/httpd start
Starting httpd: httpd: apr_sockaddr_info_get() failed for Nagios
#可忽略或者看后文解決方案
httpd: Could not reliably determine the servers fully qualified domain name, using 127.0.0.1 for ServerName
#可忽略或者看后文解決方案
[ OK ]
[root@Nagios ~]# netstat -antup | grep 80
tcp 0 0 :::80 :::* LISTEN 1780/httpd
上面的結果表明Nagios服務器端的LAMP環境是正常的。下面針對HTTP啟動時的提示進行說明
提示一:“Starting httpd: httpd: apr_sockaddr_info_get() failed for Nagios”,表示這是hosts解析問題,在/etc/hosts中配好主機名和IP的解析就好了,配置結果如下:
[root@Nagios ~]# echo "`hostname -I` nagios-server" >> /etc/hosts
[root@Nagios ~]# tail -1 /etc/hosts
192.168.0.200 nagios-server
提示二:“httpd: Could not reliably determine the servers fully qualified domain name, using 127.0.0.1 for ServerName”,這表示httpd.conf中缺少ServerName配置,可以在/etc/httpd/conf/httpd.conf中加入ServerName 127.0.0.1:80
[root@Nagios ~]# echo "ServerName 127.0.0.1:80" >> /etc/httpd/conf/httpd.conf
修改好了以后,我們再次重啟httpd服務,如下:
[root@Nagios ~]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
3.2 安裝Nagios服務器端
官方安裝文檔:http://nagios.sourceforge.net/docs/3_0/quickstart-fedora.html
[root@Nagios ~]# ls
anaconda-ks.cfg install.log install.log.syslog nagios
[root@Nagios ~]# cd nagios/
[root@Nagios nagios]# ls -l nagios-3.5.1.tar.gz
-rw-r--r--. 1 root root 1763584 Aug 12 12:22 nagios-3.5.1.tar.gz
[root@Nagios nagios]# tar xf nagios-3.5.1.tar.gz -C /usr/src/
[root@Nagios nagios]# cd /usr/src/nagios/
[root@Nagios nagios]# ./configure --with-command-group=nagcmd
#省略開頭,出現以下兩句表示配置正確
#Review the options above for accuracy. If they look okay,
#type 'make all' to compile the main program and CGIs.
[root@Nagios nagios]# make all
#省略開頭,出現下面表示正確
#Enjoy.
[root@Nagios nagios]# make install
#省略開頭,出現下面的話,表示正確結尾
#You can continue with installing Nagios as follows (type 'make'
#without any arguments for a list of all possible options):
# make install-init
# - This installs the init script in /etc/rc.d/init.d
# make install-commandmode
# - This installs and configures permissions on the
# directory for holding the external command file
# make install-config
# - This installs sample config files in /usr/local/nagios/etc
#make[1]: Leaving directory `/usr/src/nagios'
提示:
如果是編譯安裝httpd,可以執行./configure --with-command-group=nagcmd --with-httpd-conf=/usr/local/apache/conf/extra 增加一個編譯參數,即指定編譯Nagios Web配置的生成路徑
下面根據make install 命令執行后的提示繼續操作
[root@Nagios nagios]# make install-init #安裝初始化腳本到/etc/rc.d/init.d
[root@Nagios nagios]# make install-config #生成Nagios模板配置文件到/usr/local/nagios/etc
[root@Nagios nagios]# make install-commandmode #安裝配置目錄許可外部命令文件
(1)安裝Nagios Web配置文件及創建登錄用戶
接下來安裝Nagios Web配置文件(生成Nagios對應於Apache里的配置文件)。
[root@Nagios nagios]# make install-webconf #生成/etc/httpd/conf.d/nagios.conf配置文件
/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/httpd/conf.d/nagios.conf
*** Nagios/Apache conf file installed ***
創建Nagios Web監控界面后,登入時會需要用戶名及密碼,這里分別為yunjisuan和123123.
[root@Nagios nagios]# htpasswd -bc /usr/local/nagios/etc/htpasswd.users yunjisuan 123123
Adding password for user yunjisuan
[root@Nagios nagios]# cat /usr/local/nagios/etc/htpasswd.users
yunjisuan:XahUhj.D9ajcc
提示:
為什么要把密碼生成到/usr/local/nagios/etc/htpasswd.users里呢?不能改路徑嗎?
這是因為/etc/httpd/conf.d/nagios.conf配置文件已經指定了htpasswd.user路徑和文件名,如果想改可以先修改nagios.conf配置,但是沒必要改,查看方式如下:
[root@Nagios nagios]# grep AuthUserFile /etc/httpd/conf.d/nagios.conf
AuthUserFile /usr/local/nagios/etc/htpasswd.users
AuthUserFile /usr/local/nagios/etc/htpasswd.users
#重新加載Apache服務:
[root@Nagios nagios]# /etc/init.d/httpd reload
Reloading httpd:
(2)添加監控報警信息接收的Email地址
快速修改方法如下:
[root@Nagios nagios]# sed -i 's#nagios@localhost#215379068@qq.com#g' /usr/local/nagios/etc/objects/contacts.cfg
[root@Nagios nagios]# sed -n '35p' /usr/local/nagios/etc/objects/contacts.cfg
email 215379068@qq.com #這里是你的個人QQ不是,老師的QQ
常見的發送郵件方法有兩種,一種是啟動本機的郵件服務postfix。另外一種是使用網上第三方郵件服務商提供的服務,例如:QQ郵件服務或者網易郵件服務,如果是簡單測試,推薦使用網易126或者163的smtp郵件發送服務地址配置。
第一種方法:監控服務器本地開啟郵件服務。此功能依賴本機的服務。
[root@Nagios nagios]# /etc/init.d/postfix start
Starting postfix: [ OK ]
[root@Nagios nagios]# netstat -antup | grep 25
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 6578/master
tcp 0 0 ::1:25 :::* LISTEN 6578/master
[root@Nagios nagios]# chkconfig postfix on
[root@Nagios nagios]# chkconfig --list postfix
postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off
發送郵件測試:
[root@Nagios nagios]# echo "welcome to yunjisuan" | mail -s "test" 215379068@qq.com
提示:
由於郵件服務沒有做MX記錄及反向解析,因此,所發郵件經常會收不到或者被當作垃圾郵件
第二種方法:使用第三方郵件服務商提供的郵箱。
#只需要修改/etc/mail.rc在最后增加如下兩行內容即可:
set from=215379068@qq.com
smtp=smtp.qq.comset smtp-auth-user=215379068 smtp-auth-password=123123 smtp-auto=login
這個第三方服務選擇QQ郵件服務或者網易郵件服務均可,這里選擇了QQ郵件服務。
注意這里的215379068@qq.com是作為報警的發件人的,相當於使用215379068用戶和1223123密碼登陸QQ信箱,然后給人1發信,收件人就是在contact.cfg里定義的。
(3)配置啟動Apache服務
下面啟動Apache服務並加入系統開機自啟動:
[root@Nagios nagios]# /etc/init.d/httpd start
Starting httpd: [ OK ]
[root@Nagios nagios]# chkconfig httpd on
[root@Nagios nagios]# chkconfig --list httpd
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@Nagios nagios]# netstat -antup | grep 80
tcp 0 0 :::80 :::* LISTEN 6670/httpd
此時,打開客戶端計算機上的瀏覽器訪問http://192.168.0.200/nagios會出現用戶名和密碼提示窗口,如下圖所示:
輸入前面生成好的用戶名和密碼,也就是yunjisuan和123123
出現下圖所示的界面表示Nagios及Nagios的Web環境已經正常了
(4)安裝Nagios插件軟件包
Nagios插件軟件包就是一些實現獲取數據信息的命令或程序,通過這些命令或程序,Nagios可以獲取到需要的數據,然后1進行報警和展示。具體安裝過程如下。
首先安裝基礎依賴包:
[root@Nagios nagios]# yum -y install perl-devel openssl-devel
然后安裝Nagios plugins插件包:
[root@Nagios nagios-plugins-1.4.16]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules --with-mysql
#如果一行放不下,可以通過反斜線換行輸入。
[root@Nagios nagios-plugins-1.4.16]# make
[root@Nagios nagios-plugins-1.4.16]# make install
#此時檢查插件個數:
[root@Nagios nagios-plugins-1.4.16]# ls /usr/local/nagios/libexec/ | wc -l
57 #多一個少一個都是正常的,不要太在意
(5)安裝nrpe軟件
可能有同學會問,nrpe是客戶端需要安裝的軟件,為什么還要安裝在Nagios服務器端?這是因為:
- Nagios服務器端需要check_nrpe插件做被動檢查,如果服務器端不裝nrpe軟件,就沒有check_nrpe這樣的檢查插件。
- Nagios服務器端本地的資源也需要被監控,因此,Nagios服務器端也會被作為客戶端。
- nrpe軟件的安裝非常簡單,就不多費筆墨了。安裝nrpe軟件的命令集如下:
[root@Nagios nagios]# ls -l nrpe-2.12.tar.gz
-rw-r--r--. 1 root root 405725 Aug 12 12:22 nrpe-2.12.tar.gz
[root@Nagios nagios]# tar xf nrpe-2.12.tar.gz -C /usr/src/
[root@Nagios nagios]# cd /usr/src/nrpe-2.12/
[root@Nagios nrpe-2.12]# ./configure
[root@Nagios nrpe-2.12]# make all
[root@Nagios nrpe-2.12]# make install-plugin
[root@Nagios nrpe-2.12]# make install-daemon
[root@Nagios nrpe-2.12]# make install-daemon-config
[root@Nagios ~]# ls /usr/local/nagios/libexec/check_nrpe
/usr/local/nagios/libexec/check_nrpe
[root@Nagios ~]# ls /usr/local/nagios/libexec/ | wc -l
58
#提示:生成nrpe的配置文件為/usr/local/nagios/etc/nrpe.cfg
到此為止,Nagios服務器端的軟件安裝部分就配置完了。
(6)配置並啟動Nagios服務
#首先添加Nagios服務到開機自啟動
[root@Nagios ~]# chkconfig nagios on
[root@Nagios ~]# chkconfig --list nagios
nagios 0:off 1:off 2:on 3:on 4:on 5:on 6:off
#更好的設置自動開機的方法是:
[root@Nagios ~]# echo "/etc/init.d/nagios start" >> /etc/rc.local
[root@Nagios ~]# tail -1 /etc/rc.local
/etc/init.d/nagios start
#然后驗證Nagios配置文件(檢查語法)
[root@Nagios ~]# /etc/init.d/nagios checkconfig
#此命令為檢查語法的命令,但是只能報告對錯無法給出錯誤的信息
Running configuration check... OK.
#可以使用命令行命令檢查報錯,並輸出信息
[root@Nagios ~]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
省略...
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check
#<==Total Warning 警告和Total Errors錯誤都為0,表示配置正常:
#<==Total Errors:如果不為0必須解決。否則Nagios無法啟動。
#<==Total Warnings:這個如果不為0可以啟動Nagios,可以不理睬。
#可以修改/etc/init.d/nagios實現上述命令行檢查語法的詳細輸出,如下:
[root@Nagios ~]# /etc/init.d/nagios checkconfig
Running configuration check... OK.
#<==此命令默認情況只能判斷錯誤或正確,並不顯示錯誤的詳細信息,如果想顯示詳細錯誤信息,需要做下簡單的特殊處理,如下。
[root@Nagios ~]# grep 'checkconfig)' -n -A 2 /etc/init.d/nagios
181: checkconfig)
182- printf "Running configuration check..."
183- $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
#刪除定向到空的內容,可讓報錯輸出詳細信息
刪除上述腳本中的>/dev/null 2>$1;即可檢查語法:
[root@Nagios ~]# /etc/init.d/nagios checkconfig
Running configuration check...
Nagios Core 3.5.1
Copyright (c) 2009-2011 Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 08-30-2013
License: GPL
Website: http://www.nagios.org
Reading configuration data...
Read main config file okay...
Processing object config file '/usr/local/nagios/etc/objects/commands.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/contacts.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/timeperiods.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/templates.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/localhost.cfg'...
Read object config files okay...
Running pre-flight check on configuration data...
Checking services...
Checked 8 services.
Checking hosts...
Checked 1 hosts.
Checking host groups...
Checked 1 host groups.
Checking service groups...
Checked 0 service groups.
Checking contacts...
Checked 1 contacts.
Checking contact groups...
Checked 1 contact groups.
Checking service escalations...
Checked 0 service escalations.
Checking service dependencies...
Checked 0 service dependencies.
Checking host escalations...
Checked 0 host escalations.
Checking host dependencies...
Checked 0 host dependencies.
Checking commands...
Checked 24 commands.
Checking time periods...
Checked 5 time periods.
Checking for circular paths between hosts...
Checking for circular host and service dependencies...
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check
OK.
之后,啟動Nagios服務,命令如下:
[root@Nagios ~]# /etc/init.d/nagios start
Starting nagios: done.
檢查Nagios服務器端進程及端口
[root@Nagios ~]# ps -ef | grep nagios | grep -v grep
nagios 21663 1 0 07:48 ? 00:00:00 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
#<==grep -v grep 表示排除自身的這個帶grep命令的進程。
[root@Nagios ~]# netstat -antup | grep nagios #沒發現Nagios服務器端有端口
最后瀏覽Nagios Web界面檢查。打開瀏覽器訪問:http://服務器端ip/nagios會出現如下圖所示提示:
如果訪問頁面打開報錯,請關閉服務器端的iptables及selinux
至此,Nagios服務器端安裝及配置就告一段落。
3.3 關於內網服務器的時間同步服務
在企業生產環境的內網環境中,一般是要有一台服務器專門和Internet上做時間同步,然后內網中的其他服務器都和這台服務器進行時間同步。
那么在我們本次的試驗中,我們用Nagios服務器端和internet時間同步,同時所有被監控的客戶端都和Nagios服務器端進行時間同步。
[root@Nagios ~]# yum -y install ntp ntpdate #安裝ntp時間同步服務端,ntpdate時間同步客戶端
[root@Nagios ~]# crontab -l #已經進行定時同步任務
#time sync by Mr.chen at 2017-08-12
*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1
[root@Nagios ~]# /etc/init.d/ntpd start #啟動ntpd服務端進程
Starting ntpd: [ OK ]
四,Nagios客戶端安裝
4.1 Nagios客戶端安裝說明
- Nagios客戶端無需安裝LAMP環境,也無需安裝Nagios服務器端軟件包,即nagios-3.5.1.tar.gz
- 與服務器端相比,Nagios客戶端有一些額外的軟件包需要安裝,如下:
- Class-Accessor-0.31.tar.gz
- Config-Tiny-2.12.tar.gz
- Math-Calc-Units-1.0.7.tar.gz
- Nagios-Plugin-0.34.tar.gz
- Params-Validate-0.91.tar.gz
- Regexp-Common-2010010201.tar.gz
4.2 Nagios客戶端安裝准備
(1)准備2台服務器或VM虛擬機
管理IP地址 | 角色 | 備注 |
---|---|---|
192.168.0.223 | Web01 | 被監控的客戶端服務器 |
192.168.0.224 | Web02 | 被監控的客戶端服務器 |
(2)環境准備與服務器端步驟相同
客戶端的yum源安裝,關閉Nagios Server端iptables防火牆及SElinux的方法,這里不再說明
以下操作再兩台web上都進行
#nagios客戶端向Nagios服務器端進行時間同步
[root@web01 ~]# yum -y install ntpdate #安裝時間同步客戶端
[root@web01 ~]# ntpdate 192.168.0.200 #和Nagios服務器端進行時間同步
15 Aug 08:22:26 ntpdate[5242]: step time server 192.168.0.200 offset 1517558.308966 sec
[root@web01 ~]# date +%F
2017-08-15
[root@web01 ~]# echo "*/5 * * * * /usr/sbin/ntpdate 192.168.0.200 >/dev/null 2>&1" >> /var/spool/cron/root #寫定時任務
[root@web01 ~]# cat /var/spool/cron/root
*/5 * * * * /usr/sbin/ntpdate 192.168.0.200 >/dev/null 2>&1
4.3 在Nagios客戶端安裝軟件
(1)安裝基礎系統軟件
命令如下:
[root@web01 nagios]# yum -y install gcc glibc glibc-common #升級基礎工具包
[root@web01 nagios]# yum -y install mysql-server #安裝這個目的為了Nagios plugins,生成check_mysql插件
(2)上傳Nagios相關軟件
#去掉不用的,客戶端需要的軟件,如下:
[root@web01 ~]# tree nagios/
nagios/
├── check_iostat
├── check_memory.pl
├── check_mysql
├── Class-Accessor-0.31.tar.gz
├── Config-Tiny-2.12.tar.gz
├── libart_lgpl-2.3.17.tar.gz
├── Math-Calc-Units-1.07.tar.gz
├── Nagios-Plugin-0.34.tar.gz
├── nagios-plugins-1.4.16.tar.gz
├── nrpe-2.12.tar.gz
├── Params-Validate-0.91.tar.gz
└── Regexp-Common-2010010201.tar.gz
0 directories, 12 files
(3)添加Nagios用戶
添加方法如下:
[root@web01 ~]# useradd nagios -M -s /sbin/nologin
[root@web01 ~]# id nagios
uid=501(nagios) gid=501(nagios) groups=501(nagios)
(4)安裝nagios-plugins插件
操作過程如下:
[root@web01 nagios]# yum -y install perl-devel perl-CPAN openssl-devel
[root@web01 nagios-plugins-1.4.16]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules --with-mysql
[root@web01 nagios-plugins-1.4.16]# make
[root@web01 nagios-plugins-1.4.16]# make install
此時插件的個數如下:
[root@web01 ~]# ls /usr/local/nagios/libexec/ | wc -l
57
(5)安裝Nagios客戶端nrpe軟件
[root@web01 ~]# cd nagios/
[root@web01 nagios]# tar xf nrpe-2.12.tar.gz -C /usr/src/
[root@web01 nagios]# cd /usr/src/nrpe-2.12/
[root@web01 nrpe-2.12]# ./configure
[root@web01 nrpe-2.12]# make all
[root@web01 nrpe-2.12]# make install-plugin
[root@web01 nrpe-2.12]# make install-daemon
#生成nrpe.cfg
[root@web01 nrpe-2.12]# make install-daemon-config
(6)安裝其他相關的插件
以下是check_iostat插件需要的依賴包,非必須安裝,如果不使用軟件包里提供的check_iostat可以不裝,雖然命令集很多,但是安裝簡單,因此可以批量執行這些命令。
#安裝Params-Validate-0.91.tar.gz軟件包
[root@web01 ~]# cd nagios/
[root@web01 nagios]# tar xf Params-Validate-0.91.tar.gz -C /usr/src/
[root@web01 nagios]# cd /usr/src/Params-Validate-0.91/
[root@web01 Params-Validate-0.91]# perl Makefile.PL
[root@web01 Params-Validate-0.91]# make
[root@web01 Params-Validate-0.91]# make install
#安裝Class-Accessor-0.31.tar.gz軟件包
[root@web01 ~]# cd nagios/
[root@web01 nagios]# tar xf Class-Accessor-0.31.tar.gz -C /usr/src/
[root@web01 nagios]# cd /usr/src/Class-Accessor-0.31/
[root@web01 Class-Accessor-0.31]# perl Makefile.PL
[root@web01 Class-Accessor-0.31]# make && make install
#安裝Config-Tiny-2.12.tar.gz軟件包
[root@web01 ~]# cd nagios/
[root@web01 nagios]# tar xf Config-Tiny-2.12.tar.gz -C /usr/src/
[root@web01 nagios]# cd /usr/src/Config-Tiny-2.12/
[root@web01 Config-Tiny-2.12]# perl Makefile.PL
[root@web01 Config-Tiny-2.12]# make && make install
#安裝Math-Calc-Units-1.07.tar.gz軟件包
[root@web01 ~]# cd nagios/
[root@web01 nagios]# tar xf Math-Calc-Units-1.07.tar.gz -C /usr/src/
[root@web01 nagios]# cd /usr/src/Math-Calc-Units-1.07/
[root@web01 Math-Calc-Units-1.07]# perl Makefile.PL
[root@web01 Math-Calc-Units-1.07]# make && make install
#安裝Regexp-Common-2010010201.tar.gz軟件包
[root@web01 ~]# cd nagios/
[root@web01 nagios]# tar xf Regexp-Common-2010010201.tar.gz -C /usr/src/
[root@web01 nagios]# cd /usr/src/Regexp-Common-2010010201/
[root@web01 Regexp-Common-2010010201]# perl Makefile.PL
[root@web01 Regexp-Common-2010010201]# make && make install
#安裝Nagios-Plugin-0.34.tar.gz軟件包
[root@web01 ~]# cd nagios/
[root@web01 nagios]# tar xf Nagios-Plugin-0.34.tar.gz -C /usr/src/
[root@web01 nagios]# cd /usr/src/Nagios-Plugin-0.34/
[root@web01 Nagios-Plugin-0.34]# perl Makefile.PL
[root@web01 Nagios-Plugin-0.34]# make && make install
安裝sysstat系統性能分析工具,監控系統性能時程序或腳本會調用這些工具
[root@web02 ~]# yum -y install sysstat #光盤安裝即可
[root@web02 ~]# rpm -q sysstat
sysstat-9.0.4-22.el6.x86_64
提示:
sysstat工具包中包含兩類工具,分別為即時查看工具(iostat,mpstat,sar)和累計統計工具(sar)。可以看出,這兩類工具中都有sar,也就是說,sar具有這兩種功能。可見,sar是sysstat中的核心工具。為了實現sar的累計統計功能,系統必須周期性地記錄當時的信息,這是通過調用/usr/lib/sa/中的三個工具實現的:
- sa1:收集並存儲每天的系統動態信息到一個二進制的文件中,用作sadc的前端程序。
- sa2:收集每天的系統活躍信息寫入總結性的報告,用作sar的1前端程序。
- sadc:系統動態數據收集工具,收集的數據被寫入一個二進制的文件中,用作sar工具的后端程序。
這里針對監視物理組件的高級Linux命令小結如下:
內存:top,free,vmstat,mpstat,iostat,sar
CPU:top,vmstat,mpstat,iostat,sar
I/O:vmstat,mpstat,iostat,sar
進程:ipcs,ipcrm
負載:uptime
(7)配置監控內存,磁盤I/O腳本插件
批量部署命令如下:
[root@web01 ~]# yum -y install dos2unix #光盤安裝即可
[root@web01 ~]# /bin/cp /root/nagios/check_memory.pl /usr/local/nagios/libexec/
[root@web01 ~]# /bin/cp /root/nagios/check_iostat /usr/local/nagios/libexec/
#<===以上兩個命令是將事先寫好的腳本(check_memory.pl和check_iostat)放到Nagios腳本目錄下。
[root@web01 ~]# chmod 755 /usr/local/nagios/libexec/check_memory.pl
[root@web01 ~]# chmod 755 /usr/local/nagios/libexec/check_iostat
#以上兩條命令是授權讓腳本可執行
[root@web01 ~]# dos2unix /usr/local/nagios/libexec/check_memory.pl
dos2unix: converting file /usr/local/nagios/libexec/check_memory.pl to UNIX format ...
[root@web01 ~]# dos2unix /usr/local/nagios/libexec/check_iostat
dos2unix: converting file /usr/local/nagios/libexec/check_iostat to UNIX format ...
#<==使用dos2UNIX使之格式為UNIX的腳本格式,否則執行可能出錯。
#注意:
#這個路徑是上傳的軟件包所在路徑/root/nagios
4.4 配置Nagios客戶端nrpe服務
可通過以下命令配置客戶端的nrpe.cfg
#加入Nagios Server服務端的IP,用以監控當前Client
[root@web01 ~]# cd /usr/local/nagios/etc
[root@web01 etc]# ls -l nrpe.cfg
-rw-r--r--. 1 nagios nagios 7207 Aug 15 09:07 nrpe.cfg
[root@web01 etc]# sed -n '79p' nrpe.cfg
allowed_hosts=127.0.0.1
[root@web01 etc]# sed -i '79 s#$#,192.168.0.200#g' nrpe.cfg
[root@web01 etc]# sed -n '79p' nrpe.cfg
allowed_hosts=127.0.0.1,192.168.0.200
#提示:
#新加入的192.168.0.200是服務端IP不是客戶端的
#大於500台機器做集群或分布式監控時,可以使用多個Nagios Server,IP要用逗號隔開
注釋掉nrpe.cfg的199~203行內容
[root@web01 etc]# sed -n '199,203p' nrpe.cfg
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
[root@web01 etc]# sed -i '199,203 s/^/#/' nrpe.cfg
[root@web01 etc]# sed -n '199,203p' nrpe.cfg
#command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
#command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
#command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
#command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
#command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
在配置文件nrpe.cfg下面添加要監控的內容:
#在文件最后添加以下5行內容
[root@web01 etc]# tail -5 /usr/local/nagios/etc/nrpe.cfg
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_mem]=/usr/local/nagios/libexec/check_memory.pl -w 10% -c 3%
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 15% -c 7% -p/
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
command[check_iostat]=/usr/local/nagios/libexec/check_iostat -w 6 -c 10
上面依次為對負載,內存,硬盤,虛擬內存,磁盤I/O進行監控,這些都是本地的服務(我們一般通過nrpe去客戶端執行腳本插件獲取信息,這樣的模式稱為被動監控,與nsca的客戶端主動提交結果不沖突),由Nagios服務器端通過nrpe插件定時在client的nrpe服務上獲取信息。
之后,啟動Nagios client nrpe守護進程
[root@web01 etc]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@web01 etc]# netstat -antup | grep nrpe
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 21041/nrpe
此時,可以將nrpe加入開機自啟動了,命令如下:
[root@web01 etc]# echo "#nagios nrpe process cmd by Mr.chen 2017-8-15" >> /etc/rc.local
[root@web01 etc]# echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.local
[root@web01 etc]# tail -2 /etc/rc.local
#nagios nrpe process cmd by Mr.chen 2017-8-15
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
重要提醒:
客戶端的nrpe.cfg配置文件,最好保留一份到計算機上,這樣以后在其他機器上裝nrpe時,直接上傳即可,就省得費勁修改了。
五,Nagios服務器端監控
5.1 Nagios服務器端監控基礎介紹
(1)Nagios服務器端目錄結構
Nagios服務器端安裝后的目錄結構如下:
[root@Nagios ~]# ls -l /usr/local/nagios/
total 32
drwxrwxr-x. 2 nagios nagios 4096 Aug 14 10:32 bin
drwxrwxr-x. 3 nagios nagios 4096 Aug 14 10:33 etc
drwxr-xr-x. 2 root root 4096 Aug 14 10:11 include
drwxrwxr-x. 2 nagios nagios 4096 Aug 14 10:32 libexec
drwxr-xr-x. 5 root root 4096 Aug 14 10:11 perl
drwxrwxr-x. 2 nagios nagios 4096 Aug 14 07:36 sbin
drwxrwxr-x. 11 nagios nagios 4096 Aug 14 10:11 share
drwxrwxr-x. 5 nagios nagios 4096 Aug 15 11:11 var
具體含義請參考以下圖片:
所有客戶端本地服務的監控都是通過執行libexec目錄下的插件來實現的,當然,如果開啟了snmp,Nagios服務器端也可以主動抓取。
(2)Nagios服務器端核心配置文件
Nagios主配置文件為nagios.cfg,默認在/usr/local/nagios/etc目錄下,/usr/local/nagios/etc目錄下有個objects(類似Apache的extra)目錄,里面放的是組配置文件nagios.cfg包含的其他Nagios配置文件。查看命令如下:
[root@Nagios ~]# tree /usr/local/nagios/etc/
/usr/local/nagios/etc/
|-- cgi.cfg
|-- htpasswd.users
|-- nagios.cfg
|-- nrpe.cfg
|-- objects
| |-- commands.cfg
| |-- contacts.cfg
| |-- localhost.cfg
| |-- printer.cfg
| |-- switch.cfg
| |-- templates.cfg
| |-- timeperiods.cfg
| `-- windows.cfg
`-- resource.cfg
1 directory, 13 files
[root@Nagios ~]# tail -10 /usr/local/nagios/etc/resource.cfg
# Sets $USER1$ to be the path to the plugins
$USER1$=/usr/local/nagios/libexec
# Sets $USER2$ to be the path to event handlers
#$USER2$=/usr/local/nagios/libexec/eventhandlers
# Store some usernames and passwords (hidden from the CGIs)
#$USER3$=someuser
#$USER4$=somepassword
[root@Nagios ~]# tree /usr/local/nagios/etc/objects/
/usr/local/nagios/etc/objects/
|-- commands.cfg
|-- contacts.cfg
|-- localhost.cfg
|-- printer.cfg
|-- switch.cfg
|-- templates.cfg
|-- timeperiods.cfg
`-- windows.cfg
0 directories, 8 files
在nagios.cfg中既可以指定單獨包含一個cfg文件,也可以指定包含一個目錄,即1該目錄下所有的cfg文件都會包含進來。
為了讓目錄結構看起來更清晰,以及批量部署服務的需要,我們把主配置文件包含的配置文件修改為下表的形式。
(3)配置主配置文件nagios.cfg
#首先,在nagios.cfg文件中找到cfg_file部分,進行如下設置:
[root@Nagios ~]# sed -n '34,36p' /usr/local/nagios/etc/nagios.cfg
#增加如下主機和服務的配置文件
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
cfg_dir=/usr/local/nagios/etc/objects/services
#這是為備用增加的一個service目錄,初學者可以忽略此行,使用目錄的優點很多,在目錄下的文件只要符合*.cfg就可以被Nagios加載。使用腳本批量部署時可非常方便地隨機命名配置文件
#然后在配置文件里注釋掉如下一行
[root@Nagios ~]# sed -n '37,38p' /usr/local/nagios/etc/nagios.cfg
# Definitions for monitoring the local (Linux) host
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg #注釋本行
#<==localhost.cfg這個配置為監控Nagios服務器端本地服務的配置文件,注釋掉它,然后統一監控
操作完畢,保存nagios.cfg。接着,根據已有數據生成hosts.cfg主機文件,命令集如下:
[root@Nagios ~]# cd /usr/local/nagios/etc/objects/
[root@Nagios objects]# head -51 localhost.cfg > hosts.cfg
[root@Nagios objects]# chown nagios.nagios /usr/local/nagios/etc/objects/hosts.cfg
然后生成新的空的services.cfg服務文件,操作過程如下:
[root@Nagios objects]# touch services.cfg
[root@Nagios objects]# chown -R nagios.nagios services.cfg
最后,生成服務的配置文件目錄,所有放到此目錄下的配置(*.cfg)都會自動被包含到主配置文件中生效。操作過程如下:
[root@Nagios objects]# mkdir services
[root@Nagios objects]# chown -R nagios.nagios services
[root@Nagios objects]# ls -lrt
total 56
-rw-rw-r--. 1 nagios nagios 10812 Aug 14 07:46 templates.cfg
-rw-rw-r--. 1 nagios nagios 7716 Aug 14 07:46 commands.cfg
-rw-rw-r--. 1 nagios nagios 3208 Aug 14 07:46 timeperiods.cfg
-rw-rw-r--. 1 nagios nagios 5403 Aug 14 07:46 localhost.cfg
-rw-rw-r--. 1 nagios nagios 4019 Aug 14 07:46 windows.cfg
-rw-rw-r--. 1 nagios nagios 3124 Aug 14 07:46 printer.cfg
-rw-rw-r--. 1 nagios nagios 3293 Aug 14 07:46 switch.cfg
-rw-rw-r--. 1 nagios nagios 2166 Aug 14 09:15 contacts.cfg
-rw-r--r--. 1 nagios nagios 1870 Aug 15 11:49 hosts.cfg
-rw-r--r--. 1 nagios nagios 0 Aug 15 13:00 services.cfg
drwxr-xr-x. 2 nagios nagios 4096 Aug 15 13:02 services
(4)Nagios監控模式定義及監控模式選擇
根據監控的行為,將Nagios的監控分為主動監控和被動監控(即nrpe半被動和nsca全被動),nsca全被動暫不詳述,下面先來看看什么是主動監控和半被動監控。
- 主動監控:把像URL監控一樣由Nagios服務器端發出請求的主動探測監控方式,定義為主動監控方式,也就是說不需要在客戶端安裝任何插件。當然,主動監控模式也可以配置成被動模式。
- (半)被動監控:由Nagios服務器端通過nrpe插件定時去連接client的nrpe服務獲取信息,並發回到Nagios服務器端的監控稱之為半被動監控,這類監控通常是針對本地資源的,比如負載,內存,硬盤,虛擬內存,磁盤I/O,溫度,風扇轉速等,而非系統對外提供的服務,只要安裝了類似nrpe的插件方式的監控,都認為是半被動監控。
如何選擇主動監控模式和(半)被動監控模式?
1)對於本地的資源性能,一般用被動監控模式(NRPE)。例如,對負載,內存,硬盤,虛擬內存,磁盤I/O,溫度,風扇等的監控(我們也可以通過snmp實現監控部分系統資源)。
2)對於Web服務,數據庫服務這種能對外提供服務的,一般用主動模式,例如:監控httpd,sshd,mysqld,rsyncd等服務。
主動模式和被動模式是相對的,並且是可以互相轉換的,即主動模式的服務,可以改成被動模式的,被動模式的服務有時也可以改為主動模式的。
提示:
我們在后面把nrpe的監控統稱為被動監控模式
5.2 配置Nagios服務器端監控項
(1)定義要監控的Nagios客戶端主機
hosts.cfg一般用來存放Nagios要監控的主機相關配置,下面是hosts.cfg中的主機定義部分的配置參數詳解。
#精簡hosts.cfg配置模板
[root@Nagios objects]# cp hosts.cfg{,.bak}
[root@Nagios objects]# egrep -v "^$|#" hosts.cfg.bak > hosts.cfg
#將hosts.cfg里的內容修改成以下狀態。
[root@Nagios objects]# cat hosts.cfg
# Define a host for the local machine
define host{ #<==define host為關鍵字,意思是定義主機,主機內容用一對大括號括起來
use linux-server #定義主機使用的模板,具體參見templates.cfg
host_name web01 #直接定義主機名稱,根據服務功能可隨意定義
alias web01 #直接定義主機別名,同上
address 192.168.0.223 #直接定義被監控服務器的IP
}
define host{
use linux-server
host_name web02
alias web02
address 192.168.0.224
}
define hostgroup{
hostgroup_name linux-servers
alias Linux Servers
members web01,web02 #把前面定義的每一個Nagios客戶端主機名在這里用逗號隔開列出來
}
(2)配置services.cfg,定義要監控的資源服務
services.cfg文件是配置監控服務的,是Nagios最重要的配置文件之一,如果服務器數量比較少(50台以內),則需要監控的大部分服務配置都可以在這里面添加。這個配置文件默認是不存在的,需要人為定義。
先來看看services.cfg配置文件的service配置參數,詳細說明如下:
[root@Nagios objects]# cat services.cfg
define service { #磁盤分區監控(被動監控)
use generic-service
host_name web01
service_description Disk Partition
check_command check_nrpe!check_disk
}
define service { #swap監控(被動監控)
use generic-service
host_name web01
service_description Swap Useage
check_command check_nrpe!check_swap
}
define service { #內存監控(被動監控)
use generic-service
host_name web01
service_description MEM Useage
check_command check_nrpe!check_mem
}
define service { #系統負載監控(被動監控)
use generic-service
host_name web01
service_description Current Load
check_command check_nrpe!check_load
}
define service { #磁盤I/O監控(被動監控)
use generic-service
host_name web01
service_description Disk lostat
check_command check_nrpe!check_iostat!5!11
}
define service { #ping監控(主動監控)
use generic-service
host_name web01
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
提示:
(1)上述配置中的check_nrpe是服務器端的插件(是commands.cfg里預先定義的命令名),負責和客戶端的nrpe進程交流並執行check_nrpe嘆號后的插件,所以,check_nrpe ! check_load配置中的check_load是客戶端的插件名,是在與客戶端的nrpe進程對應的配置nrpe.cfg里定義的命令名。
(2)以上services.cfg中添加了對磁盤分區,Load,Mem,Swap,磁盤I/O,ping的監控。
Nagios軟件默認沒有提供客戶端的內存和I/O插件,但本文在配置時已經復制進去了,因此,只需在commands.cfg里配置即可,詳細請同學們看后面的commands.cfg說明
(3)調試hosts.cfg和service.cfg的所有配置
[root@Nagios objects]# /etc/init.d/nagios checkconfig
Running configuration check...
Nagios Core 3.5.1
Copyright (c) 2009-2011 Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 08-30-2013
License: GPL
Website: http://www.nagios.org
Reading configuration data...
Read main config file okay...
Processing object config file '/usr/local/nagios/etc/objects/commands.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/contacts.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/timeperiods.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/templates.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/hosts.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/services.cfg'...
Processing object config directory '/usr/local/nagios/etc/objects/services'...
Read object config files okay...
Running pre-flight check on configuration data...
Checking services...
Error: Service check command 'check_nrpe ' specified in service 'Current Load' for host 'web01' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'Disk Partition' for host 'web01' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'Disk lostat' for host 'web01' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'MEM Useage' for host 'web01' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'PING' for host 'web01' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'Swap Useage' for host 'web01' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'Current Load' for host 'web02' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'Disk Partition' for host 'web02' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'Disk lostat' for host 'web02' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'MEM Useage' for host 'web02' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'PING' for host 'web02' not defined anywhere!
Error: Service check command 'check_nrpe ' specified in service 'Swap Useage' for host 'web02' not defined anywhere!
Checked 12 services.
Checking hosts...
Checked 2 hosts.
Checking host groups...
Checked 1 host groups.
Checking service groups...
Checked 0 service groups.
Checking contacts...
Checked 1 contacts.
Checking contact groups...
Checked 1 contact groups.
Checking service escalations...
Checked 0 service escalations.
Checking service dependencies...
Checked 0 service dependencies.
Checking host escalations...
Checked 0 host escalations.
Checking host dependencies...
Checked 0 host dependencies.
Checking commands...
Checked 24 commands.
Checking time periods...
Checked 5 time periods.
Checking for circular paths between hosts...
Checking for circular host and service dependencies...
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...
Total Warnings: 0
Total Errors: 12
***> One or more problems was encountered while running the pre-flight check...
Check your configuration file(s) to ensure that they contain valid
directives and data defintions. If you are upgrading from a previous
version of Nagios, you should be aware that some variables/definitions
may have been removed or modified in this version. Make sure to read
the HTML documentation regarding the config files, as well as the
'Whats New' section to find out what has changed.
CONFIG ERROR! Check your Nagios configuration.
根據錯誤提示,我們可以知道,是check_nrpe插件沒有定義導致的。來看一下解決方法。
首先,需要在commands.cfg中加入check_nrpe的插件配置.
[root@Nagios objects]# tail -8 commands.cfg
#'check_nrpe'command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
此時重新執行檢查語法的命令:
[root@Nagios objects]# /etc/init.d/nagios checkconfig
#中間過程略
Checking misc settings...
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check
OK.
此時,警告和錯誤都為0,則表示已經OK了,一般來講警告可以忽略,錯誤必須要解決掉,否則,無法繼續,調試完成后啟動Nagios服務。
[root@Nagios objects]# /etc/init.d/nagios start
Starting nagios: done.
如果啟動成功的話,那么此時就可以登陸192.168.0.200/nagios。查看本地各系統狀態的監控成果了。但是如果出現如下錯誤提示。
這個錯誤表示登陸Web端的用戶yunjisuan沒有被許可查看這些服務資源,可按照如下方法解決上面問題。
[root@Nagios objects]# cd /usr/local/nagios/etc/
[root@Nagios etc]# egrep -n "^authorized_for" cgi.cfg
authorized_for_system_information=nagiosadmin
authorized_for_configuration_information=nagiosadmin
authorized_for_system_commands=nagiosadmin
authorized_for_all_services=nagiosadmin
authorized_for_all_hosts=nagiosadmin
authorized_for_all_service_commands=nagiosadmin
authorized_for_all_host_commands=nagiosadmin
上述authorized開頭的行對應的都是不同權限,可以看出,在結尾的許可處用過戶都是nagiosadmin。此時,把在前面建立的Nagios Web登陸用戶yunjisuan加到每一個許可項的后面,注意兩者用逗號隔開。
[root@Nagios etc]# sed -i 's#nagiosadmin#yunjisuan#g' cgi.cfg
[root@Nagios etc]# egrep -n "^authorized_for" cgi.cfg
119:authorized_for_system_information=yunjisuan
131:authorized_for_configuration_information=yunjisuan
144:authorized_for_system_commands=yunjisuan
157:authorized_for_all_services=yunjisuan
158:authorized_for_all_hosts=yunjisuan
171:authorized_for_all_service_commands=yunjisuan
172:authorized_for_all_host_commands=yunjisuan
提示:
1)理想情況最好換掉默認管理員用戶nagiosadmin,替換成自己的yunjisuan等,然后根據需求加適合自己的權限,比如,給別人看只給瀏覽權限就可以了。具體見cgi.cfg
2)遇到調試問題注意查看/usr/local/nagios/var/nagios.log,這點很重要。高手都這么做
現在,重新加載配置使得修改生效
[root@Nagios etc]# /etc/init.d/nagios reload
Running configuration check...done.
Reloading nagios configuration...done
正常監控后的效果圖,如下:
提示:UP代表服務器正常
服務器監控效果圖,剛配置完的主機監控圖如下:
(4)被動模式下基於Nagios監控原理排錯的案例
還記得之前的nrpe原理圖嗎?來回顧一下,見下圖:
Nagios被動模式的監控原理離不開check_nrpe這個插件,也就是說其實是利用下面這個命令工作的:
/usr/local/nagios/libexec/check_nrpe -H 192.168.0.223 -c check_mem
下面看兩個模擬Nagios配置錯誤的案例
第一個案例模擬:取消Web節點被監控端的check_memory.pl腳本的執行權限,摸你前面的錯誤。
#在Web端執行操作
[root@web01 ~]# cd /usr/local/nagios/libexec/
[root@web01 libexec]# chmod a-x check_memory.pl
[root@web01 libexec]# ll check_memory.pl
-rw-r--r--. 1 root root 4670 Aug 15 10:26 check_memory.pl
稍等一下或重啟Nagios后,此時頁面內存服務監控行出現錯誤提示。
錯誤提示為:NRPE:Unable to read output
第二個案例模擬:把Web被監控端的nrpe.cfg中的命令名寫錯,如下:
[root@web01 libexec]# tail -3 /usr/local/nagios/etc/nrpe.cfg
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 15% -c 7% -p/
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
command[check_iostat]=/usr/local/nagios/libexec/check_iostat -w 6 -c 10
[root@web01 libexec]# vim /usr/local/nagios/etc/nrpe.cfg
[root@web01 libexec]# tail -3 /usr/local/nagios/etc/nrpe.cfg
command[check_disk1]=/usr/local/nagios/libexec/check_disk -w 15% -c 7% -p/
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
command[check_iostat]=/usr/local/nagios/libexec/check_iostat -w 6 -c 10
#提示:
#把command[check_disk]寫成command[check_disk1]
重啟Web被監控端nrpe服務后,此時頁面磁盤服務監控行出現如下錯誤提示:
[root@web01 libexec]# pkill nrpe
[root@web01 libexec]# netstat -antup | grep nrpe
[root@web01 libexec]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@web01 libexec]# netstat -antup | grep nrpe
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 22084/nrpe
錯誤提示為:NRPE:Command ‘check_disk’ not defined
下面就針對上面兩個案例進行故障排查
案例1:NRPE:Unable to read output 排查
1)在Nagios服務器端執行如下命令:
[root@Nagios ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.0.223 -c check_mem
NRPE: Unable to read output
2)在客戶端本地執行命令腳本檢查(就是command[check_mem]=后面對應的腳本)
[root@web01 libexec]# /usr/local/nagios/libexec/check_memory.pl -w 10% -c 3%
#這個命令最好是復制nrpe.cfg里的配置,不要手敲,防止出錯
-bash: /usr/local/nagios/libexec/check_memory.pl: Permission denied
#提示拒絕,所以,原因就找到了。因為check_memory.pl腳本無執行權限導致的NRPE:Unalbe to read output錯誤。
[root@web01 libexec]# chmod a+x /usr/local/nagios/libexec/check_memory.pl
[root@web01 libexec]# /usr/local/nagios/libexec/check_memory.pl -w 10% -c 3%
CHECK_MEMORY OK - 845M free | free=886231040b;102851788.8:;30855536.64:
3)此時在Nagios服務器端執行如下命令:
[root@Nagios ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.0.223 -c check_mem
CHECK_MEMORY OK - 844M free | free=885813248b;102851788.8:;30855536.64:
#提示OK了,那么頁面應該也正常了。
案例2:NRPE:Command ‘check_disk’ not defined排查
該案例涉及的是磁盤監控故障,排查思路如下:
#在Nagios服務器端執行如下命令:
[root@Nagios ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.0.223 -c check_disk
NRPE: Command 'check_disk' not defined
如果同學們理解了Nagios被動監控的原理,很容易判斷上面問題的故障在於nrpe.cfg中check_disk相關命令寫錯了,或者沒配。
根據Nagios被動模式監控原理排查問題的思路如下。
1)在服務器端執行如下命令看是否返回數據
[root@Nagios ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.0.223 -c check_disk
NRPE: Command 'check_disk' not defined
2)如果前面無法正確返回數據,可在客戶端192.168.0.223本地執行如下命令看是否返回數據。
[root@web01 libexec]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_disk
NRPE: Command 'check_disk' not defined
3)如果前面還是無法正確返回數據,則執行nrpe.cfg里下面配置的內容中等號后面的命令。
即執行如下操作:
[root@web01 libexec]# /usr/local/nagios/libexec/check_disk -w 15% -c 7% -p/
DISK OK - free space: / 15219 MB (90% inode=97%);| /=1590MB;15052;16469;0;17709
由此可以看出是命令的定義名稱出現了錯誤,改回即可。
如果以上三步都OK,那么就找服務器端原因,一般是配置文件以及命令插件問題。
(5)添加http服務的URL地址及端口監控
現在增加從Nagios服務器端發起的監控,如URL地址,端口監控等。此類服務一般都開啟了對外提供服務的業務。這樣的業務,一般采用主動監控的方式,當然了,也可以寫腳本通過被動的方式來監控,但一般不這么做。
URL監控的實質是通過命令行理解HTTP的監控原理,如下:
[root@Nagios ~]# /usr/local/nagios/libexec/check_http -H 192.168.0.223
HTTP OK: HTTP/1.1 200 OK - 249 bytes in 0.002 second response time |time=0.002228s;;;0.000000 size=249B;;;0
下面是對域名URL地址http://www.yunjisuan.com進行監控的配置,將要監控的服務配置到services.cfg中即可。
#在Nagios服務端操作,在services.cfg中添加如下內容
[root@Nagios ~]# cat /usr/local/nagios/etc/objects/services.cfg
define service {
use generic-service
host_name web01,web02
service_description Disk Partition
check_command check_nrpe!check_disk
}
define service {
use generic-service
host_name web01,web02
service_description Swap Useage
check_command check_nrpe!check_swap
}
define service {
use generic-service
host_name web01,web02
service_description MEM Useage
check_command check_nrpe!check_mem
}
define service {
use generic-service
host_name web01,web02
service_description Current Load
check_command check_nrpe!check_load
}
define service {
use generic-service
host_name web01,web02
service_description Disk lostat
check_command check_nrpe!check_iostat!5!11
}
define service {
use generic-service
host_name web01,web02
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
#url examples http://www.yunjisuan.com 從這里開始是添加的內容
define service { #用自定義命令對www.yunjisuan.com監控
use generic-service
host_name web01
service_description www_url
check_command check_weburl! -H www.yunjisuan.com #check_weburl是自定義命令,在command.cfg中定義。
}
define service { #利用系統自帶插件對Web進行監控
use generic-service
host_name web01
service_description www_url
check_command check_http
}
define service { #用自定義命令對復雜URL進行監控
use generic-service
host_name web01
service_description www_static_url
check_command check_weburl! -H www.yunjisuan.com -u /static/test.html
#-H:主機IP或域名
#-u:URL的URI的部分
}
define service { #對特殊的帶傳參的URL地址進行監控時,方式如下:
use generic-service
host_name web01
service_description www_yunjisuan_url
check_command check_weburl! -H www.yunjisuan.com -u "/article/index.phpm=article&a=list&id=670"
}
(6)配置好URL后檢查Nagios語法
#若在配置好URL后,就執行如下命令檢查Nagios語法:
[root@Nagios ~]# /etc/init.d/nagios checkconfig
#會發現報錯了
Checking services...
Error: Service check command 'check_weburl' specified in service 'www_static_url' for host 'web01' not defined anywhere!
Error: Service check command 'check_weburl' specified in service 'www_yunjisuan_url' for host 'web01' not defined anywhere!
...省略若干...
Total Warnings: 0
Total Errors: 2
根據報錯信息可以知道,是因為在command.cfg配置文件中沒有定義check_weburl插件導致的。因此,需要在commands.cfg中加入check_weburl的插件配置。
#在commands.cfg中加入如下配置
[root@Nagios ~]# tail -7 /usr/local/nagios/etc/objects/commands.cfg
# 'check_weburl' command definition
define command { #定義命令check_weburl
command_name check_weburl
command_line $USER1$/check_http $ARG1$ -w 10 -c 30
}
注意:
如果是測試域名URL監控,注意在Nagios服務器端的/etc/hosts里加入如下域名解析
[root@Nagios ~]# tail -2 /etc/hosts
192.168.0.200 nagios-server
192.168.0.223 www.yunjisuan.com bbs.yunjisuan.com
此時執行檢查語法的命令:
[root@Nagios ~]# /etc/init.d/nagios checkconfig
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check
OK.
使配置文件生效的命令為:
[root@Nagios ~]# /etc/init.d/nagios reload
Running configuration check...done.
Reloading nagios configuration...done
到這里,可以看到自己配置的URL監控成果了,如下圖所示:
添加測試網頁
#在Web1被監控端操作
[root@web01 ~]# cd /usr/local/nginx/html/www/
[root@web01 www]# ls
index.html static
[root@web01 www]# ll static/
total 4
-rw-r--r--. 1 root root 13 Jul 27 06:10 index.html
[root@web01 www]# echo "welcome" >> static/test.html
[root@web01 www]# cat static/test.html
welcome
(7)監控任意TCP端口舉例
#端口檢查的實質就是1執行如下命令去監控
[root@Nagios ~]# /usr/local/nagios/libexec/check_tcp -H192.168.0.223 -p 80
TCP OK - 0.000 second response time on port 80|time=0.000216s;;;0.000000;10.000000
#在services.cfg最后加上如下定義
[root@Nagios objects]# tail -18 /usr/local/nagios/etc/objects/services.cfg
#tcp examples
define service {
use generic-service
host_name web01
service_description ssh_22
check_command check_tcp! 22
}
define service {
use generic-service
host_name web01
service_description http_80
check_command check_tcp! 80
}
#這里的check_tcp為Nagios plugin默認插件,commands.cfg會自動配置進去,不需要添加。此外,注意多端口同時監控的寫法。
說明:
從多年的監控經驗看,端口檢查也是很不錯的輔助監控方式!對於要求高的業務,一定要模擬真正用戶的訪問行為監控才好。
5.3 階段性小結
- 實例暫時就說到這里,關於如何讓不同服務在不同時間報警給不同用戶,如何配置nfs,rsync,drbd,MySQL,Oracle等特殊服務的監控,如何分組監控,如何讓多個運維值班協調解決問題等內容, 請見企業級監控Nagios實踐(下)。Nagios很復雜,不僅可以做上面的事,還可以寫插件監控業務層的問題,甚至可以監控到服務器的溫度及硬件信息。而且,Nagios給你擴展開發的機會很多,請大家多去摸索。這里針對上面的內容小結一下:
- 一般客戶端對外開啟的服務1,都會采用主動模式監控,例如:port,URL.
主動模式的監控配置過程如下:
1)在服務器端的命令行把要監控的命令先調試好。
2)在commands.cfg里定義Nagios命令,同時調用命令行的插件。
3)在服務的配置文件里定義要監控的服務,調用commands.cfg里定義Nagios的監控命令!
六 拓展實驗內容:
-
請用主動及被動模式分別監控MySQL主從同步(check_mysql)!
-
根據不同管理員顯示不同的主機和服務,根據用戶分類顯示主機和服務