rhel6-nagios的安裝與配置

軟件下載地址:http://yunpan.cn/QbFvRDxugZZsT

這裏有配置nagios所需的所有的各種版本的軟件資源,包括fetion資源


1.實驗環境:

nagios主機:desk32.example.com 192.168.122.32\

mysql主機:desk3.example.com 192.168.122.3

2.nagios的安裝:

nagios是用一些命令在被監控主機上運行後將採集的信息返回。

編譯需要的依賴包:

yum install httpd gcc make gd-devel perl-ExtUtils-Embed.x86_64 -y
yum  -y   localinstall  gd-devel-2.0.35-10.el6.x86_64
useradd nagios          #運行 nagios 服務的用戶
usermod -G nagios apache        #使 apache 用戶對 nagios 目錄具有寫權限,不然 web 頁面操作失敗
 tar jxf nagios-cn-3.2.3.tar.bz2
cd nagios-cn-3.2.3
useradd  nagios
usermod -G nagios apache
./configure --prefix=/usr/local/nagios --enable-embedded-perl
make all
make install
make install-init
make install-commandmode
make install-config
make install-webconf


bin Nagios 執行程序所在目錄,這個目錄只有一個文件 nagios

etc Nagios 配置文件位置,初始安裝完後,只有幾個*.cfg-sample 文件

sbin Nagios Cgi 文件所在目錄,也就是執行外部命令所需文件所在的目錄

share Nagios 網頁文件所在的目錄

var Nagios 日誌文件、spid 等文件所在的目錄


3.安裝nagios-plugin插件:

安裝依賴包:

yum -y install mysql-devel openssl-devel
[root@desk32 nagios-plugins-1.4.16]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-extra-opts  --enable-perl-modules
[root@desk32 nagios-plugins-1.4.16]#make && make install


再沒有安裝插件前在/usr/local/nagios/libexec下沒有東西,安裝後次目錄下安裝的是nagios用於檢查的命令。


[root@desk32 nagios-plugins-1.4.16]# cd /usr/local/nagios/
[root@desk32 nagios]# ls
bin  etc  include  libexec  perl  sbin  share  var
[root@desk32 nagios]# ls libexec/
…........check_apt check_file_age check_jabber check_nt        check_rpc       check_time   check_breeze    check_flexlm        check_load         check_ntp  …...........

主配置文件 nagios.cfg 定義和控制 nagios 行爲 需要修改

Cgi 配置文件 cgi.cfg 瀏覽器執行諸如重啓 nagios 服務等要修改

宏定義文件 Resource.cfg 定義插件路徑 不需修改

命令定義文件 Commands.cfg 定義怎麼發送短信、郵件等 需要修改

其他文件 contactgroups.cfg,contacts.cfg 監控對象配置文件,如主機 主配置文件,包含,手動聯繫人配置文件

[root@desk32 etc]# vim nagios.cfg
#註釋或刪掉這行
#cfg_file=/usr/local/nagios/etc/localhost.cfg
#主機配置文件路徑
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/service.cfg
//將服務與主機分開    
#//主機組配置文件路徑
cfg_file=/usr/local/nagios/etc/hostgroups.cfg
#聯繫人配置文件路徑
cfg_file=/usr/local/nagios/etc/contacts.cfg
#聯繫組配置文件路徑
cfg_file=/usr/local/nagios/etc/contactgroups.cfg
#服務配置文件路徑
cfg_file=/usr/local/nagios/etc/services.cfg
#監視時段配置文件路徑
cfg_file=/usr/local/nagios/etc/timeperiods.cfg
#在 web 界面下重啓 nagios、停止主機/服務檢查等操作,.默認值是 0.
check_external_commands=1
#根據自己的情況定這個命令檢查時間間隔.默認值是 1 秒.
command_check_interval=10s

注意:以上的有些文件的位置是不正確的。因爲存在軟件的升級等原因,文件位置有一些的調整

