2017-04-06 68 views
-2

作为新手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 
+0

这类问题更适合[代码评论](http://codereview.stackexchange.com/)。 – Rishav

+0

不,我们不是在这里告诉你如何创建一个功能,当这些信息只是一个谷歌搜索了。 – timgeb

+0

@timegeb。很高兴得到您的意见,我一定在寻找。 – krock1516

回答

2

@timgeb有一个非常好的点。你在找什么就像谷歌搜索一个功能,然后花一些时间阅读一样简单。

这就是说,几个指针,意识到你在这里要求人们的时间,所以它更好地了解礼仪。首先你已经发布了一个问题,似乎没有做过任何研究,至少你没有暗示任何相反的事情。提供您尝试过的例子并让我们知道您的想法会更好,因此我们可以指引您朝着正确的方向而不是比喻性地说“我希望我的代码更加复杂,但我希望你们所有人都能做好这项工作为了我”。

这就是说,这是让你去的东西。就功能而言,它是关于重复编写一次代码,以便您不必一次又一次地执行。从上面的一个简单例子中,你写了“= subprocess.call('ps -e | grep nscd>/dev/null 2> & 1',shell = True)”几次,除了nscd之外,其他所有内容都保持不变。 :

def call_function(service): 
    return subprocess.call('ps -e | grep service > /dev/null 2>&1', shell=True) 

PSNTP = call_function("ntp") 
PSNSCD = call_function("nscd") 

if PSNTP == 0: 
........ 

有意义吗?如果命令“ps -e .......”中的内容发生了变化,那么您只需要将其更改一次,而不是搜索整个程序以将其更改为多个地方。

+0

感谢你和大家的时间,来到你的观点: “意识到你在这里要求人们的时间,所以你理解礼仪要好得多。”不,我没有要求某人杀死他们的时间而只是要求建议,我已经浏览了谷歌的材料和任何我可以学到的最好的东西,我试着通过在这里了解到这些代码,我明白,时间对于现在的每个人来说都是至关重要的,这就是我欣赏@timgeb想法的方式。 – krock1516

+0

我了解真正不需要学习的礼仪,如果有人不能给出他/她的意见,那么他/她甚至不需要甚至费心提供讽刺意见。这个世界充满了我们非常高尚的人民给予世界最聪明的教导而没有像所有人一样自私的例子,Dennis ritchei,我们所有人都站在我们身边,感觉自己是所谓的专家,他甚至不打算分享一点点信息到世界,我们应该遵循他的理想,不仅仅是一个高傲的放置一个想法,这对我来说是有意义的! – krock1516