/boto-2.5.2/boto/ec2/cloudwatch/metric.py

# · Python · 175 lines · 63 code · 11 blank · 101 comment · 7 complexity · 8333597c18469987c6ba6fd677f808dd MD5 · raw file

  1. # Copyright (c) 2006-2012 Mitch Garnaat http://garnaat.org/
  2. # Copyright (c) 2012 Amazon.com, Inc. or its affiliates.
  3. # All Rights Reserved
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the
  7. # "Software"), to deal in the Software without restriction, including
  8. # without limitation the rights to use, copy, modify, merge, publish, dis-
  9. # tribute, sublicense, and/or sell copies of the Software, and to permit
  10. # persons to whom the Software is furnished to do so, subject to the fol-
  11. # lowing conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included
  14. # in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
  18. # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  19. # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. # IN THE SOFTWARE.
  23. #
  24. from boto.ec2.cloudwatch.alarm import MetricAlarm
  25. from boto.ec2.cloudwatch.dimension import Dimension
  26. class Metric(object):
  27. Statistics = ['Minimum', 'Maximum', 'Sum', 'Average', 'SampleCount']
  28. Units = ['Seconds', 'Microseconds', 'Milliseconds', 'Bytes', 'Kilobytes',
  29. 'Megabytes', 'Gigabytes', 'Terabytes', 'Bits', 'Kilobits',
  30. 'Megabits', 'Gigabits', 'Terabits', 'Percent', 'Count',
  31. 'Bytes/Second', 'Kilobytes/Second', 'Megabytes/Second',
  32. 'Gigabytes/Second', 'Terabytes/Second', 'Bits/Second',
  33. 'Kilobits/Second', 'Megabits/Second', 'Gigabits/Second',
  34. 'Terabits/Second', 'Count/Second', None]
  35. def __init__(self, connection=None):
  36. self.connection = connection
  37. self.name = None
  38. self.namespace = None
  39. self.dimensions = None
  40. def __repr__(self):
  41. return 'Metric:%s' % self.name
  42. def startElement(self, name, attrs, connection):
  43. if name == 'Dimensions':
  44. self.dimensions = Dimension()
  45. return self.dimensions
  46. def endElement(self, name, value, connection):
  47. if name == 'MetricName':
  48. self.name = value
  49. elif name == 'Namespace':
  50. self.namespace = value
  51. else:
  52. setattr(self, name, value)
  53. def query(self, start_time, end_time, statistics, unit=None, period=60):
  54. """
  55. :type start_time: datetime
  56. :param start_time: The time stamp to use for determining the
  57. first datapoint to return. The value specified is
  58. inclusive; results include datapoints with the time stamp
  59. specified.
  60. :type end_time: datetime
  61. :param end_time: The time stamp to use for determining the
  62. last datapoint to return. The value specified is
  63. exclusive; results will include datapoints up to the time
  64. stamp specified.
  65. :type statistics: list
  66. :param statistics: A list of statistics names Valid values:
  67. Average | Sum | SampleCount | Maximum | Minimum
  68. :type dimensions: dict
  69. :param dimensions: A dictionary of dimension key/values where
  70. the key is the dimension name and the value
  71. is either a scalar value or an iterator
  72. of values to be associated with that
  73. dimension.
  74. :type unit: string
  75. :param unit: The unit for the metric. Value values are:
  76. Seconds | Microseconds | Milliseconds | Bytes | Kilobytes |
  77. Megabytes | Gigabytes | Terabytes | Bits | Kilobits |
  78. Megabits | Gigabits | Terabits | Percent | Count |
  79. Bytes/Second | Kilobytes/Second | Megabytes/Second |
  80. Gigabytes/Second | Terabytes/Second | Bits/Second |
  81. Kilobits/Second | Megabits/Second | Gigabits/Second |
  82. Terabits/Second | Count/Second | None
  83. :type period: integer
  84. :param period: The granularity, in seconds, of the returned datapoints.
  85. Period must be at least 60 seconds and must be a multiple
  86. of 60. The default value is 60.
  87. """
  88. if not isinstance(statistics, list):
  89. statistics = [statistics]
  90. return self.connection.get_metric_statistics(period,
  91. start_time,
  92. end_time,
  93. self.name,
  94. self.namespace,
  95. statistics,
  96. self.dimensions,
  97. unit)
  98. def create_alarm(self, name, comparison, threshold,
  99. period, evaluation_periods,
  100. statistic, enabled=True, description=None,
  101. dimensions=None, alarm_actions=None, ok_actions=None,
  102. insufficient_data_actions=None, unit=None):
  103. """
  104. Creates or updates an alarm and associates it with this metric.
  105. Optionally, this operation can associate one or more
  106. Amazon Simple Notification Service resources with the alarm.
  107. When this operation creates an alarm, the alarm state is immediately
  108. set to INSUFFICIENT_DATA. The alarm is evaluated and its StateValue is
  109. set appropriately. Any actions associated with the StateValue is then
  110. executed.
  111. When updating an existing alarm, its StateValue is left unchanged.
  112. :type alarm: boto.ec2.cloudwatch.alarm.MetricAlarm
  113. :param alarm: MetricAlarm object.
  114. """
  115. if not dimensions:
  116. dimensions = self.dimensions
  117. alarm = MetricAlarm(self.connection, name, self.name,
  118. self.namespace, statistic, comparison,
  119. threshold, period, evaluation_periods,
  120. unit, description, dimensions,
  121. alarm_actions, insufficient_data_actions,
  122. ok_actions)
  123. if self.connection.put_metric_alarm(alarm):
  124. return alarm
  125. def describe_alarms(self, period=None, statistic=None,
  126. dimensions=None, unit=None):
  127. """
  128. Retrieves all alarms for this metric. Specify a statistic, period,
  129. or unit to filter the set of alarms further.
  130. :type period: int
  131. :param period: The period in seconds over which the statistic
  132. is applied.
  133. :type statistic: string
  134. :param statistic: The statistic for the metric.
  135. :param dimension_filters: A dictionary containing name/value
  136. pairs that will be used to filter the results. The key in
  137. the dictionary is the name of a Dimension. The value in
  138. the dictionary is either a scalar value of that Dimension
  139. name that you want to filter on, a list of values to
  140. filter on or None if you want all metrics with that
  141. Dimension name.
  142. :type unit: string
  143. :rtype list
  144. """
  145. return self.connection.describe_alarms_for_metric(self.name,
  146. self.namespace,
  147. period,
  148. statistic,
  149. dimensions,
  150. unit)