1. 程式人生 > >CentOS 6.4+Nagios+Msmtp+Mutt郵箱報警

CentOS 6.4+Nagios+Msmtp+Mutt郵箱報警

  • 環境:

    Nagios Server Nagios 6.4

    IP:192.168.0.58

    Nagios Clint Nagios 6.4

    IP:192.168.0.83

    另注意:

    Nagios需要PHP支援,安裝Apache+PHP步驟略....

    =========================================================================================

    ——安裝Nagios

    [[email protected] ~]# cd /Linux

    [[email protected] Linux]# tar fzvx nagios-4.0.2.tar.gz

    [[email protected] Linux]# cd nagios-4.0.2

    [[email protected] nagios-4.0.2]# useradd -M -s /sbin/nologin nagios

    [[email protected] nagios-4.0.2]# mkdir /usr/local/nagios

    [[email protected] nagios-4.0.2]# chown -R nagios:nagios /usr/local/nagios/

    [[email protected] nagios-4.0.2]# ./configure --prefix=/usr/local/nagios/

    [[email protected] nagios-4.0.2]# make all

    [[email protected] nagios-4.0.2]# make install

    [[email protected] nagios-4.0.2]# make install-init

    [[email protected] nagios-4.0.2]# make install-commandmode

    [[email protected] nagios-4.0.2]# make install-config

    [[email protected] nagios-4.0.2]# chkconfig --add nagios

    [[email protected] nagios-4.0.2]# chkconfig --level 35 nagios on

    [[email protected] nagios-4.0.2]# chkconfig --list nagios

    nagios 0:關閉 1:關閉 2:關閉 3:啟用 4:關閉 5:啟用 6:關閉

    [[email protected] nagios-4.0.2]# service nagios restart

    =========================================================================================

    ——服務端安裝Nagios外掛plugins

    [[email protected] Linux]# tar zfvx nagios-plugins-1.5.tar.gz

    [[email protected] Linux]# cd nagios-plugins-1.5

    [[email protected] nagios-plugins-1.5]# ./configure --prefix=/usr/local/nagios/

    [[email protected] nagios-plugins-1.5]# make && make install

    ----------------------------------------------------------------------------

    ——服務端安裝nrpe

    [[email protected] Linux]# tar zfvx nrpe-2.15.tar.gz

    [[email protected] Linux]# cd nrpe-2.15

    [[email protected] nrpe-2.15]# ./configure

    [[email protected] nrpe-2.15]# make all

    [[email protected] nrpe-2.15]# make install-plugin

    =========================================================================================

    ——使apache支援nagios [這裡安裝apache的路徑為/usr/local/apache-2.4]

    [[email protected] nagios-plugins-1.5]# vi /usr/local/apache-2.4/conf/httpd.conf

    ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"

    <Directory "/usr/local/nagios/sbin">

    AuthType Basic

    Options ExecCGI

    AllowOverride None

    Order allow,deny

    Allow from all

    AuthName "Nagios Access"

    AuthUserFile /usr/local/nagios/etc/htpasswd

    Require valid-user

    </Directory>

    Alias /nagios "/usr/local/nagios/share"

    <Directory "/usr/local/nagios/share">

    AuthType Basic

    Options None

    AllowOverride None

    Order allow,deny

    Allow from all

    AuthName "nagios Access"

    AuthUserFile /usr/local/nagios/etc/htpasswd

    Require valid-user

    </Directory>

    =========================================================================================

    ——新增nagios登陸使用者

    [[email protected] nagios-plugins-1.5]# /usr/local/apache-2.4/bin/htpasswd -c /usr/local/nagios/etc/htpasswd nagios

    New password:

    Re-type new password:

    Adding password for user nagios

    ——修改apache的使用者和使用者組

    [[email protected] nagios-plugins-1.5]# vi /usr/local/apache-2.4/conf/httpd.conf

    User nagios

    Group nagios

    --------------------------------------------------------------------------

    ——解決Nagios亂碼問題

    [[email protected] nagios-plugins-1.5]# vi /usr/local/apache-2.4/conf/httpd.conf

    LoadModule cgid_module modules/mod_cgid.so

    LoadModule actions_module modules/mod_actions.so

    [[email protected] nagios-plugins-1.5]# service httpd restart

    ——測試訪問

    wKioL1LXl87wpR_dAAJfuBut6kA717.jpg

    =========================================================================================

    ——客戶端安裝nagios-plugins

    [[email protected] ~]# cd /Linux

    [[email protected] Linux]# useradd -M -s /sbin/nologin nagios

    [[email protected] Linux]# tar zfvx nagios-plugins-1.5.tar.gz

    [[email protected] Linux]# cd nagios-plugins-1.5

    [[email protected] nagios-plugins-1.5]# ./configure --prefix=/usr/local/nagios/

    [[email protected] nagios-plugins-1.5]# make && make install

    ——客戶端安裝nrpe

    [[email protected] nagios-plugins-1.5]# cd ..

    [[email protected] Linux]# tar zfvx nrpe-2.15.tar.gz

    [[email protected] Linux]# cd nrpe-2.15

    [[email protected] nrpe-2.15]# ./configure

    [[email protected] nrpe-2.15]# make all

    [[email protected] nrpe-2.15]# make install-plugin

    [[email protected] nrpe-2.15]# make install-daemon

    [[email protected] nrpe-2.15]# make install-daemon-config

    [[email protected] nrpe-2.15]# vi /usr/local/nagios/etc/nrpe.cfg

    allowed_hosts=127.0.0.1,192.168.0.58

    -----------------------------------------------------------------------------------------

    ——啟動nrpe

    [[email protected] nrpe-2.15]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

    ——檢查是否啟動

    [[email protected] nrpe-2.15]# netstat -antup |grep 5666

    tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 48456/nrpe

    tcp 0 0 :::5666 :::* LISTEN 48456/nrpe

    ——開機啟動

    [[email protected] nrpe-2.15]# vi /etc/rc.local

    /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

    ——在服務端檢查 [如果返回NRPE版本及成功,相反則檢查配置]

    [[email protected] ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.0.83

    NRPE v2.15

    =========================================================================================

    ——配置nagios

    [[email protected] nrpe-2.15]# cd /usr/local/nagios/etc/

    [[email protected] etc]# vi nagios.cfg

    註釋下面的內容

    #cfg_file=/usr/local/nagios/etc/objects/commands.cfg

    #cfg_file=/usr/local/nagios/etc/objects/contacts.cfg

    #cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg

    #cfg_file=/usr/local/nagios/etc/objects/templates.cfg

    #cfg_file=/usr/local/nagios/etc/objects/localhost.cfg

    #cfg_file=/usr/local/nagios/etc/objects/windows.cfg

    #cfg_file=/usr/local/nagios/etc/objects/switch.cfg

    #新增監控主機目錄

    cfg_dir=/usr/local/nagios/etc/objects/hosts

    #新增模板目錄

    cfg_dir=/usr/local/nagios/etc/objects/templates

    [[email protected] etc]# mdkir objects/templates

    [[email protected] etc]# mdkir objects/hosts

    ——複製所需檔案

    [[email protected] etc]# cp objects/commands.cfg objects/templates/

    [[email protected] etc]# cp objects/timeperiods.cfg objects/templates/

    ——建立自定義模板

    [[email protected] etc]# vi objects/templates/templates.cfg

    #聯絡人模板

    define contact{

    #聯絡人名稱

    name tao

    #當服務出現異常時 傳送通知的時間段

    service_notification_period 24x7

    #當主機出現異常時 傳送通知的時間段

    host_notification_period 24x7

    #w 表示警告 u 表示不明 c 表示緊急 r 表示恢復

    service_notification_options w,u,c,r

    #d 表示宕機 u 表示不可達 r 表示重新恢復

    host_notification_options d,u,r

    #服務故障時 傳送通知的方式[郵件]

    service_notification_commands notify-service-by-email

    #主機故障時 傳送通知的方式[郵件]

    host_notification_commands notify-host-by-email

    register 0

    }

    #主機模板

    define host{

    #定義主機名

    name linux-host

    #其值為1 Nagios將收集的資料寫入某個檔案中 以備提取

    process_perf_data 1

    passive_checks_enabled 0

    #主機與服務的重新整理檢測 0為關閉

    check_freshness 0

    #事件處理開啟

    event_handler_enabled 1

    #開啟抖動感知

    flap_detection_enabled 1

    active_checks_enabled 1

    #傳送通知的時間段

    notification_period 24x7

    #檢查主機的時間段

    check_period 24x7

    #對主機的檢查時間間隔/分鐘

    check_interval 5

    #如果宕機重試檢查時間/分鐘

    retry_interval 1

    #宕機後對主機的最大檢查次數

    max_check_attempts 2

    #檢查主機狀態指令[在commands.cfg中定義]

    check_command check-host-alive

    #在主機出現異常後 故障一直沒有解決 再次傳送通知的時間/分鐘

    notification_interval 5

    #d 表示宕機 u 表示不可達 r 表示重新恢復

    notification_options d,u,r

    #1為開啟報警資訊

    notifications_enabled 0

    #指定聯絡人組[在contacts.cfg中定義]

    contact_groups admins

    #處理效能資料

    #一天重新整理檢測一次,以防止檢測結果不是實時的

    freshness_threshold 86400

    #0為不註冊,意思是這個作為模板

    register 0

    }

    #服務模板

    define service{

    #定義一個服務名稱

    name linux-service

    #啟用主動服務檢查

    active_checks_enabled 1

    #啟用被動服務檢查

    passive_checks_enabled 0

    #主機與服務的重新整理檢測

    check_freshness 0

    #開啟報警資訊

    notifications_enabled 1

    #重新發送報警資訊的間隔

    notification_interval 5

    #事件處理開啟

    event_handler_enabled 1

    #啟用抖動感知

    flap_detection_enabled 1

    #處理效能資料

    process_perf_data 1

    #傳送通知的時間段

    notification_period 24x7

    #檢查時間段

    check_period 24x7

    #如果宕機最大檢查次數

    max_check_attempts 5

    #檢查服務時間間隔/分鐘

    normal_check_interval 1

    #如果宕機再次檢查的間隔時間/分鐘

    retry_check_interval 1

    #一天重新整理檢測一次,以防止檢測結果不是實時的

    freshness_threshold 86400

    #0為不註冊,意思是這個作為模板

    register 0

    }

    define service {

    name test

    #1為開啟報警資訊

    notifications_enabled 0

    #重新發送報警資訊的間隔

    notification_interval 5

    notification_options w,u,c,

    #事件處理開啟

    event_handler_enabled 1

    #開啟抖動感知

    flap_detection_enabled 1

    #傳送通知的時間段

    notification_period 24x7

    #檢查時間段

    check_period 24x7

    #每一次檢測的間隔時間

    check_interval 1

    #如果宕機再次檢查的間隔時間

    retry_interval 1

    #檢測次數,如果2次都是宕機的話 就報警

    max_check_attempts 2

    active_checks_enabled 1

    passive_checks_enabled 0

    #處理效能資料

    process_perf_data 1

    #主機與服務的重新整理檢測 0為關閉

    check_freshness 0

    #一天重新整理檢測一次,以防止檢測結果不是實時的

    freshness_threshold 86400

    #0為不註冊,意思是這個作為模板

    register 0

    }

    [[email protected] etc]# vi objects/templates/contacts.cfg

    #聯絡人

    define contact{

    #使用者名稱稱

    contact_name nagios

    #引用模板

    use tao

    #別名

    alias Nagios Admin

    #傳送郵箱

    email [email protected]

    }

    #聯絡人組

    define contactgroup{

    #組名稱

    contactgroup_name admins

    #組別名

    alias Nagios Administrators

    #組使用者成員

    members nagios

    }

    ——新增監控主機

    最好使用被監控機IP為名稱、比如:被監控伺服器為192.168.0.83 [後期伺服器多的時候、容易維護]

    這裡為了省事、一臺伺服器監控多種服務。大家測試的時候、可以新增多個伺服器監控不同服務。

    [[email protected] etc]# vi objects/hosts/192.168.0.83.cfg

    #監控主機

    define host {

    host_name 192.168.0.83

    address 192.168.0.83

    use linux-host

    }

    #監控SSH

    define service {

    service_description check_ssh

    check_command check_ssh!-p 22

    host_name 192.168.0.83

    use linux-service

    register 1

    }

    #監控登陸使用者

    define service {

    service_description check_users

    check_command check_nrpe!check_users

    host_name 192.168.0.83

    use linux-service

    register 1

    }

    #監控mysql [需要在被監控配置、下面有配置說明]

    define service {

    service_description check_mysql

    check_command check_mysql

    host_name 192.168.0.83

    use linux-service

    register 1

    }

    #監控交換空間

    define service {

    service_description check_swap

    check_command check_nrpe!check_swap

    host_name 192.168.0.83

    use linux-service

    register 1

    }

    #監控磁碟

    define service {

    service_description check_disk

    check_command check_nrpe!check_disk

    host_name 192.168.0.83

    use linux-service

    register 1

    }

    #監控HTTP

    define service {

    service_description check_http

    check_command check_http!-p 80

    host_name 192.168.0.83

    use linux-service

    register 1

    }

    #監控負載

    define service {

    service_description check_load

    check_command check_nrpe!check_load

    host_name 192.168.0.83

    use linux-service

    register

    }

    =========================================================================================

    ——重啟啟動nagios、登陸WEB檢視

    網上很多人說配置後,執行命令檢查是否有錯誤。個人感覺完全沒必要、因為啟動的時候Nagios預設會自動檢查配置、如果有錯誤Nagios會提示。

    [[email protected] etc]# service nagios restart

    ——在客戶端 登陸nagios

    wKiom1LXniaxEQwCAARhIBZMkp8864.jpg

    =========================================================================================

    注意:監控MySQL需要在被監控端配置如下

    ——客戶端連線mysql新增nagios使用者

    mysql> create database nagios;

    Query OK, 1 row affected (0.00 sec)

    mysql> grant select on nagios.* to [email protected]'192.168.1.18' identified by 'nagios';

    Query OK, 0 rows affected (0.11 sec)

    mysql> flush privileges;

    Query OK, 0 rows affected (0.00 sec)

    mysql> quit

    -----------------------------------------------------------------------------------------

    ——Nagios伺服器端新增mysql監控

    [[email protected] nrpe-2.15]# vi /usr/local/nagios/etc/objects/templates/commands.cfg

    # 'check_mysql' command definition

    define command{

    command_name check_mysql

    command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -u nagios -d nagios -p nagios

    }

    =========================================================================================

    ——安裝msmtp

    [[email protected] Linux]# cd msmtp-1.4.31

    [[email protected] msmtp-1.4.31]# ./configure --prefix=/usr/local/msmtp

    [[email protected] msmtp-1.4.31]# make && make install

    [[email protected] msmtp-1.4.31]# cd /usr/local/msmtp/

    [[email protected] msmtp]# mkdir etc

    [[email protected] msmtp]# cd etc/

    ——配置msmtp

    [[email protected] etc]# vi msmtprc

    account default

    logfile /usr/local/msmtp/msmtp.log

    host smtp.163.com  #發件伺服器

    from [email protected] #從那發郵件,和下面的郵箱是同一個

    auth login

    user [email protected] #發件郵箱

    password xxxxxx  #郵箱密碼

    ----------------------------------------------------------------------------