【问题标题】:How to schedule a downtime in icinga2 by using icinga-api with groovy?如何通过将 icinga-api 与 groovy 一起使用来安排 icinga2 的停机时间?
【发布时间】:2015-11-27 13:15:34
【问题描述】:

我正在寻找一种使用 groovy 脚本在 icinga2 中安排停机时间的方法。

我已经尝试过创建一个小型的 groovy 脚本。尝试使用 icinga 文档中的示例:

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

但将其改编为我的脚本不起作用。我注意到每个属性名称周围的 " 非常重要。

【问题讨论】:

    标签: json api groovy downtime icinga


    【解决方案1】:

    解决办法是这样的:

    使用 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)
    }
    

    这对我有用!

    【讨论】:

    • 在 POST 请求正文中传递“filter”属性也是合理的,也可以使用“filter_vars”来指定例如按需提供主机名。在文档中的programmatic examples中可以找到一些示例,它们是使用带有X-HTTP-Method-Override的GET,但是在请求体内部传递过滤器的原理是相同的。
    猜你喜欢
    • 2019-06-19
    • 2012-12-09
    • 1970-01-01
    • 2020-11-18
    • 2011-01-21
    • 2020-07-16
    • 2023-04-02
    • 1970-01-01
    • 2015-04-19
    相关资源
    最近更新 更多