Linux

Icinga 中的智能通知(聯繫誰)

  • February 18, 2014

我安裝了 Icinga 1.10,並且正在使用 NagioSQL 對其進行配置。我在調整警報和通知方面有點困難。

Icinga 隱含繼承。假設我們有一個名為 prodweb01 的生產 Web 伺服器。它有兩個正在執行的服務檢查。一個檢查 Apache 是否工作,另一個檢查 JVM 是否工作。這兩項檢查都沒有設置警報設置,因此它從主機繼承了這些設置。主機繼承了兩個主機模板 - Generic-Linux-Host(有系統管理員的聯繫方式)和 Generic-Web-Host(有開發人員的聯繫方式)。無論如何要編寫邏輯,以便如果 Apache 出現故障,系統管理員會收到警報,如果 JVM 出現故障,開發人員會收到警報?

這甚至是最好的做事方式嗎?這種方法對我們來說會變得乏味,因為我們的主機被環境和角色分開。因此,我們將不得不為每個伺服器角色(例如 api、app、web 等)創建 prod/non-prod Generic-Linux-Host 模板和 prod/non-prod 模板。我們將不得不這樣做,以便非產品通知不會發送到簡訊,而產品通知會發送,因為我還沒有看到一種方法可以告訴 Icinga 通過簡訊提醒產品和通過電子郵件提醒非產品. 這樣做的唯一方法似乎是為同一個人創建兩個不同的聯繫人,一個使用 SMS,一個使用電子郵件,然後根據其環境將其應用於模板或主機。

編輯:我意識到有人可能會指出我為什麼不在服務本身中定義警報/通知設置?是的,這是可以做到的,但這仍然沒有回答關於如何將產品警報與非產品警報區分開來的問題。在這種情況下,解決方案是創建相同服務檢查的兩個版本,並在一個版本中設置 SMS 警報,在另一個版本中設置電子郵件。

就這樣解決了我的問題。

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                           john@doe.com
       ##This is how we send SMS to verizon phones using email.
       pager                           5555555555@vtext.com
       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,您就可以開始了。

編輯:我決定放棄隱含繼承,只在服務模板中設置通知。您可以在服務本身中設置通知。

引用自:https://serverfault.com/questions/571370