Skip to content

Commit 5abb758

Browse files
committed
Add graphite metrics API
1 parent 97d2bba commit 5abb758

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

docs/source/text/data_access.rst

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Graphite
88

99
.. autoclass:: GraphiteReader
1010
:members:
11+
:inherited-members:
1112

1213
PNP4nagios
1314
----------

pandas_metricsreader/graphite/graphite.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
from pandas import read_csv, MultiIndex, concat, DataFrame, to_datetime
1010
from pandas.compat import StringIO, string_types
1111

12-
from pandas_metricsreader.BaseReader import BaseReader, MetricsReaderError
12+
from pandas_metricsreader.BaseReader import MetricsReaderError
13+
from .metricsAPI import GraphiteMetricsAPI
1314

14-
class GraphiteReader(BaseReader):
15+
class GraphiteReader(GraphiteMetricsAPI):
1516
"""
1617
Creates a GraphiteDataReader object, which you can use to read different
1718
metrics in a pandas DataFrame
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)