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

svdata發表於2015-01-09
  • 環境:

    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

    [root@Nagios ~]# cd /Linux

    [root@Nagios Linux]# tar fzvx nagios-4.0.2.tar.gz

    [root@Nagios Linux]# cd nagios-4.0.2

    [root@Nagios nagios-4.0.2]# useradd -M -s /sbin/nologin nagios

    [root@Nagios nagios-4.0.2]# mkdir /usr/local/nagios

    [root@Nagios nagios-4.0.2]# chown -R nagios:nagios /usr/local/nagios/

    [root@Nagios nagios-4.0.2]# ./configure --prefix=/usr/local/nagios/

    [root@Nagios nagios-4.0.2]# make all

    [root@Nagios nagios-4.0.2]# make install

    [root@Nagios nagios-4.0.2]# make install-init

    [root@Nagios nagios-4.0.2]# make install-commandmode

    [root@Nagios nagios-4.0.2]# make install-config

    [root@Nagios nagios-4.0.2]# chkconfig --add nagios

    [root@Nagios nagios-4.0.2]# chkconfig --level 35 nagios on

    [root@Nagios nagios-4.0.2]# chkconfig --list nagios

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

    [root@Nagios nagios-4.0.2]# service nagios restart

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

    ——服務端安裝Nagios外掛plugins

    [root@Nagios Linux]# tar zfvx nagios-plugins-1.5.tar.gz

    [root@Nagios Linux]# cd nagios-plugins-1.5

    [root@Nagios nagios-plugins-1.5]# ./configure --prefix=/usr/local/nagios/

    [root@Nagios nagios-plugins-1.5]# make && make install

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

    ——服務端安裝nrpe

    [root@Nagios Linux]# tar zfvx nrpe-2.15.tar.gz

    [root@Nagios Linux]# cd nrpe-2.15

    [root@Nagios nrpe-2.15]# ./configure

    [root@Nagios nrpe-2.15]# make all

    [root@Nagios nrpe-2.15]# make install-plugin

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

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

    [root@Nagios 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登陸使用者

    [root@Nagios 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的使用者和使用者組

    [root@Nagios nagios-plugins-1.5]# vi /usr/local/apache-2.4/conf/httpd.conf

    User nagios

    Group nagios

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

    ——解決Nagios亂碼問題

    [root@Nagios 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

    [root@Nagios nagios-plugins-1.5]# service httpd restart

    ——測試訪問

    wKioL1LXl87wpR_dAAJfuBut6kA717.jpg

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

    ——客戶端安裝nagios-plugins

    [root@CentOS ~]# cd /Linux

    [root@CentOS Linux]# useradd -M -s /sbin/nologin nagios

    [root@CentOS Linux]# tar zfvx nagios-plugins-1.5.tar.gz

    [root@CentOS Linux]# cd nagios-plugins-1.5

    [root@CentOS nagios-plugins-1.5]# ./configure --prefix=/usr/local/nagios/

    [root@CentOS nagios-plugins-1.5]# make && make install

    ——客戶端安裝nrpe

    [root@CentOS nagios-plugins-1.5]# cd ..

    [root@CentOS Linux]# tar zfvx nrpe-2.15.tar.gz

    [root@CentOS Linux]# cd nrpe-2.15

    [root@CentOS nrpe-2.15]# ./configure

    [root@CentOS nrpe-2.15]# make all

    [root@CentOS nrpe-2.15]# make install-plugin

    [root@CentOS nrpe-2.15]# make install-daemon

    [root@CentOS nrpe-2.15]# make install-daemon-config

    [root@CentOS nrpe-2.15]# vi /usr/local/nagios/etc/nrpe.cfg

    allowed_hosts=127.0.0.1,192.168.0.58

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

    ——啟動nrpe

    [root@CentOS nrpe-2.15]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

    ——檢查是否啟動

    [root@CentOS 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

    ——開機啟動

    [root@CentOS nrpe-2.15]# vi /etc/rc.local

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

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

    [root@Nagios ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.0.83

    NRPE v2.15

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

    ——配置nagios

    [root@Nagios nrpe-2.15]# cd /usr/local/nagios/etc/

    [root@Nagios 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

    [root@Nagios etc]# mdkir objects/templates

    [root@Nagios etc]# mdkir objects/hosts

    ——複製所需檔案

    [root@Nagios etc]# cp objects/commands.cfg objects/templates/

    [root@Nagios etc]# cp objects/timeperiods.cfg objects/templates/

    ——建立自定義模板

    [root@Nagios 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

    }

    [root@Nagios etc]# vi objects/templates/contacts.cfg

    #聯絡人

    define contact{

    #使用者名稱稱

    contact_name nagios

    #引用模板

    use tao

    #別名

    alias Nagios Admin

    #傳送郵箱

    email xxxxxxxx@qq.com

    }

    #聯絡人組

    define contactgroup{

    #組名稱

    contactgroup_name admins

    #組別名

    alias Nagios Administrators

    #組使用者成員

    members nagios

    }

    ——新增監控主機

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

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

    [root@Nagios 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會提示。

    [root@Nagios 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 nagios@'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監控

    [root@Nagios 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

    [root@Nagios Linux]# cd msmtp-1.4.31

    [root@Nagios msmtp-1.4.31]# ./configure --prefix=/usr/local/msmtp

    [root@Nagios msmtp-1.4.31]# make && make install

    [root@Nagios msmtp-1.4.31]# cd /usr/local/msmtp/

    [root@Nagios msmtp]# mkdir etc

    [root@Nagios msmtp]# cd etc/

    ——配置msmtp

    [root@Nagios etc]# vi msmtprc

    account default

    logfile /usr/local/msmtp/msmtp.log

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

    from xxxxxx@163.com #從那發郵件,和下面的郵箱是同一個

    auth login

    user xxxxxx@163.com #發件郵箱

    password xxxxxx  #郵箱密碼

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

  • 測試msmtp
    /usr/local/msmtp/bin/msmtp sdata@foxmail.com
    輸入任意字元,然後按Ctrl+D退出,檢視郵件是否收到。
    可以到 /usr/local/msmtp/msmtp.log,檢視日誌,發信成功失敗都會有記錄。

    ——安裝mutt

    [root@Nagios etc]# yum install mutt -y

    [root@Nagios etc]# vi /etc/Muttrc

    set sendmail="/usr/local/msmtp/bin/msmtp"

    set use_from=yes

    set realname="YingTao" #真是姓名

    set from=hypocriticals@163.com  //發件郵箱

    set envelope_from=yes

    set editor="vim"

    set record="/usr/local/msmtp/etc/sent" #郵件記錄

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

  • 測試一下mutt是否有效
    echo “測試測試” | mutt -s “測試” 測試郵件地址
    echo “test context” |mutt -s “test taile” sdata@foxmail.com
  • ==========================================================================================

    ——Nagios預設使用sendmail傳送郵件、只需要把 /bin/mail 改為 /usr/bin/mutt即可。

    [root@Nagios etc]# vi objects/templates/commands.cfg

    # 'notify-host-by-email' command definition

    define command{

    command_name notify-host-by-email

    command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/mutt -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$

    }

     

    # 'notify-service-by-email' command definition

    define command{

    command_name notify-service-by-email

    command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/bin/mutt -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$

    }

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

    ——測試能否通過郵箱報警

    1)在被監控端關閉mysql

    [root@CentOS ~]# /etc/init.d/mysqld stop

    Shutting down MySQL. SUCCESS!

    [root@CentOS ~]# netstat -antup |grep 3306

     

    2)登陸WEB Nagios檢視MySQL狀態

    wKioL1LXqUHxkv1lAASFcXJRBDw945.jpg

    3)在服務端檢視日誌 [預設日誌路徑為/usr/local/nagios/var/]

    [root@Nagios ~]# cd /usr/local/nagios/var/

    [root@Nagios var]# tail -f nagios.log

    檢查五次、如果五次服務還是無法檢測到服務是正常的,則發郵件通知。

    [1388002399] SERVICE ALERT: 192.168.0.83;check_mysql;CRITICAL;SOFT;1;Can't connect to MySQL server on '192.168.0.83' (111)

    [1388002459] SERVICE ALERT: 192.168.0.83;check_mysql;CRITICAL;SOFT;2;Can't connect to MySQL server on '192.168.0.83' (111)

    [1388002519] SERVICE ALERT: 192.168.0.83;check_mysql;CRITICAL;SOFT;3;Can't connect to MySQL server on '192.168.0.83' (111)

    [1388002579] SERVICE ALERT: 192.168.0.83;check_mysql;CRITICAL;SOFT;4;Can't connect to MySQL server on '192.168.0.83' (111)

    [1388002639] SERVICE ALERT: 192.168.0.83;check_mysql;CRITICAL;HARD;5;Can't connect to MySQL server on '192.168.0.83' (111)

    [1388002639] SERVICE NOTIFICATION: nagios;192.168.0.83;check_mysql;CRITICAL;notify-service-by-email;Can't connect to MySQL server on '192.168.0.83' (111)

    4)登陸郵箱檢視是否收到郵件 [由於是虛擬機器時間沒有調整、和現實時間有差別]

    wKiom1LXqxajK7J6AAIL-TGYd1o835.jpg

    5)重新啟動MySQL

    [root@CentOS ~]# /etc/init.d/mysqld start

    Starting MySQL.. SUCCESS!

    [root@CentOS ~]# netstat -antup |grep 3306

    tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 51040/mysqld

    6)檢測服務恢復正常後,是否收到郵件通知

    wKiom1LXqpCCoyDjAAJW4PkeYC0802.jpg

相關文章