監控利器Nagios之一:監控本地NFS和外部HTTP、MySQL服務

監控利器Nagios之一:監控本地NFS和外部HTTPMySQL服務

 

Nagios是一款開源的免費網絡監視工具,能有效監控WindowsLinuxUnix的主機狀態,交換機路由器等網絡設置,打印機等。在系統或服務狀態異常時發出郵件或短信報警第一時間通知網站運維人員,在狀態恢復後發出正常的郵件或短信通知。

Nagios的特點:

1、監控服務httpMySQLnfstcpping

2、監控主機資源cpu、負載、I/O、虛擬及內存磁盤利用率等

3、支持郵件微信等報警通信。

4、可選web見面用於查看當前的網絡狀態等

Nagios的組成:

Nagios監控軟件是一個監控平臺。Nagios監控一般由一個主程序(nagios)、一個插件程序(nagios-plugins)和一些可選的插件組成。

1NRPE組件

Nrpe組件一般工作在備監控端,朱勇用於監控本地資源,包括負載(uptime)、CPUtopSar)、磁盤(df -hi)、內存(free)、I/Oiostat)等

2NSClient++

3NDOUtils

Nagios的監控完整圖:

wKioL1lJI1uxed3OAABBR53LMgA875.png 

原理如下:

通常由nagios服務端發起獲取數據請求,由check_nrpe插件攜帶要獲取的命令,傳給給監控端nrpe守護進程,默認是5666端口,nrpe進程讀取nrpe.Cfg裏對應服務器端發送的命令信息,通過調用本地插件獲取數據,然後返回給nagios服務器端的check_nrpe,進而傳給nagios戰術的web頁面中。

 

一:實驗目標

1、監控外部服務器NFS,服務端當做客戶端

2、監控外部服務器的MySQL服務

3、監控外部服務器httpd

4、監控外部服務器的私有信息

二:實驗環境

VMare

作用

主機名

Ip地址

安裝的軟件

RHEL-6.5

服務端

yu61

192.168.1.61

Nagios軟件,nagios插件,nrpeLAMP環境、NFS

RHEL-6.5

客戶端

yu62

192.168.1.62

nagios插件,nrpemysql-serverIO

RHEL-6.5

客戶端

yu63

192.168.1.63

nagios插件,nrpeNFSHttp

 #所有服務器都需要關閉防火牆

 

三:實驗步驟

安裝服務端環境

1、安裝LAMP環境

Nagios服務端需要有web界面用來展示監控效果,而監控的內容是屬於動態的,因此使用LAMP環境,Apache用作於web展示監控效果,PHP用於展示監控動態信息。

[root@yu61 ~]# mkdir -p /home/yu/tools
[root@yu61 ~]# yum install -y httpd gccglibc glibc-common php gd gd-devel libpng libmng libjpeg zlib mysql-server 
[root@yu61 ~]# service iptables stop 
[root@yu61 ~]# service httpd restart
[root@yu61 ~]# vim /var/www/html/index.php
<?php
phpinfo();
?>


2、添加用戶

Apache用戶默認是存在的,屬組默認daemon,需要把它改成nagios。這樣它纔能有權限訪問安裝的nagios目錄,執行相關的cgi命令,如通過瀏覽器界面關閉nagios、停止某個故障對象發送報警信息等。

添加nagios帳戶,以用來運行Nagios。當然也可以用root運行,但出於安全考慮而使用普通帳號來運行,並且不給這個賬號分配shell登錄權限

[root@yu61 tools]# useradd  -s /sbin/nollogin  nagios
[root@yu61 tools]# groupadd nagcmd
[root@yu61 tools]# usermod -a -G nagcmd nagios
[root@yu61 tools]# usermod -a -G nagcmd apache


3、安裝nagios主程序

[root@yu61 tools]# tar jxvf nagios-cn-3.2.3.tar.bz2 
[root@yu61 tools]# cd nagios-cn-3.2.3
[root@yu61 nagios-cn-3.2.3]# ./configure --with-command-group=nagcmd
[root@yu61 nagios-cn-3.2.3]#make all
[root@yu61 nagios-cn-3.2.3]# make install
[root@yu61 nagios-cn-3.2.3]# make install-init
[root@yu61 nagios-cn-3.2.3]#make install-commandmode
[root@yu61 nagios-cn-3.2.3]# make install-config
[root@yu61 nagios-cn-3.2.3]# make install-webconf
[root@yu61 nagios-cn-3.2.3]# htpasswd -c /usr/local/nagios/etc/htpasswd.users mobanche


 

