How to create function in Python 2.6 -
as being newbie python learning fellow, have created below script check various process , filesystem usage, in more sophisticated or best fashioned way wrapping functions..
please suggest way it.. python version 2.6
cat 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 running" else: print "service status: ntp service running" psnscd = subprocess.call('ps -e | grep nscd > /dev/null 2>&1', shell=true) if psnscd == 0: print "service status: nscd service running on host" , hst else: print "service status: nscd service not running", hst psmail = subprocess.call('ps -e | grep sendmail> /dev/null 2>&1', shell=true) if psmail == 0: print "service status: sendmail service running" else: print "service status: sendmail not service not running" psaltris = subprocess.call('ps -e | grep aex-plug > /dev/null 2>&1', shell=true) if psaltris == 0: print "service status: altris service running" else: print "service status: altris service not running" psautmnt = subprocess.call('ps -e| grep automount > /dev/null 2>&1', shell=true) if psautmnt == 0: print "service status: automount service running" else: print "service status: automont service not running" rootfs = subprocess.popen(['df', '-h', '/'], stdout=subprocess.pipe) output = rootfs.communicate()[0].strip().split("\n") x in output[1:]: if int(x.split()[-2][:-1]) >= threshold: print "service status: filesystem root(/) more 20 % on host" , hst else: print "service status: filesystem root(/) normal on host", hst varfs = subprocess.popen(['df', '-h', '/'], stdout=subprocess.pipe) output = varfs.communicate()[0].strip().split("\n") x in output[1:]: if int(x.split()[-2][:-1]) >= threshold: print "service status: filesystem /var more 20 % on host" , hst else: print "service status: filesystem /var normal on host", hst
@timgeb has point. you're looking simple googling function , spending time reading.
that being said, couple of pointers, realise you're asking people's time here it's far better understand etiquette. first you've posted problem without seemingly having done research, @ least haven't hinted contrary. it's far better provide examples of you've tried , let know thinking can point in right direction rather figuratively saying "i want code more sophisticated want of work me".
that being said, here's going. in terms of functions it's writing repetitive code once don't have again , again. simple example above, wrote " = subprocess.call('ps -e | grep nscd > /dev/null 2>&1', shell=true)" several times, apart nscd else remains same, therefore:
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: ........
make sense? if in command "ps -e......." changes need change 1 time rather searching through whole program change many places.
Comments
Post a Comment