2019-01-17 簡易配置爲Nagios配置postfix郵箱發送告警郵件

由於服務器本身沒有在公網上配置域名,我們也不需要用這個郵箱來收發郵件,僅僅是在出問題的時候nagios能夠發送報警郵件。
因此很簡單幾步就能做到。
首先安裝postfix以及mailutils輔助工具,因爲簡單,也不用mysql
sudo apt install postfix mailutils
如果ufw或iptables防火牆開啓的話,允許25端口
postfix的配置文件是/etc/postfix/main.cf,你可以修改一些配置,更改主機名,域名什麼的,這個另外的技術文檔有記錄。但是爲了簡易,可以什麼都不改。對,就是什麼都不改,postfix仍然可以用!用來發個告警郵件也足夠了。
測試一下,看看狀態是Active的。

root@Nagios_THN:~# systemctl status postfix
 postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/lib/systemd/system/postfix.service; enabled; vendor preset:
   Active: active (exited) since Mon 2019-01-14 23:59:24 GMT; 19min ago
  Process: 8387 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
 Main PID: 8387 (code=exited, status=0/SUCCESS)

Jan 14 23:59:24 Nagios_THN systemd[1]: Starting Postfix Mail Transport Agent...
Jan 14 23:59:24 Nagios_THN systemd[1]: Started Postfix Mail Transport Agent.
lines 1-8/8 (END)

測試下發個郵件

root@Nagios_THN:~# mail [email protected]
Cc:
Subject: This is a test mail.
Test email

Ctrl+d 發送出去,那邊立刻收到了:


那麼再看下Nagios告警郵件是否收的到。
檢查下Nagios裏的配置:
contacts.cfg裏的配置

#定義聯繫人模板
define contact {
        name                            ax-contact
        service_notification_period     24x7
        host_notification_period        24x7
        service_notification_options    w,u,c,r,f,s
        host_notification_options       d,u,r,f,s
        service_notification_commands   ax-notify-service-by-email
        host_notification_commands      ax-notify-host-by-email
        register                        0
}
#定義聯繫人
define contact {
        contact_name                    axing
        use                             ax-contact
        alias                           A Xing
        email                           [email protected]
        host_notification_options       d,u,r,s
        service_notification_options    w,u,c,r,s
}
#定義聯繫人組

define contactgroup {
        contactgroup_name               network
        alias                           Network Team
       members                         axing
}

在commands.cfg文件裏定義發郵件的動作

define command{
        command_name                    ax-notify-host-by-email
        command_line                    /usr/bin/printf "%b" "Subject: $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$\n\n***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $HOSTSTATE$\nDuration: $HOSTDURATION$\n\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n\n&" | /usr/sbin/sendmail -vt $CONTACTEMAIL$
}

define command{
        command_name                    ax-notify-service-by-email
        command_line                    /usr/bin/printf "%b" "Subject: $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$\n\n***** 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\n" | /usr/sbin/sendmail -vt $CONTACTEMAIL$
}

拿我定義的一臺主機爲例,定義了電池容量的告警參數:


define host {

    use                     generic-switch                      ; Inherit default values from a template
    host_name               ol01.com.lon                     ; The name we're giving to this switch
    alias                   MA5800 Comber       ; A longer name associated with the switch
    address                 10.XXX.XXX.2                      ; IP address of the switch
    hostgroups              olts,MA5800X7                              ; Host groups this switch is associated with
    parents                 vlan69.com
    _BRC_WARN_LEVEL 95
    _BRC_CRIT_LEVEL 65
    icon_image              olt41.jpg
#    statusmap_image  router.gd2
}

在service.cfg(或者其他什麼配置文件裏)定義了查詢電池容量的告警,這條命令是說,通過調用ax-check_snmp這個命令, -C 指定密碼, -I 要查詢的參數起個名字, -w 到達閾值發警告消息 -c 到達閾值發cirtcal消息 -o 要查詢的OID

define service {
   use                  ax-service
    hostgroup_name               MA5800X7
#    host_name               ol01.xxx.lon
   service_description  MA5800X7 Battery Capacity-1
   check_command        ax-check_snmp!-C public -l olt-BRC -w $_HOSTBRC_WARN_LEVEL$: -c $_HOSTBRC_CRIT_LEVEL$: -o 1.3.6.1.4.1.2011.6.2.1.6.1.1.2.2.0
}

現在電池容量出了問題,收到告警郵件了嗎?

轉到郵箱去看,果然收到了,郵件看起來是這個樣子的:



能夠看到主機名,IP地址,時間,那個參數出問題,告警級別等,簡單明瞭,不用打開郵件就知道是什麼告警。

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