关于grafana:使用grafana看板展示alertmanager静默列表

34次阅读

共计 2231 个字符,预计需要花费 6 分钟才能阅读完成。

# 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 InfluxDBClient

DataBasename = "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 result8

alertmanager_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 * 24

        for 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'])

正文完
 0