Python写的服务监控程序实例
作者:junjie 发布时间:2022-09-01 13:12:31
标签: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"])


猜你喜欢
- 这里以将Apache的日志写入到ElasticSearch为例,来演示一下如何使用Python将Spark数据导入到ES中。实际工作中,由于
- 求和try: while True: n=input() s=1 for x in raw_input(
- 先来看一个例子:>>> def foo(*args, **kwargs): print
- 前言本文将教你如何使用YOLOV3对象检测器、OpenCV和Python实现对图像和视频流的检测。用到的文件有yolov3.weights、
- mean_squared_error / mse 均方误差,常用的目标函数,公式为((y_pred-y_true)**2).mean()mo
- 内容摘要合理使用渐变留白网格布局提高字体应用明确而有效的导航设计漂亮、有用的页脚介绍优秀设计和卓越设计之间的区别是比较小的。一般人可能无法解
- 因为一些原因,卸载了Anaconda2的版本,转向3..发现Jupyter挂了.百思不得其解.后来了解到是因为内核找不到的问题导致的.这里整
- 前言:字体反爬是什么个意思?就是网站把自己的重要数据不直接的在源代码中呈现出来,而是通过相应字体的编码,与一个字体文件(一般后缀为ttf或w
- 什么是pyecharts?pyecharts 是一个用于生成 Echarts 图表的类库。echarts是百度开源的一个数据可视化 JS 库
- 数据库系统是管理信息系统的核心,基于数据库的联机事务处理(OLTP)以及联机分析处理(OLAP)是银行、企业、政府等部门最为重要的计算机应用
- 如何制作一个防止多次刷新计数的图片计数器?请问如何做一个专业的图片计数器? <%countlong
- 准备工作:首先,我们需要 import 几个工具包,一个是 python 标准库中的 wave 模块,用于音频处理操作,另外两个是 nump
- 0. 前言机器学习是人工智能的子集,它为计算机以及其它具有计算能力的系统提供自动预测或决策的能力,诸如虚拟助理、车牌识别系统、智能推荐系统等
- 1 报错类似如下数据库错误: Error querying database. Cause: java.sql.SQLSynta
- # 判断三角形类型def triangle(a,b,c): if a>0 and b>0 and c>0: &
- 代码如下,保存到HTML文件也可以查看效果:<html><head><meta charset="u
- 前言在github中经常可以看到下面的日历图,可以用来表示每一天在github上的活跃程度。类似的方法也可以用到空气质量的可视化方式中来,只
- 一、yield运行方式我们定义一个如下的生成器:def put_on(name): print("Hi {}, 货物来了,准备搬到
- mysql exists与not exists实例详解tableA|column1 | column1 |column3 |tableb|c
- 简介这篇宏哥就带着小伙伴们分享一下如何连接模拟器(电脑版的虚拟手机),然后再安装一款APP-淘宝为例。一、appium+pycharm+连接