From 00527198c863dbbc407ba7aa56a5d430655f7000 Mon Sep 17 00:00:00 2001 From: Steven Hardy Date: Wed, 22 Aug 2012 14:11:52 +0100 Subject: [PATCH] heat engine : Add show_watch_metric RPC action Adds show_watch_metric, which allow alarm/watch metric data to be retrieved from the engine Change-Id: Ie69691dbf781016e6262bbbabbe57f9c3396a5d5 Signed-off-by: Steven Hardy --- heat/engine/api.py | 32 ++++++++++++++++++++++++++++++++ heat/engine/manager.py | 24 ++++++++++++++++++++++++ heat/engine/rpcapi.py | 15 +++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/heat/engine/api.py b/heat/engine/api.py index 8fe52210..95f34677 100644 --- a/heat/engine/api.py +++ b/heat/engine/api.py @@ -252,3 +252,35 @@ def format_watch(watch): } return result + + +WATCH_DATA_KEYS = ( + WATCH_DATA_ALARM, WATCH_DATA_METRIC, WATCH_DATA_TIME, + WATCH_DATA_NAMESPACE, WATCH_DATA + ) = ( + 'watch_name', 'metric_name', 'timestamp', + 'namespace', 'data') + + +def format_watch_data(wd): + + # Demangle DB format data into something more easily used in the API + # We are expecting a dict with exactly two items, Namespace and + # a metric key + namespace = wd.data['Namespace'] + metric = [(k, v) for k, v in wd.data.items() if k != 'Namespace'] + if len(metric) == 1: + metric_name, metric_data = metric[0] + else: + logger.error("Unexpected number of keys in watch_data.data!") + return + + result = { + WATCH_DATA_ALARM: wd.watch_rule.name, + WATCH_DATA_METRIC: metric_name, + WATCH_DATA_TIME: heat_utils.strtime(wd.created_at), + WATCH_DATA_NAMESPACE: namespace, + WATCH_DATA: metric_data + } + + return result diff --git a/heat/engine/manager.py b/heat/engine/manager.py index 1af5f987..480fa2cf 100644 --- a/heat/engine/manager.py +++ b/heat/engine/manager.py @@ -502,3 +502,27 @@ class EngineManager(manager.Manager): result = [api.format_watch(w) for w in wrs] return result + + def show_watch_metric(self, context, namespace=None, metric_name=None): + ''' + The show_watch method returns the datapoints for a metric + arg1 -> RPC context. + arg2 -> Name of the namespace you want to see, or None to see all + arg3 -> Name of the metric you want to see, or None to see all + ''' + + # DB API and schema does not yet allow us to easily query by + # namespace/metric, but we will want this at some point + # for now, the API can query all metric data and filter locally + if namespace != None or metric_name != None: + logger.error("Filtering by namespace/metric not yet supported") + return + + try: + wds = db_api.watch_data_get_all(context) + except Exception as ex: + logger.warn('show_metric (all) db error %s' % str(ex)) + return + + result = [api.format_watch_data(w) for w in wds] + return result diff --git a/heat/engine/rpcapi.py b/heat/engine/rpcapi.py index c786f323..691a31fb 100644 --- a/heat/engine/rpcapi.py +++ b/heat/engine/rpcapi.py @@ -232,3 +232,18 @@ class EngineAPI(heat.openstack.common.rpc.proxy.RpcProxy): return self.call(ctxt, self.make_msg('show_watch', watch_name=watch_name), topic=_engine_topic(self.topic, ctxt, None)) + + def show_watch_metric(self, ctxt, namespace=None, metric_name=None): + """ + The show_watch_metric method returns the datapoints associated + with a specified metric, or all metrics if no metric_name is passed + + :param ctxt: RPC context. + :param namespace: Name of the namespace you want to see, + or None to see all + :param metric_name: Name of the metric you want to see, + or None to see all + """ + return self.call(ctxt, self.make_msg('show_watch_metric', + namespace=namespace, metric_name=metric_name), + topic=_engine_topic(self.topic, ctxt, None)) -- 2.45.2