From 0927078685cfeb38225e0fa3592cae95feddf022 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Thu, 9 May 2019 17:48:19 +0200 Subject: [PATCH] Docs: Use jo to format JSON body for API actions, esp. unix timestamps (cherry picked from commit 21225c6b48a084c3d96ee1e7c938d30e19a2e395) --- doc/12-icinga2-api.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/doc/12-icinga2-api.md b/doc/12-icinga2-api.md index a5e010a72..79c076353 100644 --- a/doc/12-icinga2-api.md +++ b/doc/12-icinga2-api.md @@ -307,6 +307,10 @@ Whenever filters and other URL parameters don't work due to encoding issues, consider passing them in the request body. For GET requests, this method is explained [here](12-icinga2-api.md#icinga2-api-requests-method-override). +You can use [jo](https://github.com/jpmens/jo) to format JSON strings on the shell. An example +for API actions shown [here](#icinga2-api-actions-unix-timestamps). + + ### Global Parameters Name | Description @@ -1025,6 +1029,35 @@ $ curl -k -s -u root:icinga -H 'Accept: application/json' \ -d '{ "attrs": { "enable_notifications": false } }' ``` +### Unix Timestamp Handling + +If you don't want to write JSON manually, especially for adding the `start_time` +and `end_time` parameters, you can use [jo](https://github.com/jpmens/jo) to format this. + +``` +$ jo -p pretty=true type=Service filter="service.name==\"ping4\"" author=icingaadmin comment="IPv4 network maintenance" fixed=true start_time=$(date +%s -d "+0 hour") end_time=$(date +%s -d "+1 hour") +{ + "pretty": true, + "type": "Service", + "filter": "service.name==\"ping4\"", + "author": "icingaadmin", + "comment": "IPv4 network maintenance", + "fixed": true, + "start_time": 1557414097, + "end_time": 1557417697 +} +``` + +Now wrap this into the actual curl command: + +``` +$ curl -k -s -u root:icinga -H 'Accept: application/json' \ + -X POST 'https://localhost:5665/v1/actions/schedule-downtime' \ + -d "$(jo -p pretty=true type=Service filter="service.name==\"ping4\"" author=icingaadmin comment="IPv4 network maintanence" fixed=true start_time=$(date +%s -d "+0 hour") end_time=$(date +%s -d "+1 hour"))" +``` + +Note: This requires GNU date. On macOS, install `coreutils` from Homebrew and use `gdate`. + ### process-check-result Process a check result for a host or a service. -- 2.40.0