码迷,mamicode.com
首页 > 编程语言 > 详细

python客户端监控工具

时间:2016-06-13 17:21:42      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:客户端   python   except   


#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

python客户端监控工具

标签:客户端   python   except   

原文地址:http://luoguoling.blog.51cto.com/1568501/1788712

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!