如何在python2.6中创建函数

2024-05-08 19:50:45 发布

您现在位置:Python中文网/ 问答频道 /正文

作为一名python新手,我创建了下面的脚本来检查各种进程和文件系统的使用情况,我希望以一种更复杂或最时尚的方式包装到函数中。。你知道吗

请建议一下做这件事的方法。。 我的python版本是2.6

猫健康检查.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

Tags: devifisstatusserviceshellcallnull
1条回答
网友
1楼 · 发布于 2024-05-08 19:50:45

@timgeb有一个很好的观点。你要找的是一个简单的谷歌搜索什么是函数,然后花一些时间阅读。你知道吗

也就是说,几点提示,你要意识到,你在这里要求别人的时间,所以你更好地理解礼仪。首先,你似乎没有做任何研究就发布了一个问题,至少你没有暗示任何相反的事情。最好提供您尝试过的例子,让我们知道您的想法,这样我们就可以为您指出正确的方向,而不是形象地说“我希望我的代码更复杂,但我希望你们都为我做工作”。你知道吗

话虽如此,还是有些东西能让你走。在函数方面,它是关于编写一次重复的代码,这样你就不必一次又一次地重复。上面一个简单的例子,你写了“=子流程调用('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………”中的某些内容发生了更改,那么您只需更改一次,而不必搜索整个程序来更改它。你知道吗

相关问题 更多 >

    热门问题