前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于Prometheus的监控告警系统的Python开发(三)

基于Prometheus的监控告警系统的Python开发(三)

原创
作者头像
保持热爱奔赴山海
发布2024-10-01 08:39:46
910
发布2024-10-01 08:39:46
举报
文章被收录于专栏:DevOps

篇一、 https://cloud.tencent.com/developer/article/2195953

篇二、 https://cloud.tencent.com/developer/article/2395624

篇一里面,我的告警截图是调用的chrome来做的,这种代码写起来比较简单,但是缺点就是速度比较慢。因此这里再补充一个基于python的matplotlib的绘图方法。

绘图的代码如下:

代码语言:python
代码运行次数:0
复制
# -*- coding: utf-8 -*-
# 调用prometheus接口,将指定表达式的趋势图绘制出来
# 参考阳明的博客 https://cloud.tencent.com/developer/article/1972462

# pip install matplotlib

import requests
from datetime import datetime, timedelta
import json
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

def draw_pic(raw_data):
    # 创建图形和轴
    fig, ax = plt.subplots(figsize=(10, 6))

    # 设置时间格式
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S'))

    # 遍历每个指标并绘制
    for metric in raw_data:

        # 将其它自定义的label提取出来
        _lab = ""
        for ii in list(raw_data[0]['metric'].keys()):
            if ii not in ['instance','job']: 
                _tmp=metric['metric'][ii]
                _lab = _lab + " - " + _tmp
    
        # 提取时间戳和值
        timestamps = [datetime.fromtimestamp(ts) for ts, _ in metric['values']]
        values = [value for _, value in metric['values']]
        
        # 拼接出metrics的label
        label = f"{metric['metric']['job']} ({metric['metric']['instance']}) {_lab}"

        
        # 绘制时间序列
        ax.plot(timestamps, values, marker='o', label=label)

    # 添加图例
    ax.legend()

    # 设置标题和标签
    ax.set_title('Prometheus Metrics')
    ax.set_xlabel('Time')
    ax.set_ylabel('Value')

    plt.xticks(rotation=45, ha='right')  # 旋转45度,并水平对齐到右侧,为了排版更好看

    # 自动调整布局
    plt.tight_layout()

    # 保存图表到文件
    output_filename = 'metrics_chart.png'
    plt.savefig(output_filename, dpi=300)  # 可以根据需要更改dpi和文件格式



def metrics(server, query, query_time, duration, step):
    # 计算开始时间和结束时间
    start = query_time - duration
    end = query_time

    # 构造查询参数
    params = {
        'query': query,
        'start': int(start.timestamp()),
        'end': int(end.timestamp()),
        'step': step
    }

    # 发送请求到 Prometheus
    try:
        response = requests.get(f"http://{server}/api/v1/query_range", params=params)
        response.raise_for_status()  # 如果响应状态码不是200,抛出异常
    except requests.RequestException as e:
        return None, f"Failed to query Prometheus: {e}"

    # 解析返回的数据
    data = response.json()
    if data['status'] != 'success':
        return None, f"Query failed: {data.get('error', 'Unknown error')}"

    # 获取结果
    result = data['data']['result']
    
    # 将结果转换为类似于 prometheus api的数据结构
    matrix = []
    for item in result:
        metric = {
            'metric': item['metric'],
            'values': [(float(ts), float(value)) for ts, value in item['values']]
        }
        matrix.append(metric)

    return matrix, None



if __name__ == "__main__":
    server = "localhost:9090"
    
    # 告警表达式
    query = 'irate(node_disk_written_bytes_total[5m])>100000'

    # 时间范围和步长
    query_time = datetime.now()
    duration = timedelta(minutes=60)  # 只拉取最近1小时的数据,画出来的指标不会太拥挤
    step = 15  # 每15秒采样一个点

    result, err = metrics(server, query, query_time, duration, step)

    if err:
        print(err)
    else:
        # print(json.dumps(result))
        draw_pic(raw_data=result)
        print("image saved")

绘制的图片大致长这样:

告警截图
告警截图

实际测试,上面这个绘图大概耗时3秒,而之前调用chrome进行绘图大概要10秒。性能提升了3倍左右。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Prometheus 监控服务
Prometheus 监控服务(TencentCloud Managed Service for Prometheus,TMP)是基于开源 Prometheus 构建的高可用、全托管的服务,与腾讯云容器服务(TKE)高度集成,兼容开源生态丰富多样的应用组件,结合腾讯云可观测平台-告警管理和 Prometheus Alertmanager 能力,为您提供免搭建的高效运维能力,减少开发及运维成本。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档