Skip to content

Commit

Permalink
added: get_graph_compare
Browse files Browse the repository at this point in the history
  • Loading branch information
delong1 committed Jun 27, 2016
1 parent c97e314 commit 142b00d
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions graphite_beacon/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,9 @@ def get_value_for_expr(self, expr, target):
rvalue = sum(history) / float(len(history))

if rvalue == COMPARISON:
LOGGER.info("COMPARISON TEST [%s]", self.compare_size)
rvalue = 100
rvalue = self.get_graph_comparison()

rvalue = expr['mod'](rvalue)
LOGGER.info("rvalue [%s]", rvalue)
return rvalue

def notify(self, level, value, target=None, ntype=None, rule=None):
Expand All @@ -232,6 +230,10 @@ def load(self):
"""Load from remote."""
raise NotImplementedError()

def get_graph_comparison(self):
"""Only from graphite."""
raise NotImplementedError()


class GraphiteAlert(BaseAlert):

Expand Down Expand Up @@ -300,6 +302,30 @@ def _graphite_url(self, query, raw_data=False, graphite_url=None):
url = "{0}&rawData=true".format(url)
return url

def get_graph_comparison(self):
time_shift = self.compare_size
if time_shift[0] != '-':
time_shift = '-' + time_shift

query = 'timeShift(' + self.query + ', "' + time_shift + '")'
url = self._graphite_url(
query, graphite_url=self.reactor.options.get('graphite_url'), raw_data=True)

http_client = hc.HTTPClient()
try:
response = http_client.fetch(url, auth_username=self.auth_username,
auth_password=self.auth_password,
request_timeout=self.request_timeout,
connect_timeout=self.connect_timeout)
record = GraphiteRecord(response.body.decode('utf-8'), self.default_nan_value, self.ignore_nan)
value = getattr(record, self.method)
LOGGER.debug("%s [%s]: %s", self.name, record.target, value)
return value
except Exception as e:
LOGGER.error('%s', str(e))
self.notify('warning', 'No data to compare', target='comparison', ntype='common')
return 0


class URLAlert(BaseAlert):

Expand Down

0 comments on commit 142b00d

Please sign in to comment.