Python在带有返回调用的函数下添加多项检查

时间:2019-03-01 05:20:28

标签: python subprocess

我有一个下面的代码片段,用于对系统和服务进行快速的运行状况检查,这工作正常,但是,我想包含一个call_functionps_rpcbind的带elif条件的检查,因为到目前为止,我正在rpc下通过检查ps -e | grep rpc检查call_function服务是否在运行,我想根据命令{{ 1}}进行交叉验证。

我可以在rpcinfo -p下添加它吗

call_function

需要什么:基于import subprocess import socket 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) return subprocess.call('ps -e | grep %s > /dev/null 2>&1' % service, shell=True) ps_ntp = call_function("ntp") ps_nscd = call_function("nscd") ps_mail = call_function("sendmail") ps_postfix = call_function("qmgr") #ps_altris = call_function("aex-plug") ps_automnt = call_function("automount") ps_rpcbind = call_function("rpc") if ps_ntp == 0: print "Service Status: NTP is Running on the host", hst_name else: print "Service Status: NTP is not Running on the host", hst_name if ps_nscd == 0: print "Service Status: NSCD is Running on the host", hst_name else: print "Service Status: NSCD is not Running on the host", hst_name if ps_rpcbind == 0: print "Service Status: Rpcbind is Running on the host", hst_name else: print "Service Status: Rpcbind is not Running on the host", hst_name if ps_mail == 0: print "Service Status: Sendmail is Running on the host", hst_name elif ps_postfix == 0: print "Service Status: Postfix is Running on the host", hst_name else: print "Service Status: Sendmail is not Running on the host", hst_name if ps_automnt == 0: print "Service Status: Automount is Running on the host" , hst_name else: print "Service Status: Automont is not Running on the host" , hst_name

rpcinfo -p

命令if ps_rpcbind == 0: print "Service Status: Rpcbind is Running on the host", hst_name elif ps_rpc == 0: print "Service Status: Rpcbind is Running on the host", hst_name else: print "Service Status: Rpcbind is not Running on the host", hst_name 返回下面的输出。

rpcinfo -p

如果需要任何详细信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

更新

因此,根据您的评论,我认为您可以执行以下操作。

if ps_rpcbind == 0:
   print "Service Status: Rpcbind is Running on the host", hst_name
elif not sb.check_call('rpcinfo -p', shell=True, stdout=sb.PIPE)
    print("RPC service is running")
else:
   print "Service Status: Rpcbind is not Running on the host", hst_name

请注意,将0作为返回代码表示服务运行正常,但是在python0False,因此您必须检查not