Beispiel #1
0
def check_qps(host,port,user,passwd,db="",charset="utf8"):
    """统计各MySQL实例的QPS
    """
    db = DB(host,port,user,passwd,db,charset,cursorclass="")
    en = Encrypt()
    db.execute("SELECT ID,INET_NTOA(IP) AS IPADDR,PORT,USER,PASSWD FROM T_INSTANCE")
    rows = db.fetchall()
    for row in rows:
        db.execute("""SELECT UPTIME,QUESTIONS FROM T_QPS WHERE INSTID = {0}
            ORDER BY ID DESC LIMIT 1""".format(row.get('ID')))
        insertrow = db.fetchone()
        if insertrow == None:
            insertrow = dict()
        tmpdb = DB(row.get('IPADDR'),row.get('PORT'),row.get('USER'),en.decrypt(row.get('PASSWD')))
        tmpdb.execute("show global status like 'Uptime'")
        uprow = tmpdb.fetchone()
        tmpdb.execute("show global status like 'Questions'")
        qurow = tmpdb.fetchone()
        db.execute("""INSERT INTO T_QPS(INSTID,UPTIME,QUESTIONS,DIFFUPTIME,DIFFQUESTIONS)
            VALUES({0},{1},{2},{3},{4})""".format(
            row.get('ID'), uprow[1], qurow[1],
            int(uprow[1])-int(insertrow.get('UPTIME',0)),
            int(qurow[1])-int(insertrow.get('QUESTIONS', 0))
        ))
        db.commit()
Beispiel #2
0
class BaseHandler(tornado.web.RequestHandler):
    """基本的类
    """
    def initialize(self):
        """初始化数据库
        """
        self.db = DB("localhost",3306,"root","123456","monitor")
        SQL = "SELECT ID,IP,PORT,USER,PASSWD FROM T_INSTANCE"
        self.db.execute(SQL)
        self.rows = self.db.fetchall()
Beispiel #3
0
def check_slow(host, port, user, passwd):
    if host and port and user and passwd:
        db = DB(host=host, port=port, user=user, passwd=passwd, db="mysql")
    else:
        print "You mst give all parmaters as host,port,user,passwd."
        exit(1)
    try:
        sql = "select start_time,user_host,query_time,lock_time,rows_sent,rows_examined,db,last_insert_id,insert_id,server_id,sql_text from slow_log"
        db.execute(sql)
        return db.fetchall()
    except MySQLdb.OperationalError as e:
        print e
Beispiel #4
0
def main():
    cg = parser_config()
    en = Encrypt()
    monitordb = DB(cg.get("host"), int(cg.get("port")), cg.get("user"), en.decrypt(cg.get("passwd")), cg.get("db"))
    monitordb.execute("SELECT ID,INET_NTOA(IP) AS IPADDR,PORT,USER,PASSWD FROM T_INSTANCE")
    # check_qps(cg.get("host"), int(cg.get("port")), cg.get("user"), en.decrypt(cg.get("passwd")), cg.get("db"))
    insts = monitordb.fetchall()
    for inst in insts:
        # rows = check_slow('10.1.2.9',3306,'root','password')
        rows = check_slow(inst[1], inst[2], inst[3], en.decrypt(inst[4]))
        rtc = store_slow(rows, inst[0], monitordb)
        if rtc:
            truncate_slow(inst[1], inst[2], inst[3], en.decrypt(inst[4]))
Beispiel #5
0
def main():
    en = Encrypt()
    cg = parser_config()
    db = DB(cg.get("host"), int(cg.get("port")), cg.get("user"), en.decrypt(cg.get("passwd")), cg.get("db"))
    SQL = "SELECT ID,INET_NTOA(IP),PORT,USER,PASSWD FROM T_INSTANCE"
    db.execute(SQL)
    rows = db.fetchall()
    for row in rows:
        tmpdb = DB(row[1],row[2],row[3],en.decrypt(row[4]))
        tmpdb.execute("SELECT COUNT(*) FROM INFORMATION_SCHEMA.PROCESSLIST")
        cnt = tmpdb.fetchone()
        db.execute("""INSERT INTO T_CONNECTION(INSTANCE,CNT)
            VALUES({0}, {1})""".format(row[0], cnt[0]))
        db.commit()
