0

如果过去 X 分钟内 CPU 的使用率超过 N%,我想设置观察者发送邮件。

首先elasticsearch每分钟通过metricbeat从远程服务器获取数据。然后我想通过使用该数据通知管理员远程服务器上的高 CPU 使用率。

如果内存使用率很高,我会设置邮件并完成部分,但问题在于 CPU 使用率,是 4 核处理器。我不写 aggs 函数和条件。我尝试使用来自github的代码,但我无法更改函数以使用 metricbeat。

4

1 回答 1

2

这对我有用。每当主机在一分钟内通知 5 次命中 (> 95% CPU) 时,它都会发送邮件:

{
  "trigger": {
    "schedule": {
      "interval": "1m"
    }
  },
  "input": {
    "search": {
      "request": {
        "search_type": "query_then_fetch",
        "indices": [
          "metricbeat-*"
        ],
        "types": [],
        "body": {
          "query": {
            "bool": {
              "filter": [
                {
                  "range": {
                    "@timestamp": {
                      "gte": "now-{{ctx.metadata.window_period}}"
                    }
                  }
                },
                {
                  "range": {
                    "system.process.cpu.total.pct": {
                      "gte": "{{ctx.metadata.threshold}}"
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  },
  "condition": {
    "compare": {
      "ctx.payload.hits.total": {
        "gte": 5
      }
    }
  },
  "actions": {
    "email_me": {
      "throttle_period_in_millis": 300000,
      "email": {
        "profile": "standard",
        "attachments": {
          "datalles.json": {
            "data": {
              "format": "json"
            }
          }
        },
        "from": "xxxx@gmail.com",
        "to": [
          "yyyy@gmail.com"
        ],
        "subject": " CPU overhead",
        "body": {
          "html": "The following hosts are running over {{ctx.metadata.threshold}}% CPU: <br><br>{{#ctx.payload.hits.hits}} <b>{{_source.beat.hostname}}</b> ({{_source.system.process.cpu.total.pct}}%) <br> {{/ctx.payload.hits.hits}}"
        }
      }
    }
  },
  "metadata": {
    "window_period": "1m",
    "threshold": 0.95
  }
}
于 2018-03-09T13:35:36.983 回答