RHEL6 Nagios 部署+fetion整合

 

RHEL6 Nagios 部署
系統環境:RHEL6 x86_64 selinux and iptables disabled
nagios 主機:192.168.0.5
mysql 主機: 192.168.0.1
一,監控本機
安裝所須要軟件
yum install httpd gcc make perl-ExtUtils-Embed.x86_64 -y
rpm -ivh gd-devel-2.0.35-10.el6.x86_64.rpm
 
 
useradd nagios                       運行 nagios 服務的用戶
usermod -G nagios apache           使 apache 用戶對 nagios 目錄具備寫權限,否則 web 頁面操做失敗
 
tar jxf nagios-cn-3.2.3.tar.bz2        解壓nagios
cd nagios-cn-3.2.3
./configure –-enable-embedded-perl 編譯
make all
make install
make install-init
make install-commandmode
make install-config
make install-webconf
 
yum install mysql-devel openssl-devel -y 編譯所需軟件包
tar zxf nagios-plugins-1.4.15.tar.gz        nagios 插件安裝
cd nagios-plugins-1.4.15
./configure —with-nagios-user=nagios —with-nagios-group=nagios
--enable-extra-opts —enable-libtap —enable-perl-modules
Make && make install
 
 
vi /usr/local/nagios/etc/nagios.cfg               #nagios配置
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg #新建 hosts.cfg 文件,存放主機與主機組定義
cfg_file=/usr/local/nagios/etc/objects/services.cfg #新建 services.cfg 文件,存放服務與服務組定義
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg#註釋 localhost.cfg 文件
以下圖:
 

 
cp -r localhost.cfg hosts.cfg
vi /usr/local/nagios/etc/objects/hosts.cfg
去掉service部分,只留下想要的的hosts部分
define host{
        use                     linux-server            ; 給網絡設備用的
        host_name               Desktop5
        alias                   nagios
        address                 192.168.0.5
        2d_coords               100,200
        3d_coords               100,200,100
        }
define hostgroup{
        hostgroup_name linux-servers ; The name of the hostgroup
        alias           Linux Servers ; Long name of the group
        members         *     ; Comma separated list of hosts that belong to this group
        }
以下圖:
 

cp -r localhost.cfg services.cfg
vi /usr/local/nagios/etc/objects/services.cfg
去掉hosts部分,只留下想要的的service部分
 
define servicegroup{
        servicegroup_name    系統負荷檢查
        alias                  負荷檢查
      members               Desktop5,進程總數,Desktop5,登陸用戶數,Desktop5,根分區,Desktop5,交換空間利用率
        }
define servicegroup{
        servicegroup_name           所有聯通性檢查
        alias                          聯通性檢查
        members                     Desktop5,PING
        }
 
define service{
        use                             local-service        
        host_name                      *
        service_description              PING
        check_command                 check_ping!100.0,20%!500.0,60%
        }
 
define service{                             
use                               local-service         
        host_name                       Desktop5
        service_description               根分區
        check_command                  check_local_disk!20%!10%!/
        }
 
                                                                                                                      
define service{
        use                             local-service        
        host_name                      Desktop5
        service_description              進程總數
        check_command                check_local_procs!250!400!RSZDT
        }
 
define service{
        use                              local-service        
        host_name                      Desktop5
        service_description              系統負荷
 check_command                     check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
        }
define service{
        use                                local-service        
        host_name                        Desktop5
        service_description                SSH
        check_command                   check_tcp!22!1.0!10.0
        notifications_enabled              0          
}
 
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
檢測語法的正確性
/etc/init.d/httpd restart
/etc/init.d/nagios restart
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin #nagios 監控頁面訪問用戶和密碼
就能訪問http://192.168.0.5/nagio
 

 
二.監控另一臺機器的數據庫(mysql)
Yum install mysql* -y
/etc/init.d/mysql restart
Mysql
mysql>create database nagdb;                                         建立用戶
mysql> grant select on nagdb.* to nagdb@'192.168.0.5';              使其具備查看權限
mysql> flush privileges;                                                刷新數庫                                               
 
在nagios服務器中查看是否可以連接到mysql主機/usr/local/nagios/libexec
/usr/local/nagios/libexec/check_mysql -H 192.168.0.1 -u nagdb -d nagdb
 

在 nagios 主機上添加對 mysql 服務監控的定義
vi /usr/local/nagios/etc/objects/commands.cfg        (添加以下行)
define command{
         command_name   check_mysql
       command_line   $USER1$/check_mysql -H $HOSTADRESS$ -u nagdb -d nagdb
}
vi /usr/local/nagios/etc/objects/hosts.cfg             (添加以下行)   
define host{
       use                     linux-server
       host_name               mysql服務器
       alias                   Desktop1
       address                 192.168.0.1
       2d_coords                500,200
        3d_coords               500,200,100
}
vi /usr/local/nagios/etc/objects/services.cfg (添加以下行)
define service{
        use                           local-service
        host_name                      mysql服務器
        check_command                 check_mysql
 }
 
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
/etc/init.d/httpd restart
/etc/init.d/ngios restart
就能訪問http://192.168.0.5/nagio
 

 
 
 
 
 
三.Nagios 經過 NRPE 監控遠程主機系統情況
 
 

 

 
遠程主機設定
useradd nagios
 
tar xzf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure —with-nagios-user=nagios --with-nagios-group=nagios --enable-extra-opts --enable-perl-modules —with-openssl --enable-libtap
 
make
make install
 
yum install xinetd -y
 
tar zxf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
make install-xinetd
 
vi /etc/xinetd.d/nrpe 修改以下行
only_from= 192.168.0.5                         #nagios 主機 ip 地址
 
