检查主机是否处于维护模式下



我正在为我们的监视写一张支票,是否在领事(0.8.5(处于维护模式。在命令行上很简单,因为我可以运行consul maint并获得适当的输出。通过休息,我可以设置维护模式,但是似乎不可能检索它。

我如何以安全的方式以安全的方式检查它,而不解析领事的多行输出?

您可以通过http://localhost:8500/v1/health/node/name_of_node检查节点是否在维护中。如果节点处于维护模式,则输出将包含带有检查ID _node_maintenance的条目。

$ curl http://localhost:8500/v1/health/node/name_of_node
[
  {
    "ModifyIndex": 270813,
    "CreateIndex": 270813,
    "ServiceTags": [],
    "Node": "name_of_node",
    "CheckID": "_node_maintenance",
    "Name": "Node Maintenance Mode",
    "Status": "critical",
    "Notes": "Maintenance mode is enabled for this node, but no reason was provided. This is a default message.",
    "Output": "",
    "ServiceID": "",
    "ServiceName": ""
  }
]

以下是一些选项以获取维护中的服务器列表:

使用卷曲和JQ:

curl http://localhost:8500/v1/health/service/<serviceName>?dc=<dcKey> | jq '.[].Checks[] | select(.CheckID == "_node_maintenance")'

使用Groovy(服务器名称的返回列表(:

List<String> getServersInMaintenance(String serviceName, String dc) {
    List<String> serversList = []
    def uri = "/v1/health/service/${serviceName}"
    //* this is a RestClient that takes uri, query and content type
    def response = this.get(uri,
            [dc: dc, passing: false],
            ContentType.JSON
    )
    List allChecks = response.data.Checks
    for (server in allChecks) {
        for(check in server) {
            if (check.Name.contains("Maint") && check.Status.equals("critical")) {
                serversList += "${check.Node}"
            }
        }
    }
    return serversList
}

最新更新