Nagios 通过电子邮件发出“警告”警报,并通过电子邮件向“严重”发出寻呼机警报

Nagios 通过电子邮件发出“警告”警报,并通过电子邮件向“严重”发出寻呼机警报

我想将 Nagios 设置为通过电子邮件发送警告(例如,轻度高负载或磁盘使用率),但对于关键项目则通过寻呼机发出警报。

目前,我们通过电子邮件和传呼机同时通知所有警告。我的通用联系人定义如下:

define contact{
    name                            generic-contact
    service_notification_options    w,u,c,r,f,s
    host_notification_options       d,u,r,f,s
    service_notification_commands   notify-service-by-email,notify-service-by-pager
    host_notification_commands      notify-host-by-email,notify-host-by-pager
    register                        0

    service_notification_period     24x7
    host_notification_period        24x7
}

我怎样才能使电子邮件通知同时针对警告和严重情况发生,但仅针对严重情况进行分页?

答案1

您应该能够通过定义不同的联系人(一个仅用于寻呼机通知,一个仅用于电子邮件通知)并分配不同的值来实现此目的host/service_notification_options

define contact{
    name                            email-contact
    service_notification_options    w,u,c,r,f,s
    host_notification_options       d,u,r,f,s
    service_notification_commands   notify-service-by-email
    host_notification_commands      notify-host-by-email
    register                        0

    service_notification_period     24x7
    host_notification_period        24x7
}

define contact{
    name                            pager-contact
    service_notification_options    c,r
    host_notification_options       d,u,r
    service_notification_commands   notify-service-by-pager
    host_notification_commands      notify-host-by-pager
    register                        0

    service_notification_period     24x7
    host_notification_period        24x7
}

如果你想保持主机/服务定义开销较低,你应该将它们聚合在一个联系组像这样:

define contactgroup{

    contactgroup_name       pager-email
    members         pager-contact,email-contact
}

并使用联系人组而不是单个联系人。

答案2

为了通过电子邮件发送“警告”警报并通过短信发送“严重”警报,我还定义了 2 个联系人:一个用于发送电子邮件,一个用于发送短信。它工作正常,但下面是我尝试仅使用一个联系人来实现此目的的方法。

这个想法是重写来(service|host)_notification_commands检查$SERVICESTATE$宏然后使用相应的方法。

command.cfg

define command{
    command_name    notify-service
    command_line    $USER1$/notify.sh $SERVICESTATE$ $LASTSERVICESTATE$ $NOTIFICATIONTYPE$ $SERVICEDESC$ $HOSTALIAS$ $HOSTADDRESS$ "$LONGDATETIME$" "$SERVICEOUTPUT$" "$SERVICENOTESURL$" $CONTACTEMAIL$ $CONTACTPAGER$ $TIME$
    }

notify.sh

#!/bin/bash

SERVICESTATE=$1
LASTSERVICESTATE=$2
NOTIFICATIONTYPE=$3
SERVICEDESC=$4
HOSTALIAS=$5
HOSTADDRESS=$6
LONGDATETIME=$7
SERVICEOUTPUT=$8
SERVICENOTESURL=$9
CONTACTEMAIL=${10}
CONTACTPAGER=${11}
TIME=${12}

send_email() {
    /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: $SERVICEOUTPUT\n\nURL: $SERVICENOTESURL" | /bin/mail -s "** $NOTIFICATIONTYPE Service Alert: $HOSTALIAS/$SERVICEDESC is $SERVICESTATE **" $CONTACTEMAIL
}

send_sms() {
    /usr/bin/wget --user=notifier --password=x "http://ip:port/smsgate/sms?tos=$CONTACTPAGER&content=$NOTIFICATIONTYPE, $SERVICEDESC, $HOSTADDRESS, $SERVICESTATE, $TIME, $SERVICEOUTPUT"

}

if [ $NOTIFICATIONTYPE = "PROBLEM" ]; then
    if [ $SERVICESTATE = "WARNING" ]; then
        send_email
    elif [ $SERVICESTATE = "CRITICAL" ]; then
        send_email
        send_sms
    fi
elif [ $NOTIFICATIONTYPE = "RECOVERY" ]; then
    if [ $LASTSERVICESTATE = "WARNING" ]; then
        send_email
    elif [ $LASTSERVICESTATE = "CRITICAL" ]; then
        send_email
        send_sms
    fi
fi

请注意,当服务正常时,我需要检查$LASTSERVICESTATE$宏来决定使用哪种方法。

contacts.cfg

define contact{
        contact_name                    quanta
        use                             single-contact
        alias                           Quan Tong Anh
        service_notifications_enabled   1
        host_notifications_enabled      1
        service_notification_period     24x7
        host_notification_period        24x7
        service_notification_options    c,w,r
        host_notification_options       d,u,r
        email                           [email protected]
        pager                           0912345678
        }

templates.cfg

define contact{
        name                            single-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   notify-service
        host_notification_commands      notify-host
        register                        0
        }

答案3

我不知道这是否是最好的选择,但我不记得 Nagios 能够只根据特殊标志进行寻呼。但是,您可以做的是复制联系人及其姓名,并将他标识为短信 (name-sms)。这将导致联系人冗余。但是,如果您使用组,则可以将联系人添加到组中。

define contact{
    name                            generic-contact-sms
    service_notification_options    c
    host_notification_options       d,u,r,f,s
    service_notification_commands   notify-service-by-pager
    host_notification_commands      notify-host-by-pager
    register                        0

    service_notification_period     24x7
    host_notification_period        24x7
}

相关内容