作者 主題: Nagios安裝筆記  (閱讀 13744 次)

0 會員 與 1 訪客 正在閱讀本文。

wlhfor

  • 懷疑的國中生
  • **
  • 文章數: 39
    • 檢視個人資料
Nagios安裝筆記
« 於: 2012-12-17 17:35 »
教學是參考這篇,該討論區可以用FB的帳號登入,登入後才可以看到圖
http://www.tshopping.com.tw/thread-58268-1-1.html
鳥哥討論區
http://phorum.vbird.org/viewtopic.php?f=2&t=34270
CentOS6.2    mini安裝

1.安裝相關套件
yum  -y groupinstall "Development Libraries" "Development Tools"     //這兩個套件可以在安裝CentOS時就選取,少安裝這兩個,在做make,make install時會有問題
yum -y install httpd gcc glibc glibc-common gd gd-devel mod_ssl openssl openssl-devel wget make xinetd  php                 //Server端安裝套件

yum -y install gcc glibc glibc-common gd gd-devel mod_ssl openssl openssl-devel wget make xinetd                            //Client端安裝套件

停用iptables與selinux
server iptables stop
server ip6tables stop
chkconfig iptables off
chkconfig ip6tables off
vi /etc/selinux/config
改成如下:
SELINUX=disabled
init 6               //OS重開

-----------------------------------------------------------------------以上Server Client都要做---------------------------------------------------------------------------------

2..建立Nagios帳號                    //一開始就用root登入就不須切換
 轉換權限為root帳號
 su –l
 建立名為nagios的新帳號並設定密碼
 /usr/sbin/useradd –m nagios;passwd nagios

3. 建立名為nagcmd的新群組帳號,以允許外部指令可以經由Web介面傳送,
 並將系統的nagios與apache帳號,加進此群組。指令如下:
 /usr/sbin/groupadd nagcmd
 /usr/sbin/usermod –a –G nagcmd nagios
 /usr/sbin/usermod –a –G nagcmd apache
     檢查nagios與apache兩個帳號是否加入nagcmd的group
      cat /etc/group | grep nagcmd
      nagcmd:x:501:nagios,apache
     
4.下載Nagios原始檔與相關外掛附件(Plugins)
 建立存放Nagios與Plugins的目錄,例如:
 mkdir ~/downloads
 cd ~/downloads
      若要最新版本,請到官網取得下戴連結後,用wget下戴,簡中版本可在GOOGLE搜尋取得下戴網址,再用WGET取得
      http://www.nagios.org/

wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.4.1.tar.gz                           //目前最新版
wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.16.tar.gz        //目前最新版

5.下載Nagios之NRPE addon
 為了讓Nagios伺服器可以監看遠端Linux主機,需要額外安裝NRPE套件。
 (被監看的Linux主機亦須安裝,詳如後述)。下載方式為:
       wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gz                             //最新版本也可以在nagios官網下戴

