silence list

# 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

image.png

image.png


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

image.png

image.png

image.png

报警消息列表

import requests, json, time, datetime
from influxdb import InfluxDBClient

DataBasename = "ipxx"
conn_db = InfluxDBClient('10.26.5.xx', '8086', 'litx', 'xxxx', DataBasename)

time_now = time.strftime('%Y-%m-%d %H:%M:%S')
messsage_temp = '#### [报警静默期剩余时间-提醒]\n> <font color="#FF0000">剩余小时</font>:silence_hours17 \n> <font color="#FF0000">开始时间</font>:startsAt17 \n> <font color="#FF0000">结束时间</font>:endsAt17 \n> <font color="#FF0000">静默条件</font>:matchers17 \n> <font color="#FF0000">创建原因</font>:comment17'
#wx_url = 'https://qyapi.weixin.qq.com/xx' ##pro
wx_url = 'https://qyapi.weixin.qqxxxxxxxxxxxxx' ##test
def send_markdown_msg(content, wx_url):
    data = json.dumps({"msgtype": "markdown", "markdown": {"content": content}})
    r = requests.post(wx_url, data, auth=('Content-Type', 'application/json'))
    print(r.json)


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 instert_alertmanager_mes(table, trigger_minute, startsAt, mes, ip, city,address, status, devicetype, service):
    silence_hours1 = int(trigger_minute)
    json_body = [
        {
            "measurement": table,
            "tags": {
                "startsAt": startsAt,
                "status": status,
                "city": city,
                'devicetype': devicetype,
                'service': service,
                "address": address,
                "ip": ip,
                "mes": mes
            },
            "fields": {"trigger_minute": 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_alerts_url = 'http://alertmanager.int.xiaohongshu.com/api/v2/alerts'
json_alertmanager_alerts = requests.get(alertmanager_alerts_url)
list_alertmanager_alerts = json_alertmanager_alerts.json()

print("###json", list_alertmanager_alerts)
alerts_mes_list = []

for i in list_alertmanager_alerts:
    #print("json i:", i)
    alert_dic = {}
    alert_dic['mes'] = i['annotations']['description'] + '--' + i['annotations']['summary']
    alert_dic['stauts'] = i['status']['state']
    start_time = transformation_time(i['startsAt'])
    alert_dic['startsAt'] = start_time
    alert_dic['ip'] = i["labels"]['instancehost']
    alert_dic['city'] = i["labels"]['city']
    alert_dic['devicetype'] = i["labels"]['devicetype']
    alert_dic['service'] = i["labels"]['service']
    alert_dic['address'] = i["labels"]['city'] + i["labels"]["building"] + i["labels"]["floor"]
    d_start = datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S')
    d_now = datetime.datetime.strptime(time_now, '%Y-%m-%d %H:%M:%S')
    delta = d_now - d_start
    #print(int(delta.seconds/3600) + delta.days * 24)
    alert_dic['trigger_minute'] = int(delta.seconds/60) + delta.days * 24 * 60
    alerts_mes_list.append(alert_dic)

print(alerts_mes_list)
for i in alerts_mes_list:
    trigger_minute = i['trigger_minute']
    startsAt = i['startsAt']
    mes = i['mes']
    ip = i['ip']
    city = i['city']
    address = i['address']
    status = i['stauts']
    service = i['service']
    devicetype = i['devicetype']
    instert_alertmanager_mes('alertmanager_megass', trigger_minute, startsAt, mes, ip, city, address, status, devicetype, service)




image.png

image.png

image.png


锅包肉
89 声望17 粉丝

这个人很懒,没有什么说的。