To facilitate the maintenance of MySQL, a script was written to provide an interface for collecting error information. These error messages come from the MySQL error log, and the path of the error log can be obtained by grep mysql. Here is all the relevant code: #!/usr/bin/env python2.7 #-*- encoding: utf-8 -*- """ This module is used to extract abnormal or error information from the daily MySQL log author: xiaomo email: [email protected] """ import os import sys import string from datetime import * # Default character decoder is 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 """ Get the error_log file path of the current MySQL instance""" 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 """ Read the line containing exception or error information in the MySQL error log""" 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 """ Assemble and return mysql error log information""" 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 [] Friends who are interested can refer to it for learning. Thank you for your support of 123WORDPRESS.COM. You may also be interested in:
|
<<: Implementation of Redis one master, two slaves and three sentinels based on Docker
>>: Vue Beginner's Guide: Creating the First Vue-cli Scaffolding Program
When nginx configures proxy_pass, the difference ...
Storage Engine What is a database storage engine?...
Operating system: Alibaba Cloud ESC instance cent...
Table of contents First we need to build the page...
1. Import the module and define a validation stat...
Preface In the past, I always switched Python ver...
This article shares the installation tutorial of ...
I re-read the source code of the Fabric project a...
Navicat reports errors 10060 and 1045 when connec...
There are two ways to deploy Angular projects wit...
Description Solution VMware 15 virtual machine br...
npm installation tutorial: 1. Download the Node.j...
MySQL installation tutorial, for your reference, ...
One day, the leader put forward a requirement to ...
Table of contents Find the problem 1. How to remo...