#!/usr/bin/python3.6 # -*- encoding: UTF-8 -*- import json import requests import time now_time=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) def prometheus_alarm(): url='http://xxx.xxx.xxx.xxx:9093/api/v1/alerts' result=requests.get(url).json() list1='截至:{0} \n'.format(now_time) list1=list1 + '目前prometheus生产未恢复告警列表: \n\n' if not result['data']: list1=list1+'暂无未恢复告警. \n\n' else: for alert in result['data']: #print(alert) alertname=alert['labels']['alertname'] instance=alert['labels']['instance'] try: description = alert['annotations']['description'] except KeyError: description = alert['annotations']['summary'] try: value = alert['annotations']['value'] except KeyError: value='' alarm_time=alert['startsAt'] alarm_time=format_time(format_time=alarm_time,type_time='p') #print(eventcases_data) list1=list1 +'主机名: '+ instance +'\n告警项: '+ alertname +'\n告警数据:'+ value +'\n告警注释: '+ description +'\n目前状态: '+ 'PROBLEM' +'\n告警产生时间: '+ alarm_time +'\n------------------------------------------------------\n' return list1