def main(filename): fp = open(filename, 'r') data = json.load(fp) fp.close() detector_data = data[0]['datapoints'] timeseries = [t[0] for t in detector_data] SpikeDetector.convert_null_values(timeseries) timestamps = [t[1] for t in detector_data] orig_series = timeseries[:] orig_stamps = timestamps[:] res = DETECTOR.detect_anomalies(timeseries, timestamps) plot_data(filename, (orig_stamps, orig_series), (timestamps, timeseries), res)
def plot_data(title, original_data, processed_data, results, show_=True): """Plots results for timeseries""" mean = numpy.mean(SpikeDetector._get_local_maxima(original_data[1])) r_t, r_d = [], [] for t, v in results: r_t.append(t) r_d.append(v) start = original_data[0][0] end = original_data[0][-1] figure(title) plot(processed_data[0], processed_data[1], r_t, r_d, "rs", original_data[0], original_data[1], "g", [start, end], [mean, mean], "r") if show_: show()
def plot_data(title, original_data, processed_data, results, show_ = True): """Plots results for timeseries""" mean = numpy.mean(SpikeDetector._get_local_maxima(original_data[1])) r_t, r_d = [], [] for t, v in results: r_t.append(t) r_d.append(v) start = original_data[0][0] end = original_data[0][-1] figure(title) plot(processed_data[0], processed_data[1], r_t, r_d, "rs", original_data[0], original_data[1], "g", [start, end], [mean, mean], "r") if show_: show()
import sys from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter from detector import SpikeDetector DESCRIPTION = "Generates CSV report with spikes found on Graphite hosts" # Sleep value bethween requests to same host DEFAULT_SLEEP_VAL = 1 # Limit of requests per host DEFAULT_HOST_LIMIT = 500 # Output file DEFAULT_OUTPUT_FILE = None # Detector class DETECTOR = SpikeDetector() def get_host_targets(host_string, pattern): """Returns targets that match pattern from a graphite host""" targets = [] try: response = requests.get('http://%s/metrics/index.json' % host_string) targets = json.loads(response.text) targets = [t for t in targets if fnmatch.fnmatch(t, pattern)] except Exception as e: print 'Exception occured while getting host target: %s' % e finally: