作为新手Python学习者,我创建了下面的脚本来检查各种进程和文件系统的用法,我想以更复杂或最好的方式来包装功能..如何在Python 2.6中创建函数
请建议做的方式.. 我的Python版本是2.6
猫healthCheck.py
#!/usr/bin/python
import subprocess
import socket
THRESHOLD = 90
HST = (socket.gethostname())
print "HostName:", HST
PSNTP = subprocess.call('ps -e| grep ntp > /dev/null 2>&1', shell=True)
if PSNTP == 0:
print "Service Status: NTP Service is Running"
else:
print "Service Status: NTP Service is Running"
PSNSCD = subprocess.call('ps -e | grep nscd > /dev/null 2>&1', shell=True)
if PSNSCD == 0:
print "Service Status: NSCD Service is Running On the host" , HST
else:
print "Service Status: NSCD Service Not is Running", HST
PSMAIL = subprocess.call('ps -e | grep sendmail> /dev/null 2>&1', shell=True)
if PSMAIL == 0:
print "Service Status: Sendmail Service is Running"
else:
print "Service Status: Sendmail is Not Service Not is Running"
PSALTRIS = subprocess.call('ps -e | grep aex-plug > /dev/null 2>&1', shell=True)
if PSALTRIS == 0:
print "Service Status: Altris Service is Running"
else:
print "Service Status: Altris Service Not is Running"
PSAUTMNT = subprocess.call('ps -e| grep automount > /dev/null 2>&1', shell=True)
if PSAUTMNT == 0:
print "Service Status: Automount Service is Running"
else:
print "Service Status: Automont Service Not is Running"
rootfs = subprocess.Popen(['df', '-h', '/'], stdout=subprocess.PIPE)
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 20 % On the Host" , HST
else:
print "Service Status: Filesystem For Root(/) is Normal on The Host", HST
varfs = subprocess.Popen(['df', '-h', '/'], stdout=subprocess.PIPE)
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 20 % On the Host" , HST
else:
print "Service Status: Filesystem For /var is Normal on The Host", HST
这类问题更适合[代码评论](http://codereview.stackexchange.com/)。 – Rishav
不,我们不是在这里告诉你如何创建一个功能,当这些信息只是一个谷歌搜索了。 – timgeb
@timegeb。很高兴得到您的意见,我一定在寻找。 – krock1516