Permalink
Browse files

Add stub of OpenTSDB adapter

  • Loading branch information...
1 parent 0073c3a commit c5d29d5d1ac0055e6eedb1b2757330ec20ee250d @oldpatricka oldpatricka committed Nov 16, 2012
Showing with 101 additions and 0 deletions.
  1. +57 −0 epu/sensors/opentsdb.py
  2. +44 −0 epu/sensors/test/test_opentsdb.py
View
@@ -0,0 +1,57 @@
+
+import urllib
+import httplib
+import datetime
+
+from epu.sensors import ISensorAggregator, Statistics
+
+
+class OpenTSDB(ISensorAggregator):
+ """Implementation of OpenTSDB sensor aggregator client
+ """
+
+ def __init__(self, host, port):
+ self.host = host
+ self.port = port
+
+ self.opentsdb = httplib.HTTPConnection(self.host, self.port)
+
+ def get_metric_statistics(self, period, start_time, end_time, metric_name,
+ statistics, dimensions=None):
+ """
+ Get a metric from the Sensor Aggregator
+
+ @param period(integer) The granularity of returned data in
+ seconds. This will be rounded to the nearest second, with a minimum
+ of 60 seconds.
+ @param start_time(datetime) Time to use for the first datapoint returned
+ @param end_time(datetime) Time to use for the last datapoint returned
+ @param metric_name(string) The name of the metric to be returned
+ @param statistics(list of Statistics Types) List of statistics to apply
+ to metric. Accepts: Average, Sum, SampleCount, Maximum, Minimum
+ @param dimensions(dict of filters) A way to filter on certain values.
+ Keys are any metric supported by the sensor aggregator, and value
+ is a value or list of values to filter on.
+ For example:
+ dimensions={'InstanceId'=['i-deadbeef']}
+ """
+ # round to nearest 60
+ if period % 60 != 0:
+ period = (period + 60)
+ period = period - (period % 60)
+ period = max(60, period)
+
+ start_time_formatted = start_time.strftime("%Y/%m/%d-%H:%M:%S")
+ end_time_formatted = end_time.strftime("%Y/%m/%d-%H:%M:%S")
+
+ print "start %s end %s" % (start_time_formatted, end_time_formatted)
+ params = urllib.urlencode({
+ 'start': start_time_formatted,
+ 'end': end_time_formatted,
+ 'm': metric_name,
+ 'ascii': 'true'
+ })
+ self.opentsdb.request('GET', '/q?%s' % params)
+ response = self.opentsdb.getresponse()
+ print response.status
+ print response.read()
@@ -0,0 +1,44 @@
+import os
+
+from nose.plugins.skip import SkipTest
+from datetime import datetime, timedelta
+
+from epu.sensors import Statistics
+from epu.sensors.opentsdb import OpenTSDB
+
+class TestRealOpenTSDB(object):
+
+ def setup(self):
+
+ host = os.environ.get("OPENTSDB_HOST")
+ port = os.environ.get("OPENTSDB_PORT")
+ #self.instance = os.environ.get("CLOUDWATCH_TEST_INSTANCE")
+ self.instance = ""
+
+ if not (host and port):
+ raise SkipTest("OpenTSDB host and port aren't in env")
+
+ self.opentsdb = OpenTSDB(host, port)
+
+ def teardown(self):
+ pass
+
+
+ def test_get_metric_statistics(self):
+
+ raise SkipTest("this is just a shell of an implementation at this point")
+
+ period = 60
+ end_time = datetime.now()
+ start_time = end_time - timedelta(minutes=20)
+ metric_name = "avg:iostat.disk.write_merged"
+ statistics = Statistics.AVERAGE
+ dimensions = {'InstanceId': [self.instance]}
+
+ result = self.opentsdb.get_metric_statistics(period, start_time,
+ end_time, metric_name, statistics, dimensions)
+
+ assert len(result) > 0
+ assert result.get(self.instance)
+ assert result[self.instance].get(Statistics.AVERAGE)
+

0 comments on commit c5d29d5

Please sign in to comment.