Notifications icinga2缓存通知

Notifications icinga2缓存通知,notifications,icinga2,Notifications,Icinga2,我想与大家分享一种通过API将通知从icinga2发送到cachet的方法 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 令牌:缓存中成员的令牌

我想与大家分享一种通过API将通知从icinga2发送到cachet的方法

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
令牌:缓存中成员的令牌

在/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$"
    }
}
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
}
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
}
[...]    
vars.cachetv2 = "1"
[...]
在/etc/icinga2/conf.d/templates.conf中创建通知模板

object NotificationCommand "cachet-incident-notification-v2" {
    import "plugin-notification-command"

    command = [ PluginDir + "/cachet-notification-v2.sh" ]

    env = {
      "SERVICESTATE" = "$service.state$"
    }
}
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
}
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
}
[...]    
vars.cachetv2 = "1"
[...]
在/etc/icinga2/conf.d/notifications.conf中创建通知

object NotificationCommand "cachet-incident-notification-v2" {
    import "plugin-notification-command"

    command = [ PluginDir + "/cachet-notification-v2.sh" ]

    env = {
      "SERVICESTATE" = "$service.state$"
    }
}
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
}
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
}
[...]    
vars.cachetv2 = "1"
[...]
在/etc/icinga2/conf.d/service/your/service.conf中的check服务中添加变量

object NotificationCommand "cachet-incident-notification-v2" {
    import "plugin-notification-command"

    command = [ PluginDir + "/cachet-notification-v2.sh" ]

    env = {
      "SERVICESTATE" = "$service.state$"
    }
}
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
}
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
}
[...]    
vars.cachetv2 = "1"
[...]
在主机配置文件/etc/icinga2/conf.d/hosts/your/host中添加变量

[...]    
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
结果是: 当您的检查结果为“严重””时,缓存中的状态将成为主要问题
当您的检查结果为“警告””时,纪念印中的状态将为“部分发行”
当您的检查结果为“OK”时,缓存中的状态将处于可操作状态
当您的检查结果为“未知””时,缓存中的状态将为性能延迟

我希望这会有帮助


尼古拉斯B.

很好。但是StackOverflow是发布这个的最好地方吗?