Skip to main content
 首页 » 编程设计

python之我如何在 Nagios 中使用 Python 文件

2025年05月04日89telwanggs

我正在尝试使用以下 Python 代码和说明从 Nagios 获得响应: http://skipperkongen.dk/2011/12/06/hello-world-plugin-for-nagios-in-python/

出于某种原因,我从来没有从 Nagios 获得 OK,它总是返回消息:返回代码 126 超出范围 - 插件可能丢失

我安装了 nagiosplugin 1.0.0,但似乎仍然没有任何效果 同时,我还有一些其他服务(不是 python 文件)可以工作,例如http 检查、当前用户和 SSH

我做错了什么?几天来我一直在努力解决这个问题

请您参考如下方法:

让 Nagios 使用您的新插件非常容易。您应该更改三个文件并重新启动 Nagios — 仅此而已。

第一个文件是/etc/nagios/command-plugins.cfg(如果您知道此文件的路径或ubuntu 中的类似文件,请留下评论)。假设插件文件放在/usr/lib/nagios/plugins/目录下:

command[check_hello_world]=/usr/lib/nagios/plugins/check_helloworld.py -m 'some message' 

下拉一个目录到/etc/nagios/objects/commands.cfg(对于ubuntu用户应该在那个目录/etc/nagios-plugins/config/):

define command { 
    command_name    check_hello_world 
    command_line    $USER1$/check_hello_world.py -m 'some message' 
} 

保存文件并打开/etc/nagios/objects/localhost.cfg(在服务定义文件的ubuntu路径中,位于/etc/nagios3/nagios.cfg,默认情况下 cfg_dir=/etc/nagios3/conf.d。因此,要在 ubuntu 中定义新服务,用户应该在该目录中创建 cfg 文件,例如 hello.cfg)。找到这个部分:

# 
# SERVICE DEFINITIONS 
# 

并添加新条目:

define service { 
    use                 local-service ; Name of service template to use 
    host_name           localhost 
    service_description Check using the hello world plugin (always returns OK) 
    check_command       check_hello_world 
} 

剩下的就是重新启动 Nagios 并验证插件是否正常工作。通过发出以下命令重新启动 Nagios:

/etc/init.d/nagios restart 

http://www.linux-mag.com/id/7706/

ubuntuforums.org - Thread: My Notes for Installing Nagios on Ubuntu Server 12.04 LTS