在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Linux/ 使用 Nagios 搭建監(jiān)控服務(wù)器
初步進(jìn)入 linux 世界
Linux 系統(tǒng)的遠(yuǎn)程登錄
配置 Tomcat
配置 samba 服務(wù)器
LNMP 環(huán)境搭建
配置 FTP 服務(wù)
linux 系統(tǒng)用戶以及用戶組管理
正則表達(dá)式
LAMP環(huán)境搭建
安裝 RPM 包或者安裝源碼包
NFS 服務(wù)配置
文檔的壓縮與打包
Linux 操作系統(tǒng)的安裝
配置 squid 服務(wù)
Linux 文件與目錄管理
Linux 磁盤管理
學(xué)會(huì)使用簡(jiǎn)單的 MySQL 操作
關(guān)于 Linux 的歷史
學(xué)習(xí) shell 腳本之前的基礎(chǔ)知識(shí)
圖形界面還是命令窗口
文本編輯工具 vim
shell 腳本
使用 Nagios 搭建監(jiān)控服務(wù)器
linux 系統(tǒng)日常管理

使用 Nagios 搭建監(jiān)控服務(wù)器

關(guān)于Nagios

Nagios是一款用于監(jiān)控系統(tǒng)和網(wǎng)絡(luò)的開(kāi)源應(yīng)用軟件,它的模式是服務(wù)器—客戶端,也就是說(shuō)首先要在在一臺(tái)服務(wù)器上(server)部署相應(yīng)的主要套件,然后在要監(jiān)控的服務(wù)器上部署客戶端程序,這樣server會(huì)和client通信,從而監(jiān)控client端的各項(xiàng)資源。Nagios功能十分強(qiáng)大幾乎所有的項(xiàng)目都可以監(jiān)控,大到服務(wù)器的存活狀態(tài),小到服務(wù)器上的某一個(gè)服務(wù)(web)。這些功能都是通過(guò)自定義插件(或者叫做腳本)來(lái)實(shí)現(xiàn)。

當(dāng)Nagios監(jiān)控到某項(xiàng)資源發(fā)生異常會(huì)通知到用戶,你可以接入手機(jī)短信接口也可以接入郵件接口。我們可以通過(guò)web頁(yè)面來(lái)查看Nagios所監(jiān)控的各項(xiàng)資源,默認(rèn)搭建的Nagios服務(wù)器只能監(jiān)控簡(jiǎn)單的幾個(gè)項(xiàng)目,而其他服務(wù)之類的監(jiān)控項(xiàng)目都是由我們自己開(kāi)發(fā)的插件來(lái)實(shí)現(xiàn)的。

需要下載的軟件

nagios-3.0.5

nagios-plugins-1.4.13

nrpe-2.12.tar.gz

apache-2.2.11

// 以上軟件版本可以不一樣

監(jiān)控中心Server端的配置

  1. 安裝apache (略,請(qǐng)參考第16章中相關(guān)內(nèi)容,只需安裝,到后邊再配置)

  2. 建立nagios賬戶

useradd nagios

  1. 下載軟件

cd /usr/local/src/

wget http://syslab.comsenz.com/downloads/linux/nagios-3.0.5.tar.gz

wget http://syslab.comsenz.com/downloads/linux/nagios-plugins-1.4.13.tar.gz

wget http://syslab.comsenz.com/downloads/linux/nrpe-2.12.tar.gz

  1. 編譯安裝nagios

cd /usr/local/src/

tar zxvf nagios-3.0.5.tar.gz

cd nagios-3.0.5

./configure --prefix=/usr/local/nagios

make all

make install

make install-init # 把nagios做成一個(gè)運(yùn)行腳本,使nagios隨系統(tǒng)開(kāi)機(jī)啟動(dòng)

make install-config # 把配置文件樣例復(fù)制到nagios的安裝目錄

make install-commandmode # 給外部命令訪問(wèn)nagios配置文件的權(quán)限

chown -R nagios:nagios /usr/local/nagios

  1. 編譯安裝nagios-plugins

cd /usr/local/src/

tar zxvf nagios-plugins-1.4.13.tar.gz

cd nagios-plugins-1.4.13

./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios

make && make install

查看是否安裝成功的方法是:

ls /usr/local/nagios/libexec/ 看這個(gè)目錄下是否有插件文件

  1. 安裝nrpe

cd /usr/local/src/

tar zxvf nrpe-2.12.tar.gz

cd nrpe-2.12

./configure --enable-ssl --enable-command-args

make all

make install-plugin

make install-daemon

make install-daemon-config

  1. 配置web接口

vim /usr/local/apache2/conf/httpd.conf

