【问题标题】:Check if host is in Consul in maintenance mode or not检查主机是否在维护模式下的 Consul
【发布时间】:2018-02-05 14:32:42
【问题描述】:

如果给定主机在 Consul (0.8.5) 中是否处于维护模式,我正在为我们的监控编写检查。在命令行上非常简单,因为我可以运行consul maint 并获得适当的输出。通过 REST 我可以设置维护模式,但似乎无法检索它。

如何在不解析 Consul 的多行输出的情况下以安全的方式在 shell 脚本中检查这一点?

【问题讨论】:

    标签: shell consul


    【解决方案1】:

    您可以通过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": ""
      }
    ]
    

    【讨论】:

      【解决方案2】:

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

      使用 curl 和 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
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-17
        • 2014-03-09
        • 1970-01-01
        • 1970-01-01
        • 2019-03-27
        • 2015-02-18
        • 2012-09-10
        相关资源
        最近更新 更多