QVolution2019.2/AoM_Service/AoM_Configs/show_config.py

105 lines
3.6 KiB
Python
Executable File

import glob
import yaml
import json
import os
import sys
import time
import re
import requests
import numpy
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
import random
import warnings
warnings.filterwarnings("ignore")
#from pdb import set_trace as bp
timeout = 180
# if no argument print help and exit
if len(sys.argv) == 1:
print("You need to specify an alert config file.")
exit(1)
#else
config_file = 'alert_configs/'+sys.argv[1]+'.yaml'
# test file exists or exit
alert_config = yaml.load(open(config_file, 'rb').read())
# We will show 10 intervals by default
if len(sys.argv) == 3:
interval = int(sys.argv[2])
else:
interval = 10
alert_config['query']['start_relative']['value'] = str(int(alert_config['query']['start_relative']['value'])*interval)
kairosdb_url = "http://kairosdb-metrics.service.eng.consul:8080/"
query_url = os.path.join(kairosdb_url + "api/v1/datapoints/query")
#ret = requests.post(query_url, data=json.dumps(query), timeout)
ret = requests.post(query_url, json.dumps(alert_config['query']), timeout)
results = ret.json()['queries'][0]['results']
# Transforming to human readable data
# for result in results[0]['values']:
# result[0] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(result[0]/1000))
# result[0] = datetime.datetime.strptime(result[0],'%Y-%m-%d %H:%M:%S')
for result in results:
for value in result['values']:
# bp()
# transform date from epoch to human readable format
value[0] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(value[0]/1000))
# transform date string to datetime object
value[0] = datetime.datetime.strptime(value[0],'%Y-%m-%d %H:%M:%S')
series = numpy.array(result['values'])
label_str = str(result['group_by'][0].get('group', ''))
line_color = tuple(numpy.random.random(size=3))
plt.plot_date(series[:,0],series[:,1], marker='.', color=line_color, linestyle='-', label=label_str)
#series = numpy.array(results[0]['values'])
#converted_dates = map(datetime.datetime.strptime, datelist, len(datelist)*['%Y-%m-%d %H:%M:%S'])
#x_axis = (converted_dates)
formatter = mdates.DateFormatter('%H:%M:%S')
# ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m"))
# series = series.astype(numpy.unicode, copy=False)
ax = plt.subplot()
#ax.set_xlabel('TIME')
#ax.set_ylabel('VALUE')
#bc = plt.axes()
#bc.xaxis.set_major_formatter(formatter)
#plt.plot_date(series[:,0],series[:,1], marker='o', color='b', linestyle='-')
#plt.plot_date(converted_dates,series[:,1], marker='o', color='b', linestyle='-')
#ax.set_xticks(series[:,0])
#ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m"))
#ax.xaxis.set_minor_formatter(mdates.DateFormatter("%Y-%m"))
# ax = plt.subplot.gcf().axes[0]
#ax.set_title(sys.argv[1])
ax.xaxis.set_major_formatter(formatter)
#plt.xaxis.set_major_formatter(formatter)
plt.title(sys.argv[1])
plt.legend()
# pyplot.gcf().autofmt_xdate(rotation=25)
#ax.xaxis_date()
# ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m"))
# ax.xaxis.set_minor_formatter(mdates.DateFormatter("%Y-%m"))
# ax.plot(series[:,0],series[:,1], marker='o', color='b', linestyle='-')
myRe = re.compile('^(?!occurrences).*_threshold$')
# Adding thresholds to the graph
for key in alert_config:
if myRe.match(key):
plt.axhline(y=float(alert_config[key]), color='r', linestyle='--', label=str(key))
plt.text(series[0][0],float(alert_config[key]),key)
#plt.gcf().autofmt_xdate()
#ax = .add_axes([0,0,1,1])
plt.gcf().autofmt_xdate(rotation=25)
#plt.axhline(y=500000, color='o', linestyle='-')
plt.show()
#results[0]['values']