当前位置: 首页 > news >正文

sql_exporter通过sql收集业务数据并通过prometheus+grafana展示

下载并解压安装sql_exporter

wget https://github.com/free/sql_exporter/releases/download/0.5/sql_exporter-0.5.linux-amd64.tar.gz
#解压
tar xvf sql_exporter-0.5.linux-amd64.tar.gz -C /usr/local/

修改主配置文件

cd /usr/local/
mv sql_exporter-0.5.linux-amd64 sql_exporter
cd /usr/local/sql_exporter
cat /usr/local/sql_exporter/config.yml
# Global defaults.
global:# Subtracted from Prometheus' scrape_timeout to give us some headroom and prevent Prometheus from timing out first.scrape_timeout_offset: 500ms# Minimum interval between collector runs: by default (0s) collectors are executed on every scrape.min_interval: 0s# Maximum number of open connections to any one target. Metric queries will run concurrently on multiple connections,# as will concurrent scrapes.max_connections: 3# Maximum number of idle connections to any one target. Unless you use very long collection intervals, this should# always be the same as max_connections.max_idle_connections: 3# Maximum number of maximum amount of time a connection may be reused. Expired connections may be closed lazily before reuse.# If 0, connections are not closed due to a connection's age.max_connection_lifetime: 5m# The target to monitor and the collectors to execute on it.
target:# Data source name always has a URI schema that matches the driver name. In some cases (e.g. MySQL)# the schema gets dropped or replaced to match the driver expected DSN format.data_source_name: 'mysql://prometheus_user:prometheus_password@dbIP:3306/DBname'# Collectors (referenced by name) to execute on the target.# Glob patterns are supported (see <https://pkg.go.dev/path/filepath#Match> for syntax).collectors: [DBname*]# Collector files specifies a list of globs. One collector definition is read from each matching file.
# Glob patterns are supported (see <https://pkg.go.dev/path/filepath#Match> for syntax).
collector_files: - "/usr/local/sql_exporter/DBname_tables.yml"

编写sql查询配置文件

cat /usr/local/sql_exporter/DBname_tables.ymlcollector_name: DBname_tables
metrics:
- metric_name: member_register_count_todaytype: gaugehelp: '当日会员注册数'values: [member_register_count_today]query: |SELECT COUNT(0) as 'member_register_count_today' FROM manage_member WHERE DATE(create_time)=CURDATE()

编写启动脚本并启动

cat > /usr/lib/systemd/system/sql-exporter-DBname.service <<EOF
[Unit]
Description=sql_exporter[Service]
ExecStart=/usr/local/sql_exporter/sql_exporter -config.file /usr/local/sql_exporter/config.yml  -web.listen-address 0.0.0.0:9501
Restart=on-failure[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable sql-exporter-DBname.service --now

接入prometheus

  - job_name: 'sql'scrape_interval: 1mscrape_timeout: 50sstatic_configs:- targets:- 'localhost:9501'

prometheus热加载生效

curl -X POST http://localhost:9090/-/reload

prometheus查看监控指标是否收集

在这里插入图片描述
curl http://localhost:9501/metrics
在这里插入图片描述

grafana配置监控面板

在这里插入图片描述

配置会员注册趋势

#当日
floor(delta(member_register_count_today[$__rate_interval]))>0 or (member_register_count_today*0)
昨日
floor(delta(member_register_count_today[$__rate_interval] offset 1d)) > 0 or (member_register_count_today*0)

在这里插入图片描述

http://www.lryc.cn/news/404798.html

相关文章:

  • pytorch 笔记:torch.optim.Adam
  • 开源AI智能名片小程序:深度剖析体验优化策略,激活小程序生命力的运营之道
  • ML.Net 学习之使用经过训练的模型进行预测
  • 为什么 centos 下使用 tree 命令看不见 .env 文件
  • 数据库基础与性能概述及相关术语
  • docker基于外部缓存加速构建方案
  • 【C语言】 作业11 链表+实现函数封装
  • 【Ubuntu】Ubuntu20修改MAC地址
  • ClickHouse集成LDAP实现简单的用户认证
  • C语言-预处理详解
  • 计算机网络-VLAN间通信(三层通信)模拟实现
  • 【JAVA】数据类型及变量
  • 微软蓝屏事件暴露的网络安全问题
  • 11 - FFmpeg - 编码 AAC
  • OS Copilot初体验的感受与心得
  • Ajax学习笔记
  • 医学深度学习与机器学习融合的随想
  • 坑人的macos tar 命令 (实际上是bsdtar)换用 gnu tar
  • 【SpringBoot3】全局异常处理
  • vue-Treeselect
  • 【机器学习框架TensorFlow和PyTorch】基本使用指南
  • matlab 中的methods(Access = protected) 是什么意思
  • 【漏洞复现】Netgear WN604 downloadFile.php 信息泄露漏洞(CVE-2024-6646)
  • 图像处理 -- ISP调优(tuning)的步骤整理
  • 【中项】系统集成项目管理工程师-第4章 信息系统架构-4.2系统架构
  • node.js中nodemon : 无法加载和使用问题,这是由于windows安全策略影起的按如下操作即可
  • 【SD】 Stable Diffusion(SD)原理详解与ComfyUI使用 2
  • 【学习笔记】无人机系统(UAS)的连接、识别和跟踪(七)-广播远程识别码(Broadcast Remote ID)
  • VMware 虚拟机 ping 不通原因排查
  • websocket状态机