def main():
    # 建议读取外部配置文件以获取监控服务器配置
    cg = parser_config()
    en = Encrypt()
    mon_db = DB(cg.get("host"), int(cg.get("port")), cg.get("user"), en.decrypt(cg.get("passwd")), cg.get("db"))
    mon_db.execute("SELECT ID,INET_NTOA(IP),PORT,USER,PASSWD FROM T_INSTANCE WHERE INSTTYPE='SLAVE'")
    rows = mon_db.fetchall()
    for row in rows:
        replicat_status = check(row[1],row[2],row[3],en.decrypt(row[4]))
        if replicat_status:
            mon_db.execute("""INSERT INTO T_REPLICAT(INSTANCE,IOTHREAD,SQLTHREAD,BEHIND)
                VALUES('{0}','{1}','{2}',{3})""".format(row[0],replicat_status.get('SLAVE_IO_RUNNING'),
                replicat_status.get('SLAVE_SQL_RUNNING'),replicat_status.get('SECONDS_BEHIND_MASTER')))
    mon_db.commit()
Beispiel #7
0
def check_slowsql(host,port,user,passwd,db):
    size = 1024
    en = Encrypt()
    db = DB(host,port,user,passwd,db)
    db.execute("SELECT ID,INET_NTOA(IP),PORT,SOCKETPORT FROM T_INSTANCE ORDER BY ID DESC")
    instrows = db.fetchall()
    for inst in instrows:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            s.connect((inst[1],inst[3]))
            print "connected to:{0}\t{1}".format(inst[1],inst[3])
        except socket.error as e:
            print '\033[91m' + "Socket error:{0}\t{1} for host {2}".format(
                e.errno, e.strerror, inst[1]) +\
                '\033[0m'
            if e.errno == 111:
                continue
        s.send('GET LaSt SQL')
        data_collect = ""
        while True:
            data = s.recv(size)
            if not data: break
            data_collect += data
        json_collect = json.loads(data_collect)
        for row in json_collect:
            try:
                insert_row = [ i[1] for i in sorted(row.items())]
                insert_row.append(inst[0])
                db.executemany("""INSERT INTO
                    T_SLOW(ONDB,LOCKTIME,QUERYTIME,ROWS_EXAMINED,ROWS_SENT,SQL_TEXT,STARTTIME,STATE,USER_HOST,OBJID,INSTID)
                    VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""",
                    insert_row)
            except MySQLdb.IntegrityError as e:
                print 'row:{0} occur error:{1}'.format(tuple(insert_row[0:]),e)
            state = db.commit()
            if not state:
                print 'UP ID:{0}'.format(insert_row[9])
                tmps = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                tmps.connect((inst[1],inst[3]))
                tmps.send('UP ID:{0}'.format(insert_row[9]))
                data = tmps.recv(size)
                tmps.close()
        s.close()
Beispiel #8
0
def check_hitrate(host, port, user, passwd, db="", charset="utf8"):
    """
    """
    db = DB(host, port, user, passwd, db, charset, cursorclass="")
    en = Encrypt()
    db.execute("SELECT ID,INET_NTOA(IP) AS IPADDR,PORT,USER,PASSWD FROM T_INSTANCE")
    rows = db.fetchall()
    sql = "INSERT INTO T_HITRATE VALUES(NULL,'%s','%s','%s')"
    for row in rows:
        tmpdb = DB(row.get("IPADDR"), row.get("PORT"), row.get("USER"), en.decrypt(row.get("PASSWD")))
        tmpdb.execute("show global status like 'Qcache_hits'")
        hits = tmpdb.fetchone()
        tmpdb.execute("show global status like 'Com_select'")
        selects = tmpdb.fetchone()
        try:
            hit_rate = int(hits[1]) / int(hits[1] + selects[1])
        except ZeroDivisionError:
            hit_rate = 0
        # 00.0%
        db.execute(sql % (row.get("ID"), datetime.datetime.now(), int(hit_rate)))
        db.commit()
