导航:首页 > 编程语言 > python性能监控工具

python性能监控工具

发布时间:2022-08-03 20:14:53

1. 如何监控在服务器上运行的多个python脚本的状态

linux系统的话,监控工具比较好的有ganglia,zabbix
windows系统的话,用自带的“性能监视器”(老版本的windows叫性能计数器)

2. 有python开发的监控工具吗

在公司里做的一个接口系统,主要是对接第三方的系统接口,所以,这个系统里会和很多其他公司的项目交互。随之而来一个很蛋疼的问题,这么多公司的接口,不同公司接口的稳定性差别很大,访问量大的时候,有的不怎么行的接口就各种出错了。
这个接口系统刚刚开发不久,整个系统中,处于比较边缘的位置,不像其他项目,有日志库,还有短信告警,一旦出问题,很多情况下都是用户反馈回来,所以,我的想法是,拿起python,为这个项目写一个监控。如果在调用某个第三方接口的过程中,大量出错了,说明这个接口有有问题了,就可以更快的采取措施。
项目的也是有日志库的,所有的info,error日志都是每隔一分钟扫描入库,日志库是用的mysql,表里有几个特别重要的字段:
有日志库,就不用自己去线上环境扫日志分析了,直接从日志库入手。由于日志库在线上时每隔1分钟扫,那我就去日志库每隔2分钟扫一次,如果扫到有一定数量的error日志就报警,如果只有一两条错误就可以无视了,也就是短时间爆发大量错误日志,就可以断定系统有问题了。报警方式就用发送邮件,所以,需要做下面几件事情:
1. 操作MySql。
2. 发送邮件。
3. 定时任务。
4. 日志。
5. 运行脚本。
明确了以上几件事情,就可以动手了。
操作数据库
使用MySQLdb这个驱动,直接操作数据库,主要就是查询操作。
获取数据库的连接:

def get_con():
host = "127.0.0.1"
port = 3306
logsdb = "logsdb"
user = "root"
password = "never tell you"
con = MySQLdb.connect(host=host, user=user, passwd=password, db=logsdb, port=port, charset="utf8")
return con

从日志库里获取数据,获取当前时间之前2分钟的数据,首先,根据当前时间进行计算一下时间。之前,计算有问题,现在已经修改。

def calculate_time():

now = time.mktime(datetime.now().timetuple())-60*2
result = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now))
return result

然后,根据时间和日志级别去日志库查询数据

def get_data():
select_time = calculate_time()
logger.info("select time:"+select_time)
sql = "select file_name,message from logsdb.app_logs_record " \
"where log_time >"+"'"+select_time+"'" \
"and level="+"'ERROR'" \
"order by log_time desc"
conn = get_con()

cursor = conn.cursor()
cursor.execute(sql)
results = cursor.fetchall()

cursor.close()
conn.close()

return results

发送邮件
使用python发送邮件比较简单,使用标准库smtplib就可以
这里使用163邮箱进行发送,你可以使用其他邮箱或者企业邮箱都行,不过host和port要设置正确。

def send_email(content):

sender = "[email protected]"
receiver = ["[email protected]", "[email protected]"]
host = 'smtp.163.com'
port = 465
msg = MIMEText(content)
msg['From'] = "[email protected]"
msg['To'] = "[email protected],[email protected]"
msg['Subject'] = "system error warning"

try:
smtp = smtplib.SMTP_SSL(host, port)
smtp.login(sender, '123456')
smtp.sendmail(sender, receiver, msg.as_string())
logger.info("send email success")
except Exception, e:
logger.error(e)

定时任务
使用一个单独的线程,每2分钟扫描一次,如果ERROR级别的日志条数超过5条,就发邮件通知。

def task():
while True:
logger.info("monitor running")

results = get_data()
if results is not None and len(results) > 5:
content = "recharge error:"
logger.info("a lot of error,so send mail")
for r in results:
content += r[1]+'\n'
send_email(content)
sleep(2*60)

日志
为这个小小的脚本配置一下日志log.py,让日志可以输出到文件和控制台中。
# coding=utf-8
import logging

logger = logging.getLogger('mylogger')
logger.setLevel(logging.DEBUG)

fh = logging.FileHandler('monitor.log')
fh.setLevel(logging.INFO)

ch = logging.StreamHandler()
ch.setLevel(logging.INFO)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)

logger.addHandler(fh)
logger.addHandler(ch)

所以,最后,这个监控小程序就是这样的app_monitor.py
# coding=utf-8
import threading
import MySQLdb
from datetime import datetime
import time
import smtplib
from email.mime.text import MIMEText
from log import logger

def get_con():
host = "127.0.0.1"
port = 3306
logsdb = "logsdb"
user = "root"
password = "never tell you"
con = MySQLdb.connect(host=host, user=user, passwd=password, db=logsdb, port=port, charset="utf8")
return con

def calculate_time():

now = time.mktime(datetime.now().timetuple())-60*2
result = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now))
return result

def get_data():
select_time = calculate_time()
logger.info("select time:"+select_time)
sql = "select file_name,message from logsdb.app_logs_record " \
"where log_time >"+"'"+select_time+"'" \
"and level="+"'ERROR'" \
"order by log_time desc"
conn = get_con()

cursor = conn.cursor()
cursor.execute(sql)
results = cursor.fetchall()

cursor.close()
conn.close()

return results

def send_email(content):

sender = "[email protected]"
receiver = ["[email protected]", "[email protected]"]
host = 'smtp.163.com'
port = 465
msg = MIMEText(content)
msg['From'] = "[email protected]"
msg['To'] = "[email protected],[email protected]"
msg['Subject'] = "system error warning"

