首页 > 解决方案 > Sensu 处理程序未触发

问题描述

似乎我的新 Sensu 处理程序没有被调用。但首先,我的配置。在 /etc/sensu/conf.d/checks.json 中:

{
"checks":
   "custom_check":{
      "command": "python3.6 /srv/custom_check.py",
      "subscribers": ["remote-checks"],
      "interval": 600,
      "ttl": 900,
      "handlers": ["custom_handler"],
      "source":"my-check-target"
   }
}

在 /etc/sensu/conf.d/handlers.json 中:

{
  "handlers": {
    "custom_handler": {
      "type": "pipe",
      "command": "python3.6 /srv/custom-sensu-handlers/handler.py"
}

在服务器日志中,我看到:

 {
  "timestamp":"2018-04-25T07:51:47.253449+0200",
  "level":"info",
  "message":"publishing check request",
  "payload":{"command":"python3.6 /srv/custom_checks/custom_check.py",
  "ttl":900,
  "handlers":["custom_handler"],
  "source":"my-check-target",
  "name":"custom_check",
  "issued":1524635507},
  "subscribers":["remote-checks"]
}

客户端日志:

{
  "timestamp": "2018-04-30T06:24:00.012625+0200",
  "level": "info",
  "message": "received check request",
  "check": {
    "command": "python3.6 /srv/custom_checks/custom_check.py",
    "ttl": 900,
    "handlers": [
      "default",
      "custom_handler"
    ],
    "source": "my_check_target",
    "name": "custom_check",
    "issued": 1525062240
  }
}

{
  "timestamp": "2018-04-30T06:24:00.349912+0200",
  "level": "info",
  "message": "publishing check result",
  "payload": {
    "client": "assensu.internal.defaultoute.eu",
    "check": {
      "command": "python3.6 /srv/custom_checks/custom_check.py",
      "ttl": 900,
      "handlers": [
        "default",
        "custom_handler"
      ],
      "source": "my_check_target",
      "name": "custom_check",
      "issued": 1525062240,
      "subscribers": [
        "remote-checks"
      ],
      "interval": 600,
      "executed": 1525062240,
      "duration": 0.337,
      "output": "Check OK",
      "status": 0
    }
  }
}

然后,日志停止生成有关支票的任何信息。我找不到任何我做错的事情。我什至添加了一行代码,一旦它被调用,就写入处理程序中的日志文件,但什么也没有。
有什么线索吗?
(如果您想知道,我正在使用 python,因为我不熟悉 ruby​​ ......)

标签: pythonsensu

解决方案


处理程序只会在以下状态类型上执行:

  • 警告
  • 批判的
  • 未知

https://docs.sensu.io/sensu-core/1.4/reference/handlers/#handler-attributes

搜索“严重性”,了解如何在处理程序定义中自定义此属性。

一个事件要么是创建,要么是解决,要么是摆动。

https://docs.sensu.io/sensu-core/1.2/reference/events/#event-actions

您的客户端日志显示“状态:0”,这意味着检查已通过,因此没有创建任何事件,也没有理由为该事件执行处理程序。尝试将您的检查设置为有目的地失败:sys.exit(2),以便报告“状态:2”,并执行处理程序。

处理程序可以处理已解决的事件类型。例如,您可能希望通过 HipChat、Slack 或电子邮件收到事件已清除的通知。(即从“状态:2”变为“状态:0”)

我们如何查看检查事件状态的 Python 示例:

if data['check']['status'] > 2:
    level = "Unknown"
elif data['check']['status'] == 2:
    level = "Critical"
elif data['check']['status'] == 1:
    level = "Warning"
elif data['check']['status'] == 0:
    level = "Cleared"

我还将确保您的 /srv/custom-sensu-handlers/handler.py 递归地归 sensu:sensu 用户/组所有,因为它位于默认的 /etc/sensu/plugins 目录之外。

https://docs.sensu.io/sensu-core/1.4/reference/handlers/#how-and-where-are-pipe-handler-commands-executed


推荐阅读