【MySQL】关于MySQL错误日志信息的收集
2013-12-11 11:12
501 查看
为方便维护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 []
收集到的信息可以发邮件通知相关人员,也可以放到网站中方便浏览,这样在一定程度上减少了错误跟踪的时间。
相关文章推荐
- 【MySQL】关于MySQL错误日志信息的收集
- 详解grep获取MySQL错误日志信息的方法
- 安装解压版本的MySQL,安装过程中的常见命令,检查windows系统错误日志的方式来检查MySQL启动错误,关于Fatal error: Can't open and lock privilege
- 安装解压版本的MySQL,安装过程中的常见命令,检查windows系统错误日志的方式来检查MySQL启动错误,关于Fatal error: Can't open and lock privilege
- 【MYSQL】关于MYSQL数据库ibdata1文件缩小及错误日志清除方法
- 错误日志中关于innodb的问题收集
- 关于Mysql错误:./bin/mysqld_safe --user=mysql& [1] 32710 121003 16:40:22 mysqld_safe Logging to '/var/log/mysqld.log'. 121003 16:40:22 mysqld_s
- 关于MySQL的慢查询日志
- 错误收集(本地Log日志)错误上传
- 设置和查询mysql错误日志、访问日志
- 关于POSIX消息队列mq_open提示错误信息:Invalid argument的问题
- php错误信息回显和错误日志
- MySQL的一个错误信息
- Sosreport:收集系统日志和诊断信息的工具
- bash - 将正常输出和错误信息保存到日志文件,同时在终端输出
- 关于MySQL的慢查询日志
- spring异步存储错误信息日志
- DedeCMS漏洞之mysql_error_trace.inc日志信息
- 设备出现错误或设备脱机。详细信息请参阅 SQL Server 错误日志。
- Mysql错误信息提示