Mysql基础监控:

         通过SHOW STATUS 可以提供服务器状态信息,也可以使用mysqladmin extendedstatus 命令获得;

以下几个参数对Myisam 和Innodb 存储引擎都计数:

    1. Com_select 执行select 操作的次数,一次查询只累加1;

    2. Com_insert 执行insert 操作的次数,对于批量插入的insert 操作,只累加一次;

    3. Com_update 执行update 操作的次数;

    4. Com_delete 执行delete 操作的次数;

以下几个参数是针对Innodb 存储引擎计数的:

    1. Innodb_rows_read select 查询返回的行数;

    2. Innodb_rows_inserted 执行Insert 操作插入的行数;

    3. Innodb_rows_updated 执行update 操作更新的行数;

    4. Innodb_rows_deleted 执行delete 操作删除的行数;

         通过以上几个参数,可以很容易的了解当前数据库的应用是以插入更新为主还是以查询操作为主,以及各种类型的SQL 大致的执行比例是多少。对于更新操作的计数,是对执行次数的计数,不论提交还是回滚都会累加。对于事务型的应用,通过Com_commit 和Com_rollback 可以了解事务提交和回滚的情况,对于回滚操作非常频繁的数据库,可能应用编写存在问题。

另外还有几个参数可以了解数据库的基本信息:

1. Connections 试图连接Mysql 服务器的次数

2. Uptime 服务器工作时间

3. Slow_queries 慢查询的次数

相关脚本如下:

#!/bin/bash
#check mysql_status 
MYSQL_USER='root'
MYSQL_HOST='127.0.0.1'
MYSQL_PORT='3306'
MYSQL_PWD='123'
MYSQL_CONN="/usr/local/mysql/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"
if [ $# -ne "1" ];then 
    echo "error error!" 
fi
case $1 in 
    Uptime) 
        result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"`              #mysql启动时间累加,mysql工作时间
        echo $result 
        ;; 
    Com_update) 
        result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3`   #执行update 操作的次数 
        echo $result 
        ;; 
    Slow_queries) 
        result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"`                  #慢查询的次数 
        echo $result 
        ;; 
    Com_select) 
        result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3`  #执行select 操作的次数,一次查询只累加1 
        echo $result 
                ;; 
    Com_rollback) 
        result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3`  #事务回滚情况,对于回滚操作非常频繁的数据库,可能意味着应用编写存在问题 
                echo $result 
                ;; 
    Questions) 
        result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"` #初始的查询操作的总数 
                echo $result 
                ;; 
    Com_insert) 
        result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3`  #执行insert 操作的次数,对于批量插入的insert 操作,只累加一次;
                echo $result 
                ;; 
    Com_delete) 
        result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3` #执行delete 操作的次数 
                echo $result 
                ;; 
    Com_commit) 
        result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3`  #事务提交次数 
                echo $result 
                ;; 
    Bytes_sent) 
        result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`  #请求流量带宽,发送
                echo $result 
                ;; 
    Bytes_received) 
        result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3` #响应流量带宽,接收
                echo $result 
                ;; 
    Com_begin) 
        result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3` 
                echo $result 
                ;; 
    Innodb_rows_read) 
        result=`${MYSQL_CONN} extended-status |grep -w "Innodb_rows_read"|cut -d"|" -f3`    #Innodb中select 查询返回的行数 
                echo $result
             ;;
    Innodb_rows_inserted) 
        result=`${MYSQL_CONN} extended-status |grep -w "Innodb_rows_inserted"|cut -d"|" -f3` #执行Insert 操作插入的行数
                echo $result
        ;;
    Innodb_rows_updated) 
        result=`${MYSQL_CONN} extended-status |grep -w "Innodb_rows_updated"|cut -d"|" -f3` #执行update 操作更新的行数
                echo $result
        ;;
    Innodb_rows_deleted) 
        result=`${MYSQL_CONN} extended-status |grep -w "Innodb_rows_deleted"|cut -d"|" -f3` #执行update 操作更新的行数
                echo $result
        ;;
    Qcache_free_memory) 
        result=`${MYSQL_CONN} extended-status |grep -w "Qcache_free_memory"|cut -d"|" -f3`  #缓存中的空闲内存 
                echo $result
                ;;
    Qcache_hits) 
        result=`${MYSQL_CONN} extended-status |grep -w "Qcache_hits"|cut -d"|" -f3`  #每次查询在缓存中命中时就增大
                echo $result
                ;;
    Qcache_inserts) 
        result=`${MYSQL_CONN} extended-status |grep -w "Qcache_inserts"|cut -d"|" -f3`  #每次插入一个查询时就增大。命中次数除以插入次数就是不中比率
                echo $result
                ;; 
    Threads_connected)
        result=`${MYSQL_CONN} extended-status |grep -w "Threads_connected"|cut -d"|" -f3`  #当前连接数
                echo $result
                ;;
        *) 
        echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin||Innodb_rows_read|Innodb_rows_inserted|Innodb_rows_updated|Innodb_rows_deleted|Qcache_free_memory|Qcache_hits|Qcache_inserts|Threads_connected)" 
        ;; 
