The editor below will bring you a mysql inspection script (a must-read). The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.
is as follows:
#!/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=''' <p class="stage-list"> <p class="stage-title"><span>{0}</span></p> <p 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> </p> </p>\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=''' <p class="list-item"> <p class="category"> <span>磁盘</span> </p> <p class="second-stage">\n''' for index in range(len(self.disks)): result=result+self.disks[index].str() result=result+''' </p> </p>\n''' return result class cpuResources(object): def init(self,status): self.status=status def str(self): result=''' <p class="list-item"> <p class="category"> <span>CPU</span> </p> <p class="second-stage"> <p class="stage-list"> <p class="stage-title"><span>global</span></p> <p 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> </p> </p> </p> </p>\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=''' <p class="list-item"> <p class="category"> <span>MEM</span> </p> <p class="second-stage"> <p class="stage-list"> <p class="stage-title"><span>global</span></p> <p 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> </p> </p> </p> </p>'''.format(self.status['total'],self.status['avariable']) return result class mysqlResources(object): def init(self,status): self.status=status def str(self): result=''' <p class="list-item"> <p class="category"> <span>MYSQL</span> </p> <p class="second-stage"> <p class="stage-list"> <p class="stage-title"><span>{0}</span></p> <p 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> </p> </p> </p> </p>'''.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> <p class="content"> <p class="list-item"> <p class="top-title">report_title</p> </p>\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+''' </p> </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='123456',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))
The above is the detailed content of Detailed introduction to the case code of mysql inspection script. For more information, please follow other related articles on the PHP Chinese website!

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

There are four main index types in MySQL: B-Tree index, hash index, full-text index and spatial index. 1.B-Tree index is suitable for range query, sorting and grouping, and is suitable for creation on the name column of the employees table. 2. Hash index is suitable for equivalent queries and is suitable for creation on the id column of the hash_table table of the MEMORY storage engine. 3. Full text index is used for text search, suitable for creation on the content column of the articles table. 4. Spatial index is used for geospatial query, suitable for creation on geom columns of locations table.

TocreateanindexinMySQL,usetheCREATEINDEXstatement.1)Forasinglecolumn,use"CREATEINDEXidx_lastnameONemployees(lastname);"2)Foracompositeindex,use"CREATEINDEXidx_nameONemployees(lastname,firstname);"3)Forauniqueindex,use"CREATEU

The main difference between MySQL and SQLite is the design concept and usage scenarios: 1. MySQL is suitable for large applications and enterprise-level solutions, supporting high performance and high concurrency; 2. SQLite is suitable for mobile applications and desktop software, lightweight and easy to embed.

Indexes in MySQL are an ordered structure of one or more columns in a database table, used to speed up data retrieval. 1) Indexes improve query speed by reducing the amount of scanned data. 2) B-Tree index uses a balanced tree structure, which is suitable for range query and sorting. 3) Use CREATEINDEX statements to create indexes, such as CREATEINDEXidx_customer_idONorders(customer_id). 4) Composite indexes can optimize multi-column queries, such as CREATEINDEXidx_customer_orderONorders(customer_id,order_date). 5) Use EXPLAIN to analyze query plans and avoid

Using transactions in MySQL ensures data consistency. 1) Start the transaction through STARTTRANSACTION, and then execute SQL operations and submit it with COMMIT or ROLLBACK. 2) Use SAVEPOINT to set a save point to allow partial rollback. 3) Performance optimization suggestions include shortening transaction time, avoiding large-scale queries and using isolation levels reasonably.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)
