使用grafana看板展示alertmanager静默列表
# Alert
GET/api/v2/alerts
POST/api/v2/alerts# AlertGroup
GET/api/v2/alerts/groups# General
GET/api/v2/status# Receiver
GET/api/v2/receivers# Silence
GET/api/v2/silences
POST/api/v2/silences
GET/api/v2/silence/{silenceID}
DELETE /api/v2/silence/{silenceID}
- /api/v2/silences
文章图片
文章图片
import requests, json, time, datetime
from influxdb import InfluxDBClientDataBasename = "ixxxx"
conn_db = InfluxDBClient('10.26.x.xx', '8086', 'litx', 'xxxxx', DataBasename)time_now = time.strftime('%Y-%m-%d %H:%M:%S')def instert_alertmanager_silenced(table, silence_hours, startsAt, endsAt, matchers):
silence_hours1 = int(silence_hours)
json_body = [
{
"measurement": table,
"tags": {
"startsAt": startsAt,
"endsAt": endsAt,
"matchers": matchers
},
"fields": {"silence_hours": silence_hours1}
}
]
conn_db.write_points(json_body)# 写入数据,同时创建表def transformation_time(time_initial):
time_trans = time_initial.split(".")
# print(time_trans[0])
result = datetime.datetime.strptime(time_trans[0], '%Y-%m-%dT%H:%M:%S')
result8 = (result + datetime.timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S")
# print(result, result8)
return result8alertmanager_silence_url = 'http://alertmanager.int.xiaxxxshu.com/api/v2/silences'
json_alertmanager_silence = requests.get(alertmanager_silence_url)
list_alertmanager_silence = json_alertmanager_silence.json()#print(list_alertmanager_silence)
silence_activce_list = []
for i in list_alertmanager_silence:
if i['status']['state'] == 'active':
silence_dic = {}end_time = i['endsAt']
silence_dic['startsAt'] = i['startsAt']
silence_dic['endsAt'] = end_time
#print(transformation_time(i['startsAt']), transformation_time(i['endsAt']), time_now)
d_end = datetime.datetime.strptime(transformation_time(end_time), '%Y-%m-%d %H:%M:%S')
d_now = datetime.datetime.strptime(time_now, '%Y-%m-%d %H:%M:%S')
delta = d_end - d_now
#print(int(delta.seconds/3600) + delta.days * 24)
silence_dic['silence_hours'] = int(delta.seconds/3600) + delta.days * 24for j in i['matchers']:
matchers_list = []
matchers_dic = {}
matchers_dic['name'] = j['name']
matchers_dic['value'] = j['value']
matchers_list.append(matchers_dic)
silence_dic['matchers'] = matchers_list
silence_activce_list.append(silence_dic)print(silence_activce_list)
for i in silence_activce_list:
instert_alertmanager_silenced(table='alertmanager_silenced', silence_hours=i['silence_hours'], startsAt=i['startsAt'], endsAt=i['endsAt'], matchers=i['matchers'])
文章图片
文章图片
【使用grafana看板展示alertmanager静默列表】
文章图片
推荐阅读
- AOP面向切面的编程使用Interceptor内置的容器进行属性注入
- 【附下载】汉化版Awvs安装与简单使用
- 实现领域驱动设计|实现领域驱动设计 - 使用ABP框架 - 创建实体
- 华为AppLinking中统一链接的创建和使用
- springCloud|nacos简单使用
- nacos|nacos介绍及使用
- idea部分内容|idea2021中Activiti BPMN visualizer简单使用
- idea部分内容|idea中terminal使用npm报不是内部或外部命令,也不是可运行的程序 或批处理文件。
- DevStream 进入 CNCF 沙箱,为 DevOps 工具提供包管理器一样的使用体验
- 使用dockerfile部署springboot应用