Python简单巡检MySQL
时间:2021-07-01 10:21:17
帮助过:8人阅读
!/usr/bin/env python
# -*- coding: utf-8 -*-
#----------------------------------------------------------#
# Date : xxxx-xx-xx #
# Author : Created by zhouwanchun. #
# Wechat : loveoracle11g #
# Function: This scripts function is ... #
# Version : 1.1 #
#----------------------------------------------------------#
# 导入模块
import os
import subprocess
import mysql.connector
import myloginpath
# Linux终端清屏
os.system(
‘clear‘)
# 注释信息
print(
"""\033[1;36m
############################################################
# Date : 2020-05-22 #
# Author : Created by zhouwanchun. #
# Wechat : loveoracle11g #
# Function: This scripts function is ... #
# Version : v1.1 #
############################################################
\033[0m""")
# 连接数据库
mylogin = myloginpath.parse(
‘rds_dba‘)
# print(mylogin, type(mylogin))
conn = mysql.connector.connect(**
mylogin)
# 创建SQL命令通道
sql_cmd =
conn.cursor()
# SQL语句
### 检查实例参数
sql1 =
"show global variables;"
### 检查每个库的库大小
sql2 =
‘select TABLE_SCHEMA as "数据库",truncate(SUM(DATA_LENGTH)/1024/1024, 2) as "大小(MB)" from information_schema.tables where TABLE_SCHEMA not in ("mysql", "information_schema", "performance_schema", "sys") group by TABLE_SCHEMA;‘
sql_cmd.execute(sql1)
sql1_result =
dict(sql_cmd.fetchall())
# 检查MySQL版本
print(
"\033[1;32m当前数据库的版本是:\033[0m" + sql1_result[
‘version‘])
# 检查MySQL端口
print(
"\033[1;35m当前数据库的端口是:\033[0m" + sql1_result[
‘port‘])
# 检查server_id
print(
"\033[1;32m当前数据库的server_id是:\033[0m" + sql1_result[
‘server_id‘])
# 检查basedir目录
print(
"\033[1;32m当前数据库的basedir在:\033[0m" + sql1_result[
‘basedir‘])
# 检查datadir目录
print(
"\033[1;35m当前数据库的datadir在:\033[0m" + sql1_result[
‘datadir‘])
# 检查tmpdir目录
print(
"\033[1;32m当前数据库的tmpdir在:\033[0m" + sql1_result[
‘tmpdir‘])
# 检查pid_file
print(
"\033[1;32m当前数据库的pid_file在:\033[0m" + sql1_result[
‘pid_file‘])
# 检查socket
print(
"\033[1;35m当前数据库的socket在:\033[0m" + sql1_result[
‘socket‘])
# 检查log_error
print(
"\033[1;32m当前数据库的log_error在:\033[0m" + sql1_result[
‘log_error‘])
# 检查binlog
print(
"\033[1;32m当前数据库是否启用记录binlog:\033[0m" + sql1_result[
‘log_slave_updates‘])
print(
"\033[1;32m当前数据库的binlog格式为:\033[0m" + sql1_result[
‘binlog_format‘])
print(
"\033[1;32m当前数据库的binlog文件在:\033[0m" + sql1_result[
‘log_bin‘])
print(
"\033[1;32m当前数据库的binlog默认保留天数为:\033[0m" + sql1_result[
‘expire_logs_days‘])
print(
"\033[1;35m当前数据库的binlog刷写策略sync_binlog:\033[0m" + sql1_result[
‘sync_binlog‘])
# 检查gtid
print(
"\033[1;32m当前数据库是否启用gtid模式gtid_mode:\033[0m" + sql1_result[
‘gtid_mode‘])
print(
"\033[1;32m当前数据库是否开启gtid安全限制enforce_gtid_consistency:\033[0m" + sql1_result[
‘enforce_gtid_consistency‘])
# 检查relay_log
print(
"\033[1;32m当前数据库的relay_log文件在:\033[0m" + sql1_result[
‘relay_log‘])
# 检查slow_query_log
print(
"\033[1;32m当前数据库是否启用slow_query_log:\033[0m" + sql1_result[
‘slow_query_log‘])
print(
"\033[1;32m当前数据库慢查询时间为:long_query_time:\033[0m" + sql1_result[
‘long_query_time‘])
print(
"\033[1;32m当前数据库慢日志在slow_query_log_file:\033[0m" + sql1_result[
‘slow_query_log_file‘])
# 检查general_log
print(
"\033[1;32m当前数据库是否启用general_log:\033[0m" + sql1_result[
‘general_log‘])
print(
"\033[1;32m当前数据库general_log文件在general_log_file:\033[0m" + sql1_result[
‘general_log_file‘])
# 检查数据库实例字符集
print(
"\033[1;32m当前MySQL服务器实例字符集是:\033[0m" + sql1_result[
‘character_set_server‘])
print(
"\033[1;32m当前MySQL数据库字符集是:\033[0m" + sql1_result[
‘character_set_database‘])
print(
"\033[1;32m当前MySQL客户端字符集是:\033[0m" + sql1_result[
‘character_set_client‘])
print(
"\033[1;32m当前MySQL客户端连接字符集是:\033[0m" + sql1_result[
‘character_set_connection‘])
print(
"\033[1;32m当前MySQL客户端返回结果字符集是:\033[0m" + sql1_result[
‘character_set_results‘])
print(
"\033[1;32m当前系统字符集是:\033[0m" + sql1_result[
‘character_set_system‘])
print(
"\033[1;32m当前文件系统字符集是:\033[0m" + sql1_result[
‘character_set_filesystem‘])
# 检查skip_name_resolve
print(
"\033[1;32m是否禁用域名解析skip_name_resolve:\033[0m" + sql1_result[
‘skip_name_resolve‘])
# 检查只读模式
print(
"\033[1;35m是否开启只读模式read_only:\033[0m" + sql1_result[
‘read_only‘])
print(
"\033[1;35m是否开启只读全局模式super_read_only:\033[0m" + sql1_result[
‘super_read_only‘])
# 检查时区设置
print(
"\033[1;32m当前数据库时区设置为time_zone:\033[0m" + sql1_result[
‘time_zone‘])
# 检查load data导入导出目录
print(
"\033[1;32m当前load data导入导出目录secure_file_priv:\033[0m" + sql1_result[
‘secure_file_priv‘])
# 检查连接数
print(
"\033[1;35m当前最大连接数max_connections:\033[0m" + sql1_result[
‘max_connections‘])
print(
"\033[1;35m当前最大用户连接数max_user_connections:\033[0m" + sql1_result[
‘max_user_connections‘])
print(
"\033[1;32m当前最大错误连接数max_connect_errors:\033[0m" + sql1_result[
‘max_connect_errors‘])
# 检查超时连接
print(
"\033[1;35m交互式超时连接时间interactive_timeout:\033[0m" + sql1_result[
‘interactive_timeout‘])
print(
"\033[1;35m非交互式超时时间wait_timeout:\033[0m" + sql1_result[
‘wait_timeout‘])
# 检查InnoDB
print(
"\033[1;35m当前实例默认的存储引擎为default_storage_engine:\033[0m" + sql1_result[
‘default_storage_engine‘])
# 检查隔离级别
print(
"\033[1;35m当前实例默认的隔离级别为transaction_isolation:\033[0m" + sql1_result[
‘transaction_isolation‘])
# 检查系统表空间
print(
"\033[1;33m当前实例系统表空间在innodb_data_home_dir:\033[0m" + sql1_result[
‘innodb_data_home_dir‘])
print(
"\033[1;33m当前实例ibdata1路径及大小:\033[0m" + sql1_result[
‘innodb_data_file_path‘])
print(
"\033[1;33m当前实例ibtmp1路径及大小:\033[0m" + sql1_result[
‘innodb_temp_data_file_path‘])
print(
"\033[1;33m当前实例innodb_buffer_pool_filename:\033[0m" + sql1_result[
‘innodb_buffer_pool_filename‘])
print(
"\033[1;35m当前实例是否启用独立表空间innodb_file_per_table:\033[0m" + sql1_result[
‘innodb_file_per_table‘])
print(
"\033[1;35m当前实例innodb_buffer_pool_size:\033[0m" + str(int(sql1_result[
‘innodb_buffer_pool_size‘])/1024/1024
))
# 检查redo_log
print(
"\033[1;35m当前数据库redo_log文件在:\033[0m" + sql1_result[
‘innodb_log_group_home_dir‘])
print(
"\033[1;35m当前数据库redo_log文件有:\033[0m" + sql1_result[
‘innodb_log_files_in_group‘] +
"\033[1;32m 组\033[0m")
print(
"\033[1;35m当前数据库每组redo_log文件大小为:\033[0m" + str(int(sql1_result[
‘innodb_log_file_size‘])/1024/1024
))
print(
"\033[1;33m当前实例redo_log_buffer大小为:\033[0m" + str(int(sql1_result[
‘innodb_log_buffer_size‘])/1024/1024
))
print(
"\033[1;35m当前实例redo_log文件刷写策略是innodb_flush_log_at_trx_commit:\033[0m" + sql1_result[
‘innodb_flush_log_at_trx_commit‘])
print(
"\033[1;35m当前实例打开数据文件及redo_log文件的模式innodb_flush_method:\033[0m" + sql1_result[
‘innodb_flush_method‘])
# 检查undo log
print(
"\033[1;33m当前数据库undo表空间在:\033[0m" + sql1_result[
‘innodb_undo_directory‘])
print(
"\033[1;33m当前数据库undo表空间有:\033[0m" + sql1_result[
‘innodb_undo_tablespaces‘] +
"\033[1;32m 组\033[0m")
# 检查查询缓存
print(
"\033[1;35m当前实例是否打开查询缓存query_cache_type:\033[0m" + sql1_result[
‘query_cache_type‘])
print(
‘\n‘)
print(
"\033[1;32m当前实例每个库的容量大小\033[0m")
subprocess.run([‘/usr/local/mysql/bin/mysql --login-path=rds_dba -e ‘ +
"‘" + sql2 +
"‘"], shell=
True)
sql_cmd.close()
conn.commit()
conn.close()
Python简单巡检MySQL
标签:log exe 文件系统 filename comm sock 模块 导入导出 etc