用於檢測文件的完整性的方法有兩種。一種是絕對路徑,一種是啓動檢測

第一種:

[root@yu61 nagios]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg


第二種:

[root@yu61 nrpe-2.12]# vim /etc/init.d/nagios


#刪掉啓動項中178行中的“  > /dev/null 2>&1;”內容。

wKiom1lJI3HhCVOCAAAfRnEMNFQ560.png 

[root@yu61 objects]# vim /usr/local/nagios/etc/cgi.cfg
use_authentication=0 #改爲0
 
[root@yu61 nrpe-2.12]# /etc/init.d/nagios checkconfig 
Total Warnings: 0
Total Errors:   0


 

4、安裝nagios-plugins插件包

Nagios的插件包就是實現獲取數據信息的命令或者程序,用過這些命令或程序,nagios可以獲取到需要的數據,然後進行報警和展示。

[root@yu61 tools]# yum install perl-devel openssl-devel -y
[root@yu61 tools]# tar zxvf nagios-plugins-1.4.16.tar.gz
[root@yu61 tools]# cd nagios-plugins-1.4.16
[root@yu61 nagios-plugins-1.4.16]# ./configure  --with-nagios-user=nagios --with-nagios-group=nagcmd --enable-perl-modules --with-mysql
[root@yu61 nagios-plugins-1.4.16]# make -j 4
[root@yu61 nagios-plugins-1.4.16]# make install


5、安裝nrpe軟件包

Nrpe是通常用於安裝被監控端的,這裏在服務端也安裝的了nrpe軟件,是因爲nagios服務器端需要check_nrpe插件做被動檢查,如果服務端不安裝nrpe插件,那麼也就沒有這個插件了,其次nagios服務器端本地的資源也需要被監控,因此nagios服務器端也會被認作客戶端。

[root@yu61 tools]# tar zxvf nrpe-2.12.tar.gz
[root@yu61 tools]# cd nrpe-2.12
[root@yu61 nrpe-2.12]# ./configure 
[root@yu61 nrpe-2.12]# make all && make instll


6、服務端附件

1)檢查登錄nagios的管理賬戶信息

[root@yu61 nagios-cn-3.2.3]# grep AuthUserFile /etc/httpd/conf.d/nagios.conf 
   AuthUserFile /usr/local/nagios/etc/htpasswd.users
   AuthUserFile /usr/local/nagios/etc/htpasswd.users
   AuthUserFile /usr/local/nagios/etc/htpasswd.users


2)設置nagios開機自啓動

[root@yu61 nagios-cn-3.2.3]# chkconfig postfix on
[root@yu61 nagios-cn-3.2.3]# chkconfig postfix --list
postfix        0:關閉1:關閉2:啓用3:啓用4:啓用5:啓用6:關閉


3)郵件報警

[root@yu61 nagios-cn-3.2.3]# /etc/init.d/httpd reload
[root@yu61 nagios-cn-3.2.3]# sed -n '35p ' /usr/local/nagios/etc/objects/contacts.cfg 
        email                           [email protected]; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
[root@yu61 nagios-cn-3.2.3]# /etc/init.d/postfix start 
[root@yu61 nagios-cn-3.2.3]# lsof -i :25
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
master  2094 root   12u  IPv4  12921      0t0  TCP localhost:smtp (LISTEN)
master  2094 root   13u  IPv6  12923      0t0  TCP localhost:smtp (LISTEN)
[root@yu61 nagios-cn-3.2.3]# mail -s "test" [email protected] < /etc/hosts
[root@yu61 nagios-cn-3.2.3]# mailq
-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
A1C723FFA6      599 Sun May 21 12:23:56  [email protected]
(lost connection with mx2.qq.com[14.17.41.170] while performing the HELO handshake)
                                         [email protected]


 

安裝客戶端端環境

註釋:三臺客戶端同時安裝,安裝的內容一樣。

1、添加用戶

[root@yu62 ~]# mkdir -p /home/yu/tools
[root@yu62 ~]# cd /home/yu/tools/
[root@yu62 tools]# yum install perl-devel openssl-devel -y
[root@yu62 tools]#  useradd -s /sbin/nologin nagios
[root@yu62 tools]#  groupadd nagcmd
[root@yu62 tools]#  usermod -G nagcmd nagios


2、安裝nagios-plugins插件包
[root@yu62 tools]# tar zxvf nagios-plugins-1.4.16.tar.gz
[root@yu62 tools]# cd nagios-plugins-1.4.16
[root@yu62 nagios-plugins-1.4.16]# ./configure  --with-nagios-user=nagios --with-nagios-group=nagcmd --enable-perl-modules --with-mysql
[root@yu62 nagios-plugins-1.4.16]# make -j 4 && make install


3、安裝nrpe軟件包

[root@yu62 tools]# tar zxvf nrpe-2.12.tar.gz
[root@yu62 tools]# cd nrpe-2.12
[root@yu62 nrpe-2.12]# ./configure &&  make all && make install


 

實戰:監控外部服務器MySQL服務

1、服務端修改配置文件

root@yu61 ~]# cd /usr/local/nagios/etc
[root@yu61 etc]# vim nagios.cfg
改:cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
爲:註釋localhost這一項,添加以下兩行:
#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/service.cfg


2、修改配置文件、生成要檢測的主機配置文件和服務配置文件:

[root@yu61 etc]# cd objects/
[root@yu61 objects]# vim hosts.cfg  # 添加以下內容
####################chreck_host###########################
define host{
        use                     linux-server            
        host_name               yu62
        alias                   mysql服務
        address                 192.168.1.64
        icon_p_w_picpath              switch.gif
        statusmap_p_w_picpath         switch.gd2
        2d_coords               100,200
        3d_coords               100,200,100
        }


[root@yu61 objects]# vim service.cfg # 添加以下內容
###################check_server_mysql#############################
define service{
                use                     local-service
                host_name               yu62
                service_groups          mysql服務組
                service_description     mysql服務
                check_command           check_mysql
                }
 
##########################check_server_mysql_groups#########################define servicegroup{
                servicegroup_name       mysql服務組
                alias                   mysql服務器
                members                 yu62,mysql服務
}
[root@yu61 objects]# vim commands.cfg #在此文件最後添加以下內容:
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H$HOSTADDRESS$ -u nagdb -d nagdb
}
[root@yu61 nrpe-2.12]# /etc/init.d/nagios checkconfig


3、客戶端上安裝MySQL服務

[root@xuegod62 ~]# yum install mysql-server -y
[root@xuegod62 ~]# service mysqld restart
[root@xuegod62~]# mysql
mysql>create database nagdb; 
mysql> grant select on nagdb.* to yu@'192.168.1.61';


4、重啓mysqld、nagios服務

[root@yu62 ~]# /etc/init.d/httpd restart
[root@yu62 ~]# /etc/init.d/mysqld restart
[root@yu61 ~]# /etc/init.d/nagios restart
[root@yu61 objects]#/usr/local/nagios/libexec/check_mysql -H 192.168.1.64 -u nagdb -d nagdb
Uptime: 516  Threads: 1 Questions: 6  Slow queries: 0  Opens: 15 Flush tables: 1  Open tables:8  Queries per second avg: 0.11


#上述命令執行後,出現以下信息,表示正常,連接192.168.1.64mysql裏面的用戶是正常的

5)測試查看主機和服務:

http://192.168.1.63/nagios/

wKioL1lJI36ALgDCAAAXHQQFZMY410.png 

 

實戰:監控外部服務器NFS服務

1、安裝配置NFS服務器

[root@yu61 objects]# service rpcbind status
rpcbind (pid  1614) 正在運行...
[root@yu61 ~]# vim /etc/exports
/tmp *(rw)
[root@yu61 ~]# /etc/init.d/nfs restart 
[root@yu61 ~]# showmount -e 192.168.1.61
Export list for 192.168.1.61:
/tmp *


2、編輯check_nfs命令腳本

