我的任务是制作一个自定义 python 脚本(因为我不擅长 Bash)在远程 NRPE 客户端上运行,该客户端递归地计算 /tmp 目录中的文件数。这是我的脚本:
#!/usr/bin/python3.5
import os
import subprocess
import sys
file_count = sum([len(files) for r, d, files in os.walk("/tmp")]) #Recursive check of /tmp
if file_count < 1000:
x = subprocess.Popen(['echo', 'OK -', str(file_count), 'files in /tmp.'], stdout=subproce$
print(x.communicate()[0].decode("utf-8")) #Converts from byteobj to str
# subprocess.run('exit 0', shell=True, check=True) #Service OK - exit 0
sys.exit(0)
elif 1000 <= file_count < 1500:
x = subprocess.Popen(['echo', 'WARNING -', str(file_count), 'files in /tmp.'], stdout=sub$
print(x.communicate()[0].decode("utf-8")) #Converts from byteobj to str
sys.exit(1)
else:
x = subprocess.Popen(['echo', 'CRITICAL -', str(file_count), 'files in /tmp.'], stdout=su$
print(x.communicate()[0].decode("utf-8")) #Converts from byteobj to str
sys.exit(2)
编辑1:我想硬编码file_count到1300,我得到了一个警告:1300 files in /tmp。看来问题仅在于nagios服务器读取客户端计算机中文件的能力/tmp。
我做了什么:
我将脚本和其他脚本一起放在目录中。
我/usr/local/nagios/etc/nrpe.cfg在客户端计算机上用以下行进行了编辑:
command[check_tmp]=/usr/local/nagios/libexec/check_tmp.py
我/usr/local/nagios/etc/servers/testserver.cfg在 nagios 服务器上编辑了这个文件,如下所示:
define service {
use generic-service
host_name wp-proxy
service_description Files in /tmp
check_command check_nrpe!check_tmp
}
输出:
正确的输出是:OK - 3 files in /tmp
当我以 root 身份在客户端机器上运行脚本时,我得到了正确的输出
当我以 nagios 用户身份在客户端机器上运行脚本时,我得到了正确的输出
我在 Nagios 核心上的输出似乎可以正常工作,但是/tmp当我知道还有更多文件时,它显示有 0 个文件。我在客户端机器上制作了 2 个文件,在 nagios 服务器上制作了 1 个文件。
服务器输出供参考:
https://puu.sh/BioHW/838ba84c3e.png
(忽略底层服务器,使用wp-proxy解决的任何问题也将在wpreess-gkanc1上进行更改)
编辑2:我在nagios服务器上运行以下命令:
/usr/local/nagios/libexec/check_nrpe -H 192.168.1.59 -c check_tmp_folder
我确实得到了一个 0 文件返回。但是,我仍然不知道如何解决此问题。
千巷猫影
相关分类