import os, os.path, sys, commands, string, re, time cachefile = "mysqlstats_cache.txt" hash = {} cachemap = {} def loadCacheMap(): if not os.path.exists(cachefile): f = open(cachefile, "w") f.close() if not os.path.exists(cachefile): return False; f = open(cachefile, "r") for line in f.readlines(): columns = string.split(string.strip(line, "\r\n"), " : ") cachemap[columns[0]] = string.join(columns[1:len(columns)], " ") # for element in cachemap: # print "[" + element + "]" + " [" + cachemap[element] + "]" f.close() return True; def writeCacheMap(): f = open(cachefile, "w+") for element in hash: f.write(element + " : " + hash[element] + "\r\n") f.close() return True; def secondsToStr(param): seconds = int(param) ret = "" amin = 60 anhour = amin*60 aday = anhour*24 days = int(seconds / aday) hours = int((seconds % aday ) / anhour) minutes = int((seconds % anhour) / amin) seconds = int(seconds % amin) if days > 0: ret += str(days) + " " + (days == 1 and "day" or "days" ) + ", " if hours > 0: ret += str(hours) + " " + (hours == 1 and "hour" or "hours" ) + ", " if minutes > 0: ret += str(minutes) + " " + (minutes == 1 and "minute" or "minutes" ) + ", " ret += str(seconds) + " " + (seconds == 1 and "second" or "seconds" ) return ret; def printCounterAndRate(key, elapsed): try: print key + " : " + hash[key] print key + "_rate : " + str((int(hash[key]) - int(cachemap[key])) / float(elapsed)) except: return False; return True; def printCounter(key): try: print key + " : " + hash[key] except: return False; return True; def printRatio(label, keynum, keydem): try: dem = float(hash[keydem]) - float(cachemap[keydem]); if(dem > 0): print label + " : " + str((float(hash[keynum]) - float(cachemap[keynum])) / dem) else: print label + " : 0" except: return False; return True; if sys.platform == "win32": print "unique : debug" print "name : debugName" print "tty : debugTTY" print "when : debugWhen" print "idle : debugIdle" print "pid : debugPID" print "from : debugFrom" print "" else: version = commands.getstatusoutput("mysql --version") output = commands.getstatusoutput("mysql --execute='show status;'") if output[0] == 0 and version[0] == 0: loadCacheMap() rows = string.split(output[1], "\n") for row in rows: # print "[" + row + "]" columns = string.split(row) if columns[0] != "Variable_name": hash[columns[0]] = string.join(columns[1:len(columns)], " ") # for element in hash: # print element + " : " + hash[element] hash["Timestamp"] = str(time.time()) #calculate elapsed time try: elapsed = float(hash["Timestamp"]) - float(cachemap["Timestamp"]) except: elapsed = 1 print "Version_str : " + version[1] print "Uptime : " + hash["Uptime"] print "Uptime_str : " + secondsToStr(hash["Uptime"]) printCounterAndRate("Bytes_received", elapsed) printCounterAndRate("Bytes_sent", elapsed) printCounterAndRate("Aborted_clients", elapsed) printCounterAndRate("Aborted_connects", elapsed) printCounter("Open_tables") printCounter("Open_files") printCounter("Threads_cached") printCounter("Threads_connected") printCounterAndRate("Threads_created", elapsed) printCounter("Threads_running") printCounterAndRate("Qcache_hits", elapsed) printCounter("Key_blocks_unused") printCounterAndRate("Slow_queries", elapsed) printCounterAndRate("Created_tmp_tables", elapsed) printCounterAndRate("Created_tmp_files", elapsed) printCounterAndRate("Created_tmp_disk_tables", elapsed) printCounterAndRate("Questions", elapsed) printRatio("Key_read_ratio", "Key_reads", "Key_read_requests") printRatio("Key_write_ratio", "Key_writes", "Key_write_requests") writeCacheMap()