【问题标题】:How do I use Python file in Nagios?如何在 Nagios 中使用 Python 文件?
【发布时间】:2013-04-24 09:27:09
【问题描述】:

我正在尝试使用以下 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

我做错了什么?我已经尝试解决几天了

【问题讨论】:

    标签: python plugins nagios


    【解决方案1】:

    让 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/中创建cfg文件强>):

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

    保存文件并打开 /etc/nagios/objects/localhost.cfg(在 /etc/nagios3/nagios.cfg 中的服务定义文件的 ubuntu 路径中,默认情况下 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

    【讨论】:

    • 感谢您的快速回复,我的问题尚未解决,因为我在尝试创建时没有文件 command-plugins.cfg(我正在运行 Nagios 3.2.3-3ubuntu1)将此文件添加到 nagios.cfg 中会引发错误。如果有的话,在您的 nagios.cfg 中添加了 command-plugins.cfg 的哪个部分?
    • @user2314782 你安装了 nagios-plugins 包了吗?
    • 是的,我已经安装了插件并使用了其中的一些。所以如上所述,当我重新加载 nagios 时,我创建了文件 command-plugins.cfg 并将其添加到 cfg_file=/etc/nagios3/command-plugins.cfg 下的 nagios.cfg(否则它不会知道它在那里)此错误:文件 commands-plugins.cfg 中出现意外的标记或语句。当我删除指向 nagios.cfg 中 commands-plugins.cfg 的行并尝试重新加载时 - 它重新加载正常。我还使我的 python 脚本文件可执行,但它仍然不起作用:(
    • @user2314782 我认为您的问题是特定于 ubuntu 的......也许您已经拥有 command-plugins.cfg 但名称不同,并且可能在其他目录中。尝试在 ubuntuforums.org 上寻找答案(如果找到答案,请在此处评论)
    • 经过几天的搜索,我找到了导致问题的原因:我在 Windows 机器上创建了 Python 文件并将其复制到 ubuntu 中。显然,windows 给 Unix 加入了不同的行尾,这就是问题所在!从不同平台移动文件时要小心
    【解决方案2】:

    即使文件中的 shebang 指定了它,我也必须预先添加 python2.7 的路径。

    在命令定义中我有这个:

    command_line /usr/local/bin/python2.7 $USER1$/check_rabbit_queues.py --host $HOSTADDRESS$ --password $ARG1$
    

    即使实际 python 文件的顶部有:

    #!/usr/bin/env python2.7
    

    即使脚本在没有指定解释器的情况下从命令行执行并返回正常。

    我尝试过的其他方法似乎都不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2023-03-11
      • 1970-01-01
      相关资源
      最近更新 更多