参考资料:
 
行列结合,作为prometheus_client的输出。 话不多说,直接上脚本。
 
cat mysql_ccpay.py
#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)
            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])
            print(bs_name)
            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)
                            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(9500)
    ccpayGauge = Gauge('ccpayGauge','Description of gauge', ['mylabelname'])
    while True:
        time.sleep(60)
        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输出

Python3.5+SQL+Prometheus+Grafana报表/监控的更多相关文章

  1. Spring Boot 微服务应用集成Prometheus + Grafana 实现监控告警

    Spring Boot 微服务应用集成Prometheus + Grafana 实现监控告警 一.添加依赖 1.1 Actuator 的 /prometheus端点 二.Prometheus 配置 部 ...

  2. Prometheus+Grafana企业监控系统

    Prometheus+Grafana企业监控系统 作者 刘畅 实验配置: 主机名称 Ip地址 controlnode 172.16.1.70/24 slavenode1 172.16.1.71/24 ...

  3. go-zero docker-compose 搭建课件服务(七):prometheus+grafana服务监控

    0.转载 go-zero docker-compose 搭建课件服务(七):prometheus+grafana服务监控 0.1源码地址 https://github.com/liuyuede123/ ...

  4. Prometheus+Grafana搭建监控系统

    之前在业务中遇到服务器负载过高问题,由于没有监控,一直没发现,直到业务方反馈网站打开速度慢,才发现问题.这样显得开发很被动.所以是时候搭建一套监控系统了. 由于是业余时间自己捯饬,所以神马业务层面的监 ...

  5. prometheus+grafana实现监控过程的整体流程

    prometheus安装较为简单,下面会省略安装步骤: 一.服务器启动 Prometheus启动 ./prometheus --config.file=prometheus.yml Grafana启动 ...

  6. Prometheus+Grafana+kafka_exporter监控kafka

    Prometheus+Grafana+kafka_exporter搭建监控系统监控kafka 一.Prometheus+Grafana+kafka_exporter搭建监控系统监控kafka 1.1K ...

  7. 基于Centos7.4搭建prometheus+grafana+altertManger监控Spring Boot微服务(docker版)

    目的:给我们项目的微服务应用都加上监控告警.在这之前你需要将 Spring Boot Actuator引入 本章主要介绍 如何集成监控告警系统Prometheus 和图形化界面Grafana 如何自定 ...

  8. k8s实战之部署Prometheus+Grafana可视化监控告警平台

    写在前面 之前部署web网站的时候,架构图中有一环节是监控部分,并且搭建一套有效的监控平台对于运维来说非常之重要,只有这样才能更有效率的保证我们的服务器和服务的稳定运行,常见的开源监控软件有好几种,如 ...

  9. Kubernetes prometheus+grafana k8s 监控

    参考: https://www.cnblogs.com/terrycy/p/10058944.html https://www.cnblogs.com/weiBlog/p/10629966.html ...

随机推荐

  1. vim的个性化配置- 再谈vim的折叠和展开 -- 彻底掌握vim 的展开和折叠!

    http://www.wklken.me/posts/2016/02/03/some-vim-configs.html 一般把 设置成 逗号, 是比较好的, 因为逗号比默认的leader  要方便键入 ...

  2. Elasticsearch 基础概念知识

    接近实时(NRT) Elasticsearch是一个接近实时的搜索平台.这意味着,从索引一个文档直到这个文档能够被搜索到有一个轻微的延迟(通常是1秒). 集群(cluster) 一个集群就是由一个或多 ...

  3. win10中mount和unmount iso文件

    https://www.windowscentral.com/how-mount-or-unmount-iso-images-windows-10 You can also right-click t ...

  4. linux 下 安装nginx

    http://www.cnblogs.com/lovexinyi8/p/5845017.html 测试可用. 参看 https://www.cnblogs.com/liujuncm5/p/671378 ...

  5. Java自学入门新的体会0.2

    Java 基本数据类型 变量就是申请内存来存储值,也就是说,当创建变量的时候,需要在内存中申请空间. 内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来存储该类型数据. 因此,通过定义不 ...

  6. 【转载】TCP慢启动、拥塞避免、快速重传、快速回复

    转载自:TCP慢启动.拥塞避免.快速重传.快速回复 转自:http://blog.csdn.net/itmacar/article/details/12278769 感谢博主的辛勤成果! 为了防止网络 ...

  7. 网页图片提取助手(支持背景图、选择dom范围)

    网页图片提取助手(支持背景图.选择dom范围) 网页图片下载工具.网页图片批量保存. 使用场景: 作为web前端开发首——学习小生的你我,仿学在线页面是常有的事,但是一些在线资源,比如图片,图片有im ...

  8. 03_Flume多节点Failover实践

    1.实践场景 模拟上游Flume Agent在发送event时的故障切换 (failover) 1)初始:上游Agent向active的下游节点Collector1传递event 2)Collecto ...

  9. 样本打散后计算单特征 NDCG

    单特征 NDCG 能计算模型的 NDCG,也就能计算单特征的 NDCG,用于评估单特征的有效性,跟 Group AUC 用途一样 单特征 NDCG 如何衡量好坏 如果是 AUC,越大于或小于 0.5, ...

  10. Use a layout_width of 0dip instead of fill_parent for better performance

    安装了最新的ATD 18之后,新加的Lint Warnings插件会给我们检测出许多xml布局中不当的地方,例如: Use a layout_width of 0dip instead of fill ...