搭建nagios監控步驟

1、實驗環境:

主機名:            ip              需要的軟件環境

nagios-server     192.168.124.143    Apache、Php、Nagios、nagios-plugins、nrpe

nagios-linux      192.168.124.203    nagios-plugins、nrpe

監控機 nagios-server上需要 安裝nagios軟件,對監控的數據做處理,並且提供 web界面查看和管理。當然也對本機自身的信息進行監控;

被監控機 nagios-linux需安裝NRPE及nagios plugins,根據監控機的請求執行監 控,然後將結果回傳給監控機;

一、Nagios監控機(192.168.124.143)上的環境準備

      1、 安裝編譯環境

      使用yum安裝

      [root@localhost ~]# yum install -y gcc glibc glibc-common gd xinetd openssl-devel mysql-devel

      2、 創建nagios用戶和用戶組

     [root@localhost ~]#useradd -s /sbin/nologin nagios

     [root@localhost ~]#mkdir /usr/local/nagios

     [root@localhost ~]#chown -R nagios:nagios /usr/local/nagios 

      3、 yum安裝httpd和php,並配置httpd支持php     

           只要能實現瀏覽器能正常訪問 http://本機IP/index.php 既可

      4、 編譯安裝nagios    

      上傳nagios源碼包至/root目錄下 

      [root@localhost ~]#tar zxvf nagios-4.0.3.tar.gz   

      [root@localhost ~]#cd nagios-4.0.3   

      [root@localhost ~]#./configure --prefix=/usr/local/nagios --with-nagiosuser=nagios --with-nagios-group=nagios  

      [root@localhost ~]#make all   

      [root@localhost ~]#make install   

      [root@localhost ~]#make install-init

      [root@localhost ~]#make install-commandmode

      [root@localhost ~]#make install-config

      [root@localhost ~]#make install-webconf

      [root@localhost ~]#chkconfig --add nagios                           #       將nagios加入系統 服務

      [root@localhost ~]#chkconfig nagios on

      5、 驗證nagios是否被正確安裝

         切換目錄到安裝路徑(這裏是/usr/local/nagios),看是否存在etc、bin、 libexec、sbin、share、var  這些目錄,如果存在則可以表明程序被正確的安裝到 系統了。

         Nagios 各個目錄用途說明如下:     

        

     目錄名             用途

    bin Nagios       可執行程序所在目錄 

    etc Nagios       配置文件所在目錄 

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

    libexec         Nagios 外部插件所在目錄 

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

    var/archives    Nagios 日誌自動歸檔目錄 

    var/rw        用來存放外部命令文件的目錄

      6、 安裝nagios插件

         上傳nagios-plugins-1.5.tar.gz至/root目錄下

     [root@localhost ~]#tar zxvf nagios-plugins-1.5.tar.gz

     [root@localhost ~]#cd nagios-plugins-1.5

      [root@localhost ~]#./configure --prefix=/usr/local/nagios --with-mysql --with-nagios-user=nagios --with-nagiosgroup=nagios

     [root@localhost ~]#make && make install

     備註:

      --with-mysql 使nagios插件支持監控mysql(系統要先yum安裝好 mysql-devel軟件包),在libexec目錄下會有 check_mysql和check_mysql_query命令

      7、 安裝check_nrpe插件

        上傳nrpe-2.12.tar.gz至/root目錄下  

      [root@localhost ~]#tar zxvf nrpe-2.12.tar.gz   

     [root@localhost ~]#cd nrpe-2.12

     [root@localhost ~]#./configure --with-nrpe-user=nagios      --with-nrpe-group=nagios      --with-nagiosuser=nagios      --with-nagios-group=nagios      --enable-command-args      --enable-ssl      

     [root@localhost ~]#make all

     [root@localhost ~]#make  install-plugin

      監控機上只運行到這一步就行,因爲只需要nrpe中的check_nrpe插件             

      8、 修改apache的配置文件/etc/httpd/conf/httpd.conf

       [root@localhost ~]#vim /etc/httpd/conf/httpd.conf

         User apache修改爲User nagios

        Group apache修改爲Group nagios

         DirectoryIndex index.html修改爲DirectoryIndex index.php

         保存

      9、 爲了安全起見,一般情況下要讓nagios 的web 監控頁面必須經過授權才能訪問              編譯安裝nagios 4.0.3版本完成後,已自動配置httpd並設定了默認的htpasswd驗證文件                  /usr/local/nagios/etc/ htpasswd.users     

      [root@localhost ~]#htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin                       #指定用戶 nagiosadmin可以訪問  

      10、 啓動httpd服務

      [root@localhost ~]#service httpd restart  

      [root@localhost ~]#service nagios start   

      11、 瀏覽器訪問nagios站點

      http://192.168.124.143/nagios  

    二、被監控機(192.168.124.203)安裝nrpe及nagios-plugins  

      1、 安裝編譯環境

        使用yum安裝 

      [root@localhost ~]# yum install -y gcc glibc glibc-common gd xinetd openssl-devel mysql-devel

      2、 編譯安裝nagios插件

    上傳nagios-plugins-1.5.tar.gz至/root目錄下  

      [root@localhost ~]#tar -zxvf nagios-plugins-1.5.tar.gz 

      [root@localhost ~]#cd nagios-plugins-1.5

      [root@localhost ~]#./configure  --with-mysql --with-nagios-user=nagios --with-nagios-group=nagios    

      [root@localhost ~]#make && make install       

     這一步完成後會在/usr/local/nagios/下生成兩個目錄libexec和share,修改目錄權限            [root@localhost ~]#useradd -s /sbin/nologin nagios

       [root@localhost ~]#chown  -R nagios:nagios /usr/local/nagios       

     3、 編譯安裝nrpe 

    上傳nrpe-2.12.tar.gz至/root目錄下 

      [root@localhost ~]#tar -zxvf nrpe-2.12.tar.gz   

      [root@localhost ~]#cd nrpe-2.12   

      [root@localhost ~]#./configure --with-nrpe-user=nagios      --with-nrpe-group=nagios      --with-nagiosuser=nagios      --with-nagios-group=nagios      --enable-command-args      --enable-ssl    

       [root@localhost ~]#make all    

       [root@localhost ~]#make install-plugin 

       [root@localhost ~]#make install-daemon  

       [root@localhost ~]#make install-daemon-config 

     #現在再查看/usr/local/nagios 目錄就會發現有4個子目錄

      [root@localhost ~]#make install-xinetd   

      [root@localhost ~]#vim /etc/xinetd.d/nrpe  

        修改在末尾項 only_from    = 192.168.124.0/24

        這裏設置允許監控機所在的網段訪問   

       [root@localhost ~]#vim /etc/services  

     #編輯/etc/services 文件,末尾增加NRPE服務內容   

         在最後添加   nrpe    5666/tcp   #nrpe

       [root@localhost ~]#service xinetd restart 

      [root@localhost ~]#netstat -ntulp|grep 5666  

    #查看nrpe是否已啓動並監聽5666端口    

      4、 測試nrpe是否正常工作 

    在監控機(192.168.124.143)執行check_nrpe這個插件來測試與被監控機(192.168.124.203)的通訊    

      [root@localhost ~]#/usr/local/nagios/libexec/check_nrpe -H 192.168.124.203              如顯示     NRPE V2.12   ,則nrpe通訊正常    

           

    三、監控機(192.168.124.143)上的配置

     1、 在commands.cfg文件末尾增加check_nrpe的定義

       [root@localhost ~]#vim /usr/local/nagios/etc/objects/commands.cfg  

       #添加如下 代碼

      define command{         

          command_name    check_nrpe           

          command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$            }       

    2、 定義對Nagios-Linux 主機的監控,創建nagios_linux.cfg文件並加入以下 代碼內容


      [root@localhost ~]#vim /usr/local/nagios/etc/objects/nagios_linux.cfg 

      define host{       

         use             linux-server

         host_name         nagios-linux

         alias           nagios-linux    

         address          192.168.124.67                 #此處設置被監控機的IP地址            }

      define service{          #添加監控CPU負載的服務 

         use            local-service   

         host_name        nagios-linux    

         service_description   CPU Load

       check_command        check_nrpe!check_load

                 }

      define service{            #添加監控總進程數的服務

          use             local-service    

          host_name         nagios-linux  

          service_description  Total Processes 

          check_command      check_nrpe!check_total_procs   

                 }   

      上述代碼分別定義了CPU Load(cpu負載)以及Total Processes(總進程數)的監 控服務,還可以在此文件中增加其他的監控服務

      3、 在nagios的主配置文件nagios.cfg末尾增加如下代碼內容  

       [root@localhost ~]#vim /usr/local/nagios/etc/nagios.cfg  

      cfg_file=/usr/local/nagios/etc/objects/nagios_linux.cfg   

      #在nagios的主配置文件中增 加被監控機的配置文件 

      4、 重啓各項服務

       [root@localhost ~]#service httpd restart

       [root@localhost ~]#service nagios restart

      5、 驗證nagios是否能監控到本機(localhost)以及被監控機nagios-linux         

         http://ip/nagios    

      6、[root@localhost ~]#service httpd restart

        [root@localhost ~]#service nagios restart

       #重啓驗證新增的監控信息  

            

ok!到此結束!希望對你有所幫助


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