在最后加入以下內(nèi)容:

ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin
<Directory "/usr/local/nagios/sbin/">
     AllowOverride AuthConfig
     Options ExecCGI
     Order allow,deny
     Allow from all
</Directory>
  Alias /nagios/ /usr/local/nagios/share/
<Directory "/usr/local/nagios/share">
     Options None
     AllowOverride AuthConfig
     Order allow,deny
     Allow from all
</Directory>
  1. 配置nagios

cd /usr/local/nagios/etc/

vim cgi.cfg

把 use_authentication=1 改成 use_authentication=0 意思是不用用戶驗(yàn)證

  1. 啟動(dòng)nagios

在啟動(dòng)前先檢測(cè)一下:

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

如果最后顯示如下,則說(shuō)明配置沒(méi)有問(wèn)題了。

Total Warnings: 0

Total Errors: 0

啟動(dòng)命令:

/etc/init.d/nagios start

或者:

/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg

此時(shí),就可以訪問(wèn)web頁(yè)面的nagios了,在瀏覽器中輸入:

http://IP/nagios/ 看看吧。

在要監(jiān)控的機(jī)器上client部署nagios

如果你打開(kāi)了web頁(yè)面,點(diǎn)擊左欄的Host Detail 會(huì)在右欄看到一行數(shù)據(jù),其中Host 名為 “l(fā)ocalhost” ,Status顯示為”up”,并且顯示為綠色,如果是其他顏色就說(shuō)明你的localhost出了問(wèn)題。目前只有一行數(shù)據(jù),也就是說(shuō)只監(jiān)控了監(jiān)控中心(localhost)一臺(tái)主機(jī),那么如何添加其他機(jī)器被它監(jiān)控呢?這就需要在要被監(jiān)控的機(jī)器上也部署nagios軟件。

  1. 添加賬戶

useradd nagios

  1. 安裝nrpe

cd /usr/local/src/

wget http://syslab.comsenz.com/downloads/linux/nrpe-2.12.tar.gz

tar zxvf nrpe-2.12.tar.gz

cd nrpe-2.12

./configure --enable-ssl --enable-command-args

make all

make install-plugin

make install-daemon

make install-daemon-config

  1. 安裝nagios-plugin

cd /usr/local/src/

wget http://syslab.comsenz.com/downloads/linux/nagios-plugins-1.4.13.tar.gz

tar zxvf nagios-plugins-1.4.13.tar.gz

cd nagios-plugins-1.4.13

./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios

make && make install

到此就算安裝完成了,請(qǐng)查看/usr/local/nagios/目錄下是否有四個(gè)目錄分別為:bin etc libexec share 另外在libexec目錄下會(huì)有很多check_開(kāi)頭的文件。如果你的機(jī)器上沒(méi)有,就請(qǐng)重新安裝吧。

  1. 配置

vim /usr/local/nagios/etc/nrpe.cfg

找到”allowed_hosts=127.0.0.1” 改成 “allowed_hosts=127.0.0.1,10.0.4.67”

// 后邊的IP是server的IP

找到” dont_blame_nrpe=0” 改成 “dont_blame_nrpe=1”

  1. 啟動(dòng)nrpe

/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

在監(jiān)控中心添加被監(jiān)控主機(jī)

添加主機(jī)當(dāng)然是要到server端(監(jiān)控中心)修改配置文件了。

  1. 修改主配置文件

cd /usr/local/nagios/etc/

vim nagios.cfg

增加內(nèi)容:

cfg_dir=/usr/local/nagios/etc/services ##定義一個(gè)目錄,以后把新增加的主機(jī)信息文件全部放到這里

  1. 添加被監(jiān)控主機(jī)信息

mkdir /usr/local/nagios/etc/services

cd /usr/local/nagios/etc/services

vim 10.0.4.56.cfg 加入如下內(nèi)容:

define host{
        use     linux-server
        host_name       10.0.4.56
        alias           10.0.4.56
        address         10.0.4.56
}
define service{
        use     generic-service
        host_name       10.0.4.56
        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       10.0.4.56
        service_description     check_ssh
        check_command           check_ssh
        max_check_attempts 5
        normal_check_interval 1
}
define service{
        use     generic-service
        host_name       10.0.4.56
        service_description     check_http
        check_command           check_http
        max_check_attempts 5
        normal_check_interval 1
}

