1

I'm searching for a way to schedule a downtime in icinga2 with a groovy script.

I already tried creating a small groovy script. Tried using the examples from icinga documentation:

curl -u root:icinga -k -s 'https://localhost:5665/v1/actions/schedule-downtime?type=Host&filter=host.vars.os==%22Linux%22' -d '{ "author" : "michi", "comment": "Maintenance.", "start_time": 1441136260, "end_time": 1441137260, "duration": 1000 }' -X POST | python -m json.tool

but adapting this to my script didn't work. Very important are the " around each attribute name, I noted.

Markus
  • 1,360
  • 1
  • 16
  • 22

1 Answers1

2

Solution was this way:

Using wslite as webservice client. This is the minimal example.

Now I connect to my server with api enabled. The certificate is self signed, why "sslTrustAllCerts" was needed.

I select all services from my host "testserver" and set the downtime (duration in seconds).

@Grab('com.github.groovy-wslite:groovy-wslite:1.1.2')
import wslite.rest.*
import wslite.http.auth.*

def client = new RESTClient("https://myicinga2server:5665/")
client.authorization = new HTTPBasicAuthorization("root", "secret")

def timeFrom = System.currentTimeMillis() / 1000L
def timeDurationSec = 600
def timeTo = timeFrom + timeDurationSec

try
{    
    def response = client.post(
        path: '/v1/actions/schedule-downtime?type=Service&filter=host.name==%22testserver%22',
        headers: ["Accept": "application/json"],
        sslTrustAllCerts: true) 
        {
            json "author":"mstein", "comment":"Test-Downtime", "start_time": timeFrom, "end_time": timeTo, "duration": timeDurationSec, "fixed": true
        }

        assert 200 == response.statusCode
        print response.text    
}
catch (Exception exc)
{
    println "Error: " + exc.getClass().toString()
    println "Message: " + exc.getMessage()
    println "Response: " + exc.getResponse()
    System.exit(1)
}

That worked for me!

Markus
  • 1,360
  • 1
  • 16
  • 22
  • 1
    It's also reasonable to pass the "filter" attribute inside the POST request body, and also use "filter_vars" to specify e.g. the host name on demand. Some examples can be found in the programmatic examples in the docs, which are using GET with X-HTTP-Method-Override, but the principle of passing filters inside the request body is the same. – dnsmichi Nov 27 '15 at 14:55