[root@desk32 objects]# cp localhost.cfg hosts.cfg #創建主機定義文件
[root@desk32 objects]# cp localhost.cfg service.cfg   #創建服務定義文件
[root@desk32 objects]#vim hosts.cfg  #只剩下對於主機及主機組的定義將服務的定義刪掉
#########################################
# HOST DEFINITION
#########################################
# Define a host for the local machine
define host{
        use                     linux-server         
        host_name               desk32.example.com
        alias                   ngios服務器
        address                 127.0.0.1
        icon_image              switch.gif
        statusmap_image         switch.gd2
        2d_coords               100,200
        3d_coords               100,200,100
        }
define host{
        use                     linux-server
        host_name               王茲銀家電機
        alias                   家庭上網用機
        parents                 desk32.example.com
        address                 127.0.0.1
        icon_image              web.gif
        statusmap_image         web.gd2
        2d_coords               100,300
        3d_coords               100,300,100
        }
#####################################################################
# HOST GROUP DEFINITION
#####################################################################
# Define an optional hostgroup for Linux machines
define hostgroup{
        hostgroup_name  linux-servers
        alias           Linux Servers
        members         *       #包含的主機“*”代表包含上定義的所有的主機
        }
define hostgroup{
        hostgroup_name 家用機器組
        alias   放在家裏用的PC機
       members 王茲銀家電機   #只包含主機“王茲銀家電機”
#        members     *
       #包含所有主機
        }


以下就是主機組的區別:

164342481.jpg

以下關於服務的定義:

[root@desk32 objects]# vim /usr/local/nagios/etc/objects/service.cfg
#########################################
# SERVICE DEFINITIONS
#########################################
define service{
        use                             local-service      
        host_name                       *
      #表示此服務將監控所有主機
        service_description             PING
        }
define service{
        use                             local-service       
        host_name                       *
        service_description             根分區
        }
define service{
        use                             local-service      
        host_name                       desk32.example.com
   #該服務只監控desk32
        service_description             登錄用戶數
        }
define service{
        use                             local-service      
        host_name                       desk32.example.com
        service_description             進程總數
        }
define service{
        use                             local-service       
        host_name                       desk32.example.com
        service_description             系統負荷
        }
define service{
        use                             local-service     
        host_name                       desk32.example.com
        service_description             交換空間利用率
        }
define service{
        use                             local-service    
        host_name                       desk32.example.com
        service_description             SSH
        }
define service{
        use                             local-service     
        host_name                       desk32.example.com
        service_description             HTTP
        }
#########################################
#DEFINE SERVICEGROUP
#########################################
define servicegroup{
        servicegroup_name       系統檢查
        alias                   系統狀況
    embers desk32.example.com,PING,desk32.example.com,根分區,desk32.example.com,登錄用戶數,desk32.example.com,系統負荷,desk32.example.com,交換空間利用率,desk32.example.com,SSH,desk32.example.com,HTTP,desk32.example.com,進程總數
        }


164541315.jpg

ot@desk32 objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Total Warnings: 0
Total Errors:   0
#保證配置文件的語法沒有問題
[root@desk32 etc]# /etc/init.d/nagios start


4.apache的安裝配置:

yum -y install httpd
配置apache的虛擬主機:
[root@desk32 conf]# vim /etc/httpd/conf/httpd.conf
NameVirtualHost *:80
<VirtualHost *:80>
    DocumentRoot /usr/local/nagios/share
    ServerName nagios.example.com
</VirtualHost>
[root@desk32 conf]# /etc/init.d/httpd restart
[root@desk32 conf]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
#nagios 監控頁面訪問用戶和密碼


以上的管理員的用戶信息實在:/usr/local/nagios/etc/cgi.cfg中定義的,當然是可以修改的;

#如有多個用戶,中間用逗號隔開

authorized_for_system_information=sery

authorized_for_configuration_information=sery

authorized_for_system_commands=sery

authorized_for_all_services=sery

authorized_for_all_hosts=nagiosadmin,sery

authorized_for_all_service_commands=sery

authorized_for_all_host_commands=sery

在這裏指定的用戶”sery”可以通過瀏覽器操縱 nagios 服務的關閉、重啓等各種操作