try:
smtp = smtplib.SMTP_SSL(host, port)
smtp.login(sender, '123456')
smtp.sendmail(sender, receiver, msg.as_string())
logger.info("send email success")
except Exception, e:
logger.error(e)

def task():
while True:
logger.info("monitor running")
results = get_data()
if results is not None and len(results) > 5:
content = "recharge error:"
logger.info("a lot of error,so send mail")
for r in results:
content += r[1]+'\n'
send_email(content)
time.sleep(2*60)

def run_monitor():
monitor = threading.Thread(target=task)
monitor.start()

if __name__ == "__main__":
run_monitor()

运行脚本
脚本在服务器上运行,使用supervisor进行管理。
在服务器(centos6)上安装supervisor,然后在/etc/supervisor.conf中加入一下配置:

复制代码代码如下:
[program:app-monitor]
command = python /root/monitor/app_monitor.py
directory = /root/monitor
user = root

然后在终端中运行supervisord启动supervisor。
在终端中运行supervisorctl,进入shell,运行status查看脚本的运行状态。
总结
这个小监控思路很清晰,还可以继续修改,比如:监控特定的接口,发送短信通知等等。
因为有日志库,就少了去线上正式环境扫描日志的麻烦,所以,如果没有日志库,就要自己上线上环境扫描,在正式线上环境一定要小心哇~

3. 如何实现Python服务器性能监控

其实你完全可以使用现成的工具:linux系统的话,监控工具比较好的有ganglia,zabbix windows系统的话,用自带的“性能监视器”(老版本的windows叫性能计数器)

4. 请问大佬有Glances(硬件监控工具) V3.1.7 官方版软件免费百度云资源吗

链接:

提取码:468s

软件名称:Glances(硬件监控工具)V3.1.7官方版

语言:简体中文

大小:6.74MB

类别:系统工具

介绍:Glances是一个跨平台的监控工具,glances是一个基于python语言开发,可以为linux或者UNIX性能提供监视和分析性能数据的功能。glances在用户的终端上显示重要的系统信息,并动态的进行更新,让管理员实时掌握系统资源的使用情况!

5. 如何Zabbix和Python脚本批量监控网站性能指标

带界面的工具 1、MySQL可视化工具 这些工具都可以免费使用: a、MySQL查询浏览器(MySQL Query Browser):这个不用说了… b、MySQL管理员(MySQL Administrator):功能集中在服务器管理上,所以它最适合DBA使用,

6. 如何用python编写一个服务器状态监控脚本

其实你完全可以使用现成的工具:
linux系统的话,监控工具比较好的有ganglia,zabbix
windows系统的话,用自带的“性能监视器”(老版本的windows叫性能计数器)

7. 怎么用python实现电脑cpu温度监控

def monitor_process(key_word, cmd):
p1 = subprocess.Popen(['ps',
'-ef'], stdout=subprocess.PIPE)
p2 = subprocess.Popen(['grep', key_word],
stdin=p1.stdout, stdout=subprocess.PIPE)
p3 = subprocess.Popen(['grep',
'-v', 'grep'], stdin=p2.stdout, stdout=subprocess.PIPE)

lines = p3.stdout.readlines()
if len(lines) > 0:

return

sys.stderr.write('process[%s] is lost, run [%s]\n' % (key_word,
cmd))
subprocess.call(cmd, shell=True)

8. 使用python,在linux上监控远程windows的CPU、硬盘、内存使用率

你需要安装wmic,它实现了linux下能使用wmi,安装以后就可以用了,下面是例子。
import wmi_client_wrapper as wmi
wmic = wmi.WmiClientWrapper(
username="Administrator",
password="password",
host="192.168.1.149",
)
output = wmic.query("SELECT * FROM Win32_Processor")

9. 如何用python做一个设备运维软件

Python开发的jumpserver跳板机

使用python语言编写的调度和监控工作流的平台内部用来创建、监控和调整数据管道。任何工作流都可以在这个使用Python来编写的平台上运行。

企业主要用于解决:通俗点说就是规范运维的操作,加入审批,一步一步操作的概念。

是一种允许工作流开发人员轻松创建、维护和周期性地调度运行工作流(即有向无环图或成为DAGs)的工具。这些工作流包括了如数据存储、增长分析、Email发送、A/B测试等等这些跨越多部门的用例。

这个平台拥有和 Hive、Presto、MySQL、HDFS、Postgres和S3交互的能力,并且提供了钩子使得系统拥有很好地扩展性。除了一个命令行界面,该工具还提供了一个基于Web的用户界面让您可以可视化管道的依赖关系、监控进度、触发任务等。

来个小总结

10. 怎样对持续运行的python程序进行性能分析监控

不可能吧,你可以写一个类似于django中间件的东西做性能分析,然后将结果写入日志
这样做容易一些

阅读全文

与python性能监控工具相关的资料

热点内容
gz压缩文件夹 浏览:177
字母h从右往左跑的c语言编程 浏览:127
安卓手机如何拥有苹果手机横条 浏览:765
业余编程语言哪个好学 浏览:137
按照文件夹分个压缩 浏览:104
航空工业出版社单片机原理及应用 浏览:758
如何在电信app上绑定亲情号 浏览:376
安卓的怎么用原相机拍月亮 浏览:805
配音秀为什么显示服务器去配音了 浏览:755
c盘清理压缩旧文件 浏览:325
app怎么交付 浏览:343
图虫app怎么才能转到金币 浏览:175
如何做征文app 浏览:446
用什么app管理斐讯 浏览:169
安卓如何下载宝可梦剑盾 浏览:166
编译器开发属于哪个方向 浏览:940
megawin单片机 浏览:687
以色列加密货币监督 浏览:909
程序员前端现在怎么样 浏览:499
服务器和接口地址ping不通 浏览:557