【问题标题】:nagios core external agent using python scripting使用 python 脚本编写的 nagios 核心外部代理
【发布时间】:2019-02-13 20:23:01
【问题描述】:

我有一个用于执行被动检查(即外部代理/应用程序)的 bash 脚本。我尝试将 bash 脚本转换为 python,但是当我执行文件时,在我的 nagios 核心界面上没有看到任何关于我的被动检查结果的响应。

import os
import datetime

CommandFile='/usr/local/nagios/var/rw/nagios.cmd'
datetime = datetime.datetime.now()
os.stat(CommandFile)
f = open(CommandFile, 'w')
f.write("/bin/echo " + str(datetime) + " PROCESS_SERVICE_CHECK_RESULT;compute-1;python dummy;0;I am dummy python")
f.close()

我的 bash 脚本代码是:

#!/bin/sh

# Write a command to the Nagios command file to cause
# it to process a service check result

echocmd="/bin/echo"

CommandFile="/usr/local/nagios/var/rw/nagios.cmd"

# get the current date/time in seconds since UNIX epoch
datetime=`date +%s`

# create the command line to add to the command file
cmdline="[$datetime] PROCESS_SERVICE_CHECK_RESULT;host-name;dummy bash;0;I am dummy bash"

# append the command to the end of the command file
`$echocmd $cmdline >> $CommandFile`

【问题讨论】:

  • 请分享您的 bash 脚本以进行比较。
  • 感谢您的快速回复。我已经更新了我的帖子。
  • 我认为您想打开文件进行追加而不是写入。
  • 是的,先生,但是当我尝试追加时,它会抛出一个错误,提示“错误:29:非法搜索”
  • 1) 使用适当的权限和所有权恢复原始 nagios.cmd 文件。也许用 nagios 服务器重启?由于它被覆盖,我不相信权限是正确的。 2)在命令行字符串的末尾(内部)添加换行符'\n'。

标签: python nagios


【解决方案1】:

更改了我的代码,现在它工作得很好。我可以在 Nagios 界面中看到响应。

import time
import sys

HOSTNAME = "compute-1"

service = "python dummy"

return_code = "0"

text = "python dummy is working .....I am python dummy"

timestamp = int(time.time())

nagios_cmd = open("/usr/local/nagios/var/rw/nagios.cmd", "w")

nagios_cmd.write("[{timestamp}] PROCESS_SERVICE_CHECK_RESULT;{hostname};{service};{return_code};{text}\n".format

                    (timestamp = timestamp,

                     hostname = HOSTNAME,

                     service = service,

                     return_code = return_code,

                     text = text))


nagios_cmd.close()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多