解决方案是这样的:
使用 wslite 作为 web 服务客户端。这是最小的例子。
现在我连接到启用了 api 的服务器。证书是自签名的,为什么需要“sslTrustAllCerts”。
我从我的主机“testserver”中选择所有服务并设置停机时间(持续时间以秒为单位)。
@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)
}
这对我有用!