企業級監控nagios實踐
nagios 監控服務應用指南
小區:視訊監控,保安
企業工作中為什麼要部署監控系統
監控系統相當於哨兵的作用,監控幾百臺上千臺伺服器,監控系統非常重要。
監控系統都需要監控
1. 本地資源:負載uptime cpu(top,sar),磁碟(df),記憶體(free),io(iostat),raid,溫度,passwd檔案的變化,本地所有檔案指紋識別
2. 網路服務:埠,url ,丟包,程序數,網路流量
3. 其他裝置: 路由器、交換機埠流量,印表機,windows等
4. 業務資料:使用者登入失敗次數,使用者登入網站次數,輸入驗證碼失敗的次數,某個api
介面流量併發,電商網站定單,支付交易的數量。
5. 監控軟體本身僅僅是一個平臺,我們想監控的內容,理論上只要在伺服器命令列可以獲取到就可以被監控軟體監控
前言 nagios 監控工具介紹及原理
nagios(難夠死)監控工具介紹與優勢
nagios是一款開源的網路及服務的監控工具,功能強大,靈活性強,能有效監控 windows linux和unix 等系統的主機各種狀態資訊,交換機,路由器等裝置,主機埠及url服務等,根據不同業務故障級別發出告警資訊(郵件、微信、簡訊、語音報警、飛信、msn)給管理員,當故障恢復時也會發出恢復訊息給管理員
nagios服務端可以在linux系統和類unix系統上執行,目前無法再windows上執行(客戶端軟體),windows可以作為被監控的主機,但是不能被作為監控伺服器
nagios 官方網站地址為http://www.nagios.org
nagios的特點
01)監控網路服務(smtp 、pop3 、http、tcp、ping等)
02)監控主機資源
03)簡單的外掛設計模式使得使用者可以方便的定製符合自己的服務的檢測方法
04)並行服務檢查機制
05)具備定義網路分層結構的能力,用‘parent’主機定義來表達網路主機間的關係,這種 關係可被用來返現和明晰主機宕機或不可達狀態
06)當服務或主機問題產生與解決後將及時通報聯絡人(mail、im、sms、sound);
07)自動的日誌回滾
08)可選的web介面用於檢視當前的網路狀態、通知和故障歷史、日誌檔案等
14.2.3 nagios監控系統家族成員的構成
nagios監控一般由一個主程式(nagios)、一個外掛程式(nagios-plugins)和一些可選的附加程式(NRPE、NSClient++、NSCA和NDOUtils)等組成
nagios本身只是一個監控的品臺而已,其具體的監控工作都是通過各類外掛(例如:nagios-plugins)來實現的,也可以自己編寫外掛,因此,nagios主程式和nagios-plugins外掛都是nagios伺服器端必須安裝的程式組建。不過,一般nagios-plugins也要安裝於被監控端,用來獲取響應的資料,nagios可選的附加組建描述如下
1.NRPE元件
存在的位置:工作於被監控端,作業系統為linux系統
作用:用於在被監控的遠端主機上執行指令碼外掛,獲取資料回傳給伺服器端,以實現對這些主機資源和服務的監控
存在形式:守護程序模式,開啟的埠號是5666
監控的資源:主要用於監控本地資源,包括負載(uptime)、cpu(top、sar)磁碟(df -hi)
記憶體(free)、io(iostat)、raid磁碟故障、cpu溫度、passwd檔案的變化,以及本地所有檔案的指紋識別監控,當然也可以監控程序、埠、url等
圖為nrpe元件的執行原理圖
工作原理:通常由nagios伺服器端發起獲取資料請求,由check_nrpe外掛攜帶要獲取的命令,傳給被監控端的nrpe守護程序,nrpe程序讀取nrpe.cfg裡對應伺服器端傳送的命令資訊,呼叫本地外掛獲取資料,然後返回給nagios伺服器端check_nrpe,進而傳給nagios展示到web介面中,嚴格講可以稱之為半被動工作模式,本文主要講解這個npre元件的功能
1.NSClient++元件
用於被監控端為windows系統的伺服器
2.NDOUtils元件(不推薦用)
作用:將nagios的配置資訊和各event產生的資料存入資料庫
3.nsca元件 目前應用場景較少
14.2.4 nagios監控系統完整圖解
14.3 nagios伺服器的安裝
14.3.1 nagios安裝準備
1)準備三臺伺服器
管理ip地址 |
角色 |
備註 |
192.168.1.11 |
nagios-server |
nagios伺服器端 |
192.168.1.12 |
web01 |
被監控的客戶端 |
192.168.1.13 |
web02 |
被監控的客戶端 |
2)解決perl軟體編譯問題
[root@hd1 ~]# echo 'export LC_ALL=C' >>/etc/profile
[root@hd1 ~]# tail -1 /etc/profile
export LC_ALL=C
[root@hd1 ~]# source /etc/profile
[root@hd1 ~]# echo $LC_ALL
C
3)關閉nagios server端防火牆及SELINUX
通過配置檔案檢視selinux的狀態
[root@hd1 ~]# cat /etc/selinux/config
用命令關閉 selinux
[root@hd05 ~]# setenforce 0
5)安裝nagios伺服器端所需軟體包
nagios伺服器端需要有web介面展示監控效果,介面的展示主要使用php程式,因此,需要lamp環境,有些網友總想安裝lnmp環境,這完全是自我麻煩,yum安裝的lamp環境是配合nagios服務端展示介面的最佳環境
安裝下面軟體包
[root@hd1 ~]# yum install gcc glibc glibc-common -y
[root@hd1 ~]# yum install gd gd-devel -y
[root@hd1 ~]# yum -y install mysql-server
[root@hd1 ~]# yum install httpd php php-gd -y
檢視
[root@hd1 ~]# rpm -qa mysql httpd php
mysql-5.1.73-3.el6_5.x86_64
php-5.3.3-38.el6.x86_64
httpd-2.2.15-39.el6.centos.x86_64
6)建立nagios伺服器端需要的使用者及組
批量執行命令如下:
[root@hd1 ~]# useradd nagios
[root@hd1 ~]# groupadd nagcmd
[root@hd1 ~]# usermod -a -G nagcmd nagios
[root@hd1 ~]# usermod -a -G nagcmd apache
[root@hd1 ~]# id -n -G nagios
nagios nagcmd
[root@hd1 ~]# id -n -G apache
apache nagcmd
[root@hd1 ~]# groups nagios
nagios : nagios nagcmd
[root@hd1 ~]# groups apache
apache : apache nagcmd
7)啟動lamp環境的http服務
[root@hd1 ~]# /etc/init.d/httpd start
[root@hd1 ~]# /etc/init.d/httpd start
Starting httpd: httpd: apr_sockaddr_info_get() failed for hd1.com
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[ OK ]
14.3.2安裝nagios伺服器端
[root@hd1 ~]# tar xf nagios-3.5.1.tar.gz
[root@hd1 ~]# cd nagios
[root@hd1 nagios]# ./configure --with-command-group=nagcmd
[root@hd1 nagios]#make all
[root@hd1 nagios]#make install
[root@hd1 nagios]# make install-init 安裝初始化指令碼到/etc/init.d/
[root@hd1 nagios]# make install-config 生成nagios模板配置
[root@hd1 nagios]# make install-commandmode 安裝配置目錄許可外部命令檔案
1)安裝nagios web配置檔案及建立登入使用者
接著來安裝nagios web配置檔案(生成nagios對應於apache裡的配置檔案)
[root@hd1 nagios]# make install-webconf
建立nagios web監控介面後,登入時需要使用者名稱及密碼,這裡分別為admin和123456
[root@hd1 nagios]# htpasswd -bc /usr/local/nagios/etc/htpasswd.users admin 123456
Adding password for user admin
[root@hd1 nagios]# cat /usr/local/nagios/etc/htpasswd.users
admin:4SH4NvORhXMFs
重新載入apache服務
[root@hd1 nagios]# /etc/init.d/httpd reload
2)配置啟動apache服務
啟動apache服務加入系統開機自啟動
[root@hd1 ~]# chkconfig httpd on
開啟客戶端上的瀏覽器訪問http://192.168.1.11/nagios 使用者名稱是:admin密碼是123456
1)安裝nagios外掛軟體包
nagios外掛軟體包就是一些實現獲取資料資訊的命令或程式,通過這些命令或程式, nagios可以獲取到需要的資料,然後進行報警和展示,具體安裝過程如下:
先安裝基礎依賴包
[root@hd1 ~]# yum -y install perl-devel openssl-devel -y
安裝nagios plugins外掛包
[root@hd1 ~]#tar xf nagios-plugins-1.4.13.tar.gz
[root@hd1 ~]# cd nagios-plugins-1.4.13
[root@hd1 nagios-plugins-1.4.13]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-mysql
[root@hd1 nagios-plugins-1.4.13]make
[root@hd1 nagios-plugins-1.4.13]make install
檢視外掛個數
[root@hd1 nagios-plugins-1.4.13]# ls /usr/local/nagios/libexec/|wc -l
61
2)安裝nrpe軟體
nrpe是客戶端安裝的軟體,為什麼還要安裝在nagios伺服器端?
1.nagios服務端需要check_nrpe外掛做被動檢查,如果伺服器端不裝nrpe軟體,就沒有check_npre這樣的檢查外掛
2.nagios伺服器端本地的資源也需要被監控,因此,nagios服務端也會被作為客戶端
[root@hd1 ~]# tar zxvf nrpe-2.12.tar.gz
[root@hd1 ~]# cd nrpe-2.12
[root@hd1 nrpe-2.12]# ./configure
[root@hd1 nrpe-2.12]# make all
[root@hd1 nrpe-2.12]# make install-plugin
[root@hd1 nrpe-2.12]# make install-daemon
[root@hd1 nrpe-2.12]# make install-daemon-config
檢查check_nrpe外掛
[root@hd1 ~]# ls /usr/local/nagios/libexec/check_nrpe
/usr/local/nagios/libexec/check_nrpe
[root@hd1 nrpe-2.12]# ls /usr/local/nagios/libexec/|wc -l
62
提示:生成的nrpe的配置檔案為/usr/local/nagios/etc/nrpe.cfg
6.配置並啟動nagios服務
[root@hd1 ~]# chkconfig nagios on
[root@hd1 ~]# chkconfig nagios --list
nagios 0:off 1:off 2:on 3:on 4:on 5:on 6:off
7.更好的設定自動開機的方法是:
[root@hd1 ~]# echo "/etc/init.d/nagios start" >>/etc/rc.local
檢驗nagios配置檔案(檢查語法)
[root@hd1 ~]# /etc/init.d/nagios checkconfig
Running configuration check... OK.
此命令為檢查語法的命令,但是隻能報告對錯無法給出錯誤的資訊
可以使用命令列檢查報錯,並輸出資訊
[root@hd1 ~]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Total Warnings: 0
Total Errors: 0
可以修改/etc/init.d/nagios實現上述命令列檢查語法的詳細輸出,如下:
[root@hd1 ~]# grep 'checkconfig)' -n -A 2 /etc/init.d/nagios
181: checkconfig)
182- printf "Running configuration check..."
183- $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
grep表示搜尋 checkconfig) 字串,-n表示列印行號,-A 2 表示列印符合條件字串最近的兩行
把 $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1的輸出重定向去掉就可以
[root@hd1 ~]# /etc/init.d/nagios checkconfig
Total Warnings: 0
Total Errors: 0
最後瀏覽nagios web介面檢查,開啟瀏覽器訪問
到此,nagios服務端的安裝及配置就告一段落
14.4 nagios客戶端安裝
14.4.1 nagios客戶端安裝準備
準備2臺伺服器或vm虛擬機器
安裝基礎系統軟體
[root@hd2 ~]# yum install gcc glibc glibc-common -y
[root@hd2 ~]# yum install mysql-server -y
安裝這個目的是為了安裝nagios plugins 生成check_mysql外掛
上傳nagios相關軟體
新增nanios使用者
[root@hd2 ~]# useradd nagios -M -s /sbin/nologin
[root@hd2 ~]# id nagios
uid=503(nagios) gid=503(nagios) groups=503(nagios)
安裝nagios-plugins外掛
[root@hd2 ~]# yum install perl-devel perl-CPAN openssl-devel -y
[root@hd2 ~]# tar xf nagios-plugins-1.4.13.tar.gz
[root@hd2 ~]# cd nagios-plugins-1.4.13
[root@hd2 nagios-plugins-1.4.13]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules --with-mysql
[root@hd2 nagios-plugins-1.4.13]#make && make install
此時,檢查外掛個數
[root@hd2 ~]# ls /usr/local/nagios/libexec/|wc -l
59
5.安裝nagios客戶端的nrpe軟體
[root@hd2 ~]# tar xf nrpe-2.12.tar.gz
[root@hd2 ~]# cd nrpe-2.12
[root@hd2 nrpe-2.12]# ./configure
[root@hd2 nrpe-2.12]# make all
[root@hd2 nrpe-2.12]#make install-plugin
[root@hd2 nrpe-2.12]#make install-daemon
[root@hd2 nrpe-2.12]#make install-daemon-config
6.安裝其他相關的外掛
以下是check_iostat外掛需要的依賴包
root@hd2 ~]# tar zxf Params-Validate-0.91.tar.gz
[root@hd2 ~]# cd Params-Validate-0.91
[root@hd2 Params-Validate-0.91]# perl Makefile.PL
[root@hd2 Params-Validate-0.91]# make && make install
[root@hd2 ~]# tar zxf Class-Accessor-0.31.tar.gz
[root@hd2 ~]# cd Class-Accessor-0.31
[root@hd2 Class-Accessor-0.31]# perl Makefile.PL
[root@hd2 Class-Accessor-0.31]# make && make install
[root@hd2 ~]# tar zxf Config-Tiny-2.12.tar.gz
[root@hd2 ~]# cd Config-Tiny-2.12
[root@hd2 Config-Tiny-2.12]# perl Makefile.PL
[root@hd2 Config-Tiny-2.12]# make && make install
[root@hd2 ~]# tar zxf Math-Calc-Units-1.07.tar.gz
[root@hd2 ~]# cd Math-Calc-Units-1.07
[root@hd2 Math-Calc-Units-1.07]# perl Makefile.PL
[root@hd2 Math-Calc-Units-1.07]# make && make install
[root@hd2]# tar zxf Regexp-Common-2010010201.tar.gz
[root@hd2]# cd Regexp-Common-2010010201
[root@hd2 Regexp-Common-2010010201]# perl Makefile.PL
[root@hd2 Regexp-Common-2010010201]# make && make install
[root@hd2 ~]# tar zxf Nagios-Plugin-0.34.tar.gz
[root@hd2 ~]# cd Nagios-Plugin-0.34
[root@hd2 Nagios-Plugin-0.34]# perl Makefile.PL
[root@hd2 Nagios-Plugin-0.34]# make && make install
[root@hd2 ~]# yum install sysstat -y
sysstat工具包中包含兩類工具,分別為即時檢視工具(iostat、mpstat、sar)和累計統計工具(sar)可以看到,這兩類工具都有sar,可見sar具有著兩種功能
為了實現sar的累計統計功能,系統必須週期性的記錄當時的資訊,這是通過呼叫/usr/lib/sa中的三個工具實現的
sa1 :收集並存儲每天的系統動態資訊到一個二進位制的檔案中,用作sadc的前端程式
sa2:收集每天的系統活躍資訊寫入總結性的報告,用作sar的前端程式
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@hd2 ~]# cp check_memory.pl /usr/local/nagios/libexec/
[root@hd2 ~]# cp check_iostat /usr/local/nagios/libexec/
授權指令碼可執行
[root@hd2 ~]# chmod 755 /usr/local/nagios/libexec/check_memory.pl
[root@hd2 ~]# chmod 755 /usr/local/nagios/libexec/check_iostat
改變指令碼格式為unix的格式
[root@hd2 ~]# yum -y install dos2unix # 安裝dos2unix命令
[root@hd2 ~]# dos2unix /usr/local/nagios/libexec/check_memory.pl
dos2unix: converting file /usr/local/nagios/libexec/check_memory.pl to UNIX format ...
[root@hd2 ~]# dos2unix /usr/local/nagios/libexec/check_iostat
dos2unix: converting file /usr/local/nagios/libexec/check_iostat to UNIX format ...
14.4.4 配置nagios客戶端nrpe服務
[root@hd2 ~]# cd /usr/local/nagios/etc
[root@hd2 etc]# cp nrpe.cfg nrpe.cfg.bak # 備份原始配置檔案
[root@hd2 etc]# sed -n 79p nrpe.cfg #print
allowed_hosts=127.0.0.1
[root@hd2 etc]# sed -i 's#allowed_hosts=127.0.0.1#allowed_hosts=127.0.0.1,192.168.1.11#' nrpe.cfg
[root@hd2 etc]# sed -n 79p nrpe.cfg
allowed_hosts=127.0.0.1,192.168.1.11
加入可以監控nagios server的ip地址192.168.1.11
使用vim命令編輯nrpe.cfg的內容
1)註釋199-203的內容
2)在下面新增新內容
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
我們一般通過nrpe去客戶端執行指令碼外掛獲取資訊,這樣的模式成為被動監控,與nsca的客戶端主動提交結果不衝突,由nagios服務端通過nrpe外掛定時在client的nrpe服務上獲取資訊
啟動nagios nrpe守護程序
[root@hd2 etc]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@hd2 etc]# netstat -lntup|grep nrpe
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 93592/nrpe
這時,可以將nrpe加入開機自啟動了,命令如下
[root@hd2 etc]# echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >>/etc/rc.local
注意:客戶端nrpe.cfg配置檔案,最好保留一份到計算機上,這樣以後在其他的機器上裝nrpe時,直接上傳即可
到此,客戶端的安裝配置部分全部結束
14.5 nagios伺服器端監控
14.5.1 nagios伺服器端監控基礎介紹
1. nagios伺服器端目錄結構
[root@hd1 ~]# ls -l /usr/local/nagios
total 24
drwxrwxr-x. 2 nagios nagios 4096 Oct 21 17:43 bin
drwxrwxr-x. 3 nagios nagios 4096 Oct 21 17:43 etc
drwxrwxr-x. 2 nagios nagios 4096 Oct 21 17:43 libexec
drwxrwxr-x. 2 nagios nagios 4096 Oct 20 17:09 sbin
drwxrwxr-x. 11 nagios nagios 4096 Oct 21 16:55 share
drwxrwxr-x. 5 nagios nagios 4096 Oct 20 17:12 var
bin 目錄為nagios相關命令
etc目錄為nagios的配置檔案及目錄資訊
etc
|-- cgi.cfg
|-- cgi.cfg~
|-- htpasswd.users
|-- nagios.cfg
|-- nagios.cfg~
|-- nrpe.cfg
|-- objects
| |-- commands.cfg
nagios.cfg主配置檔案
nrpe.cfg伺服器端的nrpe的配置檔案
objects具體物件配置檔案
libexec為所有外掛的目錄路徑
var為nagios資料及日誌目錄
share為nagios介面展示的php程式等內容的目錄
所有客戶端本地服務的監控都是通過執行libexec目錄下的外掛來實現的,如果開啟了snmp(簡單網路管理協議)
nagios伺服器端也可以主動抓取
2. nagios伺服器端核心配置檔案
objects目錄裡面放的是主配置檔案nagios.cfg包含的其他nagios配置檔案
配置檔名稱 |
說明 |
command.cfg |
存放nagios命令相關配置,這裡的命令是nagios裡定義的命令和外掛命令相關聯的一個檔案 |
services.cfg |
存放具體被監控的服務相關配置檔案內容 |
hosts.cfg |
存放具體被監控主機相關的配置內容 |
contacts.cfg |
存放報警聯絡人相關配置的檔案 |
timeperiods.cfg |
存放報警週期時間等相關配置內容 |
templates.cfg |
模板配置檔案 |
3. 配置主配置檔案nagios.cfg
[root@hd1 etc]# vim nagios.cfg +34
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
新增37,38行註釋掉36行
localhost.cfg這個配置為監控伺服器本地服務的配置檔案,註釋掉,然後統一監控
根據已有資料生成hosts.cfg主機檔案
[root@hd1 etc]# cd /usr/local/nagios/etc/objects/
[root@hd1 objects]# head -51 localhost.cfg >hosts.cfg
[root@hd1 objects]# chown nagios.nagios hosts.cfg
生成新的services.cfg服務檔案
[root@hd1 objects]# touch services.cfg
[root@hd1 objects]# chown nagios.nagios services.cfg
最後生成服務的配置檔案目錄,所有放到此目錄下的配置都會自動被包含到主配置檔案中生效
[root@hd1 objects]# mkdir services
[root@hd1 objects]# chown -R nagios.nagios services
1. nagios監控模式定義及監控模式選擇
根據監控的行為,將nagios的監控分為主動和被動
主動監控:nagios伺服器發出請求的主動探測監控方式,不需要客戶端安裝任何外掛
被動監控:nagios服務端通過nrpe外掛定時去連線client的nrpe服務獲取資訊,併發回到nagios伺服器的監控是被動監控,這類監控通常是針對本地資源的
如何選擇主動和被動監控
1.對於本地的資源檢視,一般是被動模式例如:負載,記憶體、硬碟、溫度、風扇等
2.對於web服務,資料庫服務這種能對外提供服務的,一般是主動監控 例如httpd sshd mysqld rsyncd 等
主動模式和被動模式也可以相互轉換
14.5.2 配置nagios伺服器端監控項
1.定義要監控的nagios客戶端主機
hosts.cfg一般用來存放nagios要監控的主機相關配置,下面是hosts.cfg中的主機定義部分的配置引數詳解
新增所有需要監控的客戶端主機和主機組
例如:
2.配置services.cfg,定義要監控的資源服務
例項如下
[root@hd1 objects]# vim services.cfg
define service {
use generic-service
host_name web01
service_description Disk Partition
check_command check_nrpe!check_disk
}
define service {
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 {
use generic-service
host_name web01
service_description disk iostat
check_command check_nrpe!check_iostat!5!11
}
define service {
use generic-service
host_name web01
service_description ping
check_command check_ping!100.0,20%!500.0,60%
}
提示:check_nrpe是伺服器端的外掛(是commands.cfg裡預先定義的命令名),負責和客戶端的nrpe程序交流並執行check_nrpe歎號後的外掛,所以,check_nrpe!check_load中的check_load是客戶端的外掛名,是在客戶端的nrpe程序對應的配置nrpe.cfg定義的命令名
nagios軟體預設沒有提供客戶端的記憶體和i/o外掛,但是本文在配置時已經複製進去了,因此,只需在commands.cfg裡配置即可
3..除錯hosts.cfg和services.cfg的所有配置
1)需要在commands.cfg中加入check_nrpe的外掛配置
執行檢查語法命令
[root@hd1 objects]# /etc/init.d/nagios checkconfig
Total Warnings: 0
Total Errors: 0
警告和錯誤都是0 表示已經ok了
啟動nagios服務
[root@hd1 objects]# /etc/init.d/nagios reload
Running configuration check...done.
Reloading nagios configuration...done
此時,可以看到自己配置的本地個系統狀態的監控結果了
出現如上結果,表示登入web使用者沒有被許可檢視這些服務資源,可按照如下方法解決上面的問題
[root@hd1 objects]# cd /usr/local/nagios/etc
檢視許可權
[root@hd1 etc]# grep "^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
替換成admin使用者
[root@hd1 etc]# sed -i 's#nagiosadmin#admin#g' cgi.cfg
再次檢視
[root@hd1 etc]# grep "^authorized_for" cgi.cfg
authorized_for_system_information=admin
authorized_for_configuration_information=admin
authorized_for_system_commands=admin
authorized_for_all_services=admin
authorized_for_all_hosts=admin
authorized_for_all_service_commands=admin
authorized_for_all_host_commands=admin
重新載入配置檔案
[root@hd1 etc]# /etc/init.d/nagios reload
再次通過瀏覽器檢視
up 意思是伺服器正常
pending的意思是伺服器還沒確定狀態 稍等
紅色表示有故障
提示:
1)最好換掉預設管理員使用者nagiosadmin,替換成admin
2)遇到除錯問題注意檢視/usr/local/nagios/var/nagios.log這點很重要
#要養成經常檢視日誌的習慣
》》》》基於nagios監控原理排錯的案例
nagios被動監控的原理其實就是利用下面這個命令工作的
[root@hd1 objects]# /usr/local/nagios/libexec/check_nrpe -H 192.168.1.12 -c check_iostat
IOSTAT OK - user 0.07 nice 0.00 sys 0.20 iowait 0.04 idle 0.00 | iowait=0.04%;; idle=0.00%;; user=0.07%;; nice=0.00%;; sys=0.20%;;
我們來看一個錯誤的案例
上圖說明有一個服務顯示不正常 disk iostat
1)在nagios伺服器端執行如下命令
[root@hd1 objects]# /usr/local/nagios/libexec/check_nrpe -H 192.168.1.12 -c check_iostat
check_swap: Warning free space should be more than critical free space
這個跟圖形報錯一樣
2)在客戶端本地執行指令碼檢查命令
[root@hd2 ~]# cd /usr/local/nagios/libexec/
[root@hd2 libexec]# ./check_iostat -w 6 -c 10
IOSTAT OK - user 0.07 nice 0.00 sys 0.20 iowait 0.04 idle 0.00 | iowait=0.04%;; idle=0.00%;; user=0.07%;; nice=0.00%;; sys=0.20%;;
發現很正常,這說明客戶端的配置檔案或者是伺服器端的配置檔案的問題
3)檢查客戶端的配置檔案
[root@hd2 etc]# vim nrpe.cfg
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
command[check_iostat]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
4)修改
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
5)重啟
[root@hd2 etc]# pkill nrpe
[root@hd2 etc]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
6)重新整理檢視(務必耐心等待)
可以看到disk iostat服務ok了
5新增http服務的url地址及埠監控
url監控的是指是通過命令列理解http的監控原理,如下
[root@hd1 ~]# cd /usr/local/nagios/libexec/
[root@hd1 libexec]# ./check_http -H 192.168.1.12
下面將192.168.1.12配置成一個web伺服器
[root@hd2]# yum -y install httpd
[root@hd2]# /etc/init.d/httpd start
[root@hd2]# cd /var/www/html/
[root@hd2 html]# echo "woshi 192.168.1.12" >index.html
下面對url地址進行監控,將要監控的服務配置到services.cfg中即可
define service {
use generic-service
host_name web01
service_description http_url
check_command check_http
}
配置好之後,檢查nagios語法
[root@hd1 objects]# /etc/init.d/nagios checkconfig
Total Warnings: 0
Total Errors: 0
重新載入配置檔案
[root@hd1 objects]# /etc/init.d/nagios reload
此時,可以看到自己配置的url監控成果了
7.監控任意tcp udp埠舉例
埠監控的實質就是執行如下命令去監控
ip 協議 網路層 三層
tcp協議,傳輸控制協議,四層,穩定的協議,打電話
upd 協議,四層,使用者資料報協議,短訊息qq
http 應用層的協議 check_http check_tcp 80
我們把22埠號新增進去進行監控
define service {
use generic-service
host_name web01
service_description sshd
check_command check_tcp!22
}
[root@hd1 objects]# /etc/init.d/nagios reload
Running configuration check...done.
Reloading nagios configuration...done
這裡的check_tcp為nagios plugin預設外掛,commands.cfg會自動配置進去,不需要新增
埠檢查也是很不錯的輔助監控方式
小結:
主動模式的監控配置過程如下:
1)在伺服器端的命令列把要監控的命令先除錯好
2)在commands.cfg裡定義nagios命令,同時呼叫命令列的外掛
3)在服務的配置檔案裡定義要監控的服務,呼叫commands.cfg裡定義的nagios的監控命令
4)如果我們在伺服器端寫了一個指令碼,必須在commands.cfg定義
5)如果我們在客戶端寫了一個指令碼,必須在客戶端的nrpe.cfg裡定義命令
14.6 伺服器端nagios圖形監控顯示和管理
14.6.1 伺服器端安裝pnp生成圖形監控曲線
1.pnp出圖基礎依賴軟體安裝
先通過下面的命令安裝pnp軟體需要的基礎包
[root@hd1 ~]# yum -y install cairo pango zlib zlib-devel freetype freetype-devel gd gd-devel
然後安裝rrdtool依賴的libart_lgpl相關軟體包,這個軟體包要優於rrdtool安裝
[root@hd1 ~]# yum install libart_lgpl libart_lgpl-devel -y
png工具最終是通過rrdtool實現的畫圖,因此需要提前安裝rrdtool
root@hd1 ~]# yum install rrdtool -y
2.安裝出圖web介面展示軟體pnp
此處選擇0.4.14的pnp版本,如果選擇高版本在出圖方面可能會有坑,正常情況下,選0.4版已經足夠了
[root@hd1 ~]# yum -y install perl-Time-HiRes
[root@hd1 ~]# tar zxf pnp-0.4.13.tar.gz
[root@hd1 ~]# cd pnp-0.4.13
[root@hd1 pnp-0.4.13]# ./configure \
> --with-rrdtool \
> --with-perfdata-dir=/usr/local/nagios/share/perfdata/
[root@hd1 pnp-0.4.13]# make all
[root@hd1 pnp-0.4.13]# make install
[root@hd1 pnp-0.4.13]# make install-config
[root@hd1 pnp-0.4.13]# make install-init
[root@hd1 pnp-0.4.13]# ls /usr/local/nagios/libexec/|grep process
process_perfdata.pl
pnp提供了一個獲取資料出圖的perl指令碼,可以用如下命令查到,
[root@hd1 pnp-0.4.13]# ll /usr/local/nagios/libexec/|grep process
-rwxr-xr-x. 1 nagios nagios 30138 Oct 27 10:33 process_perfdata.pl
此時開啟瀏覽器訪問http://192.168.1.11/nagios/pnp 會出現下圖所示
過一會兒重新訪問上述地址就會恢復正常
如果過了很長時間還是現實不正常,可以執行如下命令
[root@hd1 pnp-0.4.13]# yum install -y php-gd
[root@hd1 pnp-0.4.13]# yum install -y gd
[root@hd1 pnp-0.4.13]# rpm -ivh /root/gd-devel-2.0.35-11.el6.x86_64.rpm
3.nagios出圖相關配置
1)執行編輯命令 ‘vi /usr/local/nagios/etc/nagios.cfg +835’ ,修改主配置檔案835行,將如下引數對應的值從0改為1 ,表示記錄資料
大概從848行開始,找到如下兩項,取消引數開頭的註釋,修改後的最終結果如下:
2)執行編輯命令 ‘vi /usr/loca/nagios/etc/objects/commands.cfg +227’ ,修改commands.cfg命令配置檔案,定義出圖獲取資料的命令
要修改的是commands.cfg配置檔案的第227-238行,預設的配置如下
define command{
command_name process-host-perfdata
command_line /usr/local/nagios/libexec/process_perfdata.pl
}
# 'process-service-perfdata' command definition
define command{
command_name process-service-perfdata
command_line /usr/local/nagios/libexec/process_perfdata.pl
}
重啟配置檔案
[root@hd1 ~]# /etc/init.d/nagios reload
Running configuration check...done.
Reloading nagios configuration...done
4)此時在瀏覽器輸入’http://192.168.1.11/nagios/pnp/index.php’開啟頁面,正確的pnp介面如下
到這裡為止,pnp軟體的出圖就ok了,但是還沒有業務資料的圖形趨勢,因為還沒有配置呢,接下去就來配置
14.6.2 配置主機及服務獲取狀態資料出圖
1.設定讓被監控的主機記錄資料
如果要讓所有的主機獲取資料並出趨勢圖,則需編輯nagios的主機hosts.cfg檔案,
不過,只要在每一個被監控主機的配置下面增加同一個引數項 ‘process_perf_data 1’
即可
[root@hd1 ~]# cd /usr/local/nagios/etc/objects/
[root@hd1 objects]# vi hosts.cfg
# Define a host for the local machine
define host{
use linux-server
host_name web01
alias web01
address 192.168.1.12
process_perf_data 1
}
2.設定讓被監控主機對應的服務記錄資料
[root@hd1 objects]# vim services.cfg
define service {
use generic-service
host_name web01
service_description Disk Partition
check_command check_nrpe!check_disk
process_perf_data 1
}
define service {
use generic-service
host_name web02
service_description Disk Partition
check_command check_nrpe!check_disk
process_perf_data 1
}
如何批量插入一行呢?
[root@hd1 objects]# sed -i "/check_command/a process_perf_data 1 " services.cfg
[root@hd1 objects]# sed -i "s#,,,# #" services.cfg
還可以對採取對所有服務對應的統一模板裡新增配置引數的方式
服務裡的use generic-service 已經配置了
[root@hd1 objects]# sed -n '154,177p' templates.cfg
name generic-service
failure_prediction_enabled 1
process_perf_data 1
重啟nagios服務
[root@hd1 objects]# /etc/init.d/nagios reload
Running configuration check...done.
Reloading nagios configuration...done
到此,如果等一段時間,然後檢視pnp url就可以發現生成了圖形資料,有些資料需要壓測或者真實環境才能看
14.6.3 整合pnp url超連結到nagios web介面
1.給被監控的所有主機新增超連結圖示
[root@hd1 objects]# cat hosts.cfg
define host{ uselinux-server host_nameweb01 aliasweb01 address192.168.1.12 process_perf_data1 action_url/nagios/pnp/index.php?host=$HOSTNAME$
2.給被監控的主機指定的服務新增超連結圖示
刪除空行 : sed '/^$/d' services.cfg
重新載入配置
[root@hd1 objects]# /etc/init.d/nagios reload
測試一下
14.7 實現對nagios故障報警給管理員
將要nagios故障報警給管理員時,常用的方式包括郵件報警和手機報警,下面分別介紹
1.郵件報警
普通郵件報警就是在故障發生或恢復時,將報警資訊發到系統管理員或相關維護人的信箱中,最好是公司內部郵箱。
定義收郵件的郵箱
[root@hd1 ~]# vim /usr/local/nagios/etc/objects/contacts.cfg
常見的傳送郵件的方法有兩種 ,一種是啟動本機的郵件服務postfix ,另外一種是使用網上第三方郵件服務提供商的服務,例如:qq郵件服務或網易郵件服務
法1的配置
[root@hd1 nagios]# /etc/init.d/postfix restart
Shutting down postfix: [ OK ]
Starting postfix: [ OK ]
[root@hd1 nagios]# lsof -i :25
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
master 32807 root 12u IPv4 57932 0t0 TCP localhost:smtp (LISTEN)
master 32807 root 13u IPv6 57934 0t0 TCP localhost:smtp (LISTEN)
如果postfix啟動比較慢,可以修改/etc/hosts做好本機ip和主機名的對映
提示:此環境下由於沒有外網ip,並且郵件服務沒有做好mx記錄及反向解析,因此,郵件經常會收不到,或者當做垃圾郵件
法二:使用網上第三方郵件服務提供商比如163的郵箱
注意:這裡的163郵箱作為報警的發件人,相當於[email protected] 使用者登入郵箱,然後給人發信,收件人就是在contact.cfg裡定義的
[root@hd1 ~]# vim /etc/mail.rc
[root@hd1 nagios]# /etc/init.d/postfix restart
在這裡說一下有些運營商 可以開啟郵件簡訊提醒也比較方便
14.8 nagios外掛開發
14.8.1 概述
1.什麼是nagios外掛
前文在部署nagios服務時已經安裝了nagios-plugins-1.4.6.tar.gz這個軟體包就是nagios外掛安裝包
可以看到有很多外掛,其實,nagios軟體本身僅僅是一個監控的平臺,如果要監控具體的主機及服務的狀態和資料資訊,還必須配置或呼叫外掛或程式檔案才能完成任務,因此,如果沒有外掛,nagios就是一個空殼,什麼都做不了
2.為什麼要開發nagios外掛
大部分服務都不需要開發外掛,但有部分我們要監控的服務,是nagios裡沒有外掛,需要我們自己開發編寫指令碼
14.8.2 編寫nagios外掛的規則
1.編寫nagios外掛說明
nagios外掛是nagios提供的一種通過可擴充套件的方式部署的程式元件,該外掛可通過shell、java、c++、php、python等多種開發語言開發,運維或系統架構人員只要通過修改nagios配置檔案和相應的引數,就能很方便地將該外掛整合到nagios中,實現對目標系統的監控
nagios服務為外掛程式提供了兩個返回值介面和外掛互動:一個是外掛執行後的退出狀態碼,另一個是外掛執行過程中在控制檯列印的第一行資料,退出狀態碼可以被nagios主程式作為判斷被監控系統服務狀態的依據,控制檯列印的第一行數可以被nagios主程式作為被監控系統服務狀態的補充說明,會顯示在web管理介面裡
注意:如果是一個主動監控外掛,需要放在nagios伺服器端/usr/local/nagios/liexec,並且需要在commands.cfg檔案中定義本外掛,然後需要在services.cfg 中新增一個新的監控服務,在這個服務裡,呼叫這個外掛,如果是一個被動監控外掛,需要放在nagios客戶端/usr/local/nagios/liexec,並且需要在客戶端的nrpe.cfg裡定義這個外掛,然後需要在nagios伺服器端services.cfg 裡新增一個新的監控服務,並且在這個服務裡,呼叫這個外掛,注意呼叫的時候,別忘了前面有check_nrpe!chanjian
退出狀態碼和說明如下:
ok 退出程式碼,0表示服務工作正常
warning 退出程式碼,1表示服務處於警告狀態
critical 退出程式碼,2表示服務處於緊急、嚴重狀態
unknown 退出狀態碼,3表示服務出於未知狀態
[root@hd1 libexec]# head -7 utils.sh
#! /bin/sh
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4
2.nagios外掛開發原理
nagios外掛程式中需要呼叫監控服務規定的操作序列,並根據預選定義的規則,對返回結果進行分析,判斷服務的當前狀態,然後以指定的狀態碼退出程式,同時將對該狀態的說明不換行輸出到控制檯
14.8.3 使用shell開發nagios外掛
1.編寫檢查webrul地址的外掛
[root@hd1 libexec]# cat check_url.sh
#!/bin/sh
#get the shell script name
progname=`basename $0`
#get the file path
progpath=`dirname $0`
usage() {
echo "usage: /bin/sh $progname url"
exit 1
}
#
[ $# -ne 1 ] && usage
wget -T 10 --spider $1 >/dev/null 2>&1
#此處解釋 wget命令
#-T, --timeout=SECONDS設定讀取時超過的時間為SECONDS秒.
#-t 設定重試次數。當連線中斷(或超時)時,wget將試圖重新連線。如果指定-t0,則重#試次數設為無窮多。
#-c指定斷點續傳功能。實際上,wget預設具有斷點續傳功能,只有當你使用別的ftp工具#下載了某一檔案的一部分,並希望wget接著完成此工作的時候,才需要指定此引數。
if [ $? -eq 0 ]
then
echo "url $1 ok "
exit 0
else
echo "url $1 critical"
exit 2
fi
上述指令碼中 basename、dirname是系統命令
[root@hd1 libexec]# basename /usr/local/nagios/libexec
libexec
[root@hd1 libexec]# dirname /usr/local/nagios/libexec
/usr/local/nagios
測試一下webrul外掛指令碼
[root@hd1 libexec]# sh check_url.sh 192.168.1.12
url 192.168.1.12 ok
2.weburl外掛指令碼部署過程
1)將check_url.sh放到/usr/local/nagios/libexec中,並授權為可執行
[root@hd1 libexec]# chmod +x check_url.sh
2)在command.cfg建立check_url命令;
3)在services.cfg裡新增監控上述url地址的服務
define service {
use generic-service
host_name web01
service_description http_zhudong_url
check_command check_url
4.重新載入nagios ,檢視結果
[root@hd1 objects]# /etc/init.d/nagios reload
Running configuration check...done.
Reloading nagios configuration...done
5.重新檢視nagios服務頁面的監控結果
注意:此次屬於主動監控的方式,所以可以停掉客戶端的nrpe程序
3.利用被動模式的nrpe方式監控/etc/passwd 檔案是否變化
nagios被動模式下的所有外掛都需要部署在被監控的nagios客戶端,部署步驟如下:
1)在nagios客戶端web01 上取/etc/passwd的檔案指紋,即md5值
[root@hd2 ~]# md5sum /etc/passwd >/root/ps.md5
2)在nagios客戶端上web01上開發外掛指令碼,並測試
[root@hd2 libexec]# cat check_passwd #!/bin/bash orimd5="f7e63f1940dba72443afc319dfadc7f6" currmd5=`md5sum /etc/passwd|cut -c 1-32` if [ "$orimd5" == "$currmd5" ] then echo "/etc/passwd:ok" exit 0 else echo "/etc/passwd:failed" exit 2 fi
測試指令碼
[root@hd2 libexec]# sh check_passwd
/etc/passwd:ok
給指令碼許可權
[root@hd2 libexec]# chmod +x check_passwd
1)在nagios客戶端web01上編輯nrpe.cfg,插入如下的內容後儲存
command[check_passwd]=/usr/local/nagios/libexec/check_passwd
4)在nagios客戶端上重啟nrpe,並檢查是否重啟成功
[root@hd2 etc]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@hd2 etc]# ps -ef|grep nrpe|grep -v grep
nagios 126175 1 0 09:00 ? 00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
5)在nagios伺服器端上上進入services.cfg 檔案新增如下集行內容
6)在nagios服務端檢查語法並重啟服務
[root@hd1 objects]# /etc/init.d/nagios reload
Running configuration check...done.
Reloading nagios configuration...done
7)進入伺服器端的/usr/local/nagios/libexec 目錄,手動測試
8)在客戶端進行測試
9)通過頁面進行檢查
到此,nagios結束了。
常見問題
1.出現nrpe:unable to read output
可能是:客戶端對應外掛命令不存在或者無執行許可權等原因導致
2.出現nrpe:command ·check_passwd· not defined 類似的問題
可能是:伺服器端的服務裡配置的命令與客戶端的nrpe.cfg裡配置的命令名不匹配導致的
開發一個主動監控的外掛
1.在libexec下編寫外掛並給執行許可權
[root@hd1 libexec]# cat check_htf_ping #! /bin/bash ping 192.168.1.12 -c 4 >/dev/null 2>&1 if [ $? -eq 0 ] then echo "web01 is online" else echo "web01 no online" fi
[root@hd1 libexec]# chmod 755 check_htf_ping
2.在commands.cfg 定義外掛命令
[root@hd1 libexec]# cat ../etc/objects/commands.cfg # 'check_ping' command definition define command{ command_namecheck_htf_ping command_line$USER1$/check_htf_ping }
3.在services.cfg裡配置服務,呼叫這個外掛
[root@hd1 libexec]# cat ../etc/objects/services.cfg define service { usegeneric-service host_nameweb01 service_descriptionping 192.168.1.12 check_commandcheck_htf_ping process_perf_data1 action_url/nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$ }
4.重啟服務,去瀏覽器上檢視並驗證
[root@hd1 libexec]# /etc/init.d/nagios restart
編寫一個被動外掛,監控客戶端的/dev/sda3分割槽的如果磁碟佔用率超過7%報警exit 1
如果不大於7%不報警,exit 0
在web01 的nrpe.cfg 中新增一行
然後重啟 nrpe
在伺服器端 services.cfg中新增
然後重新載入nagios
/etc/init.d/nagios reload
收到郵件也受到了簡訊通知 服務搭建ok!!
提示:
ok 退出程式碼,0表示服務工作正常
warning 退出程式碼,1表示服務處於警告狀態
critical 退出程式碼,2表示服務處於緊急、嚴重狀態
如果有什麼不太好解決的問題,建議大家多分析系統日誌。
/var/log/messages