简体   繁体   English

如何使用Sensu监视Python脚本?

[英]How to monitor Python scripts using Sensu?

I would like to use Sensu Core to monitor python scripts and I am confused how to do it. 我想使用Sensu Core监视python脚本,但是我很困惑该怎么做。

From Sensu documentation this requires Sensu Checks . 从Sensu文档中,这需要Sensu Checks In the provided example ruby script checks that chef-client is running: 在提供的示例中,ruby脚本检查Chef-client是否正在运行:

#!/usr/bin/env ruby

# get the current list of processes
processes = `ps aux`

# determine if the chef-client process is running
running = processes.lines.detect do |process|
  process.include?('chef-client')
end

# return appropriate check output and exit status code
if running
  puts 'OK - Chef client process is running'
  exit 0
else
  puts 'WARNING - Chef client process is NOT running'
  exit 1
end

How to implement such a check for a specific script and not the application? 如何对特定脚本而不是应用程序实施这种检查? That is, how would I go about monitoring a specific python script (eg test.py) and not python in general? 也就是说,我将如何监视特定的python脚本(例如test.py),而不是一般的python?

So, I have been running some python scripts in sensu for my AWS Linux clients successfully , this is a good example of my check definition: 因此,我已经在sensu中为我的AWS Linux客户端成功运行了一些python脚本,这是我的检查定义的一个很好的例子:

{
 "checks": {
"check-rds-limit": {
  "interval": 86400, 
  "command": "/etc/sensu/plugins/rds-limit-check.py",
  "contacts": [
    "cloud-ops"
  ],
  "occurrences": 1,   
  "subscribers": [
    "cloud-ops-subscription"
  ], 
  "handlers": [
    "email",
    "slack"
  ]
}
  }
}

And your python plugin can start with defining the shebang path: 并且您的python插件可以从定义shebang路径开始:

#!/usr/bin/env python
import sys
...
...
//<code condition for warning>
sys.exit(1)
//<code condition for critical>
sys.exit(2)
//<code condition where everything is fine>
sys.exit(0)

More generally the above script is searching for the string chef-client in the running processes. 通常,以上脚本在运行的进程中搜索字符串chef-client You could replace that with any other string, like test.py , which would detect if any program running has test.py in its name. 您可以将其替换为任何其他字符串,例如test.py ,它将检测正在运行的任何程序的名称是否为test.py (You might need to match the sub-string test.py if you run the program with python test.py , I don't know ruby.) (您可能需要相匹配的子串test.py如果你运行的程序python test.py ,我不知道红宝石)。

I would suggest that you use Sensu process check plugin for a more general function which includes more customization. 我建议您将Sensu进程检查插件用于更通用的功能,其中包括更多的自定义功能。 Look at the other sensu plugins too. 看看其他的sensu插件

Why not monitor the expected result or operation of the script rather than the process itself. 为什么不监视脚本的预期结果或操作,而不是过程本身。 Typically, we will setup checks to monitor an end point, in the case of a web application, or an observed behavior, such as messages in a database, to determine if the application is running. 通常,我们将设置检查以监视Web应用程序的端点或观察到的行为(例如数据库中的消息),以确定应用程序是否正在运行。

There will be times when the process is technically running but not anything due to an error condition or resource issue. 有时,该流程在技​​术上正在运行,但由于错误状况或资源问题而无法执行任何操作。 Monitoring the expected result is a much better option than watching a process. 监视预期结果是比监视过程更好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM