我想与您分享一种通过api从icinga2发送通知以进行缓存的方法。
Icinga2版本:2.4.10-1
缓存版本:2.3.9
首先,您必须知道要使用的组件ID(在我的情况下,因为您可以按名称更新组件)
要获取组件ID,可以使用curl命令:
curl --insecure --request GET --url https://URL/api/v1/components -H "X-Cachet-Token: TOKEN"
URL :您的显眼安装的URL
令牌:Cachet中成员的令牌
在/etc/icinga2/conf.d/commands.conf
中创建命令object NotificationCommand "cachet-incident-notification-v2" {
import "plugin-notification-command"
command = [ PluginDir + "/cachet-notification-v2.sh" ]
env = {
"SERVICESTATE" = "$service.state$"
}
}
在/etc/icinga2/conf.d/templates.conf中创建通知模板
template Notification "cachet-incident-notification-v2" {
command = "cachet-incident-notification-v2"
states = [ OK, Warning, Critical, Unknown ]
types = [ Problem, Acknowledgement, Recovery, Custom,
FlappingStart, FlappingEnd,
DowntimeStart, DowntimeEnd, DowntimeRemoved ]
/*
period = "24x7"
*/
interval = 0
}
在/etc/icinga2/conf.d/notifications.conf中创建通知
apply Notification "cachet-incident-notification-v2" to Service {
import "cachet-incident-notification-v2"
user_groups = host.vars.notification.pager.groups
assign where service.vars.cachetv2 == "1" && host.vars.cachetv2 == "1"
interval = 0 # Disable Re-notification
}
在/etc/icinga2/conf.d/service/your/service.conf中的检查服务中添加变量
[...]
vars.cachetv2 = "1"
[...]
在/etc/icinga2/conf.d/hosts/您的主机中的主机配置文件中添加变量
[...]
vars.cachetv2 = "1"
[...]
在/usr/lib/nagios/plugins/cachet-notification-v2.sh中创建脚本
#!/bin/bash
# Some Constants
NOW="$(date +'%d/%m/%Y')"
CACHETAPI_URL="https://URL/api/v1/components/<ID DU COMPOSANT>"
CACHETAPI_TOKEN="TOKEN><"
# Map Notification states for icinga2
# OK - 1 operational
# Warning - 3 Partial outage
# Critical - 4 Major outage
# Unknown - 2 Performance issues
case "$SERVICESTATE" in
'OK')
COMPONENT_STATUS=1
;;
'WARNING')
COMPONENT_STATUS=3
;;
'CRITICAL')
COMPONENT_STATUS=4
;;
'UNKNOWN')
COMPONENT_STATUS=2
;;
esac
curl -X PUT -H "Content-Type: application/json;" -H "X-Cachet-Token: ${CACHETAPI_TOKEN}" -d '{"status": "'"${COMPONENT_STATUS}"'"}' ${CACHETAPI_URL} -k
PS :授予脚本执行权限
检查语法并重新加载
/etc/init.d/icinga2 checkconfig && /etc/init.d/icinga2 reload
结果:
当您的检查结果为“ 关键”时,Cachet中的状态将为“重大问题”
当您的检查结果为“ 警告”时,Cachet中的状态将为“部分问题”
当您的检查结果为“ 确定”时,Cachet中的状态将为OPERATIONAL
当检查结果为“ 未知”时,Cachet中的状态将为PERFORMANCE DELAY
我希望它会有所帮助。
尼古拉斯·B。