我的call_函数在python 2.6的if-else条件下无法正常工作
我已经学习并尝试在下面的代码中创建这两个函数,其中第一个函数用于检查各种进程状态,如ntp、nscd等。 现在,在执行脚本时,call_函数没有给出进程的正确状态,只是返回eg NTP服务,而不是在主机上为每个服务运行。而为fs使用而创建的第二个函数fs_函数工作正常 你们能告诉我,我犯了什么错。。 下面是代码我的call_函数在python 2.6的if-else条件下无法正常工作,python,Python,我已经学习并尝试在下面的代码中创建这两个函数,其中第一个函数用于检查各种进程状态,如ntp、nscd等。 现在,在执行脚本时,call_函数没有给出进程的正确状态,只是返回eg NTP服务,而不是在主机上为每个服务运行。而为fs使用而创建的第二个函数fs_函数工作正常 你们能告诉我,我犯了什么错。。 下面是代码 #!/usr/bin/python import subprocess import socket threshold = 9 hst_name = (socket.gethostnam
#!/usr/bin/python
import subprocess
import socket
threshold = 9
hst_name = (socket.gethostname())
print "HostName:", hst_name
############### Function to Check the Different process & Service Status #########
def call_function(service):
return subprocess.call('ps -e | grep service > /dev/null 2>&1', shell=True)
ps_ntp = call_function("ntp")
ps_nscd = call_function("nscd")
ps_mail = call_function("sendmail")
ps_altris = call_function("aex-plug")
ps_automnt = call_function("automount")
if ps_ntp == 0:
print "Service Status: NTP Service is Running On the host" , hst_name
else:
print "Service Status: NTP Service is Not Running On the host" , hst_name
if ps_nscd == 0:
print "Service Status: NSCD Service is Running On the host" , hst_name
else:
print "Service Status: NSCD Service is Not Running On the host", hst_name
if ps_mail == 0:
print "Service Status: Sendmail Service is Running On the host" , hst_name
else:
print "Service Status: Sendmail Service is Not Running" , hst_name
if ps_altris == 0:
print "Service Status: Altris Service is Running On the host" , hst_name
else:
print "Service Status: Altris Service is Not Running On the host" , hst_name
if ps_automnt == 0:
print "Service Status: Automount Service is Running On the host" , hst_name
else:
print "Service Status: Automont Service is Not Running On the host" , hst_name
####### Fucntion to Check the File-system thereshold Status #############
def fs_function(usage):
return subprocess.Popen(['df', '-h', usage], stdout=subprocess.PIPE)
rootfs = fs_function("/")
varfs = fs_function("/var")
output = rootfs.communicate()[0].strip().split("\n")
for x in output[1:]:
if int(x.split()[-2][:-1]) >= threshold:
print "Service Status: Filesystem For Root(/) is more than 90% On the Host" , hst_name
else:
print "Service Status: Filesystem For Root(/) is Normal on The Host", hst_name
output = varfs.communicate()[0].strip().split("\n")
for x in output[1:]:
if int(x.split()[-2][:-1]) >= threshold:
print "Service Status: Filesystem For /var is more than 90% On the Host" , hst_name
else:
print "Service Status: Filesystem For /var is Normal on The Host", hst_name
使用以下字符串格式设置方法:
return subprocess.call('ps -e | grep %s > /dev/null 2>&1' % service, shell=True)
字符串中的grep服务不使用service变量。你在告诉grep找文字服务,而不是ntp或nscd或其他什么。请将你的问题编辑成a,并特别强调最低限度。。另外,请注意,使用shell=True是众所周知的安全性hazard@SB87,当然,我会小心的。@user2357112,说得好。。我知道了。我会检查建议的更改,然后向您回复状态。