完成以上的所有操作後我們在裝有瀏覽器的主機上添加虛擬主機中的域名解析:

192.168.122.32nagios.example.com

網頁訪問:http://nagios.example.com

164803347.jpg

164826805.jpg


2ngios監控其他的主機

1)監控mysql主機的mysql服務:

mysql 主機上運行有 mysql 服務,在其上創建監控檢測帳戶

mysql> create database nagdb;
mysql> grant select on nagdb.* to nagdb@'192.168.122.32';
mysql> flush privileges;
[root@desk32 libexec]# cd /usr/local/nagios/libexec/
[root@desk32 libexec]# ./check_mysql -H 192.168.122.3 -u nagdb -d nagdb
Uptime: 363  Threads: 1  Questions: 6  Slow queries: 0  Opens: 15  Flush tables: 1  Open tables: 8  Queries per second avg: 0.16
## -H 指定檢測的主機,-u 制定select mysql的用戶,-d  指定登錄select的數據庫


添加監控主機信息:

vim /usr/local/nagios/etc/objects/hosts.cfg   添加:
define host{
        use                     linux-server
        host_name              desk3.example.com
        alias                   mysql服務器
        address                 192.168.122.3 ;desk3.example.com
        icon_image              server.gif
        statusmap_image         server.gd2
        2d_coords               500,200
        3d_coords               500,200,100
        }
[root@desk32 objects]# vim /usr/local/nagios/etc/objects/service.cfg
添加:
###################check_mysql#############################
define service{
        use                     local-service
        host_name               desk3.example.com
        service_groups          mysql服務組
        service_description     mysql服務
        check_command           check_mysql
        }
##########################check_mysql_groups#########################
define servicegroup{
        servicegroup_name       mysql服務組
        alias                   mysql服務器
        members desk3.example.com,mysql服務
        }
對於位置在libexec下的命令還必須在commands.cfg中定義纔可以使用;
#####################check_mysql###########################
define command{
        command_name    check_mysql
        command_line    $USER1$/check_mysql -H $HOSTADDRESS$ -u nagdb -d nagdb
        }
