Python写的服务监控程序实例

前言:

Redhat下安装Python2.7

rhel6.4自带的是2.6, 发现有的机器是python2.4。 到python网站下载源代码,解压到Redhat上,然后运行下面的命令:

代码如下:

# ./configure --prefix=/usr/local/python27
# make
# make install

这样安装之后默认不会启用Python2.7,需要使用/usr/local/python27/bin/python2.7调用新版本的python。

而下面的安装方式会直接接管现有的python

代码如下:

# ./configure
# make
# make install

开始:

服务子进程被监控主进程创建并监控,当子进程异常关闭,主进程可以再次启动之。使用了python的subprocess模块。就这个简单的代码,居然互联网上没有现成可用的例子。没办法,我写好了贡献出来吧。

首先是主进程代码:service_mgr.py

代码如下:

#!/usr/bin/python 
#-*- coding: UTF-8 -*- 
# cheungmine 
# stdin、stdout和stderr分别表示子程序的标准输入、标准输出和标准错误。 
#  
# 可选的值有: 
#   subprocess.PIPE - 表示需要创建一个新的管道. 
#   一个有效的文件描述符(其实是个正整数) 
#   一个文件对象 
#   None - 不会做任何重定向工作,子进程的文件描述符会继承父进程的. 
#  
# stderr的值还可以是STDOUT, 表示子进程的标准错误也输出到标准输出. 
#  
# subprocess.PIPE 
# 一个可以被用于Popen的stdin、stdout和stderr 3个参数的特输值,表示需要创建一个新的管道. 
#  
# subprocess.STDOUT 
# 一个可以被用于Popen的stderr参数的特输值,表示子程序的标准错误汇合到标准输出. 
################################################################################ 
import os 
import sys 
import getopt 
 
import time 
import datetime 
 
import codecs 
 
import optparse 
import ConfigParser 
 
import signal 
import subprocess 
import select 
 
# logging 
# require python2.6.6 and later 
import logging   
from logging.handlers import RotatingFileHandler 
 
## log settings: SHOULD BE CONFIGURED BY config 
LOG_PATH_FILE = "./my_service_mgr.log" 
LOG_MODE = 'a' 
LOG_MAX_SIZE = 4*1024*1024 # 4M per file 
LOG_MAX_FILES = 4          # 4 Files: my_service_mgr.log.1, printmy_service_mgrlog.2, ...   
LOG_LEVEL = logging.DEBUG   
 
LOG_FORMAT = "%(asctime)s %(levelname)-10s[%(filename)s:%(lineno)d(%(funcName)s)] %(message)s"   
 
handler = RotatingFileHandler(LOG_PATH_FILE, LOG_MODE, LOG_MAX_SIZE, LOG_MAX_FILES) 
formatter = logging.Formatter(LOG_FORMAT) 
handler.setFormatter(formatter) 
 
Logger = logging.getLogger() 
Logger.setLevel(LOG_LEVEL) 
Logger.addHandler(handler)  
 
# color output 

pid = os.getpid()  
 
def print_error(s): 
    print '\033[31m[%d: ERROR] %s\033[31;m' % (pid, s) 
 
def print_info(s): 
    print '\033[32m[%d: INFO] %s\033[32;m' % (pid, s) 
 
def print_warning(s): 
    print '\033[33m[%d: WARNING] %s\033[33;m' % (pid, s) 
 
 
def start_child_proc(command, merged): 
    try: 
        if command is None: 
            raise OSError, "Invalid command" 
 
        child = None 
 
        if merged is True: 
            # merge stdout and stderr 
            child = subprocess.Popen(command, 
                stderr=subprocess.STDOUT, # 表示子进程的标准错误也输出到标准输出 
                stdout=subprocess.PIPE    # 表示需要创建一个新的管道 
            ) 
        else: 
            # DO NOT merge stdout and stderr 
            child = subprocess.Popen(command, 
                stderr=subprocess.PIPE, 
                stdout=subprocess.PIPE) 
 
        return child 
 
    except subprocess.CalledProcessError: 
        pass # handle errors in the called executable 
    except OSError: 
        pass # executable not found 
 
    raise OSError, "Failed to run command!" 
 
 
def run_forever(command): 
    print_info("start child process with command: " + ' '.join(command)) 
    Logger.info("start child process with command: " + ' '.join(command)) 
 
    merged = False 
    child = start_child_proc(command, merged) 
 
    line = '' 
    errln = '' 
 
    failover = 0 
 
    while True: 
        while child.poll() != None: 
            failover = failover + 1 
            print_warning("child process shutdown with return code: " + str(child.returncode))            
            Logger.critical("child process shutdown with return code: " + str(child.returncode)) 
 
            print_warning("restart child process again, times=%d" % failover) 
            Logger.info("restart child process again, times=%d" % failover) 
            child = start_child_proc(command, merged) 
 
        # read child process stdout and log it 
        ch = child.stdout.read(1) 
        if ch != '' and ch != '\n': 
            line += ch 
        if ch == '\n': 
            print_info(line) 
            line = '' 
 
        if merged is not True: 
            # read child process stderr and log it 
            ch = child.stderr.read(1) 
            if ch != '' and ch != '\n': 
                errln += ch 
            if ch == '\n': 
                Logger.info(errln) 
                print_error(errln) 
                errln = '' 
 
    Logger.exception("!!!should never run to this!!!")   
 
 
if __name__ == "__main__": 
    run_forever(["python", "./testpipe.py"])

然后是子进程代码:testpipe.py

代码如下:

#!/usr/bin/python 
#-*- coding: UTF-8 -*- 
# cheungmine 
# 模拟一个woker进程,10秒挂掉 
import os 
import sys 
 
import time 
import random 
 
cnt = 10 
 
while cnt >= 0: 
    time.sleep(0.5) 
    sys.stdout.write("OUT: %s\n" % str(random.randint(1, 100000))) 
    sys.stdout.flush() 
 
    time.sleep(0.5) 
    sys.stderr.write("ERR: %s\n" % str(random.randint(1, 100000))) 
    sys.stderr.flush() 
 
    #print str(cnt) 
    #sys.stdout.flush() 
    cnt = cnt - 1 
 
sys.exit(-1)

Linux上运行很简单:

代码如下:

$ python service_mgr.py

Windows上以后台进程运行:

代码如下:

> start pythonw service_mgr.py

代码中需要修改:

代码如下:

run_forever(["python", "testpipe.py"])

(0)

相关推荐

  • python操作摄像头截图实现远程监控的例子

    最近用python写了一个远程监控的程序,主要功能有:1.用邮件控制所以功能2.可以对屏幕截图,屏幕截图发送到邮箱3.可以用摄像头获取图片,这些图片上传到七牛4.开机自启动 复制代码 代码如下: ##coding by loster#import win32apiimport win32conimport platformimport socketimport timeimport osimport smtplibimport poplibfrom VideoCapture import Dev

  • Python中使用Inotify监控文件实例

    Inotify地址:访问 # -*- coding:utf-8 -*- import os import pyinotify from functions import * WATCH_PATH = '' #监控目录 if not WATCH_PATH: wlog('Error',"The WATCH_PATH setting MUST be set.") sys.exit() else: if os.path.exists(WATCH_PATH): wlog('Watch statu

  • python实现实时监控文件的方法

    在业务稳定性要求比较高的情况下,运维为能及时发现问题,有时需要对应用程序的日志进行实时分析,当符合某个条件时就立刻报警,而不是被动等待出问题后去解决,比如要监控nginx的$request_time和$upstream_response_time时间,分析出最耗时的请求,然后去改进代码,这时就要对日志进行实时分析了,发现时间长的语句就要报警出来,提醒开发人员要关注,当然这是其中一个应用场景,通过这种监控方式还可以应用到任何需要判断或分析文件的地方,所以今天我们就来看看如何用python实现实时监

  • 使用Python的Supervisor进行进程监控以及自动启动

    做服务器端开发的同学应该都对进程监控不会陌生,最近恰好要更换 uwsgi 为 gunicorn,而gunicorn又恰好有这么一章讲进程监控,所以多研究了下. 结合之前在腾讯工作的经验,也会讲讲腾讯的服务器监控是怎么做的.同时也会讲下小团队又该怎么敏捷的解决. 下面按照监控的方法依次介绍. 一.按照进程名监控 在腾讯内部所有server都是要打包发布的,而在打包过程中是需要填写要监控的进程名,然后在crontab中定时通过ps查询进程是否存在. 这种方法是比较简单的方法,但是考虑到很多进程会在启

  • Python写的一个简单监控系统

    市面上有很多开源的监控系统:Cacti.nagios.zabbix.感觉都不符合我的需求,为什么不自己做一个呢 用Python两个小时徒手撸了一个简易的监控系统,给大家分享一下,希望能对大家有所启发 首先数据库建表 建立一个数据库"falcon",建表语句如下: CREATE TABLE `stat` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `host` varchar(256) DEFAULT NULL, `mem_free`

  • Python脚本实现网卡流量监控

    #/usr/bin/env/python #coding=utf-8 import sys,re,time,os maxdata = 50000 #单位KB memfilename = '/tmp/newnetcardtransdata.txt' netcard = '/proc/net/dev' def checkfile(filename): if os.path.isfile(filename): pass else: f = open(filename, 'w') f.write('0'

  • 写了个监控nginx进程的Python脚本

    复制代码 代码如下: #!/usr/bin/env python import os, sys, time while True: time.sleep(3) try: ret = os.popen('ps -C nginx -o pid,cmd').readlines() if len(ret) < 2: print "nginx process killed, restarting service in 3 seconds." time.sleep(3) os.system(

  • python实现监控windows服务并自动启动服务示例

    使用Python 2.7 + pywin32 + wxpython开发 每隔一段时间检测一下服务是否停止,如果停止尝试启动服务.进行服务停止日志记录 AppMain.py 复制代码 代码如下: #!/usr/bin/env python#-*- encoding:utf-8 -*- """1. 每隔一分钟检测一次服务状态2. 如果发现服务状态已经停止,那么尝试启动服务3. 自动记录日志4. 任务栏图标显示""" import sys;reload

  • python动态监控日志内容的示例

    日志文件一般是按天产生,则通过在程序中判断文件的产生日期与当前时间,更换监控的日志文件程序只是简单的示例一下,监控test1.log 10秒,转向监控test2.log 程序监控使用是linux的命令tail -f来动态监控新追加的日志 复制代码 代码如下: #!/usr/bin/python# encoding=utf-8# Filename: monitorLog.pyimport osimport signalimport subprocessimport time logFile1 =

  • python监控网卡流量并使用graphite绘图的示例

    复制代码 代码如下: #!/usr/bin/env pythonimport sys,timefrom socket import socketdef read_interface(in_file):    with file(in_file) as f:        return f.readlines()[2:]def set_interface(inter_msg):    dic={}    for i in xrange(len(inter_msg)):        dic[int

随机推荐