[root@yu61 libexec]# pwd
/usr/local/nagios/libexec
[root@yu61 libexec]# cat check_nfs 
check_nfs
#!/bin/bash
. /data1/server/nagios/libexec/utils.sh
CHECKNAME="Mount NFS"
RESULT=""
EXIT_STATUS=$STATE_OK
NFSFSTAB=`LC_ALL=C awk '!/^#/ && $3 ~ /^nfs/ && $3 != "nfsd" && $4 !~ /noauto/ { print $1 }' /etc/fstab`
NFSMTAB=`LC_ALL=C awk '$3 ~ /^nfs/ && $3 != "nfsd" && $2 != "/" { print $1 }' /proc/mounts`
theend() {
    echo $RESULT
    exit $EXIT_STATUS
}
CHECK_MOUNT(){
    LIST=""
    for list in $NFSFSTAB
    do
        NFS_MOUNT=$(LC_ALL=C cat /proc/mounts |cut -d " " -f 1|grep "${list}$")
        if [ -z "${NFS_MOUNT}" ];then
            LIST="$LIST$list "
        fi
    done
}
if [ -n "${NFSMTAB}" ];then
    CHECK_MOUNT
    if [ -z "${LIST}" ];then
        RESULT="$CHECKNAME OK - NFS BE MOUNTED PROPERLY"
        EXIT_STATUS=$STATE_OK
    else
        RESULT="$CHECKNAME WARNING - ${LIST} NOT BE MOUNTED"
        EXIT_STATUS=$STATE_WARNING
    fi
else
    RESULT="$CHECKNAME CRITICAL - ALL NFS NOT BE MOUNTED"
    EXIT_STATUS=$STATE_CRITICAL
fi
Theend


註釋:爲了安全起見修改權限,不修改默認爲644 root

[root@yu61 libexec]# chmod 755 check_nfs 
[root@yu61 libexec]# chown nagios:nagcmd check_nfs 
[root@yu61 libexec]# ll check_nfs 
-rwxr-xr-x 1 nagios nagcmd 973 5月  22 13:27 check_nfs


3、修改配置文件

[root@yu61 objects]# cat hosts.cfg    ##末尾添加
####################chreck_host###########################
define host{
        use                     linux-server            
        host_name               yu61
        alias                   nfs服務
        address                 192.168.1.61
        icon_p_w_picpath              switch.gif
        statusmap_p_w_picpath         switch.gd2
        2d_coords               100,200
        3d_coords               100,200,100
        }


[root@yu61 objects]# cat service.cfg  ##末尾添加
###################check_server_mysql#############################
define service{
                use                     local-service
                host_name               yu61
                service_groups          nfs服務組
                service_description     nfs服務
                check_command           check_nrpe!check_nfs
                }
 
##########################check_server_mysql_groups######################
define servicegroup{
                servicegroup_name       nfs服務組
                alias                   nfs服務器
                members                 yu61,nfs服務
}


[root@yu61 objects]# vim commands.cfg   ##末尾添加
#'check_nrpe' command definition
definecommand{
        command_name    check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c$ARG1$
        }
 
define command{
       command_name    check_nfs
       command_line    $USER1$/check_nfs
       }


4、檢測服務配置及啓動服務

[root@yu61 objects]# /etc/init.d/nagios checkconfig
Total Warnings: 0
Total Errors:   0
[root@yu61 objects]# /etc/init.d/nagios reload


5、測試查看主機和服務

http://192.168.1.63/nagios/

wKiom1lJI4zT_xI4AAAu27nGztU009.png 

 

實戰:監控外部服務器httpd服務

1、修改配置文件

[root@yu61 objects]# cat hosts.cfg   ##最後編輯
define host{
        use                     linux-server            
        host_name               yu63
        alias                   http服務
        address                 192.168.1.63
        icon_p_w_picpath              switch.gif
        statusmap_p_w_picpath         switch.gd2
        2d_coords               100,200
        3d_coords               100,200,100
        }


[root@yu61 objects]# cat service.cfg   ##最後添加
###################check_server_httpd-63#############################
define service{
                use                     local-service
                host_name                yu63
                service_groups             http服務組
                service_description         http服務
                check_command           check_http
                }
 
##########################check_server_http-63_groups######################
define servicegroup{
                servicegroup_name         http服務組
                alias                     http服務器
                members                 yu63,http服務
}


 

#Check_http的命令默認就有的

 

2、檢測配置文件和重啓服務

[root@yu61 objects]# /etc/init.d/nagios checkconfig 
Total Warnings: 0
Total Errors:   0
[root@yu63 nrpe-2.12]# service iptables stop
[root@yu63 nrpe-2.12]# setenforce 1
setenforce: SELinux is disabled
[root@yu63 nrpe-2.12]# service httpd restart


3、測試查看主機和服務

wKiom1lJI5jQI8XfAAAxqs9nmR8822.png 


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