6..編譯與安裝Nagios
 解壓縮Nagios原始檔:
 tar zxvf nagios-3.4.1.tar.gz
       cd nagios                                                   //3.41版解壓縮完只會有nagios目錄,不會加版本號碼
       執行Nagios安裝設定程式,並提供nagios群組名稱nagcmd作為輸入參數:
 ./configure --with-command-group=nagcmd
 編譯Nagios原始檔:
 make all
 分別安裝Nagios Binary程式、初始程序init script、設定範例檔及設定外部指令存放目錄之權限,
 指令分別如下:
 make install
 make install-init
 make install-config
 make install-commandmode
       make install-webconf
 .客製化設定值
 另/usr/local/nagios/etc/objects/contacts.cfg設定檔中,需將nagiosadmin改成實際接收訊息的Email帳號。
       alias                           Nagios Admin            ; Full name of user
        email                           nagios@localhost        ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******        //這行就是警告信的寄件人,自定即可,也可以不改

      替Nagios之Web介面進行身分驗證保護,使用HTTP Basic Authentication方式:
 htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
 此代表將系統詢問的密碼,存放於/usr/local/nagios/etc/htpasswd.users,而使用此密碼的帳號
       

     7.接著請重新啟動Apache以套用新的設定值。
 #service httpd restart        //若有出現下列訊息,請修改/etc/httpd/conf/httpd.conf裡的ServerName,把前面#號拿掉,ServerName改一下即可,再重啟httpd
          Starting httpd: httpd: apr_sockaddr_info_get() failed for CentOS
           httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
        #chkconfig httpd on


    8.編譯與安裝Nagios之外掛附件(Plugins)
 編譯與安裝Nagios之Plugins時,同樣進入下載目錄,並依照前述安裝Nagios之方式進行安裝:
 cd ~/downloads
 tar xzvf nagios-plugins-1.4.13.tar.gz
 cd nagios-plugins-1.4.13
 編譯與安裝Nagios之Plugins。
 ./configure --with-nagios-user=nagios --with-nagios-group=nagios
 make
 make install
 9.編譯與安裝NRPE
 cd ~/downloads
 tar xzvf nrpe-2.12.tar.gz
 cd nrpe-2.12
 ./configure
 make all
 make install-plugin
 10.啟動Nagios
 將Nagios服務登錄到系統服務列表中:
 chkconfig --add nagios
 chkconfig nagios on
 檢驗Nagios之設定值是否正確:
 /usr/local/nagios/bin/nagios –v /usr/local/nagios/etc/nagios.cfg
       之後請啟動Nagios。
 service nagios start
       此階段應可正常使用Nagios之Web介面。請使用瀏覽器登入下列URL:
        http://IP/nagios
        若出現
         You don't have permission to access /nagios/ on this server
         1 , #cd  /etc/httpd   | grep  –rH  ‘Include’ *
         2 ,# yum -y install php
         3 , #service  httpd  restart


11.Client端安裝

1.有關Nagios監看Linux主機,常見方式為使用Nagios的NRPE套件協助,此外尚需要Nagios Plugins,
 相關步驟依序如下:
 .建立Nagios帳號
 轉換權限為root帳號
 su –l
 建立名為nagios的新帳號並設定密碼
 /usr/sbin/useradd –m nagios
 passwd nagios
 2.下載NRPE與Nagios Plugins
        # wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.16.tar.gz
       #wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gz
   3.編譯與安裝Nagios Plugins
 cd ~/downloads
 tar xzvf nagios-plugins-1.4.13.tar.gz
       
 cd nagios-plugins-1.4.13
 ./configure --with-nagios-user=nagios --with-nagios-group=nagios
 make
 make install

4.編譯與安裝NRPE
 cd ~/downloads
 tar xzvf 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
5.設定NRPE為系統服務
      限定可連線之監看主機IP
 修改/etc/xinetd.d/nrpe,限定僅有設定之Nagios監看主機。
 如圖1所示的10.3.89.17,主要修改only_from設定值,將其改為:
 only_from = 127.0.0.1 10.3.89.17                                                       //設定Nagios Server的IP

6.    指定NRPE欲使用之系統通訊埠
 修改/etc/services,使NRPE可以接收監看主機的連線需求。
 在此假定NRPE所用的通訊埠為TCP 5666,即增加下行於檔案中:
 nrpe 5666/tcp # nrpe daemon

7.–啟動xinetd服務
 service xinetd start

除了可以使用 xinetd 啟動 nrpe,nrpe 也是可以獨立啟動的
在 nrpe 被編譯完後,目錄裡會有個 init-script 檔,把它複製至 /etc/init.d/

cp -rp nrpe-2.12/init-script /etc/init.d/nrpe
chmod +x /etc/init.d/nrpe
chown nagios.nagios /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios/libexec

就可以這樣子啟動 nrpe 了

/etc/init.d/nrpe start

再設定開機啟動

chkconfig nrpe on


8.啟動服務
      /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
9.–檢查NRPE服務是否正常
 /usr/local/nagios/libexec/check_nrpe -H localhost
 若回應的訊息為NRPE及其版本資訊,即代表安裝成功。