esac


TPS/QPS监控:

QPS:Queries Per Second---查询量/秒,是一台服务器每秒能够相应的查询次数,是对一个特定的查询服务器在规定时间内所处理查询量多少的衡量标准。

TPS :  Transactions Per Second---事务数/秒,是一台数据库服务器在单位时间内处理的事务的个数。

监控脚本如下:

#!/usr/bin/env python
#coding=utf-8
#description:monitor mysql tps and qps
import time
import sys,os
import MySQLdb
def main(opt):
    try:
        conn = MySQLdb.connect(host='127.0.0.1',port=3306,user='root',passwd='123', charset='utf8')
    except MySQLdb.ERROR,e:
        print "Error %d:%s " %(e.args[0],e.args[1])
        exit(1)
    conn.autocommit(True)
    cursor=conn.cursor()
    diff=2
    mystat1={}
    mystat2={}
    sql="show global status where Variable_name in ('Com_commit','Com_delete','Com_insert','Com_rollback','Com_select','Com_update','Questions');"
    if True:
        try:
            cursor.execute(sql)
            results1 = cursor.fetchall()
            mystat1=dict(results1)
            time.sleep(diff)
            cursor.execute(sql)
            results2 = cursor.fetchall()
            mystat2=dict(results2)
            Com_diff = (int(mystat2['Com_commit'])-int(mystat1['Com_commit']))/diff
            rol_diff = (int(mystat2['Com_rollback'])-int(mystat1['Com_rollback']))/diff
            del_diff = (int(mystat2['Com_delete'])-int(mystat1['Com_delete']))/diff
            ins_diff = (int(mystat2['Com_insert'])-int(mystat1['Com_insert']))/diff
            sel_diff = (int(mystat2['Com_select'])-int(mystat1['Com_select']))/diff
            upd_diff = (int(mystat2['Com_update'])-int(mystat1['Com_update']))/diff
            que_diff = (int(mystat2['Questions'])-int(mystat1['Questions']))/diff
            qps_s = sel_diff
            tps_iud = del_diff+ins_diff+upd_diff
            qps_ques=que_diff
            tps_Com_rol=Com_diff+rol_diff
            if opt == 'mysql_qps':
                print qps_s
            elif opt == 'mysql_tps':
                print tps_Com_rol
            else:
                print "Input error"
        except KeyboardInterrupt:
            print "Error,exit..."
            sys.exit()
    else:
        pass
    conn.close()
if __name__ == '__main__':
    opt= sys.argv[1]
    main(opt)


修改zabbix-agentd监控配置:

UserParameter=mysql.ping,mysqladmin -uroot -p123 -P3306 -h127.0.0.1  ping | grep -c alive
UserParameter=mysql.status[*],/usr/local/zabbix/scripts/check_mysql.sh $1
UserParameter=mysql.tps,/usr/bin/python /usr/local/zabbix/scripts/mysql_tps_qps.py mysql_tps
UserParameter=mysql.qps,/usr/bin/python /usr/local/zabbix/scripts/mysql_tps_qps.py mysql_qps


对check_mysql.sh,mysql_tps_qps.py修改相应的属性和权限,重启zabbix_agentd,服务端查看监控是否成功。