简体   繁体   English

我的call_function无法在python 2.6的if else条件下正常使用

[英]My call_function is not working Correctly with if else condition with python 2.6

I have learned & tried to create the two Functions in my below code, Where the First function is designed to Check the various process Status like ntp,nscd etc. Now While executing the scripts the call_function not giving the correct Status of the process and Just returning with eg NTP Service is Not Running On the host for each Service. 我已经学习并尝试在下面的代码中创建两个函数,其中第一个函数旨在检查各种进程状态,例如ntp,nscd等。现在在执行脚本时, call_function不能给出正确的进程状态,而Just例如, NTP Service is Not Running On the host返回的NTP Service is Not Running On the host While Second Function fs_function which is created for FS usage is working correctly. 为FS使用而创建的Second Function fs_function可以正常工作。

Can you guys suggest, What mistake i'm doing here.. Below is the Code.. 你们能建议我在这里犯什么错误吗?

#!/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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM