#!/usr/bin/env python3.5

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=''' <div class="stage-list">
<div class="stage-title"><span>{0}</span></div>
<div class="detail">
<p class="detail-list">
<span class="detail-title">区分格式</span>
<span class="detail-describe">{1}</span>
</p>
<p class="detail-list">
<span class="detail-title">总空间大小</span>
<span class="detail-describe">{2:8.2f}G</span>
</p>
<p class="detail-list">
<span class="detail-title">空闲空间(%)</span>
<span class="detail-describe">{3:8.2f}</span>
</p>
<p class="detail-list"> </p>
</div>
</div>\n'''.format(self.mount_point,self.status['fstype'],self.status['total'],self.status['used_percent'])
return result class diskResources(object):
def __init__(self,status):
self.disks=[]
for mount_point in status.keys():
self.disks.append(diskResource(mount_point,status[mount_point])) def __str__(self):
result=''' <div class="list-item">
<div class="category">
<span>磁盘</span>
</div>
<div class="second-stage">\n'''
for index in range(len(self.disks)):
result=result+self.disks[index].__str__()
result=result+''' </div>
</div>\n'''
return result class cpuResources(object):
def __init__(self,status):
self.status=status
def __str__(self):
result=''' <div class="list-item">
<div class="category">
<span>CPU</span>
</div>
<div class="second-stage">
<div class="stage-list">
<div class="stage-title"><span>global</span></div>
<div class="detail">
<p class="detail-list">
<span class="detail-title">用户空间使用(%)</span>
<span class="detail-describe">{0}</span>
</p>
<p class="detail-list">
<span class="detail-title">内核空间使用(%)</span>
<span class="detail-describe">{1}</span>
</p>
<p class="detail-list">
<span class="detail-title">空闲(%)</span>
<span class="detail-describe">{2}</span>
</p>
<p class="detail-list">
<span class="detail-title">硬中断(%)</span>
<span class="detail-describe">{3}</span>
</p>
<p class="detail-list">
<span class="detail-title">软中断(%)</span>
<span class="detail-describe">{4}</span>
</p>
<p class="detail-list">
<span class="detail-title">io等待(%)</span>
<span class="detail-describe">{5}</span>
</p>
<p class="detail-list"> </p>
</div>
</div>
</div>
</div>\n'''.format(self.status['user'],self.status['system'],self.status['idle'],self.status['hardirq'],self.status['softirq'],self.status['iowait'])
return result class memResources(object):
def __init__(self,status):
self.status=status def __str__(self):
result=''' <div class="list-item">
<div class="category">
<span>MEM</span>
</div>
<div class="second-stage">
<div class="stage-list">
<div class="stage-title"><span>global</span></div>
<div class="detail">
<p class="detail-list">
<span class="detail-title">总大小</span>
<span class="detail-describe">{0:8.2f}G</span>
</p>
<p class="detail-list">
<span class="detail-title">空闲大小</span>
<span class="detail-describe">{1:8.2f}G</span>
</p> <p class="detail-list"> </p>
</div>
</div>
</div>
</div>'''.format(self.status['total'],self.status['avariable'])
return result class mysqlResources(object):
def __init__(self,status):
self.status=status
def __str__(self):
result=''' <div class="list-item">
<div class="category">
<span>MYSQL</span>
</div>
<div class="second-stage">
<div class="stage-list">
<div class="stage-title"><span>{0}</span></div>
<div class="detail">
<p class="detail-list">
<span class="detail-title">innodb_log_wait</span>
<span class="detail-describe">{1}</span>
</p>
<p class="detail-list">
<span class="detail-title">binlog_cache_use</span>
<span class="detail-describe">{2}</span>
</p>
<p class="detail-list">
<span class="detail-title">create_temp_disk_table</span>
<span class="detail-describe">{3}</span>
</p>
<p class="detail-list">
<span class="detail-title">Slow_querys</span>
<span class="detail-describe">{4}</span>
</p> <p class="detail-list"> </p>
</div>
</div>
</div>
</div>'''.format(self.status['port'],self.status['Innodb_log_waits'],self.status['Binlog_cache_use'],
self.status['Created_tmp_disk_tables'],self.status['Slow_queries']) return result class hostResources(object):
def __init__(self,cpu_info,mem_info,disk_info,mysql_info,report_title='MySQL巡检报告'):
self.cpu=cpuResources(cpu_info)
self.mem=memResources(mem_info)
self.disk=diskResources(disk_info)
self.mysql=mysqlResources(mysql_info)
self.report_title=report_title
def __str__(self):
result='''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>巡检报告</title>
<style>
*{
margin: 0;
padding: 0;
}
.content{
width:1000px;
height: auto;
margin: 30px auto;
border-bottom:1px solid #b2b2b2;
}
.list-item{
border:1px solid #b2b2b2;
border-bottom: none;
transition: all .35s;
overflow: hidden;
display: flex;
}
.list-item:empty{
display: none;
}
.top-title{
line-height: 32px;
font-size: 16px;
color: #333;
text-indent: 10px;
font-weight: 600;
}
.category{
width:97px;
height: auto;
border-right: 1px solid #b2b2b2;
float: left;
text-align: center;
position: relative;
}
.stage-title>span,
.category>span{
display: block;
height: 20px;
width:100%;
text-align: center;
line-height: 20px;
position: absolute;
top: 50%;
margin-top: -10px;left: 0;
}
.second-stage{
width:900px;
float: left;
}
.stage-list{
border-bottom: 1px solid #b2b2b2;
display: flex;
}
.stage-list:last-child{
border-bottom: 0;
}
.stage-title{
width:99px;
border-right: 1px solid #b2b2b2;
position: relative;
}
.detail{
flex: 1;
}
.detail-list{
border-bottom: 1px solid #b2b2b2;
height: 40px;
display: flex;
transition: all .35s;
}
.detail-title{
padding: 10px;
height: 20px;
line-height: 20px;
border-right: 1px solid #b2b2b2;
width:200px;
}
.detail-describe{
flex: 1;
padding: 10px;line-height: 20px;
}
.detail-list:last-child{
border-bottom: 0;
}
.list-item:hover{
background-color: #eee;
}
.detail-list:hover{
background-color: #d1d1d1;
}
</style>
</head>
<body>
<div class="content">
<div class="list-item">
<p class="top-title">report_title</p>
</div>\n''' result=result.replace('report_title',self.report_title)
result=result+self.cpu.__str__()
result=result+self.mem.__str__()
result=result+self.disk.__str__()
result=result+self.mysql.__str__()
result=result+''' </div>
</body>
</html>'''
return result if __name__=="__main__":
parser=argparse.ArgumentParser()
parser.add_argument('--verbose',type=int,default=1,help='verbose for output')
parser.add_argument('--user',default='chkuser',help='user name for connect to mysql')
parser.add_argument('--password',default='',help='user password for connect to mysql')
parser.add_argument('--host',default='127.0.0.1',help='mysql host ip')
parser.add_argument('--port',default=3306,type=int,help='mysql port')
parser.add_argument('--int-status',default=('Com_select,Com_insert,Com_update,Com_delete,Innodb_log_waits,'
'Binlog_cache_disk_use,Binlog_cache_use,Created_tmp_disk_tables,'
'Slow_queries')
,help='mysql status its value like int')
parser.add_argument('--report-title',default='MySQL巡检报告',help='report title')
parser.add_argument('--output-dir',default='/tmp/',help='default report file output path')
args=parser.parse_args()
cpu_info=get_cpu_info(args.verbose)
mem_info=get_mem_info(args.verbose)
disk_info=get_disk_info(args.verbose)
status_list=[ IntStatus(name=item) for item in args.int_status.split(',')]
mysql_info=get_mysql_info(args,status_list)
#dr=diskResources(disk_info)
#cr=cpuResources(cpu_info)
#mr=memResources(mem_info)
#msr=mysqlResources(mysql_info)
hr=hostResources(cpu_info,mem_info,disk_info,mysql_info,args.report_title)
now=str(datetime.datetime.now()).replace(' ','^')
if args.output_dir.endswith('/') != True:
args.output_dir=args.output_dir+'/'
filename=args.output_dir+'mysql_inspection_{0}.html'.format(now)
with open(filename,'w') as output:
output.write(hr.__str__())
print('[report] the report been saved to {0} ok.... ....'.format(filename))

mysql巡检脚本的更多相关文章

  1. python编写的简单的mysql巡检脚本

    准备工作:1    安装python 3.5,本次使用源码安装.2    安装psutil模块,使用python3.5自带的easy_install包直接运行cd /opt/python3/bin./ ...

  2. 小麦苗数据库巡检脚本,支持Oracle、MySQL、SQL Server和PG等数据库

    目录 一.巡检脚本简介 二.巡检脚本特点 三.巡检结果展示 1.Oracle数据库 2.MySQL数据库 3.SQL Server数据库 4.PG数据库 5.OS信息 四.脚本运行方式 1.Oracl ...

  3. 巡检脚本OS+Oracle

    巡检脚本 主机巡检脚本:OSWatcher.sh Oracle巡检脚本:ORAWatcher.sh 脚本使用方法 1.建立脚本放置目录 # mkdir /var/collect 2.把脚本ORAWat ...

  4. 非常全面的SQL Server巡检脚本来自sqlskills团队的Glenn Berry 大牛

    非常全面的SQL Server巡检脚本来自sqlskills团队的Glenn Berry 大牛 Glenn Berry 大牛会对这个脚本持续更新 -- SQL Server 2012 Diagnost ...

  5. 主机巡检脚本:OSWatcher.sh

    主机巡检脚本:OSWatcher.sh 2016-09-26更新,目前该脚本只支持Linux操作系统,后续有需求可以继续完善. 注意: 经测试,普通用户执行脚本可以顺利执行前9项检查: 第10项,普通 ...

  6. Oracle巡检脚本:ORAWatcher.sh

    Oracle巡检脚本:ORAWatcher.sh #!/usr/bin/ksh echo "" echo "ORAWatcher Version:1.0.1" ...

  7. centos shell编程6一些工作中实践脚本 nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志 直接送给bc做计算 gzip innobackupex/Xtrabackup 第四十节课

    centos   shell编程6一些工作中实践脚本   nagios监控脚本 自定义zabbix脚本 mysql备份脚本 zabbix错误日志  直接送给bc做计算  gzip  innobacku ...

  8. mysql定时脚本(event),类似oracle的job

    mysql定时脚本(event),类似oracle的job   我有2张表:tb_push_data 和 tb_push_data_log 现在需要每隔一段时间将tb_push_data 符合条件的 ...

  9. Linux/hp unix/AIX日常巡检脚本(转)

    以下为Linux/hp unix/AIX日常巡检脚本,大家可以参考着进行改写,用于自己的服务器. #!/usr/bin/ksh syserrdate=`date +"%m/%d"` ...

随机推荐

  1. sql server 的ANSI_NULLS设置

    当 SET ANSI_NULLS 为 ON 时,表示SQL语句遵循SQL-92标准.当 SET ANSI_NULLS 为 OFF 时,表示不遵从 SQL-92 标准. SQL-92 标准要求对空值(N ...

  2. radio 和checkbox与文字对齐问题

    今天在项目中遇到radio和文字对齐问题(ie不明显,火狐和google比较明显),在此记录. 1.浏览器默认文字大小为14px,因而当文字字体为14px时radio和checkbox与文字对齐良好, ...

  3. Visual Studio Code 与 Github 集成

    使用Visual Studio Code进行Nodejs开发充满了便利,为了更好的进行开发工作,有必要使用Github进行代码管理. Visual Studio Code已经集成了GIT组件: htt ...

  4. poj1190 生日蛋糕 dfs

    题意:生日蛋糕有m层,总体积是V.从下向上,每一层的半径r和高度h都是递减的. 给m.v,求最小的表面积s.(不算底面接地的面积) 题目链接:poj1190 剪枝都还没加..样例输出都是错的...还没 ...

  5. webservice(CXF)基于3.1.1版本实例

    引言 有没有一种办法可以实现跨应用程序进行通信和跨平台进行通信呢? 换句话说,就是有什么办法可以实现我的应用程序 A 可以和应用程序 B 进行通信呢? 或者说是,我用 Java 写的应用程序和用 . ...

  6. MVC三层架构编程(Dao、service、servlet 之间的关系)

    木哈哈~先开心一会儿,人生的第一篇博客aaa.我一定好好写.不过之前也没怎么看别人写过,还是有点小激动呢,加油.好好总结,会总结的宝宝才会有提高! 今天想总结一下mvc三层架构模型编程,宝宝学习不怎么 ...

  7. iOS应用内语言切换功能

    当我们的应用仅仅面向国内用户群,一般仅支持一种语言--中文就可以了.当面向国外用户时就需要进行国际化了,不仅仅是语言的转变,也可能包括设计风格,页面布局.交互效果的转变,如微信,微博,QQ这类应用都有 ...

  8. jquery图片滚动

    注:代码来自17sucai网,已去除部分冗余代码,只保留图片效果 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...

  9. AsMVC:一个简单的MVC框架的Java实现

    当初看了<从零开始写一个Java Web框架>,也跟着写了一遍,但当时学艺不精,真正进脑子里的并不是很多,作者将依赖注入框架和MVC框架写在一起也给我造成了不小的困扰.最近刚好看了一遍sp ...

  10. Hadoop安装测试简单记录

    安装的节点如下:1个namenode.1个hiveserver.3个dataNode192.168.1.139   namenode1192.168.1.146   hiveserver 192.16 ...