python监控进程状态,记录重启时间及进程号的实例
作者:加尔鲁什 发布时间:2022-04-22 07:47:20
标签:python,监控,进程,重启
本脚本为本人在性能测试过程中编写,用于对进程状态的监控,也可以用于日常的监控,适用性一般,扩展性还行
# -*- coding: UTF-8 -*-
# author=baird_xiang
import os
import time
import re
import copy
nginxRestart_num= -1
nginxReload_num= -1
logSender_num= -1
es_num= -1
nginxParent_pid=[]
nginxChild_pid=[]
logSender_pid=[]
es_pid=[]
nginxRestart_time =[]
nginxReload_time=[]
logSender_time=[]
es_time=[]
def get_restart(thread_name):
global nginxRestart_num,nginxReload_num,logSender_num,es_num
while True:
try:
for i in thread_name:
if i=='nginx_restart':
nP_pid = os.popen("sudo pgrep -lo nginx |grep -v grep|awk '{print $1}'").read()
nP_time = os.popen("sudo ps aux|grep nginx |grep -v grep|awk 'NR==1{print $9}'").read()
nginx_path = os.popen("sudo ps aux|grep nginx |grep -v grep|awk 'NR==1{print $11}'").read()
date = time.strftime('%Y-%m-%d',time.localtime(time.time()))
nP_time_now = date + '-' + nP_time.split('\n')[0]
if nP_pid and (nP_pid not in nginxParent_pid) and (nginx_path=='/usr/sbin/nginx\n'):
nginxParent_pid.append(nP_pid)
nginxRestart_num=nginxRestart_num+1
# if nP_time and (nP_time_now not in nginxRestart_time) and (color!='-c\n'):
nginxRestart_time.append(nP_time_now)
elif i=='nginx_reload':
nR_pid = os.popen("sudo pgrep -ln nginx|grep -v grep |awk '{print $1}'").read()
nR_time = os.popen("sudo ps aux|grep nginx |grep -v grep|awk 'NR==2{print $9}'").read()
nginx_path = os.popen("sudo ps aux|grep nginx |grep -v grep|awk 'NR==1{print $11}'").read()
date = time.strftime('%Y-%m-%d',time.localtime(time.time()))
nR_time_now = date + '-' + nR_time.split('\n')[0]
if nR_pid and (nR_pid not in nginxChild_pid) and (nginx_path=='/usr/sbin/nginx\n') :
nginxChild_pid.append(nR_pid)
nginxReload_num=nginxReload_num+1-nginxRestart_num
#if nR_time and (nR_time_now not in nginxReload_time) and (color!='-c\n'):
nginxReload_time.append(nR_time_now)
elif i=='log_sender':
lS_pid = os.popen("sudo ps aux|grep log_sender |grep -v grep|awk 'NR==1{print $2}'").read()
lS_time = os.popen("sudo ps aux|grep log_sender |grep -v grep|awk 'NR==1{print $9}'").read()
color = os.popen("sudo ps aux|grep log_sender |grep -v grep|awk 'NR==1{print $12}'").read()
wwwdate = os.popen("sudo ps aux|grep log_sender |grep -v grep|awk 'NR==1{print $1}'").read()
date = time.strftime('%Y-%m-%d',time.localtime(time.time()))
lS_time_now = date + '-' + lS_time.split('\n')[0]
if lS_pid and (color!='-c\n') and ( lS_pid not in logSender_pid ) and (wwwdate=='www-data\n'):
logSender_pid.append(lS_pid)
logSender_num=logSender_num+1
#if lS_time and (lS_time_now not in logSender_time) and (color!='-c\n'):
logSender_time.append(lS_time_now)
elif (i=='elasticsearch') and (os.popen("sudo ps -ef |grep elasticsearch |grep -v grep|awk 'NR==1{print $2}'").read()):
time.sleep(1)
e_pid = os.popen("sudo ps aux|grep elasticsearch |grep -v grep|awk 'NR==1{print $2}'").read()
e_time = os.popen("sudo ps aux|grep elasticsearch |grep -v grep|awk 'NR==1{print $9}'").read()
color = os.popen("sudo ps aux|grep elasticsearch |grep -v grep|awk 'NR==1{print $12}'").read()
elastic = os.popen("sudo ps aux|grep elasticsearch |grep -v grep|awk 'NR==1{print $1}'").read()
date = time.strftime('%Y-%m-%d',time.localtime(time.time()))
e_time_now = date + '-' + e_time.split('\n')[0]
if e_pid and (color!='-c\n') and (e_pid not in es_pid) and (elastic =='elastic+\n'):
es_pid.append(e_pid)
es_num=es_num+1
# if e_time and (e_time_now not in es_time) and (color!='-c\n') and (elastic =='elastic+\n'):
es_time.append(e_time_now)
else:
pass
except (OSError,IOError): #防止进入循环但是这个时候进程重启,导致popen读取不到进程信息就会出错
pass
def set_nginxRestart_txt():
now_path = os.getcwd()
file_name = now_path + '/nginxRestart_%s.txt'%(nginxRestart_time[-1])
#写入文本
file1 = open(file_name,'w')
for i in range(1,len(nginxRestart_time)):
file1.write('重启时间:'+nginxRestart_time[i]+'重启前父进程号: '+ nginxParent_pid[i-1]+'重启后父进程号: ' +nginxParent_pid[i] + '\n')
file1.close()
file2 = open(file_name,'a+')
file2.write('nginx restart次数为: ' + str(nginxRestart_num) + '\n')
file2.close()
def set_nginxReload_txt():
now_path = os.getcwd()
file_name = now_path + '/nginxReload_%s.txt'%(nginxReload_time[-1])
#写入文本
file1 = open(file_name,'w')
for i in range(1,len(nginxReload_time)):
file1.write('重启时间:'+nginxReload_time[i] + '\n')
file1.close()
file2 = open(file_name,'a+')
file2.write('nginx reload次数为:' + str(nginxReload_num) + '\n')
file2.close()
def set_logsender_txt():
now_path = os.getcwd()
file_name = now_path + '/logsender_restart_%s.txt'%(logSender_time[-1])
#写入文本
file1 = open(file_name,'w')
for i in range(1,len(logSender_time)):
file1.write('重启时间:'+logSender_time[i]+'重启前进程号: '+ logSender_pid[i-1]+'重启后进程号: ' + logSender_pid[i] + '\n')
file1.close()
file2 = open(file_name,'a+')
file2.write('logsender重启次数为: '+ str(logSender_num) + '\n')
file2.close()
def set_es_txt():
now_path = os.getcwd()
file_name = now_path + '/esRestart_%s.txt'%(es_time[-1])
#写入文本
file1 = open(file_name,'w')
for i in range(1,len(es_time)):
file1.write('重启时间:'+es_time[i] +'重启前进程号: ' +es_pid[i-1] +'重启后进程号: ' + es_pid[i] +'\n')
file1.close()
file2 = open(file_name,'a+')
file2.write('elasticsearch重启次数为: ' + str(es_num) + '\n')
file2.close()
if __name__ =="__main__":
thread_name=['nginx_restart','nginx_reload','log_sender','elasticsearch']
try:
get_restart(thread_name)
except (KeyboardInterrupt,SystemExit):
set_nginxRestart_txt()
set_nginxReload_txt()
set_logsender_txt()
set_es_txt()
来源:https://blog.csdn.net/a1496785/article/details/80511290


