Change Contacts for Notifications with API

Hi there,

I’m trying to change or add an contact to an host for notification but i’m not able to do so.
Maybe my API call is wrong or it isn’t possible at all?

Here i already changed those Parameters via API successfully


via

curl -k -s -S -i -u user:password -H 'Accept: application/json' \
 -X POST 'https://localhost:5665/v1/objects/hosts' \
 -d '{ "attrs": { "vars.ping_wrta" : "200", "vars.ping_wpl" : "10", "vars.ping_crta" : "300", "vars.ping_cpl" : "20" }, "pretty": true }'

curl -k -s -S -i -u user:password -H 'Accept: application/json' \
 -X POST 'https://localhost:5665/v1/objects/hosts' \
 -d '{ "templates": ["generic-host"], "attrs": { "vars.ping_wrta" : "200", "vars.ping_wpl" : "10", "vars.ping_crta" : "300", "vars.ping_cpl" : "20" }, "pretty": true }'

But now trying to add here additional OpsGenie


via

curl -k -s -S -i -u user:password -H 'Accept: application/json' \
 -X POST 'https://localhost:5665/v1/objects/hosts/hostname' \
 -d '{ "attrs": {
		"contacts" : ["datacenter", "opsgenie", "sound" ],
		"contact_groups": [ "datacenter" ]
	},
	"pretty": true }'



curl -k -s -S -i -u user:password -H 'Accept: application/json' \
 -X POST 'https://localhost:5665/v1/objects/hosts/hostname' \
 -d '{ "templates": ["generic-host"], "attrs": { "vars.notifications_opsgenie": "true" }, "pretty": true }'

curl -k -s -S -i -u user:password -H 'Accept: application/json' \
 -X POST 'https://localhost:5665/v1/objects/hosts/hostname' \
 -d '{ "attrs": { "vars.notifications_opsgenie" : "true" }, "pretty": true }'

Here are the Variables used and we’re using the three Notification Variables to manage the same notifications on the services of the host.

Regards
dcz01

I guess, you try to manipulate the wrong object.

Object Types - Host
vs
Object Types - Notification

I believe the true value ought to be specified without the surrounding quotes, if notifications_opsgenie is defined as a boolean variable.

Could you share the full output of the failing curl command, even if the above solves the issue? It will help future searches for the same issue :wink:

I tried it without the quotes but then i’ll get the same like with:

HTTP/1.1 200 OK
Server: Icinga/r2.14.3-1
Content-Type: application/json
Content-Length: 188

{
    "results": [
        {
            "code": 200,
            "name": "hostname",
            "status": "Attributes updated.",
            "type": "Host"
        }
    ]
}

and it doesn’t solve the problem :frowning:

But here are the applys for the Notifications (if they help):

apply Notification "notify-opsgenie-host" to Host {
  command = "opsgenie-host-notification"
  users = ["opsgenie"]
  states = [ Down, Up ]
  types = [ Problem, Recovery, Acknowledgement ]

  # assign rules
  assign where host
  assign where host.vars.notifications_opsgenie == "true"
  ignore where host.vars.notifications_opsgenie == "false"

apply Notification "notify-opsgenie-service" to Service {
  command = "opsgenie-service-notification"
  users = ["opsgenie"]
  states = [ Critical, OK ]
  types = [ Problem, Recovery, Acknowledgement ]

  # assign rules
  assign where service.vars.notifications_opsgenie == "true" && service.enable_notifications == true
  ignore where host.vars.notifications_opsgenie == "false"

So I understand the API call is working :slight_smile:

This should read where service.vars.notifications_opsgenie && service.enable_notifications

If using Director, use the operator is true (or set) instead of the operator ==.

Thanks for that clearification.
But we’re not using the Director and only managing all files directly via DSL.
We create new hosts via Ansible over the API and then we’ll using these variables exactly like

notifications_opsgenie == "true"

Thanks for posting. While your API query looks valid, the issue is the missing evaluation of the apply rule when modifying objects through the API. The documentation states:

Modified attributes do not trigger a re-evaluation of existing static apply rules and group assignments. Delete and re-create the objects if you require such changes or consider funding this feature request.

Thus, please consider re-creating the object.

1 Like

Re-evaluation would be such a force multiplier.
I was thinking about automatically detecting stuff like network dependencies and discussing with @moreamazingnick about it and just got reminded by reading about it in your link. Proposal to add the feature of gathering facts from an agent [auto discovery] · Issue #9204 · Icinga/icinga2 · GitHub

1 Like