10.修改NRPE設定檔
 NRPE主要設定檔為位於/usr/local/nagios/etc/nrpe.cfg,其中包含用來檢查的指令,
 如下列為檢查使用者的check_users,與檢查負載的check_load指令。
 -w代表Warning;-c代表Critical,分別表示訊息嚴重等級。如check_users。
 若系統同時使用者達到5個,則送出Warning等級訊息;若達到10個,就送出Critical等級訊息。

      此外在command[xxxx]中,其中xxxx代表可透過chek_nrpe指令,呼叫相關檢查指令,請依序進行檢查:
 /usr/local/nagios/libexec/check_nrpe -H localhost -c check_users
 /usr/local/nagios/libexec/check_nrpe -H localhost -c check_load
 /usr/local/nagios/libexec/check_nrpe -H localhost -c check_hda1
 /usr/local/nagios/libexec/check_nrpe -H localhost -c check_total_procs
 /usr/local/nagios/libexec/check_nrpe -H localhost -c check_zombie_procs
 若回應預期訊息,代表功能正常;反之,回應找不到指令的錯誤訊息,代表在”-c”後,
 所接的指令名稱有誤,或該指令名稱,對應/usr/local/nagios/etc/nrpe.cfg的設定值有誤,
 請再檢查排除。
 例如在check_hda1設定,由於NRPE預設是IDE介面硬碟機/dev/hda1,
 假如系統實際使用的硬碟機,為SCSI介面的(/dev/sda1),
 則請將/usr/local/nagios/etc/nrpe.cfg 的
 command[check_hda1]=/usr/local/nagios/libexec/check_disk –w 20% -c 10% -p /dev/hda1
 改為
 command[check_sda1]=/usr/local/nagios/libexec/check_disk –w 20% -c 10% -p /dev/sda1
 將/dev/hda1改為/dev/sda1。
 另外command[check_hda1]改為command[check_sda1]僅為方便識別,可自行決定是否變更,
 或改為通用名稱,如將hda1改為disk1。

Nagios伺服器與用戶端串連設定
 Linux受監看主機連結設定
 在Nagios伺服器與Linux主機用戶端的連結,採用NRPE進行資訊溝通。
 當Nagios伺服器安裝NRPE後,即可使用check_nrpe指令測試,指令為:
 /usr/local/nagios/libexec/check_nrpe –H ClientIP                                                   //在Nagios Server下指令
       若回應的訊息為NRPE及其版本資訊,即代表安裝成功。
若出現CHECK_NRPE: Error - Could not complete SSL handshake
1.在CLIENT端
     #vi /usr/local/nagios/etc/nrpe.cfg
          尋找allowed_hosts=127.0.0.1,Server端IP                     
       改完存檔離開
2.重啟nrpe
      #service xinetd restart
      #ps aux | grep nrpe                    //看NRPE的PID
      #kill -9 nrpe的PID
       #/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
       #netstat -an | grep ":5666"       //nrpe用的PORT,有找到表示服務有起來

另亦可使用下列指令,檢查遠端Linux主機的使用者數量。
 /usr/local/nagios/libexec/check_nrpe -H ClientIP -c check_users
有傳訊息回來表示client端已經OK

3.修改NagiosServer設定
#vi /usr/local/nagios/etc/objects/commands.cfg,增加check_nrpe設定
-----------------------------------------------------------請自行新增下列設定----------------------------------------------------------------------------
# 'check_nrpe' command definition
define command{
        command_name    check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }
----------------------------------------------------------------------------------------------------------------------------------------------------------------
4.新增監控設定檔
以類似設定Windows主機監看的方式,在/usr/local/nagios/etc/objects/增加名為linux.cfg的檔案
#cp /usr/local/nagios/etc/objects/windows.cfg /usr/local/nagios/etc/objects/linux.cfg

5.修改nagios.cfg
#vi /usr/local/nagios/etc/nagios.cfg
找下列這行MARK起來,新增另一行,才會吃自己的設定檔
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
cfg_file=/usr/local/nagios/etc/objects/linux.cfg
改完存檔離開

6.修改linux.cfg

接下來處理服務的部份, 因為所有要監控的服務都會認主機名稱
使用 :%s/winserver/suseserver130/gc 將 define service{ } 內的
host_name winserver
改為
host_name suseserver130                       //設NagiosClinet的hostname
     

