【发布时间】: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'。