cat mysql_status_output.py
#coding=utf-8
import jaydebeapi
import sys
import pymysql
import os
from prometheus_client import Gauge,start_http_server
import time
#coding=utf-8
import jaydebeapi
import sys
import pymysql
import os
from prometheus_client import Gauge,start_http_server
import time
#v_host=os.popen('echo $HOSTNAME')
#hostname=v_host.read()
#hstname="".join(hostname)
#print(hostname.strip())
#hostname=v_host.read()
#hstname="".join(hostname)
#print(hostname.strip())
class MySQL_Status_Output:
def __init__(self,host,port,user,password):
try:
self.db = pymysql.connect(host=host,port=port,user=user,password=password)
self.cursor = self.db.cursor()
except Exception as e:
print('Wrong')
print(e)
def mysql_status_select(self,x):
try:
sql='show global status like %s'
data=x
self.cursor.execute(sql,data)
v_result=self.cursor.fetchall()
return v_result
except Exception as e:
print(e)
def mysql_select_sql(self,sql):
try:
self.cursor.execute(sql)
v_result=self.cursor.fetchall()
return v_result
except Exception as e:
print(e)
def close(self):
self.db.close()
def __init__(self,host,port,user,password):
try:
self.db = pymysql.connect(host=host,port=port,user=user,password=password)
self.cursor = self.db.cursor()
except Exception as e:
print('Wrong')
print(e)
def mysql_status_select(self,x):
try:
sql='show global status like %s'
data=x
self.cursor.execute(sql,data)
v_result=self.cursor.fetchall()
return v_result
except Exception as e:
print(e)
def mysql_select_sql(self,sql):
try:
self.cursor.execute(sql)
v_result=self.cursor.fetchall()
return v_result
except Exception as e:
print(e)
def close(self):
self.db.close()
if __name__ == "__main__":
start_http_server(9400)
mysqlGauge = Gauge('mysqlGauge','Description of gauge', ['mylabelname'])
while True:
try:
time.sleep(1)
pro_db = MySQL_Status_Output('127.0.0.1',3306,'dbadmin','dbadmin')
my_result = pro_db.mysql_select_sql('select MONITOR_NAME from dbadmin.db_monitor_tab where status=1 and MONITOR_NAME is not null')
for j in range(len(my_result)):
monitor_name = "".join(tuple(my_result[j]))
v_out = pro_db.mysql_status_select(monitor_name)
for i in range(int(len(v_out))):
mysqlGauge.labels(mylabelname=v_out[i][0]).set(v_out[i][1])
pro_db.close()
except Exception as e:
print('Is Wrong')
print(e)
start_http_server(9400)
mysqlGauge = Gauge('mysqlGauge','Description of gauge', ['mylabelname'])
while True:
try:
time.sleep(1)
pro_db = MySQL_Status_Output('127.0.0.1',3306,'dbadmin','dbadmin')
my_result = pro_db.mysql_select_sql('select MONITOR_NAME from dbadmin.db_monitor_tab where status=1 and MONITOR_NAME is not null')
for j in range(len(my_result)):
monitor_name = "".join(tuple(my_result[j]))
v_out = pro_db.mysql_status_select(monitor_name)
for i in range(int(len(v_out))):
mysqlGauge.labels(mylabelname=v_out[i][0]).set(v_out[i][1])
pro_db.close()
except Exception as e:
print('Is Wrong')
print(e)