設定host
define host{
        use             linux-server    ; Inherit default values from a template
        host_name       NagiosClient    ; The name we're giving to this host                                                       //設NagiosClinet的hostname       
        alias           My Linux Server ; A longer name associated with the host                                                       
        address         192.168.1.231   ; IP address of the host                                                                                  //1台以上的機器用逗號隔開
        }



接下來定義群組

將原來的

代碼:
define hostgroup{
hostgroup_name  windows-servers ; The name of the hostgroup
alias   Windows Servers ; Long name of the group
}



改為

define hostgroup{
hostgroup_name  suse-servers ; The name of the hostgroup 可自行定義的群組名稱(短)
alias  SUSE Servers ; Long name of the group 可自行定義的群組名稱(長)
members   suseserver130 ;群組內的主機, 故要把相關的host_name寫入
}

設定要監控的服務
將define service{ } 內的 service_description 及 check_command
修改(由check_nt 改為check_nrpe)


設定服務
原來的:
代碼:
define service{
use   generic-service
host_name   suseserver130
service_description  NSClient++ Version
check_command  check_nt!CLIENTVERSION
}


改為(以線上使用者為例)

define service{
use generic-service
host_name suseserver130
service_description Online Users
check_command check_nrpe!check_users
}



define service{
       use                     generic-service
       host_name               suseserver130
       service_description     Check /
       check_command           check_nrpe!check_sda2
       }
define service{
       use                     generic-service
       host_name               suseserver130
       service_description     Check /home
       check_command           check_nrpe!check_sda3
       }
define service{
       use                     generic-service
       host_name               suseserver130
       service_description     Check Load
       check_command           check_nrpe!check_load
       }
define service{
       use                     generic-service
       host_name               suseserver130
       service_description     Total_Procs
       check_command           check_nrpe!check_total_procs
       }
define service{
       use                     generic-service
       host_name               suseserver130
       service_description     Check SSH
       check_command           check_nrpe!check_ssh
       }
define service{
       use                     generic-service
       host_name               suseserver130
       service_description     Check SMTP
       check_command           check_nrpe!check_smtp
       }

注意:
check_command           check_nrpe!check_ssh
這裡的Service必須在nagiosclient的nrpe.cfg中定義才能使用
exp:
NagiosServer的linux.cfg
define service{
       use                     generic-service
       host_name               suseserver130
       service_description     Check Load
       check_command           check_nrpe!check_load
       }
NagiosClinet的nrpe.cfg必須先有下面的定義

command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20


以上設定完,可連進nagios webUI看

Niko

  • 活潑的大學生
  • ***
  • 文章數: 281
  • 性別: 男
    • 檢視個人資料
Re: Nagios安裝筆記
« 回覆 #1 於: 2012-12-17 21:04 »
讚!!!
謝謝wlhfor大大分享!!!

netman

  • 管理員
  • 俺是博士!
  • *****
  • 文章數: 17484
    • 檢視個人資料
    • http://www.study-area.org
Re: Nagios安裝筆記
« 回覆 #2 於: 2012-12-18 17:03 »
感謝分享~~~!

huckly

  • 區域板主
  • 俺是博士!
  • *****
  • 文章數: 3420
    • 檢視個人資料
    • http://blog.huckly.net
Re: Nagios安裝筆記
« 回覆 #3 於: 2012-12-18 18:24 »
此文不推枉為人
IT doesn't matter

wlhfor

  • 懷疑的國中生
  • **
  • 文章數: 39
    • 檢視個人資料
Re: Nagios安裝筆記
« 回覆 #4 於: 2012-12-19 11:05 »
感謝大家哦~~~有問題再來討論

sakana

  • 酷!學園 學長們
  • 俺是博士!
  • *****
  • 文章數: 2590
  • 性別: 男
    • 檢視個人資料
    • sakananote
Re: Nagios安裝筆記
« 回覆 #5 於: 2012-12-20 08:59 »
感謝分享
^___^
專心投入研究才是王道!!
http://sakananote2.blogspot.com