// 注意,這里的IP是client端的IP,監(jiān)控的項(xiàng)目有三個(gè)ping, ssh, http。其實(shí)這三個(gè)項(xiàng)目使用的腳本都為本地腳本,也就是說(shuō),即使遠(yuǎn)程主機(jī)沒(méi)有安裝nagios和nrpe同樣可以監(jiān)控這些項(xiàng)目。但是如果想監(jiān)控load,disk,等等就需要通過(guò)nrpe服務(wù)來(lái)搞定了,道理很簡(jiǎn)單,load和disk都需要登錄到遠(yuǎn)程主機(jī)上去獲得信息,而ping,ssh,http都不需要的。這個(gè)到遠(yuǎn)程主機(jī)獲取相關(guān)的信息的過(guò)程是由nrpe完成的。如果你的client上沒(méi)有啟動(dòng)nrpe服務(wù)那么我們是無(wú)法獲取遠(yuǎn)程主機(jī)的load和disk等信息的。下面筆者配置一下使用nrpe來(lái)監(jiān)控遠(yuǎn)程主機(jī)的相關(guān)項(xiàng)目。

在server端編輯/usr/local/nagios/etc/objects/commands.cfg

vim /usr/local/nagios/etc/objects/commands.cfg # 在最后面添加如下內(nèi)容

define command{
        command_name    check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }

然后編輯10.0.4.56.cfg (還是server上)

cd /usr/local/nagios/etc/services

vim 10.0.4.56.cfg # 加入如下內(nèi)容:

define service{
        use     generic-service
        host_name       10.0.4.56
        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       10.0.4.56
        service_description     check_disk_hda1
        check_command           check_nrpe!check_hda1
        max_check_attempts 5
        normal_check_interval 1
}

define service{
        use     generic-service
        host_name       10.0.4.56
        service_description     check_disk_hda2
        check_command           check_nrpe!check_hda2
        max_check_attempts 5
        normal_check_interval 1
}

這里需要解釋一下相關(guān)的”check_command”, 先看這個(gè)” check_nrpe!check_load” 這里的check_nrpe就是上面/usr/local/nagios/etc/objects/commands.cfg中剛剛定義的,后面的check_load是在遠(yuǎn)程主機(jī)上定義的一個(gè)命令腳本。具體在哪里定義稍后介紹。為什么中間加一個(gè)”!”,這個(gè)是nagios特有的形式,無(wú)需關(guān)心。下面需要到遠(yuǎn)程主機(jī)上去定義上面用到的腳本了。

在遠(yuǎn)程主機(jī)上編輯/usr/local/nagios/etc/nrpe.cfg 文件

vim /usr/local/nagios/etc/nrpe.cfg (client上)

把” command[check_hda1]”那行改成:

command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1

然后再增加一行:

command[check_hda2]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda2

這里的check_hda1 和 check_hda2 都是自定義的,和server端的定義的service中的check_command對(duì)應(yīng)。也就是說(shuō),如果在server端定義了一個(gè)service(通過(guò)nrpe方式)那么必須要在客戶端上的nrpe.cfg中定義相應(yīng)的腳本。保存這個(gè)文件后,需要重新啟動(dòng)一下nrpe服務(wù)。

killall nrpe ; /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d (client上)

  1. 重啟nagios服務(wù)

修改了配置需要重啟服務(wù)才能使添加的監(jiān)控主機(jī)生效。

/etc/init.d/nagios restart (server上)

此時(shí)再到web頁(yè)面去觀察是否多了一臺(tái)10.0.4.56

在nagios客戶端上自定義監(jiān)控腳本

在開(kāi)始,筆者就講過(guò),我們可以自定義寫(xiě)監(jiān)控腳步,從上面的例子中也可以看到監(jiān)控磁盤狀態(tài)時(shí),是根據(jù)磁盤分區(qū)來(lái)監(jiān)控的。這樣不免有些麻煩,因?yàn)槊颗_(tái)主機(jī)的磁盤分區(qū)狀況都不一樣(一樣還好),而且有多少個(gè)分區(qū)就需要定義多少個(gè)命令。所以筆者就自定義寫(xiě)一個(gè)shell腳本來(lái)監(jiān)控所有的磁盤分區(qū):

  1. 在客戶端上創(chuàng)建腳本/usr/local/nagios/libexec/check_disk.sh

vim /usr/local/nagios/libexec/check_disk.sh 寫(xiě)入如下內(nèi)容:(client上)

#!/bin/bash
row=`df -h -P|wc -l`
status=0
for i in `seq 2 $row`
do
        spare=`df -h -P|sed -n "$i"p|awk '{print $4}'`
        use_percentage=`df -h -P|sed -n "$i"p|sed -n "s/\%//"p|awk '{print $5}'`
        spare_percentage=`expr 100 - $use_percentage`
        partition_name=`df -h -P|sed -n "$i"p|awk '{print $6}'`
        if [ "$spare_percentage" -lt "3"  ];then
                echo -n "$partition_name CRITICAL ${spare_percentage}% $spare  "
                status[$i]=2
        elif [ "$spare_percentage" -lt "5" ];then
                echo -n  "$partition_name WARNING! ${spare_percentage}% $spare  "
                status[$i]=1
        else
                echo -n  "$partition_name OK ${spare_percentage}% $spare  "
                status[$i]=0
        fi
done
zhuangtai=0
for j in `seq 2 $row`
do
        if [ "${status[$j]}" -gt "$zhuangtai"  ];then
                zhuangtai=${status[$j]}
        fi
done
exit $zhuangtai
  1. 保存后,修改該腳本的權(quán)限

chmod +x /usr/local/nagios/libexec/check_disk.sh (client上)

  1. 然后編輯/usr/local/nagios/etc/nrpe.cfg文件

vim /usr/local/nagios/etc/nrpe.cfg # 加入一行:(client上)

command[check_disk]=/usr/local/nagios/libexec/check_disk.sh

保存,重啟nrpe服務(wù)

killall nrpe ; /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d (client上)

  1. 檢測(cè)剛才的腳本是否正常運(yùn)行的方法是,到server端執(zhí)行如下命令:

/usr/local/nagios/libexec/check_nrpe -H 10.0.4.56 -c check_disk (server上)

如果正常的話,會(huì)輸出一行磁盤檢測(cè)的數(shù)據(jù),否則可能會(huì)報(bào)錯(cuò)。

  1. 到server上添加相應(yīng)的service

cd /usr/local/nagios/etc/services (server上)

vim 10.0.4.56.cfg # 加入如下內(nèi)容:

define service{
        use     generic-service
        host_name       10.0.4.56
        service_description     check_disk
        check_command           check_nrpe!check_disk
        max_check_attempts 5
        normal_check_interval 1
}
  1. 重啟nagios服務(wù)

/etc/init.d/nagios restart (server上)

配置nagios報(bào)警郵件

現(xiàn)在139郵箱有順便發(fā)短信的功能,所以當(dāng)有報(bào)警時(shí),只需發(fā)送到你的139郵箱你就同樣會(huì)收到一條報(bào)警短信。這樣做的優(yōu)勢(shì)就是不用再去買短信網(wǎng)關(guān)了,節(jié)省了很大一筆錢。

vim /usr/local/nagios/etc/objects/contacts.cfg

把” email nagios@localhost” 修改成 “email 你的139郵箱”

vim /usr/local/nagios/etc/objects/templates.cfg

找到:

define service{
        name                            generic-service

之所以看這一段,是因?yàn)樵谏厦嫣砑拥?0.0.4.56.cfg 定義了很多generic-service所以要關(guān)注這段的配置。

define service{
name     generic-service                
active_checks_enabled          1                             
passive_checks_enabled          1                      
parallelize_check               1                      
obsess_over_service             1                              
check_freshness                 0                     
notifications_enabled           1                             
event_handler_enabled           1                      
flap_detection_enabled          1                      
failure_prediction_enabled      1                      
process_perf_data               1                      
retain_status_information        1                              
retain_nonstatus_information    1                      
is_volatile                     0                      
check_period                    24x7        
max_check_attempts              3                     
normal_check_interval           10                     
retry_check_interval            2                      
ontact_groups                  admins                 
notification_options            w,u,c,r                
notification_interval           60                     
notification_period             24x7                   
register                        0                      
}

其中有幾個(gè)參數(shù)需要你注意:

  • notifications_enabled : 是否開(kāi)啟提醒功能。1為開(kāi)啟,0為禁用。一般,這個(gè)選項(xiàng)會(huì)在主配置文件(nagios.cfg)中定義,效果相同。
  • notification_interval: 重復(fù)發(fā)送提醒信息的最短間隔時(shí)間。默認(rèn)間隔時(shí)間是60分鐘。如果這個(gè)值設(shè)置為0,將不會(huì)發(fā)送重復(fù)提醒。
  • notification_period: 發(fā)送提醒的時(shí)間段。非常重要的主機(jī)(服務(wù))我定義為7×24,一般的主機(jī)(服務(wù))就定義為上班時(shí)間。如果不在定義的時(shí)間段內(nèi),無(wú)論什么問(wèn)題發(fā)生,都不會(huì)發(fā)送提醒。
  • notification_options: 這個(gè)參數(shù)定義了發(fā)送提醒包括的情況:d = 狀態(tài)為DOWN, u = 狀態(tài)為UNREACHABLE , r = 狀態(tài)恢復(fù)為OK , f = flapping。,n=不發(fā)送提醒。

要想正確發(fā)送郵件,上面的參數(shù)得配置合理才行。