我想将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 }
我该如何做到这一点,使电子邮件通知发生的警告和关键,但分页关键只?
您应该能够通过定义不同的联系人来实现这一点 – 一个仅用于寻呼机通知,一个用于电子邮件通知 – 并分配不同的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 }
并使用联系人组而不是单个联系人。
我不知道这是不是最好的select,但我不记得Nagios能够只在一个特殊的标志页面。 然而,你可以做的是重复与他的名字的联系人,并将他作为短信(姓名短信)。 这将导致多余的联系人。 但是,如果您正在使用群组,则只需将联系人添加到群组即可。
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 }
要通过电子邮件发送WARNING提醒和通过SMS发送CRITICAL提醒,我还定义了2个联系人:一个用于电子邮件,一个用于SMS。 它工作正常,但以下是我试图用只有一个联系人完成这一点。
这个想法是重写(service|host)_notification_commands来检查$SERVICESTATE$macros,然后使用相应的方法。
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$macros以决定使用哪种方法。
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 }