Beispiel #9
0
class DB_Info(object):
    """数据库信息
    """
    def __init__(self,host,port=3306,user="",passwd="",db="",charset="utf8"):
        """
        """
        self.db = DB(host,port,user,passwd,db,charset)
    def get_status(self):
        """
        """
        self.db.execute("show status")
        rows = self.db.fetchall()
        return dict([(row[0].upper(),row[1]) for row in rows])
    def get_master(self):
        """
        """
        self.db.execute("SHOW MASTER STATUS")
        row = self.db.fetchone()
        master_status = dict(zip(
            ("FILE", "POSITION", "BINLOG_DO_DB", "BINLOG_IGNORE_DB"), row
        ))
        cnt = self.db.execute("SHOW SLAVE HOSTS")
        rows = self.db.fetchall()
        master_hosts= list()
        for row in rows:
            master_hosts.append(
                dict(zip(
                    ("SERVER_ID", "HOST", "PORT", "MASTER_ID"),
                    row)))
        return master_status, master_hosts
    def get_slave(self):
        """
        """
        self.db.execute("SHOW STATUS")
        rows = self.db.fetchall()
        server_status = dict([(row[0].upper(),row[1]) for row in rows])
        cnt = self.db.execute("SHOW SLAVE STATUS")
        row = self.db.fetchone()
        if cnt == 0:
            print "Slave host not start."
            return server_status, None
        replicat_status = dict(zip(
            ("SLAVE_IO_STATE","MASTER_HOST","MASTER_USER","MASTER_PORT","CONNECT_RETRY",
            "MASTER_LOG_FILE","READ_MASTER_LOG_POS","RELAY_LOG_FILE","RELAY_LOG_POS",
            "RELAY_MASTER_LOG_FILE","SLAVE_IO_RUNNING","SLAVE_SQL_RUNNING","REPLICATE_DO_DB",
            "REPLICATE_IGNORE_DB","REPLICATE_DO_TABLE","REPLICATE_IGNORE_TABLE",
            "REPLICATE_WILD_DO_TABLE","REPLICATE_WILD_IGNORE_TABLE","LAST_ERRNO","LAST_ERROR",
            "SKIP_COUNTER","EXEC_MASTER_LOG_POS","RELAY_LOG_SPACE","UNTIL_CONDITION",
            "UNTIL_LOG_FILE","UNTIL_LOG_POS","MASTER_SSL_ALLOWED","MASTER_SSL_CA_FILE",
            "MASTER_SSL_CA_PATH","MASTER_SSL_CERT","MASTER_SSL_CIPHER","MASTER_SSL_KEY",
            "SECONDS_BEHIND_MASTER","MASTER_SSL_VERIFY_SERVER_CERT","LAST_IO_ERRNO","LAST_IO_ERROR",
            "LAST_SQL_ERRNO","LAST_SQL_ERROR","REPLICATE_IGNORE_SERVER_IDS","MASTER_SERVER_ID")
        ,row))
        return server_status, replicat_status
    def get_db(self):
        """
        """
        self.db.execute("""SELECT SCHEMA_NAME,DEFAULT_CHARACTER_SET_NAME,DEFAULT_COLLATION_NAME
            FROM INFORMATION_SCHEMA.SCHEMATA""")
        rows = self.db.fetchall()
        database_status = list()
        for row in rows:
            database_status.append(
                dict(zip(("SCHEMA_NAME","DEFAULT_CHARACTER_SET_NAME","DEFAULT_COLLATION_NAME"),
                    row)))
        return database_status
    def get_table(self):
        """
        """
        SQL = """SELECT TABLE_SCHEMA,TABLE_NAME,TABLE_TYPE,ENGINE,ROW_FORMAT,CREATE_TIME,UPDATE_TIME
        FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'"""
        table_status = list()
        self.db.execute(SQL)
        rows = self.db.fetchall()
        for row in rows:
            table_status.append(dict(zip(("TABLE_SCHEMA","TABLE_NAME","TABLE_TYPE","ENGINE","ROW_FORMAT","CREATE_TIME","UPDATE_TIME"), row)))
        return table_status
    def get_column(self):
        """
        """
        column_status = list()
        SQL = """SELECT TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,
            IS_NULLABLE,DATA_TYPE,CHARACTER_SET_NAME,COLLATION_NAME,COLUMN_TYPE
            FROM INFORMATION_SCHEMA.COLUMNS"""
        self.db.execute(SQL)
        rows = self.db.fetchall()
        for row in rows:
            column_status.append(dict(zip(("TABLE_SCHEMA","TABLE_NAME","COLUMN_NAME","IS_NULLABLE","DATA_TYPE","CHARACTER_SET_NAME","COLLATION_NAME","COLUMN_TYPE"),
                row)))
        return column_status