如何用python编写的简单的mysql巡检脚本,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
准备工作:
1 安装python 3.5,本次使用源码安装。
2 安装psutil模块,使用python3.5自带的easy_install包直接运行
cd /opt/python3/bin
./easy_install-3.5psuitl安装
3 安装mysql_connector模块,同样使用easy_install安装
easy_install-3.5 mysql-connector==2.1.4
准备工作完成后便可运行如下脚本:
[mysql@jing1 scripts]$ more healthcheck.py
#!/usr/bin/env python3
# -*- coding: gbk -*-
import psutil
import mysql.connector
import argparse
import json
import datetime
def get_cpu_info(verbose):
cpu_info={}
if verbose >0:
print(“[cpu] start collect cpu info …”)
data=psutil.cpu_times_percent(3)
cpu_info[‘user’]=data[0]
cpu_info[‘system’]=data[2]
cpu_info[‘idle’]=data[3]
cpu_info[‘iowait’]=data[4]
cpu_info[‘hardirq’]=data[5]
cpu_info[‘softirq’]=data[6]
cpu_info[‘cpu_cores’]=psutil.cpu_count()
if verbose >0:
print(“{0}”.format(json.dumps(cpu_info,ensure_ascii=False,indent=4)))
print(“[cpu] collection compeleted …”)
return cpu_info
def get_mem_info(verbose):
mem_info={}
if verbose >0:
print(“[mem] start collect mem info …”)
data=psutil.virtual_memory()
mem_info[‘total’]=data[0]/1024/1024/1024
mem_info[‘avariable’]=data[1]/1024/1024/1024
if verbose>0:
print(“{0}”.format(json.dumps(mem_info,ensure_ascii=False,indent=4))免费主机域名)
print(“[mem] collection compeletd …”)
return mem_info
def get_disk_info(verbose):
disk_info={}
if verbose >0:
print(“[disk] start collect disk info …”)
partitions=psutil.disk_partitions()
partitions=[(partition[1],partition[2])for partition in partitions if partition[2]!=’iso9660′]
disk_info={}
for partition in partitions:
disk_info[partition[0]]={}
disk_info[partition[0]][‘fstype’]=partition[1]
for mount_point in disk_info.keys():
data=psutil.disk_usage(mount_point)
disk_info[mount_point][‘total’]=data[0]/1024/1024/1024
disk_info[mount_point][‘used_percent’]=data[3]
if verbose >0:
print(“{0}”.format(json.dumps(disk_info,ensure_ascii=False,indent=4)))
print(“[disk] collection compeleted ….”)
return disk_info
def get_mysql_info(cnx_args,status_list):
config={
‘user’:cnx_args.user,
‘password’:cnx_args.password,
‘host’:cnx_args.host,
‘port’:cnx_args.port}
cnx=None
cursor=None
mysql_info={}
try:
cnx=mysql.connector.connect(**config)
cursor=cnx.cursor(prepared=True)
for index in range(len(status_list)):
status_list[index].get_status(cursor)
免费主机域名status=status_list[index]
mysql_info[status.name]=status.value
mysql_info[‘port’]=config[‘port’]
except mysql.connector.Error as err:
print(err)
finally:
if cursor != None:
cursor.close()
if cnx != None:
cnx.close()
return mysql_info
class Status(object):
def __init__(self,name):
self.name=name
self._value=None
def get_status(self,cursor):
stmt=”show global status like ‘{0}’;”.format(self.name)
cursor.execute(stmt)
value=cursor.fetchone()[1].decode(‘utf8’)
self._value=int(value)
@property
def value(self):
if self._value==None:
raise Exception(“cant get value befor execute the get_status function”)
else:
return self._value
IntStatus=Status
class diskResource(object):
def __init__(self,mount_point,status):
self.mount_point=mount_point
self.status=status
def __str__(self):
result=”’
range_format
{1}
total_space
{2:8.2f}G
used_space(%)
{3:8.2f}
user_space(%)
{0}
kernel_space(%)
{1}
space(%)
{2}
hard_cruppt(%)
{3}
soft_cruppt(%)
{4}
io_wait(%)
{5}
total
{0:8.2f}G
free
{1:8.2f}G
innodb_log_wait
{1}
binlog_cache_use
{2}
create_temp_disk_table
{3}
Slow_querys
{4}
report_title看完上述内容,你们掌握如何用python编写的简单的mysql巡检脚本的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注云技术行业资讯频道,感谢各位的阅读!
这篇文章给大家介绍Oracle数据库日常维护是怎么样的,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。在Oracle数据库运行期间,DBA应该对数据库的运行日志及表空间的使用情况进行监控,及早发现数据库中存在的问题。一、Oracle警告日志…