为方便维护MySQL,写了个脚本用以提供收集错误信息的接口。这些错误信息来自与MySQL错误日志,而 通过grep mysql可以获取error-log的路径。
以下是全部相关代码:
#!/usr/bin/env python2.7
#-*- encoding: utf-8 -*-
"""
该模块用于提取每天mysql日志中的异常或错误信息
author: xiaomo
email: moxiaomomo@gmail.com
"""
import os
import sys
import string
from datetime import *
# 預設字符解碼器為utf-8
reload(sys)
sys.setdefaultencoding('utf-8')
COMMON_FLAGS = ["error", "exception", "fail", "crash", "repair"]
def _contain_flag(cur_str):
for flag in COMMON_FLAGS:
if flag in string.lower(cur_str):
return True
return False
"""
获取当前mysql实例的error_log文件路径
"""
def _get_mysql_error_log_path():
log_path = ''
grep_infos = os.popen('ps aux | grep mysql | grep "log-error"').read()
if len(grep_infos) > 1:
grep_infos = grep_infos.split("log-error=")
if len(grep_infos) > 1:
grep_infos = grep_infos[1].split(' ')
if len(grep_infos) > 1:
log_path = grep_infos[0]
return log_path
"""
读取mysql错误日志中包含异常或错误信息的行
"""
def _get_error_info(error_log, begin_date):
error_infos = []
f = open(error_log, 'r')
lines = f.readlines()
for line in lines:
data_array = line.split(' ')
if len(data_array) > 0 and len(data_array[0]) == 10:
dt_strs = data_array[0].split('-')
cur_date = date(int(dt_strs[0]), int(dt_strs[1]), int(dt_strs[2]))
if cur_date >= begin_date and _contain_flag(line):
error_infos.append(line)
f.close()
return error_infos
"""
组装并返回mysql错误日志信息
"""
def get_mysql_errors(begin_date=date.today()-timedelta(1)):
try:
err_log_path = _get_mysql_error_log_path()
if len(err_log_path) > 1:
return _get_error_info(err_log_path, begin_date)
except Exception,e:
print "[get_mysql_errors]%s"%e
return []
有兴趣的朋友们参考学习下,感谢大家对的支持。
标签:
grep,MySQL
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“详解grep获取MySQL错误日志信息的方法”评论...
更新日志
2025年11月05日
2025年11月05日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]