`
java-mans
  • 浏览: 11741965 次
文章分类
社区版块
存档分类
最新评论

MySQL5.5 real time monitor at linux(centos)——MySQL5.5实时监控基于centos

 
阅读更多

原文链接:http://blog.csdn.net/ylqmf/article/details/7878498

利用值班时间用python写了个mysql 实时监控脚本,使用前要确认安装python和mysqldb:

yum install python MySQL-python -y

直接执行脚本就可以了 python monitor.py

下面是执行后的结果,硬盘和网卡监控尚未加入:


'''
Created on 2012-8-16
MySQL real time status
@author: tudou@b2c.xiaomi.com
'''
import MySQLdb,os,time
from decimal import Decimal
mysql_host='localhost'
unix_socket='/tmp/mysql.sock'
mysql_user='root'
mysql_pwd='123456'
mysql_db='test'
disk_list=['sda']
sleep_time=2

class mysqlrealtimestatus(object):
    
    def __init__(self,conf):
        self.conf=conf
        self.db=db(conf)
        self.previoustatus=None
        self.nextstatus=None
        self.previousdisk=None
        self.nextdisk=None
        
        
    def run(self):
        while 1:
            i=os.system('clear')
            self.getstatus()
            time.sleep(self.conf['sleep']);
            
            
    def getstatus(self):
        self.previoustatus = self.nextstatus
        sql = "show global status;"
        self.nextstatus = dict(self.db.execute(sql))
        #print self.nextstatus
        sql="show full processlist;"
        set = self.db.execute(sql,'dict')
        
        self.now = time.strftime('%H:%M:%S',time.localtime(time.time()))
        if self.previoustatus!=None and long(self.nextstatus['Uptime_since_flush_status'])>long(self.previoustatus['Uptime_since_flush_status']):
            self.computer();
        print('==========================slow sql==========================')
        #mysqlrealtimestatus.printl(('id','user','host','db','command','time','state','info'),8)
        for process in set:
            if str(process['Command'])=='Query' and int(process['Time'])>2:
                print('Id:'+str(process['Id'])+'\t'+
                'User:'+str(process['User'])+'\t'+
                'Host:'+str(process['Host'])+'\t'+
                'db:'+str(process['db'])+'\t'+
                'Command:'+str(process['Command'])+'\t'+
                'Time:'+str(process['Time'])+'\t'+
                'State:'+str(process['State']))
                print('Info:'+str(process['Info']))
                print('---------------------------------------------------------------------------------')
                
        
    def computer(self):
        ops=Decimal(self.relcount('Questions'))/Decimal(self.relcount('Uptime_since_flush_status'))
        tps=(Decimal(self.relcount('Com_commit'))+Decimal(self.relcount('Com_rollback')))/Decimal(self.relcount('Uptime_since_flush_status'))
        sps=Decimal(self.relcount('Com_select')+self.relcount('Qcache_hits'))/Decimal(self.relcount('Uptime_since_flush_status'))
        ips=Decimal(self.relcount('Com_insert')+self.relcount('Com_insert_select'))/Decimal(self.relcount('Uptime_since_flush_status'))
        ups=Decimal(self.relcount('Com_update')+self.relcount('Com_update_multi'))/Decimal(self.relcount('Uptime_since_flush_status'))
        dps=Decimal(self.relcount('Com_delete')+self.relcount('Com_delete_multi'))/Decimal(self.relcount('Uptime_since_flush_status'))
        rps=Decimal(self.relcount('Com_replace')+self.relcount('Com_replace_select'))/Decimal(self.relcount('Uptime_since_flush_status'))
        
        bsent_ps=Decimal(self.relcount('Bytes_sent'))/Decimal(self.relcount('Uptime_since_flush_status'))
        if(bsent_ps<0):
            bsent_ps=Decimal(self.status['Bytes_sent'])/Decimal(self.status['Uptime_since_flush_status'])
        breceived_ps=Decimal(self.relcount('Bytes_received'))/Decimal(self.relcount('Uptime_since_flush_status'))
        if(breceived_ps<0):
            breceived_ps=Decimal(self.status['Bytes_received'])/Decimal(self.status['Uptime_since_flush_status'])
        if Decimal(self.relcount('Innodb_buffer_pool_read_requests'))>0:
            ib_read_hits=1-Decimal(self.relcount('Innodb_buffer_pool_reads')+self.relcount('Innodb_buffer_pool_read_ahead'))/Decimal(self.relcount('Innodb_buffer_pool_read_requests'))
        else:
            ib_read_hits=1
        
        ib_used_percent=1-Decimal(self.nextstatus['Innodb_buffer_pool_pages_free'])/Decimal(self.nextstatus['Innodb_buffer_pool_pages_total'])
        ib_dirty_page_percent=Decimal(self.nextstatus['Innodb_buffer_pool_pages_dirty'])/Decimal(self.nextstatus['Innodb_buffer_pool_pages_total'])
        
        if(self.nextstatus.has_key('Innodb_row_lock_waits')):
            ir_lock_waits_ps=Decimal(self.relcount('Innodb_row_lock_waits'))/Decimal(self.relcount('Uptime_since_flush_status'))
        else:
            ir_lock_waits_ps=0
        if(self.relcount('Questions')>0):
            sq_percent=Decimal(self.relcount('Slow_queries'))/Decimal(self.relcount('Questions'))
        else:
            sq_percent=0
        sq_ps=Decimal(self.relcount('Slow_queries'))/Decimal(self.relcount('Uptime_since_flush_status'))
        if(self.relcount('Created_tmp_tables')>0):
            td_percent=Decimal(self.relcount('Created_tmp_disk_tables'))/Decimal(self.relcount('Created_tmp_tables'))
        else:
            td_percent=0
        opened_tables_ps=Decimal(self.relcount('Opened_tables'))/Decimal(self.relcount('Uptime_since_flush_status'))
        if(self.nextstatus.has_key('Opened_files')):
            opened_files_ps=Decimal(self.relcount('Opened_files'))/Decimal(self.relcount('Uptime_since_flush_status'))
        else:
            opened_files_ps=0
        if(self.relcount('Connections')>0):
            thread_cache_hits=1-Decimal(self.relcount('Threads_created'))/Decimal(self.relcount('Connections'))
        else:
            thread_cache_hits=1
        
        mysqlrealtimestatus.printl(('time','ops','tps','sps','ips','ups','dps','rps','bsps','brps','%ihpct','%upct','%dpct','ilwps','%sqpct','%tdpct','ofps','%tcpct'))
        mysqlrealtimestatus.println((self.now,
                                     mysqlrealtimestatus.dFormat(ops),
                                     mysqlrealtimestatus.dFormat(tps),
                                     mysqlrealtimestatus.dFormat(sps),
                                     mysqlrealtimestatus.dFormat(ips),
                                     mysqlrealtimestatus.dFormat(ups),
                                     mysqlrealtimestatus.dFormat(dps),
                                     mysqlrealtimestatus.dFormat(rps),
                                     mysqlrealtimestatus.dFormat(bsent_ps),
                                     mysqlrealtimestatus.dFormat(breceived_ps),
                                     mysqlrealtimestatus.perF(ib_read_hits),
                                     mysqlrealtimestatus.perF(ib_used_percent),
                                     mysqlrealtimestatus.perF(ib_dirty_page_percent),
                                     mysqlrealtimestatus.dFormat(ir_lock_waits_ps),
                                     mysqlrealtimestatus.perF(sq_percent),
                                     mysqlrealtimestatus.perF(td_percent),
                                     mysqlrealtimestatus.dFormat(opened_files_ps),
                                     mysqlrealtimestatus.perF(thread_cache_hits)
                                     ))
        #i=os.system('dstat -cglmpdy --tcp')
        loadavg=self.load_stat()
        mem=self.memory_stat()
        swap=self.swap_stat()
        self.previousdisk=self.nextdisk
        self.nextdisk=self.disk_stat()
        mysqlrealtimestatus.printl(('time','lavg1','lavg5','lavg15','mTotal','mUsed','Buffer','Cached','mFree','swapt','swapu',),8)
        mysqlrealtimestatus.println((self.now,
                                     mysqlrealtimestatus.dFormat(loadavg['lavg_1']),
                                     mysqlrealtimestatus.dFormat(loadavg['lavg_5']),
                                     mysqlrealtimestatus.dFormat(loadavg['lavg_15']),
                                     mysqlrealtimestatus.dFormat(Decimal(str(mem['MemTotal']))),
                                     mysqlrealtimestatus.dFormat(Decimal(str(mem['MemUsed']))),
                                     mysqlrealtimestatus.dFormat(Decimal(str(mem['Buffers']))),
                                     mysqlrealtimestatus.dFormat(Decimal(str(mem['Cached']))),
                                     mysqlrealtimestatus.dFormat(Decimal(str(mem['MemFree']))),
                                     mysqlrealtimestatus.dFormat(Decimal(str(swap['swapt']))*1024),
                                     mysqlrealtimestatus.dFormat(Decimal(str(swap['swapu']))*1024)
                                     ),8)
        #print 
    #!/usr/bin/env python
    def load_stat(self):
        loadavg = {}
        f = open("/proc/loadavg")
        con = f.read().split()
        f.close()
        loadavg['lavg_1']=Decimal(con[0])
        loadavg['lavg_5']=Decimal(con[1])
        loadavg['lavg_15']=Decimal(con[2])
        return loadavg
    #!/usr/bin/env python
    def memory_stat(self):
        mem = {}
        f = open("/proc/meminfo")
        lines = f.readlines()
        f.close()
        for line in lines:
            if len(line) < 2: continue
            name = line.split(':')[0]
            var = line.split(':')[1].split()[0]
            mem[name] = long(var) * 1024.0
        mem['MemUsed'] = mem['MemTotal'] - mem['MemFree'] - mem['Buffers'] - mem['Cached']
        return mem
    def disk_stat(self):
        disk=[]
        f = open("/proc/diskstats")
        lines = f.readlines()
        f.close()
        for disk_name in disk_list:
            for row in lines:
                if str(row).find(' '+disk_name+' ')>0:
                    con=str(row).split(' ')
                    disk.append({'disk_name':disk_name,'rcount':con[2],'rrcount':con[3],'rdcount':con[3],'rtime':con[4],'wcount':con[5],'rwcount':con[6],'wdcount':con[7],'wtime':con[8],})
                    break
                
        return disk
    def swap_stat(self):
        swap={}
        f = open("/proc/swaps")
        l = f.readlines()
        f.close()
        con=str(l[1]).split('\t')
        swap['swapt']=con[1]
        swap['swapu']=con[2]
        return swap
    
    #!/usr/bin/env python
    def net_stat(self):
        net = []
        f = open("/proc/net/dev")
        lines = f.readlines()
        f.close()
        for line in lines[2:]:
            con = line.split()
            
            intf = {}
            intf['interface'] = con[0].lstrip(":")
            intf['ReceiveBytes'] = int(con[1])
            intf['ReceivePackets'] = int(con[2])
            intf['ReceiveErrs'] = int(con[3])
            intf['ReceiveDrop'] = int(con[4])
            intf['ReceiveFifo'] = int(con[5])
            intf['ReceiveFrames'] = int(con[6])
            intf['ReceiveCompressed'] = int(con[7])
            intf['ReceiveMulticast'] = int(con[8])
            intf['TransmitBytes'] = int(con[9])
            intf['TransmitPackets'] = int(con[10])
            intf['TransmitErrs'] = int(con[11])
            intf['TransmitDrop'] = int(con[12])
            intf['TransmitFifo'] = int(con[13])
            intf['TransmitFrames'] = int(con[14])
            intf['TransmitCompressed'] = int(con[15])
            #intf['TransmitMulticast'] = int(con[16])
            """
            intf = dict(
                zip(
                    ( 'interface','ReceiveBytes','ReceivePackets',
                      'ReceiveErrs','ReceiveDrop','ReceiveFifo',
                      'ReceiveFrames','ReceiveCompressed','ReceiveMulticast',
                      'TransmitBytes','TransmitPackets','TransmitErrs',
                      'TransmitDrop', 'TransmitFifo','TransmitFrames',
                      'TransmitCompressed','TransmitMulticast' ),
                    ( con[0].rstrip(":"),int(con[1]),int(con[2]),
                      int(con[3]),int(con[4]),int(con[5]),
                      int(con[6]),int(con[7]),int(con[8]),
                      int(con[9]),int(con[10]),int(con[11]),
                      int(con[12]),int(con[13]),int(con[14]),
                      int(con[15]),int(con[16]))
                )
            )
            """
            net.append(intf)
        return net
    
    def relcount(self,param):
        return Decimal(self.nextstatus[param])-Decimal(self.previoustatus[param])
    
    @staticmethod
    def println(param,s=7):
        p=""
        for i in param:
            if type(i)==type(""):
                p+=i+" "
            else:
                p+=str(i[0]).ljust(s)
        
        print p
    @staticmethod
    def printl(param,s=7):
        p=""
        for i in param:
            if str(i)=='time':
                p+=str(i)+"     "
            else:
                p+=str(i).ljust(s)
        
        print p
        
    @staticmethod
    def perF(param):
        return mysqlrealtimestatus.dFormat(param*100)
    
    @staticmethod
    def dFormat(val):
        k=1024
        m=k*k
        g=k*m
        t=k*g
        p=k*t
        dp=0
        dm=""
        if(val!=0):
            if(val>p):
                dp=p
                dm="P"
            elif(val>t):
                dp=t
                dm="T"
            elif(val>g):
                dp=g
                dm="G"
            elif(val>m):
                dp=m
                dm="M"
            elif(val>k):
                dp=k
                dm="k"
            else:
                dp=1
            return ["%2.2f" % (Decimal(val)/Decimal(dp)) +dm]
        else:
            return ["%2.2f" % 0]
'''

'''
class MySQLHelper(object):
    @staticmethod
    def getConn(conf):
        pot = 3306
        if(conf.has_key('port')):
            pot=conf['port']
        dbname='test'
        if(conf.has_key('db')):
            dbname=conf['db']
        
        if(conf.has_key('socket')):
            return MySQLdb.connect(host=conf['host'],unix_socket=conf['socket'],user=conf['user'],passwd=conf['pwd'],db=dbname)
        else:
            return MySQLdb.connect(host=conf['host'],port=pot,user=conf['user'],passwd=conf['pwd'],db=dbname)
'''

'''
class db (object):
    def __init__(self,conf):
        self.conn=None
        self.conn=MySQLHelper.getConn(conf)
    
    def execute(self,sql,mod=''):
        if(mod=='dict'):
            cursor=self.conn.cursor(MySQLdb.cursors.DictCursor)
        else:
            cursor=self.conn.cursor()
        cursor.execute(sql)
        set=cursor.fetchall()
        return set
    
    def executeNoQuery(self,sql,param={}):
        cursor=self.conn.cursor()
        try:
            if(param=={}):
                rownum=cursor.execute(sql)
            else:
                rownum=cursor.executemany(sql,param)
            self.conn.commit()
            return rownum
        finally:
            cursor.close()
    
    def __del__(self):
        if (self.conn!=None):
            self.conn.close()

if __name__ == '__main__':
    conf={'host':mysql_host,'socket':unix_socket,'user':mysql_user,'pwd':mysql_pwd,'db':mysql_db,'sleep':sleep_time}
    status=mysqlrealtimestatus(conf);
    status.run();

原文链接:http://blog.csdn.net/ylqmf/article/details/7878498

分享到:
评论

相关推荐

    MySQL 5.5 server 64位 centos

    MySQL-server-5.5.25a-1.linux2.6.x86_64.rpm mysql5.5 sever 64位版本在centos上安装的rpm包

    centos 6.5配置mysql5.5

    ### CentOS 6.5 配置 MySQL 5.5 的详细步骤 在 CentOS 6.5 上安装并配置 MySQL 5.5 是一项常见的任务,尤其是对于那些希望在服务器环境中部署稳定、可靠的数据库服务的系统管理员来说。本文将详细介绍如何在 CentOS...

    CentOS安装MySQL 5.5

    ### CentOS安装MySQL 5.5知识点详解 #### 1. 概述 本文档旨在指导如何在CentOS系统上从源代码编译、安装并配置MySQL 5.5数据库管理系统。该过程涉及到软件环境的准备、编译工具的安装、MySQL源代码的编译与配置等...

    centos5.5安装mysql5.5

    集合网上资料和自身实际安装整理的centos5.5安装mysql5.5,文档中有5.5和5.6的下载地载,如果安装5.6还需要找一点资料。

    Linux CentOS5.5 web配置

    ### Linux CentOS5.5 Web配置详解 #### 一、概述 在Linux CentOS5.5系统中进行Web配置是一项常见的任务,对于服务器管理和网站部署至关重要。本文将深入探讨CentOS5.5下的Web配置方法,包括基本配置、路径设置、...

    mysql5.5-linux.zip

    MySQL 5.5是MySQL数据库管理系统的一个重要版本,尤其在Linux环境下广泛应用。这个压缩包"mysql5.5-linux.zip"包含两个RPM(Red Hat Package Manager)文件:MySQL-server-5.5.58-1.el6.x86_64.rpm和MySQL-client-...

    linux 安装mysql5.5文档

    在开始安装MySQL 5.5之前,首先确保Linux系统(本例为CentOS)已准备好并创建了用于存放软件包的目录。具体操作如下: 1. **创建目录**:在`/usr/local/src/`目录下准备存放MySQL安装包。 ```bash mkdir -p /...

    MySQL5.5-deps

    RPM(Red Hat Package Manager)是Linux世界中一种广泛使用的软件包管理器,尤其在基于Red Hat的系统如Fedora、CentOS和Scientific Linux中。RPM包包含了一个软件的所有文件、元数据以及安装和卸载脚本,使得用户...

    mysql5.5升级完整包

    mysql5.5升级的所有包。 1、安装MySQL 5.5.x的yum源: rpm -Uvh http://repo.webtatic.com/yum/centos/5/latest.rpm 2、安装MySQL客户端的支持包: yum install libmysqlclient15 --enablerepo=webtatic 3、卸载...

    mysql5.5版本含安装教程

    MySQL 5.5是MySQL数据库管理系统的一个重要版本,它在2010年发布,带来了许多性能提升和新特性。本教程将详细讲解如何下载、安装以及配置MySQL 5.5,帮助您顺利掌握这一数据库系统的操作。 首先,**下载MySQL 5.5**...

    Linux上源码安装Mysql5.5

    ### Linux上源码安装MySQL 5.5详细步骤与知识点解析 #### 一、环境准备与文件上传 在开始安装之前,确保您的Linux系统已经具备以下条件: 1. **操作系统**:选择支持的Linux发行版,例如CentOS、Ubuntu等。 2. **...

    Linux-mysql5.5

    【MySQL 5.5在Linux系统中的安装与配置】 MySQL 5.5是MySQL数据库管理系统的一个重要版本,它提供了许多增强的功能和性能优化。在Linux环境下安装和配置MySQL 5.5是许多系统管理员和开发者的基本技能之一。下面将...

    centos 5.5 32位 下载地址

    CentOS(Community ENTerprise Operating System)是基于Red Hat Enterprise Linux(RHEL)源代码重新编译而成的开源Linux发行版。它旨在提供一个稳定、可预测且免费的企业级计算平台。由于CentOS与RHEL共享相同的...

    centos安装mysql5.5.txt

    ### CentOS 安装 MySQL 5.5 的详细步骤与注意事项 #### 一、背景介绍 在 CentOS 系统上安装 MySQL 数据库是一项常见的任务,尤其是对于 Web 开发者和系统管理员来说。本文将详细介绍如何在 CentOS 系统上安装 ...

    CentOS 5.5下安装MySQL 5.5全过程分享

    打开终端 切换到根目录 [shell@localhost ~]# su -安装Mysql5.5之前先卸载CentOS自带的Mysql5.0。 [root@localhost ~]# yum remove mysql 安装cmake 下载cmake源码包cmake-2.8.5.tar.gz [root@localhost ~]# wget ...

    docker-mysql:基于CentOS和MySQL 5.5的Docker MySQL映像

    码头工人MySQL 基于CentOS和MySQL 5.5的Docker MySQL映像

    MySQL5.5多实例安装配置文件

    MySQL5.5启动多实例详细配置文件,多实例安装方法可参考博文:Centos6.10采用cmake编译安装MySQL5.5多实例安装

    CentOS 5.5使用yum安装Apache+PHP+MySQL

    在本文中,我们将详细介绍如何在 CentOS 5.5 操作系统上使用 YUM 工具安装 Apache、PHP 和 MySQL,创建一个完整的 LAMP (Linux, Apache, MySQL, PHP) 服务器环境。 首先,确保您已经安装了 CentOS 5.5。这个版本...

    linux中mysql5.5--rpm安装包(含安装过程)

    RPM是一种软件包管理器,主要用于Fedora、CentOS和RHEL等基于Red Hat的Linux发行版。它负责安装、升级、查询、验证和卸载软件包,简化了系统维护工作。 二、MySQL 5.5 RPM安装前准备 1. 更新系统:确保你的Linux...

    Centos5.5下Qt的安装配置

    CentOS 5.5 下 Qt 的安装配置 在 CentOS 5.5 平台下安装 Qt 需要经过多个步骤,每个步骤都需要注意一些重要细节。本文将详细讲解 CentOS 5.5 下 Qt 的安装配置过程。 一、安装软件列表 在开始安装 Qt 之前,需要...

Global site tag (gtag.js) - Google Analytics