我安装了 Icinga 1.10,并使用 NagiosQL 对其进行配置。我在调整警报和通知方面遇到了一些困难。
Icinga 具有隐含继承。假设我们有一个名为 prodweb01 的生产 Web 服务器。它上面运行着两个服务检查。一个用于检查 Apache 是否正常工作,另一个用于检查 JVM 是否正常工作。这两个检查均未设置警报设置,因此它从主机继承了这些设置。主机继承了两个主机模板 - Generic-Linux-Host(具有系统管理员的联系人)和 Generic-Web-Host(具有开发人员的联系人)。有没有办法编写逻辑,以便如果 Apache 发生故障,系统管理员会收到警报,如果 JVM 发生故障,开发人员会收到警报?
这真的是最好的做事方式吗?这种方法对我们来说会很乏味,因为我们有按环境和角色分隔的主机。因此,我们将不得不为每个服务器角色(例如 api、应用程序、Web 等)创建 prod/non-prod Generic-Linux-Host 模板和 prod/non-prod 模板。我们必须这样做,以便非 prod 通知不会发送到 SMS,而 prod 通知会发送到 SMS,因为我还没有看到一种方法来告诉 Icinga 通过 SMS 为 prod 发出警报,通过电子邮件为非 prod 发出警报。似乎唯一的方法是为同一个人创建两个不同的联系人,一个使用 SMS,一个使用电子邮件,然后根据其环境将其应用于模板或主机。
编辑:我意识到有人可能会指出,为什么我不直接在服务本身中定义警报/通知设置?是的,可以这样做,但这仍然没有回答如何将生产警报与非生产警报区分开的问题。在这种情况下,解决方案是创建同一服务检查的两个版本,并在一个版本中设置短信警报,在另一个版本中设置电子邮件警报。
答案1
这样就解决了我的问题。
Create two separate notification scripts in Icinga in commands.cfg
define command {
command_name Custom-Host-Notify
command_line /etc/icinga/Custom-Host-Notify "$HOSTNAME$" "$HOSTSTATE$" "$SHORTDATETIME$" "$HOSTOUTPUT$" "$HOSTADDRESS$" "$NOTIFICATIONTYPE$" "$HOSTDISPLAYNAME" "$NOTIFICATIONAUTHOR$" "$NOTIFICATIONCOMMENT$" "$HOSTNOTESURL$" "$CONTACTPAGER$" "$CONTACTEMAIL$"
register 1
}
define command {
command_name Custom-Service-Notify
command_line /etc/icinga/Custom-Service-Notify "$HOSTNAME$" "$HOSTALIAS$" "$SERVICEDESC$" "$SERVICESTATE$" "$SHORTDATETIME$" "$SERVICEOUTPUT$" "$HOSTADDRESS$" "$NOTIFICATIONTYPE$" "$SERVICEDISPLAYNAME$" "$NOTIFICATIONAUTHOR$" "$NOTIFICATIONNAME$" "$SERVICENOTESURL$" "$CONTACTPAGER$" "$CONTACTEMAIL$"
register 1
}
定义 contacts.cfg 如下
define contact {
contact_name John Doe
alias Senior Systems Engineer
contactgroups SystemAdministrators
host_notifications_enabled 1
service_notifications_enabled 1
host_notification_period 24x7
service_notification_period 24x7
host_notification_options d,u,r,f,s
service_notification_options w,u,c,r,f,s
host_notification_commands Custom-Host-Notify
service_notification_commands Custom-Service-Notify
email [email protected]
##This is how we send SMS to verizon phones using email.
pager [email protected]
register 1
}
在 icinga.cfg 中,启用此功能 -
enable_environment_macros=1
在 /etc/icinga/ 中,创建两个被调用的脚本 -
自定义主机通知
#!/bin/bash
##Icinga passes variables in this format - Custom-Host-Notify "$HOSTNAME$" "$HOSTSTATE$" "$SHORTDATETIME$" "$HOSTOUTPUT$" "$HOSTADDRESS$" "$NOTIFICATIONTYPE$" "$HOSTDISPLAYNAME" "$NOTIFICATIONAUTHOR$" "$NOTIFICATIONCOMMENT$" $HOSTNOTESURL$" "$CONTACTPAGER$" "$CONTACTEMAIL$"
##The quotes are essential since some of the variables have spaces and we need to capture the variable as a single variable. Bash sees space separated words as separate variables.
#Define variables
HOSTNAME="$1"
HOSTSTATE="$2"
SHORTDATETIME="$3"
HOSTOUTPUT="$4"
HOSTADDRESS="$5"
NOTIFICATIONTYPE="$6"
HOSTDISPLAYNAME="$7"
NOTIFICATIONAUTHORS="$8"
NOTIFICATIONCOMMENT="$9"
HOSTNOTESURL="${10}"
CONTACTPAGER="${11}"
CONTACTEMAIL="${12}"
ENV=`echo $1 | awk '{print substr($0,8,1)}'`
##Evaluate if the environment is Production.Please note, your logic may differ here. This is based on our naming convention.
if [ "$ENV" == "p" ];then
##If Production, then send SMS
/usr/bin/printf "%b" "$NOTIFICATIONTYPE - $HOSTNAME is $HOSTSTATE $SHORTDATETIME - Info:$HOSTOUTPUT ($HOSTADDRESS$)" | /bin/mail $CONTACTPAGER$
else
/usr/bin/printf "%b" "$SHORTDATETIME - Info:\n\n$HOSTOUTPUT\n\n$NOTIFICATIONTYPE - Host $HOSTNAME is $HOSTSTATE\n\n(Address: $HOSTADDRESS Displayname: $HOSTDISPLAYNAME)\n\nNotes: $NOTIFICATIONAUTHOR $NOTIFICATIONCOMMENT\n$HOSTNOTESURL\n\nUrl:\nhttps://icinga-tech.apartmentsext.com/icinga/cgi-bin/extinfo.cgi?type=1&host=$HOSTNAME\n\n" | /bin/mail -s "$NOTIFICATIONTYPE - $HOSTNAME is $HOSTSTATE" $CONTACTEMAIL
fi
定制服务通知
#!bin/bash
##Icinga passes variables in this format - Custom-Service-Notify "$HOSTNAME$" "$HOSTALIAS$" "$SERVICEDESC$" "$SERVICESTATE$" "$SHORTDATETIME$" "$SERVICEOUTPUT$" "$HOSTADDRESS$" "$NOTIFICATIONTYPE$" "$SERVICEDISPLAYNAME" "$NOTIFICATIONAUTHOR$" "$NOTIFICATIONNAME$" $SERVICENOTESURL$" "$CONTACTPAGER$" "$CONTACTEMAIL$"
##The quotes are essential since some of the variables have spaces and we need to capture the variable as a single variable. Bash sees space separated words as separate variables.
#Define variables
HOSTNAME="$1"
HOSTALIAS="$2"
SERVICEDESC="$3"
SERVICESTATE="$4"
SHORTDATETIME="$5"
SERVICEOUTPUT="$6"
HOSTADDRESS="$7"
NOTIFICATIONTYPE="$8"
SERVICEDISPLAYNAME="$9"
NOTIFICATIONAUTHORS="${10}"
NOTIFICATIONNAME="${11}"
SERVICENOTESURL="${12}"
CONTACTPAGER="${13}"
CONTACTEMAIL="${14}"
ENV=`echo $1 | awk '{print substr($0,8,1)}'`
##Evaluate if the environment is Production.Please note, your logic may differ here. This is based on our naming convention.
if [ "$ENV" == "p" ];then
##If Production, then send SMS
echo "$NOTIFICATIONTYPE - $HOSTALIAS/$SERVICEDESC is $SERVICESTATE on $HOSTADDRESS at $SHORTDATETIME ($SERVICEOUTPUT)" | /bin/mail $CONTACTPAGER
else
##If not production, send email
/usr/bin/printf "%b" "$SHORTDATETIME - Info:\n\n$SERVICEOUTPUT\n\n$NOTIFICATIONTYPE - Service $SERVICEDESC on Host $HOSTALIAS is $SERVICESTATE\n\n(Address: $HOSTADDRESS Displayname: $SERVICEDISPLAYNAME)\n\nNotes: $NOTIFICATIONAUTHOR $NOTIFICATIONCOMMENT\n$SERVICENOTESURL\n\nUrl:\nhttps://icinga-tech.apartmentsext.com/icinga/cgi-bin/status.cgi?host=$HOSTNAME\n\n" | /bin/mail -s "$NOTIFICATIONTYPE - $HOSTALIAS/$SERVICEDESC is $SERVICESTATE" $CONTACTEMAIL
fi
您可以随时根据自己的喜好自定义消息和变量。
重新启动 Icinga 就可以了。
编辑:我决定放弃隐含继承,只在服务模板中设置通知。您可以在服务本身中设置通知。