Overview
2025年9月5日小于 1 分钟
api
import requests
import json
# Prometheus API 地址
PROM_URL = "http://103.118.40.237:30090/api/v1/query"
def get_prometheus_metric(query):
try:
response = requests.get(PROM_URL, params={'query': query}, timeout=10)
response.raise_for_status()
data = response.json()
if data['status'] != 'success' or not data['data']['result']:
print("没有获取到数据")
return None
# 提取指标值
value = data['data']['result'][0]['value']
timestamp = value[0]
metric_value = round(int(value[1]) / 1024 /1024 / 1024, 2)
print(f"时间戳: {timestamp}, 指标值: {metric_value}")
return metric_value
except requests.RequestException as e:
print(f"请求 Prometheus 失败: {e}")
return None
if __name__ == "__main__":
# 查询表达式
query = 'increase(node_network_transmit_bytes_total{device="ens3",instance="cdnone"}[360d])'
r = get_prometheus_metric(query)
query = 'node_network_transmit_bytes_total{device="ens3",instance="cdntwo"}'
r2 = get_prometheus_metric(query)
msg = f"\U0001F4BB服务器: cdnone \U0001F6DC当前出网流量总计: {r} G \n\U0001F4BB服务器: cdntwo \U0001F6DC当前出网流量总计: {r2} G"
nohup ./prometheus &
mysqld_exporter
# 配置mysqld_exporter
vi .my.cnf
[client]
user=root
password=Mysql@2023
port=3306
# 启动mysqld_exporter
监控数据量大小
./mysqld_exporter --web.telemetry-path=/metrics --collect.info_schema.innodb_tablespaces
mysql_info_schema_innodb_tablespace_file_size_bytes
https://github.com/prometheus/mysqld_exporter
模糊查询使用 ~
mysql_info_schema_innodb_tablespace_file_size_bytes{instance="192.168.3.204:9104", job="mysql-105", tablespace_name=~"cs.*"}
# prometheus 配置
- job_name: 'mysql-105' # 给被监控主机取个名字
static_configs:
- targets: ['192.168.3.204:9104'] # 这里填写被监控主机的IP和端口