猜你喜欢
- 本文实例为大家分享了vue实现页面添加水印的具体代码,供大家参考,具体内容如下js文件建一个watermark.js文件let setWat
- 可扩展标记语言 (XML) 是用于描述数据集内容以及应如何将数据输出到设备上或如何在 Web 页上显示数据的语言。标记语言的创建来源于出版商
- 5位数日期戳读取 .mat 文件处理里面数据时,发现里面的日期数据全部都是 “5位数” 数字,很不解;后来查到可以在excel中通过设置单元
- 有时候我们的爬虫程序添加了代理,但是我们不知道程序是否获取到了ip,尤其是动态转发模式的,这时候就需要进行检测了,以下是一种代理是否伪装成功
- 前言本篇给大家分享一下《通过Python的pdfplumber库将pdf转为图片》。一、pdfplumber库是什么?pdfplumber是
- 网站设计时,有一个最常用的指导性原则:页面长度原则上不超过3屏,宽度不超过1屏。这个原则明显是从用户的体验出发,特别是宽度不超过一屏,其最基
- 什么是 Python? Python 之父 Guido van Rossum 说:Python是一种高级程序语言,其核心设计哲学是代码可读性
- Django中集成jquery首先,静态的资源通常放入static文件夹中:static/ css/
- 昨天公司的网络更换,然后在使用git操作代码的时候,遇见了标题所述问题。仅以此文,记录该问题的解决过程。首先第一步: 得到本机的I
- 网上提供了很多sqlserver发送邮件的文章,转载居多,大部分介绍win2000+outlook2003环境,在操作系统和office已经
- 本文实例讲述了GO语言标准错误处理机制error用法。分享给大家供大家参考。具体分析如下:在 Golang 中,错误处理机制一般是函数返回时
- 其实这个话题已经在侧面写了好几篇深刻反思,用我自己几年工作实践的体会来看,性格决定了将来的发展。某些特质虽然可以掩饰,但在这之上必然不可能有
- 人工智能有多火,相信铺天盖地的新闻已经证实了这一点,不可否认,我们已经迎来了人工智能的又一次高潮。与前几次人工智能的飞跃相比,这一次人工智能
- 本文实例讲述了Django框架静态文件使用/中间件/禁用ip功能。分享给大家供大家参考,具体如下:静态文件一、静态文件的使用静态文件:网页中
- 最近做项目需要我们前端对金额进行千分位格式化(也就是说每三位用逗号隔开),代码已经做了修改 之前的版本是本人疏忽 真对不住大家了
- 方案一:利用selenium+phantomjs * 面浏览器的形式访问网站,再获取cookie值:from selenium import
- 一、使用 print() 函数在 Python 中,print() 函数支持格式化输出,与 C 语言的 printf 类似。1. 格式化输出
- print(X.shape):查看矩阵的行列号print(len(X)):查看矩阵的行数print(X.ndim):查看矩阵的维数1 查看矩
- 本文实例为大家分享了python绘制直方图的具体代码,供大家参考,具体内容如下运行结果如下代码如下from matplotlib impor
- 本文实例讲述了python求pi的方法,是一篇翻译自国外网站的文章,分享给大家供大家参考。具体实现方法如下:#_*_ coding=utf-