Python在带有返回调用的函数下添加多重检查
我有一个下面的代码片段来运行系统和服务的快速运行状况检查,但是运行良好,我想为Python在带有返回调用的函数下添加多重检查,python,subprocess,Python,Subprocess,我有一个下面的代码片段来运行系统和服务的快速运行状况检查,但是运行良好,我想为call_函数I eps\u rpcbind包含一个带有elif条件的检查,因为现在我正在检查rpc服务,无论它运行是否简单,都要在call_函数下通过检查ps-e | grep rpc,我想根据命令添加一个条件rpcinfo-p以交叉验证相同内容 我可以在call\u函数 import subprocess import socket hst_name = (socket.gethostname()) print "
call_函数
I eps\u rpcbind
包含一个带有elif条件的检查,因为现在我正在检查rpc
服务,无论它运行是否简单,都要在call_函数
下通过检查ps-e | grep rpc
,我想根据命令添加一个条件rpcinfo-p
以交叉验证相同内容
我可以在call\u函数
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
命令rpcinfo-p
返回以下输出
# rpcinfo -p
program vers proto port service
100000 4 tcp 111 portmapper
100000 3 tcp 111 portmapper
100000 2 tcp 111 portmapper
100000 4 udp 111 portmapper
100000 3 udp 111 portmapper
100000 2 udp 111 portmapper
如果需要任何详细信息,请告诉我。更新 因此,根据你的评论,我认为你可以做到以下几点
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
作为返回代码意味着服务运行良好,但在python
中,0
是False
,所以您必须检查not
,所以您想用rpcinfo-p
验证什么,我的意思是,正如您所示,rpcinfo-p
的输出包含很多东西,那么,你到底在它的输出中寻找什么呢?@Rohit,正如你所看到的,它只是为了检查服务是否正在运行,所以有两种方法,一种是我有ps-e | grep rpc
另一种方法,我想包括rpcinfo-p
如果它将返回一些值,那么好吧,否则它将失败,所以这只是另一种相同的检查。因此,评估真正基于布尔值0
和1
,因此,如果rpcinfo-p
不正确(或没有返回任何内容),那么它将确认服务失败。感谢Rohit给出的好答案,但是我需要在调用中添加这个作为辅助检查,正如我在Desired
部分所示:elif ps_rpc==0:
这相当简单,只需在elif
部分中移动这个代码。
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