#$USER1$ 這的就是libexec目錄
[root@desk32objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
[root@desk32 objects]# /etc/init.d/nagios reload

165234294.jpg


刷新網頁後發現主機數多了一個:在該主機下的服務:

165300292.jpg


以上的mysql服務之所以可以直接就可以監控遠程的主機,是因爲chek_mysql的命令本身就帶有-H指定主機訪問主機的能力;其他的監控命令

並沒有這樣的功能,所以監控遠程的其他的服務必須藉助nrpe插件:

165329972.jpg


The NRPE addon consists of two pieces:

The check_nrpe plugin, which resides on the local monitoring machine

The NRPE daemon, which runs on the remote Linux/Unix machine

When Nagios needs to monitor a resource of service from a remote Linux/Unix machine:

Nagios will execute the check_nrpe plugin and tell it what service needs to be checked

The check_nrpe plugin contacts the NRPE daemon on the remote host over an (optionally) SSL-protected connection

The NRPE daemon runs the appropriate Nagios plugin to check the service or resource

The results from the service check are passed from the NRPE daemon back to the check_nrpe plugin, which then returns the check results to the Nagios process.

Note: The NRPE daemon requires that Nagios plugins be installed on the remote Linux/Unix host. Without these,the daemon wouldn't be able to monitor anything.


nagios主機上使用check_nrpe通過ssl鏈接遠程的主機的nrpe使用遠程主機上的命令檢查主機信息然後又將檢查的信息返回;

所以在遠程主機上要按裝nrpenagios-plugins


在遠程主機desk3:


tar zxf nagios-plugins-1.4.16.tar.gz
cd nagios-plugins-1.4.16
./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-extra-opts --enable-perl-modules  --enable-libtap –with-openssl
make && make install
[root@desk3 nrpe-2.14]# cd /usr/local/nagios/
[root@desk3 nagios]# chown  nagios.nagios . -R
[root@desk3 nrpe-2.14]# yum -y install xinetd #用於管理nrpe的啓動端口信息
tar zxf nrpe-2.14.tar.gz
cd nrpe-2.14
[root@desk3 nrpe-2.14]./configure
[root@desk3 nrpe-2.14]# make all
[root@desk3 nrpe-2.14]# make install
[root@desk3 nrpe-2.14]# make install-daemon-config
[root@desk3 nrpe-2.14]# make install-xinetd
[root@desk3 nagios]# vim /etc/xinetd.d/nrpe
        only_from       = 192.168.122.32
[root@desk3 nagios]# vim /etc/services
nrpe            5666/tcp #TCP port service nrpe
[root@desk3 etc]# vim /usr/local/nagios/etc/nrpe.cfg
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
因爲系統上的分區可能會是lvm所以這樣修改
[root@desk3 etc]# /etc/init.d/xinetd restart
[root@desk3 etc]# netstat -anplt
tcp        0      0 :::5666                     :::*                LISTEN      3984/xinetd
因爲在nagios主機上是通命令check_nrpe實現的所以在ngios主機上同樣安裝nrpe
[root@desk32 libexec]# ./check_nrpe -H 192.168.122.3
NRPE v2.14
    #看見如此信息則表明nrpe征程鏈接



Configuring Things On The Nagios Host


command definition for the check_nrpe plugin would look like this:

[root@desk32 libexec]# vim /usr/local/nagios/etc/objects/commands.cfg
###################chek_nrpe##############################
define command{
        command_name    check_nrpe
        command_line    /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }
###################check_nrpe#########################################
define service{
        use                     generic-service
        host_name               desk3.example.com
        service_description     CPU-LOAD
        check_command           check_nrpe!check_load
        }
define service{
        use                     generic-service
        host_name               desk3.example.com
        service_description     USERS
        check_command           check_nrpe!check_users
        }
define service{
        use                     generic-service
        host_name               desk3.example.com
        service_description     GEN-Free-Space
        check_command           check_nrpe!check_disk
        }
define service{
        use                     generic-service
        host_name               desk3.example.com
        service_description     Zombie-Process
        check_command           check_nrpe!check_zombie_procs
        }
define service{
        use                     generic-service
        host_name               desk3.example.com
        service_description     Total-Process
        check_command           check_nrpe!check_total_procs
        }
##########################check_nrpe+mysql_groups#####################
define servicegroup{
        servicegroup_name       遠程服務組
        alias                  mysql+nrpe服務
 members desk3.example.com,CPU-LOAD,desk3.example.com,USERS,    \
desk3.example.com,GEN-Free-Space,desk3.example.com,Zombie-          \
Process,desk3.example.com,Total-Process,desk3.example.com,mysql服務       
        }
爲了方便查看將host.cfg中的主機的定義中的desk3.example.com的alias改爲了“遠程主機”
[root@desk32 libexec]# /usr/local/nagios/bin/nagios -v               \
/usr/local/nagios/etc/nagios.cfg
[root@desk32 libexec]# /etc/init.d/nagios reload

165740638.jpg


可以看見遠程的主機desk3nrpe的那幾個服務已經添加進來了;


測試:

mysql測試:

遠程mysql主機上:


[root@desk3 etc]# /etc/init.d/mysqld stop

165901832.jpg

[root@desk3 etc]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vol0-root
    3.9G  1.1G  2.6G  31% /
[root@desk3 etc]# dd if=/dev/zero of=/bigfile bs=510M count=5
[root@desk3 etc]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vol0-root
    .9G  3.6G   66M  99% /

165939234.jpg


naios採集到的所有的圖形信息,均存儲與rrdtool的環形數據庫中,其數據在/usr/local/nagios/var/rrd下存儲的;


5.圖形日誌顯示:

170020316.jpg

後“圖書”標誌爲圖形日誌,當前無法顯示:

[root@desk32 ~]# yum localinstall rrdtool-perl-1.3.8-6.el6.x86_64.rpm
[root@desk32 logs]# yum -y install perl-CGI

刷新後即可出現圖形化的日誌信息



6.ngios整合飛信報警


linux: http://www.it-adv.net/fetion/downng/fetion20090406003-linux.tar.gz

linux 下需要的四個文件:http://www.it-adv.net/fetion/downng/library_linux.tar.gz

libACE.so.5.6.8

libACE_SSL.so.5.6.8

libcrypto.so.0.9.8

libssl.so.0.9.8

>> 使用說明 <<

以下參數提供登錄用的賬號密碼(三種方式,手機號-密碼 飛信號-密碼 文件--索引)

--mobile=[手機號]登錄手機號

--sid=[飛信號]登錄飛信號

--pwd=[密碼]登錄密碼

--config=[文件名]存儲手機號、密碼的文件。

--index=[索引號]索引

--msg-utf8=[信息]發送的消息,UTF8 編碼

--msg-gb=[信息]發送的消息,GB 編碼

--file-utf8=[文件 utf8 格式]發送文件內容

--file-gb=[文件 gb 格式]發送文件內容

--msg-type=[0/1/2]發送消息類型:普通消息 長消息 智能短信


[root@desk32 ~]# mv fetion /usr/local/nagios/libexec/

[root@desk32 ~]# chmod a+x /usr/local/nagios/libexec/fetion

[root@desk32 ~]# tar zxf linuxso_20101113.tar.gz -C /usr/local/lib

[root@desk32 ~]# ldconfig /usr/local/lib

[root@desk32 ~]# chown nagios.nagios /usr/local/nagios/libexec/fetion

[root@desk32 libexec]#cd /usr/local/nagios/libexec

[root@desk32 libexec]# ./fetion #安裝所有的依賴關係

當出現以下內容後則表示fetion已經準備妥當:

*************** IMPORTANT STATEMENT **************************

** PLEASEDON'TUSE THIS SOFTWARE TO S JUNK SHORT MESSAGES.**

**OTHERWISE PLEASE BEAR YOUR OWN CONSEQUENCES.**

**Version:[20101205002-linux]**

*******************************************************************

測試:

切換至nagios用戶:

[nagios@desk32./fetion--mobile=15829476129 --pwd=password --to=15829476129 --msg-utf8="good luck"

第一次要輸入驗證碼:

圖形驗證碼已經生成,文件名爲:15829476129.jpg請識別後輸入圖形驗證碼:

CG5Y

您輸入的識別碼是:CG5Y

SIP-C/4.0 280 Send SMS OK

T: sip:[email protected];p=7195

I: 2

Q: 1 M

L: 112

D: Wed, 28 Aug 2013 13:18:34 GMT

XI: C4134FD7BC7ED4B93982B2334E44124C

則表示短信發送成功;



7nagiosfetion的整合


[root@desk32 libexec]# su - nagios

[nagios@desk32 ~]$ cd /usr/local/nagios/libexec/

[nagios@desk32 libexec]$ vim fetion.sh

/usr/local/nagios/libexec/fetion --mobile=15829476129 --pwd=password --to="$1" --msg-utf8="$2" > /dev/null

[nagios@desk32 libexec]$ chmod a+x fetion.sh

[root@desk32 objects]# vim commands.cfg #定義監控的飛信命令

# 'notify-host-by-fetion' command definition

define command{

command_namenotify-host-by-fetion

command_line$USER1$/fetion.sh $CONTACTPAGER$ "$NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$"

}

# 'notify-service-by-fetion' command definition

define command{

command_namenotify-service-by-fetion

command_line$USER1$/fetion.sh $CONTACTPAGER$ "$NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$"

}


[root@desk32 objects]# vim templates.cfg #fetion 報警選項

service_notification_commandsnotify-service-by-email,notify-service-by-fetion

host_notification_commandsnotify-host-by-email,notify-host-by-fetion

[root@desk32 objects]# vim contacts.cfg#連接文件用於鏈接mail及發送短信的對象

emailnagios@localhost

pager182205090xx

[root@desk32 objects]# /etc/init.d/nagios reload

測試:

關閉desk32.example.comssh服務,觀察看有沒有mailfetion的通知。






西安石油大學計算機學院

王茲銀

904483782

[email protected]




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章