新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
詳解grep獲取MySQL錯(cuò)誤日志信息的方法
為方便維護(hù)MySQL,寫了個(gè)腳本用以提供收集錯(cuò)誤信息的接口。這些錯(cuò)誤信息來(lái)自與MySQL錯(cuò)誤日志,而 通過(guò)grep mysql可以獲取error-log的路徑。
為龍海等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及龍海網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站制作、成都網(wǎng)站制作、龍海網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
以下是全部相關(guān)代碼:
#!/usr/bin/env python2.7 #-*- encoding: utf-8 -*- """ 該模塊用于提取每天mysql日志中的異?;蝈e(cuò)誤信息 author: xiaomo email: [email protected] """ import os import sys import string from datetime import * # 預(yù)設(shè)字符解碼器為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 """ 獲取當(dāng)前mysql實(shí)例的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錯(cuò)誤日志中包含異?;蝈e(cuò)誤信息的行 """ 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錯(cuò)誤日志信息 """ 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 []
有興趣的朋友們參考學(xué)習(xí)下,感謝大家對(duì)創(chuàng)新互聯(lián)的支持。
網(wǎng)站欄目:詳解grep獲取MySQL錯(cuò)誤日志信息的方法
標(biāo)題網(wǎng)址:http://fisionsoft.com.cn/article/ieooso.html