#coding:utf-8
import Queue,threading,time,random,getopt,sys,os
import checkdisk,checkcpu,checkmem
q = Queue.Queue(0)
NUM_WORKERS = 10
class MonitorThread(threading.Thread):
def __init__(self,jobInput):
self._jobq = jobInput
threading.Thread.__init__(self)
def run(self):
while True:
if self._jobq.qsize() > 0:
try:
MonitorJob = self._jobq.get(False)
self.MonitorProcessJob(MonitorJob)
except Queue.Empty:
queue_size = 0
else:
break
def MonitorProcessJob(self,MonitorJob):
doMonitorJob(MonitorJob)
def doMonitorJob(MonitorJob):
MonitorJob.Run()
def funzionedeamon():
q.put(checkdisk)
q.put(checkcpu)
q.put(checkmem)
for x in range(NUM_WORKERS):
MonitorThread(q).start()
def createdaemon():
try:
if os.fork() > 0:
os._exit(0)
except OSError,error:
os._exit(1)
os.chdir("/root/monitor/")
os.setsid()
try:
pid = os.fork()
if pid > 0:
fd = open("/var/run/monitor.pid","w")
fd.write(str(pid))
fd.close()
os._exit(0)
except OSError,error:
os._exit(1)
funzionedeamon()
def help():
print "this script is monitor program"
print "-h print help information"
print "-s start|stop"
def startpro():
if os.path.exists("/var/run/monitor.pid"):
fd = open("/var/run/monitor.pid")
pid = fd.read()
fd.close()
if pid.strip() == "":
createdaemon()
else:
print "进程未关闭"
else:
createdaemon()
def stoppro():
fd = open("/var/run/monitor.pid")
pid = fd.read()
fd.close()
os.kill(int(pid.strip()),signal.SIGKILL)
fd = open("/var/run/monitor.pid","w")
fd.write("")
fd.close()
def main():
opt,agrs = getopt.getopt(sys.argv[1:],"s:h")
opsign = ""
for op,value in opt:
if op == "-h":
help()
os._exit(0)
elif op == "-s":
opsign = value
if opsign == "start":
startpro()
elif opsign == "stop":
stoppro()
if __name__ == "__main__":
main()
本文出自 “自动化rolin” 博客,请务必保留此出处http://luoguoling.blog.51cto.com/1568501/1788712
原文地址:http://luoguoling.blog.51cto.com/1568501/1788712