# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024/1/30 9:42
# @Function : Prometheus client
import os
import time
from prometheus_client import start_http_server,Gauge
#定义数据类型,metric,describe(描述),标签
#统计altermanager的告警配置数量
altermanager_cnf_num = Gauge('altermanager_cnf_num','Count the number of alarm',['instance'])
def get_altermanager_cnf_num():
#获取指标
altermanager_cnf_path = "/opt/prometheus/rules"
altermanager_cnf_nums = os.popen(f"ls -l {altermanager_cnf_path} | grep -v 'total' | wc -l").read().strip()
#获取实例名称
hostname = os.popen("hostname").read().strip()
altermanager_cnf_num.labels(instance=hostname).set(altermanager_cnf_nums)
if __name__ == '__main__':
#启动web服务
start_http_server(80)
while True:
#不断执行函数更新数据源
get_altermanager_cnf_num()
#更新间隔
time.sleep(3)
##运维开发如何提交作品? 代码只能写1000行,没有这么少的代码啊