本篇文章给大家带来的内容是关于怎么通过grep 获取MySQL错误日志信息的方法,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
为方便维护MySQL,写了个脚本用以提供收集错误信息的接口。这些错误信息来自与MySQL错误日志,而 通过grep mysql可以获取error-log的路径。
#!/usr/bin/env python2.7#-*- encoding: utf-8 -*-"""该模块用于提取每天mysql日志中的异常或错误信息author: xiaomoemail: moxiaomomo@gmail.com"""import osimport sysimport stringfrom datetime import *# 預設字符解碼器為utf-8reload(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错误日志信息的方法代码示例的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/382057.html
微信扫一扫
支付宝扫一扫