|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" A class, which implements the metric API of graphite """ |
| 5 | + |
| 6 | +from __future__ import print_function, absolute_import |
| 7 | + |
| 8 | +import os |
| 9 | +import urlparse |
| 10 | + |
| 11 | +from pandas_metricsreader.BaseReader import BaseReader |
| 12 | + |
| 13 | +class GraphiteMetricsAPI(BaseReader): |
| 14 | + """ |
| 15 | + Creates a GraphiteMetricAPI object, which you can use to request, which |
| 16 | + metrics are available in the graphite cluster |
| 17 | +
|
| 18 | + Arguments: |
| 19 | + url: string |
| 20 | + the base url to the Graphite host |
| 21 | + tls_verify: string or bool |
| 22 | + enable or disable certificate validation. You can als specify the |
| 23 | + path to a certificate or a directory, which must have been |
| 24 | + processed using the c_rehash utily supplied with OppenSSL |
| 25 | + (default: True) |
| 26 | + session: a requests.Session object (default None) |
| 27 | + timeout: float or tuple |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__(self, |
| 31 | + url, |
| 32 | + tls_verify='/etc/ssl/certs/', |
| 33 | + session=None, |
| 34 | + timeout=30, |
| 35 | + ): |
| 36 | + self._metrics_api = '/metrics' |
| 37 | + |
| 38 | + super(GraphiteMetricsAPI, self).__init__( |
| 39 | + url=url, |
| 40 | + tls_verify=tls_verify, |
| 41 | + session=session, |
| 42 | + timeout=timeout, |
| 43 | + ) |
| 44 | + |
| 45 | + def find(self, target, start=None, end=None): |
| 46 | + """ |
| 47 | + Finds metrics under a given path. |
| 48 | + """ |
| 49 | + url_path = os.path.join(self._metrics_api, 'find') |
| 50 | + url = urlparse.urljoin(self.url, url_path) |
| 51 | + params = { 'query': target, |
| 52 | + 'formater': 'treejson', |
| 53 | + 'from': start, |
| 54 | + 'until': end, |
| 55 | + 'wildcards': 0, |
| 56 | + } |
| 57 | + r = self._get(url, params=params) |
| 58 | + return r.json() |
| 59 | + |
| 60 | + def expand(self, targets, group_by_expr=False, leaves_only=False): |
| 61 | + """ |
| 62 | + Expands the given query with matching paths. |
| 63 | + """ |
| 64 | + if not isinstance(group_by_expr, bool): |
| 65 | + raise TypeError('group_by_expr has to be of type bool') |
| 66 | + if not isinstance(leaves_only, bool): |
| 67 | + raise TypeError('leaves_only has to be of type bool') |
| 68 | + |
| 69 | + if group_by_expr: |
| 70 | + group_by_expr = 1 |
| 71 | + else: |
| 72 | + group_by_expr = 0 |
| 73 | + |
| 74 | + if leaves_only: |
| 75 | + leaves_only = 1 |
| 76 | + else: |
| 77 | + leaves_only = 0 |
| 78 | + |
| 79 | + url_path = os.path.join(self._metrics_api, 'expand') |
| 80 | + url = urlparse.urljoin(self.url, url_path) |
| 81 | + params = { 'query': targets, |
| 82 | + 'groupByExpr':group_by_expr, |
| 83 | + 'leavesOnly':leaves_only, |
| 84 | + } |
| 85 | + r = self._get(url, params=params) |
| 86 | + return r.json()['results'] |
| 87 | + |
| 88 | + def index(self): |
| 89 | + """ |
| 90 | + Walks the metrics tree and returns every metric found as a sorted JSON |
| 91 | + array. |
| 92 | + """ |
| 93 | + raise NotImplementedError |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + print(__doc__) |
0 commit comments