Python3.5+SQL+Prometheus+Grafana報表/監控 pymysql 單獨獲取表的欄位名稱 pymysql返回資料為字典形式(key:value--列:值)
阿新 • • 發佈:2018-11-28
參考資料:
pymysql 單獨獲取表的欄位名稱
pymysql返回資料為字典形式(key:value--列:值)
#coding=utf-8
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())
class MySQL_Status_Output:
def __init__(self,host,port,user,password):
try:
self.db = pymysql.connect(host=host,port=port,user=user,password=password)
except Exception as e:
print('Database Connection Error')
print(e)
def mysql_select_colum_sql(self,sql):
try:
self.cursor = self.db.cursor(cursor = pymysql.cursors.DictCursor) ##返回key:value形式的結果
self.cursor.execute(sql)
v_result=self.cursor.fetchall()
return v_result
except Exception as e:
print('SQL Result One Column Error')
print(e)
def mysql_select_sql(self,sql):
try:
self.cursor = self.db.cursor()
self.cursor.execute(sql)
col=self.cursor.description ##返回列名/欄位名稱
v_result=self.cursor.fetchall()
return v_result,col
except Exception as e:
print('SQL Result More Column Error')
print(e)
def close_db(self):
self.db.close() def ccpay_sql_one_exec(sql_exec):
try:
pro_db = MySQL_Status_Output('127.0.0.1',3306,'dbadmin','dbadmin')
ccpay_sql = pro_db.mysql_select_colum_sql(sql_exec)
pro_db.close_db()
#print('輸出行數為:',len(ccpay_sql))
if int(len(ccpay_sql))==1:
for i in range(len(ccpay_sql)):
c=ccpay_sql[i]
a=list(c.keys())
for j in range(len(a)):
print(a[j],':',c.get(a[j]))
ccpayGauge.labels(mylabelname=a[j]).set(c.get(a[j]))
else:
pass
except Exception as e:
print('One Colum ccpayGauge Error')
print(e) def ccpay_sql_more_exec(sql_exec):
try:
pro_db = MySQL_Status_Output('127.0.0.1',3306,'dbadmin','dbadmin')
ccpay_sql,col_name = pro_db.mysql_select_sql(sql_exec)
if int(len(ccpay_sql))>1:
bs_name=[]
for i in range(len(ccpay_sql)):
bs_name.append(ccpay_sql[i][0])
for bsname in range(len(bs_name)):
col_name_list=[]
for i in range(len(col_name)):
col_name_list.append(col_name[i][0])
col_name_tuple=tuple(col_name_list)
ccpay_value_list=[]
for j in range(len(ccpay_sql)):
if ccpay_sql[j][0]==bs_name[bsname]:
ccpay_value_list=ccpay_sql[j]
me=dict(zip(col_name_tuple,ccpay_value_list))
for key in me:
bn=str(bs_name[bsname]+key) ##列名/欄位名稱+第一個欄位值做key,其他值做value
bkey=str(me[key])
if str(bs_name[bsname])==str(bkey):
#print(bs_name[bsname])
pass
else:
print(bn,':',bkey)
ccpayGauge.labels(mylabelname=bn).set(bkey)
except Exception as e:
print('More Colums ccpayGauge Error')
print(e)
if __name__ == "__main__":
start_http_server(8500)
ccpayGauge = Gauge('ccpayGauge','Description of gauge', ['mylabelname'])
while True:
time.sleep(3)
try:
ccpay_sql_one_exec(" select count(1) '全盤交易筆數',count(case when trade_status !='FAILED' then 1 else null end) '全盤出碼成功',count(case when trade_status ='SUCCESS' then 1 else null end) '全盤交易成功',count(case when notify_status ='SUCCESS' then 1 else null end) '全盤通知成功',sum(case when trade_status ='SUCCESS' then TRADE_AMOUNT else null end)/100 '全盤成功金額',count(case when trade_status ='FAILED' and trade_errmsg = '指定裝置無響應' then 1 else null end) '指定裝置無響應',count(case when trade_status ='FAILED' and trade_errmsg = '找不到可用的裝置' then 1 else null end) '找不到可用的裝置',TRUNCATE(count(case when trade_status ='SUCCESS' then 1 else null end)*100/count(1),2) '全盤交易成功率' from ifpay_ccpay.trade_info where CREATE_TIME > curdate() ")
ccpay_sql_one_exec(" select count(1) '可用裝置' from ifpay_ccpay.machine where ENABLE_STATUS = '1' ")
ccpay_sql_one_exec(" select count(distinct qrcode_url.qrcode_num) '新生碼交易成功筆數' from ifpay_ccpay.trade_info inner join ifpay_ccpay.qrcode_url on trade_info.qrcode_num=qrcode_url.qrcode_num where trade_status='SUCCESS' and date(trade_info.create_time)=date(current_date()) and date(trade_info.create_time)=date(qrcode_url.created_date) ")
ccpay_sql_more_exec(" select MERCHANT_name '商戶名稱',total '交易筆數',qrcode '出碼成功',success '交易成功',notify '通知成功',(case when amount is null then 0 else amount end) '成功金額' from (select a.MERCHANT_name MERCHANT_name,count(1) total,count(case when trade_status !='FAILED' then 1 else null end) qrcode,count(case when trade_status ='SUCCESS' then 1 else null end) success,count(case when notify_status ='SUCCESS' then 1 else null end) notify,sum(case when trade_status ='SUCCESS' then TRADE_AMOUNT else null end)/100 amount from ifpay_ccpay.trade_info,ifpay_ccpay.merchant a where MERCHANT_NO=a.MERCHANT_id and CREATE_TIME > curdate() group by a.MERCHANT_name) a ")
except Exception as e:
print('Is Wrong')
print(e) 輸出結果:
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())
class MySQL_Status_Output:
def __init__(self,host,port,user,password):
try:
self.db = pymysql.connect(host=host,port=port,user=user,password=password)
except Exception as e:
print('Database Connection Error')
print(e)
def mysql_select_colum_sql(self,sql):
try:
self.cursor = self.db.cursor(cursor = pymysql.cursors.DictCursor) ##返回key:value形式的結果
self.cursor.execute(sql)
v_result=self.cursor.fetchall()
return v_result
except Exception as e:
print('SQL Result One Column Error')
print(e)
def mysql_select_sql(self,sql):
try:
self.cursor = self.db.cursor()
self.cursor.execute(sql)
col=self.cursor.description ##返回列名/欄位名稱
v_result=self.cursor.fetchall()
return v_result,col
except Exception as e:
print('SQL Result More Column Error')
print(e)
def close_db(self):
self.db.close() def ccpay_sql_one_exec(sql_exec):
try:
pro_db = MySQL_Status_Output('127.0.0.1',3306,'dbadmin','dbadmin')
ccpay_sql = pro_db.mysql_select_colum_sql(sql_exec)
pro_db.close_db()
#print('輸出行數為:',len(ccpay_sql))
if int(len(ccpay_sql))==1:
for i in range(len(ccpay_sql)):
c=ccpay_sql[i]
a=list(c.keys())
for j in range(len(a)):
print(a[j],':',c.get(a[j]))
ccpayGauge.labels(mylabelname=a[j]).set(c.get(a[j]))
else:
pass
except Exception as e:
print('One Colum ccpayGauge Error')
print(e) def ccpay_sql_more_exec(sql_exec):
try:
pro_db = MySQL_Status_Output('127.0.0.1',3306,'dbadmin','dbadmin')
ccpay_sql,col_name = pro_db.mysql_select_sql(sql_exec)
if int(len(ccpay_sql))>1:
bs_name=[]
for i in range(len(ccpay_sql)):
bs_name.append(ccpay_sql[i][0])
for bsname in range(len(bs_name)):
col_name_list=[]
for i in range(len(col_name)):
col_name_list.append(col_name[i][0])
col_name_tuple=tuple(col_name_list)
ccpay_value_list=[]
for j in range(len(ccpay_sql)):
if ccpay_sql[j][0]==bs_name[bsname]:
ccpay_value_list=ccpay_sql[j]
me=dict(zip(col_name_tuple,ccpay_value_list))
for key in me:
bn=str(bs_name[bsname]+key) ##列名/欄位名稱+第一個欄位值做key,其他值做value
bkey=str(me[key])
if str(bs_name[bsname])==str(bkey):
#print(bs_name[bsname])
pass
else:
print(bn,':',bkey)
ccpayGauge.labels(mylabelname=bn).set(bkey)
except Exception as e:
print('More Colums ccpayGauge Error')
print(e)
if __name__ == "__main__":
start_http_server(8500)
ccpayGauge = Gauge('ccpayGauge','Description of gauge', ['mylabelname'])
while True:
time.sleep(3)
try:
ccpay_sql_one_exec(" select count(1) '全盤交易筆數',count(case when trade_status !='FAILED' then 1 else null end) '全盤出碼成功',count(case when trade_status ='SUCCESS' then 1 else null end) '全盤交易成功',count(case when notify_status ='SUCCESS' then 1 else null end) '全盤通知成功',sum(case when trade_status ='SUCCESS' then TRADE_AMOUNT else null end)/100 '全盤成功金額',count(case when trade_status ='FAILED' and trade_errmsg = '指定裝置無響應' then 1 else null end) '指定裝置無響應',count(case when trade_status ='FAILED' and trade_errmsg = '找不到可用的裝置' then 1 else null end) '找不到可用的裝置',TRUNCATE(count(case when trade_status ='SUCCESS' then 1 else null end)*100/count(1),2) '全盤交易成功率' from ifpay_ccpay.trade_info where CREATE_TIME > curdate() ")
ccpay_sql_one_exec(" select count(1) '可用裝置' from ifpay_ccpay.machine where ENABLE_STATUS = '1' ")
ccpay_sql_one_exec(" select count(distinct qrcode_url.qrcode_num) '新生碼交易成功筆數' from ifpay_ccpay.trade_info inner join ifpay_ccpay.qrcode_url on trade_info.qrcode_num=qrcode_url.qrcode_num where trade_status='SUCCESS' and date(trade_info.create_time)=date(current_date()) and date(trade_info.create_time)=date(qrcode_url.created_date) ")
ccpay_sql_more_exec(" select MERCHANT_name '商戶名稱',total '交易筆數',qrcode '出碼成功',success '交易成功',notify '通知成功',(case when amount is null then 0 else amount end) '成功金額' from (select a.MERCHANT_name MERCHANT_name,count(1) total,count(case when trade_status !='FAILED' then 1 else null end) qrcode,count(case when trade_status ='SUCCESS' then 1 else null end) success,count(case when notify_status ='SUCCESS' then 1 else null end) notify,sum(case when trade_status ='SUCCESS' then TRADE_AMOUNT else null end)/100 amount from ifpay_ccpay.trade_info,ifpay_ccpay.merchant a where MERCHANT_NO=a.MERCHANT_id and CREATE_TIME > curdate() group by a.MERCHANT_name) a ")
except Exception as e:
print('Is Wrong')
print(e) 輸出結果:
Grafana輸出