vi /etc/services      添加以下行
nrpe                 5666/tcp                   nrpe務監聽端口
 
vi /usr/local/nagios/etc/nrpe.cfg                  修改以下行
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
#根分區監測
service xinetd restart
netstat -antlp |grep 5666
tcp 0 0 :::5666
#看到以下輸出說明 NRPE 正常啓動,並確保防火牆容許此端口
:::*
LISTEN
26359/xinetd
 
 
監控主機設定
tar zxf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make all
make install-plugin
/usr/local/nagios/libexec/check_nrpe -H 192.168.0.1
NRPE v2.12
#如看到以下輸出說明鏈接正常
 
vi /usr/local/nagios/etc/objects/commands.cfg 添加以下行
define command{
command_name        check_nrpe
command_line         $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
 
vi /usr/local/nagios/etc/objects/services.cfg 添加以下行
define service{
        use                           local-service
        host_name                     mysql服務器
        check_command                 check_mysql
        service_description            mysql服務
}
define service{
       use                                generic-service
       host_name                          mysql服務器
       service_description                CPU 使用率
       check_command                      check_nrpe!check_load
}
define service{
       use                                generic-service
       host_name                          mysql服務器
       service_description                登陸用戶數量
       check_command                      check_nrpe!check_users
}
 
define service{
        use                               generic-service
host_name                          mysql服務器
service_description                  根使用率
check_command                     check_nrpe!check_disk
}
 
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
service nagios reload
#檢測無誤後重載 nagios
 
 
 
Nagios 整合飛信

http://www.it-adv.net/ 下載 fetion 主程序與支持庫
步驟
(1)IP:192.168.0.85:
cd ~
lftp 127.0.0.1
cd /pub/docs/nagios
get fetion cenos54X64_20101113.rar
cd /pub/docs
get rarlinux-3.8.0.tar.gz
 

cp fetion /usr/local/nagios/libexec
cd /usr/local/nagios/libexec
chmod +x fetion
chown nagios.nagios fetion
(2)執行 /usr/local/nagios/libexec/fetion
出現沒有/lib/ld-linux.so
yum install /lib/ld-linux.so.2 -y
cd /usr/local/nagios/libexec/
./fetion
cd ~
tar zxf rarlinux-3.8.0.tar.gz -C /opt/
cd /opt/rar/unrar
/opt/rar/unrar x cenos54X64_20101113.rar(可能會出錯,須要安裝如下的包,而後從新執行此步。)yum install libstdc++.so.6
(3) cd ~
lftp 192.168.0.254
cd /pub/docs/nagios
get linuxso_20111113.tar.gz
tar zxf linuxso_20111113.tar.gz -C /usr/local/lib(將庫文件放到/usr/local/ib中。ldconfig /usr/local/lib使其生效。)
cd /usr/local/lib
ls 會有4個so文件。
chmod +x *
須要的時候可能須要安裝: yum install libgssapi_krb5.so.2 libz.so.1 zlib文件。
ldconfig /usr/local/lib
/usr/local/nagios/libexec/fetion 7執行成功。

(4)vi /etc/ld.so.conf.d/mysql-x86_64.conf
/usr/lib64/mysql
(5) vi fetion.conf
/usr/local/lib
ldconfig
(6) su - nagios
/usr/local/nagios/libexec/fetion --mobile=158029XXXXX --pwd=XXXXXX(飛信密碼) --to=15802955XXXX --msg-utf8="hello"
而後你去/usr/local/nagios/libexec找到1509154xxxx.jpg,而後將其複製到桌面,查看其驗證碼,而後寫上去,你就會在1509154xxxx收到一個由mobile=1509154xxxx發來的信息哦!)
你第一次用驗證碼,第二次就不用驗證碼。
實驗五:
fetion 與 nagios 配置
步驟(1)cd /usr/local/nagios/libexec/
vi fetion.sh
/usr/local/nagios/libexec/fetion --mobile=158029XXXXX --pwd=qqXXXXX --to="$1" --msg-utf8="$2" > /dev/null
(2) chmod +x fetion.sh
chown ngios.nagios fetion.sh
su - nagios
cd /usr/local/nagios/libexec/
./fetion.sh 1509154xxxx "hello" (測試給1509154xxxx發送當心「hello「)
此時由於你從新調用的程序,須要從新找到驗證碼並輸入.....
而後就能夠收到信息嘍......
(3) cd /usr/local/nagios/etc/objects/
vi templates.cfg

Generic contact definition template - This is NOT a real contact, just a template!
define contact{
name generic-contact ; The name of this contact template
.
.
.
service_notification_commands notify-service-by-email,notify-service-by-fetion ; send service notifications via email
host_notification_commands notify-host-by-email,notify-host-by-fetion ; send host notifications via email
..........................
}
(4) vi /usr/local/nagios/etc/objects/commands.cfg
增長如下內容:
# 'notify-host-by-fetion' command definition
define command{
command_name notify-host-by-fetion
command_line $USER1$/fetion.sh $CONTACTPAGER$ "$NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$"
}

# 'notify-service-by-fetion' command definition
define command{
command_name notify-service-by-fetion
command_line $USER1$/fetion.sh $CONTACTPAGER$ "$NOTIFICATIONTYPE$: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$"
}
(5)vi /usr/local/nagios/etc/objects/contacts.cfg

define contact{
.
.
.
email 997365139@qq.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
pager 1509154xxxx(接收飛信信息的 手機號碼
)
}
(6)測試:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#校驗後從新載入 nagios 配置
/etc/init.d/nagios reload
此時你若把被監控端的: service mysqld stop此時會產生報警。而後下次檢測的時候你的 手機和郵箱會同時收到郵件和信息哦。。
相關文章
相關標籤/搜索