Mysql inspection script (required), mysql inspection script

Source: Internet
Author: User
Tags mysql host

Mysql inspection script (required), mysql inspection script

As follows:

#! /Usr/bin/env python3.5import psutilimport mysql. connectorimport argparseimport jsonimport datetimedef 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 ['softidq'] = 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_infodef 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_infodef 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]/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_infodef 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'] Doesn't mysql. connector. error as err: print (err) finally: if cursor! = None: cursor. close () if cnx! = None: cnx. close () return mysql_infoclass 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.exe cute (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 func Tion ") else: return self. _ valueIntStatus = Statusclass 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"> Format differentiation </span> <span class = "detail- describe "> {1} </span> </p> <p cl Ass = "detail-list"> <span class = "detail-title"> total space size </span> <span class = "detail-describe" >{8 8. 2f} G </span> </p> <p class = "detail-list"> <span class = "detail-title"> free space (%) </span> <span class = "detail-describe"> {3: 8. 2f} </span> </p> <p class = "detail-list"> </p> </div> \ n '''. format (self. mount_point, self. status ['fstype '], self. status ['Total'], self. status ['used _ percent ']) return resultclass diskResou Rces (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> disk </span> </div> <div class =" second-stage "> \ n''' for index in range (len (self. disks): result = result + self. disks [index]. _ str _ () result = result + ''' </div> \ N'' 'Return resultclass 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"> user space usage (%) </span> <span class = "detai L-describe ">{ 0} </span> </p> <p class =" detail-list "> <span class =" detail-title "> kernel space usage (%) </span> <span class = "detail-describe"> {1} </span> </p> <p class = "detail-list"> <span class =" detail-title "> idle (%) </span> <span class = "detail-describe"> {2} </span> </p> <p class = "detail-list"> <span class =" detail-title "> hard interrupt (%) </span> <span class = "detail-describe" >{3 }</span> </p> <p class = "detail-list"> <span class =" Detail-title "> Soft Interrupt (%) </span> <span class = "detail-describe"> {4} </span> </p> <p class = "detail-list"> <span class =" detail-title "> io wait (%) </span> <span class = "detail-describe" >{5 }</span> </p> <p class = "detail-list"> </p> </div> </div> \ n '''. format (self. status ['user'], self. status ['system'], self. status ['idle'], self. status ['hardirq'], self. status ['softidq'], self. status ['iowait']) return resultc Lass 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"> total size </span> <span class = "detail-describe"> {0: 8. 2f} G </Span> </p> <p class = "detail-list"> <span class = "detail-title"> idle size </span> <span class = "detail- describe ">. 2f} G </span> </p> <p class = "detail-list"> </p> </div>> '''. format (self. status ['Total'], self. status ['avariable']) return resultclass mysqlResources (object): def _ init _ (self, status): self. status = status def _ str _ (self): result = ''' <div class = "list-item"> <div class = "cate Gory "> <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 cl Ass = "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> '''. format (self. status ['Port'], self. status ['innodb _ log_waits '], self. status ['binlog _ cache_use '], self. statu S ['created _ tmp_disk_tables '], self. status ['slow _ queries ']) return resultclass hostResources (object): def _ init _ (self, cpu_info, mem_info, disk_info, mysql_info, report_title = 'mysql inspection Report'): 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> 

The above mysql inspection script (which must be read) is all the content shared by the editor. I hope to give you a reference and support for the customer's house.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.