Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. # -*- encoding: utf-8 -*-
  2. # Copyright (c) 2017 Servionica
  3. #
  4. # Authors: Alexander Chadin <a.chadin@servionica.ru>
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  15. # implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18.  
  19. from datetime import datetime
  20. import time
  21.  
  22. from oslo_config import cfg
  23. from oslo_log import log
  24.  
  25. from watcher.common import clients
  26. from watcher.common import exception
  27. from watcher.common import utils as common_utils
  28.  
  29. CONF = cfg.CONF
  30. LOG = log.getLogger(__name__)
  31.  
  32.  
  33. class GnocchiHelper(object):
  34. def __init__(self, osc=None):
  35. """:param osc: an OpenStackClients instance"""
  36. self.osc = osc if osc else clients.OpenStackClients()
  37. self.gnocchi = self.osc.gnocchi()
  38. def query_retry(self, f, *args, **kwargs):
  39. for i in range(CONF.gnocchi_client.query_max_retries):
  40. try:
  41. return f(*args, **kwargs)
  42. except Exception as e:
  43. LOG.exception(e)
  44. time.sleep(CONF.gnocchi_client.query_timeout)
  45. raise
  46. def statistic_aggregation(self,
  47. resource_id,
  48. metric,
  49. granularity,
  50. start_time=None,
  51. stop_time=None,
  52. aggregation='mean'):
  53. """Representing a statistic aggregate by operators
  54. :param metric: metric name of which we want the statistics
  55. :param resource_id: id of resource to list statistics for
  56. :param start_time: Start datetime from which metrics will be used
  57. :param stop_time: End datetime from which metrics will be used
  58. :param granularity: frequency of marking metric point, in seconds
  59. :param aggregation: Should be chosen in accordance with policy
  60. aggregations
  61. :return: value of aggregated metric
  62. """
  63. if start_time is not None and not isinstance(start_time, datetime):
  64. raise exception.InvalidParameter(parameter='start_time',
  65. parameter_type=datetime)
  66. if stop_time is not None and not isinstance(stop_time, datetime):
  67. raise exception.InvalidParameter(parameter='stop_time',
  68. parameter_type=datetime)
  69. if not common_utils.is_uuid_like(resource_id):
  70. kwargs = dict(query={"=": {"original_resource_id": resource_id}}, limit=1)
  71. resources = self.query_retry(
  72. f=self.gnocchi.resource.search, **kwargs)
  73. if not resources:
  74. # raise self.gnocchi.exceptions.ResourceNotFound(resource_id=resource_id)
  75. raise exception.ResourceNotFound(name=resource_id)
  76. resource_id = resources[0]['id']
  77. raw_kwargs = dict(
  78. metric=metric,
  79. start=start_time,
  80. stop=stop_time,
  81. resource_id=resource_id,
  82. granularity=granularity,
  83. aggregation=aggregation,
  84. )
  85. kwargs = {k: v for k, v in raw_kwargs.items() if k and v}
  86. statistics = self.query_retry(
  87. f=self.gnocchi.metric.get_measures, **kwargs)
  88. if statistics:
  89. # return value of latest measure
  90. # measure has structure [time, granularity, value]
  91. return statistics[-1][2]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement