OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2010 Reza Lotun http://reza.lotun.name |
| 2 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: |
| 10 # |
| 11 # The above copyright notice and this permission notice shall be included |
| 12 # in all copies or substantial portions of the Software. |
| 13 # |
| 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 # IN THE SOFTWARE. |
| 21 # |
| 22 |
| 23 from datetime import datetime |
| 24 from boto.resultset import ResultSet |
| 25 from boto.ec2.cloudwatch.listelement import ListElement |
| 26 from boto.ec2.cloudwatch.dimension import Dimension |
| 27 from boto.compat import json |
| 28 |
| 29 |
| 30 class MetricAlarms(list): |
| 31 def __init__(self, connection=None): |
| 32 """ |
| 33 Parses a list of MetricAlarms. |
| 34 """ |
| 35 list.__init__(self) |
| 36 self.connection = connection |
| 37 |
| 38 def startElement(self, name, attrs, connection): |
| 39 if name == 'member': |
| 40 metric_alarm = MetricAlarm(connection) |
| 41 self.append(metric_alarm) |
| 42 return metric_alarm |
| 43 |
| 44 def endElement(self, name, value, connection): |
| 45 pass |
| 46 |
| 47 |
| 48 class MetricAlarm(object): |
| 49 |
| 50 OK = 'OK' |
| 51 ALARM = 'ALARM' |
| 52 INSUFFICIENT_DATA = 'INSUFFICIENT_DATA' |
| 53 |
| 54 _cmp_map = { |
| 55 '>=': 'GreaterThanOrEqualToThreshold', |
| 56 '>': 'GreaterThanThreshold', |
| 57 '<': 'LessThanThreshold', |
| 58 '<=': 'LessThanOrEqualToThreshold', |
| 59 } |
| 60 _rev_cmp_map = dict((v, k) for (k, v) in _cmp_map.iteritems()) |
| 61 |
| 62 def __init__(self, connection=None, name=None, metric=None, |
| 63 namespace=None, statistic=None, comparison=None, |
| 64 threshold=None, period=None, evaluation_periods=None, |
| 65 unit=None, description='', dimensions=None, |
| 66 alarm_actions=None, insufficient_data_actions=None, |
| 67 ok_actions=None): |
| 68 """ |
| 69 Creates a new Alarm. |
| 70 |
| 71 :type name: str |
| 72 :param name: Name of alarm. |
| 73 |
| 74 :type metric: str |
| 75 :param metric: Name of alarm's associated metric. |
| 76 |
| 77 :type namespace: str |
| 78 :param namespace: The namespace for the alarm's metric. |
| 79 |
| 80 :type statistic: str |
| 81 :param statistic: The statistic to apply to the alarm's associated |
| 82 metric. |
| 83 Valid values: SampleCount|Average|Sum|Minimum|Maximum |
| 84 |
| 85 :type comparison: str |
| 86 :param comparison: Comparison used to compare statistic with threshold. |
| 87 Valid values: >= | > | < | <= |
| 88 |
| 89 :type threshold: float |
| 90 :param threshold: The value against which the specified statistic |
| 91 is compared. |
| 92 |
| 93 :type period: int |
| 94 :param period: The period in seconds over which teh specified |
| 95 statistic is applied. |
| 96 |
| 97 :type evaluation_periods: int |
| 98 :param evaluation_period: The number of periods over which data is |
| 99 compared to the specified threshold. |
| 100 |
| 101 :type unit: str |
| 102 :param unit: Allowed Values are: |
| 103 Seconds|Microseconds|Milliseconds, |
| 104 Bytes|Kilobytes|Megabytes|Gigabytes|Terabytes, |
| 105 Bits|Kilobits|Megabits|Gigabits|Terabits, |
| 106 Percent|Count| |
| 107 Bytes/Second|Kilobytes/Second|Megabytes/Second| |
| 108 Gigabytes/Second|Terabytes/Second, |
| 109 Bits/Second|Kilobits/Second|Megabits/Second, |
| 110 Gigabits/Second|Terabits/Second|Count/Second|None |
| 111 |
| 112 :type description: str |
| 113 :param description: Description of MetricAlarm |
| 114 |
| 115 :type dimensions: list of dicts |
| 116 :param description: Dimensions of alarm, such as: |
| 117 [{'InstanceId':['i-0123456,i-0123457']}] |
| 118 |
| 119 :type alarm_actions: list of strs |
| 120 :param alarm_actions: A list of the ARNs of the actions to take in |
| 121 ALARM state |
| 122 |
| 123 :type insufficient_data_actions: list of strs |
| 124 :param insufficient_data_actions: A list of the ARNs of the actions to |
| 125 take in INSUFFICIENT_DATA state |
| 126 |
| 127 :type ok_actions: list of strs |
| 128 :param ok_actions: A list of the ARNs of the actions to take in OK state |
| 129 """ |
| 130 self.name = name |
| 131 self.connection = connection |
| 132 self.metric = metric |
| 133 self.namespace = namespace |
| 134 self.statistic = statistic |
| 135 if threshold is not None: |
| 136 self.threshold = float(threshold) |
| 137 else: |
| 138 self.threshold = None |
| 139 self.comparison = self._cmp_map.get(comparison) |
| 140 if period is not None: |
| 141 self.period = int(period) |
| 142 else: |
| 143 self.period = None |
| 144 if evaluation_periods is not None: |
| 145 self.evaluation_periods = int(evaluation_periods) |
| 146 else: |
| 147 self.evaluation_periods = None |
| 148 self.actions_enabled = None |
| 149 self.alarm_arn = None |
| 150 self.last_updated = None |
| 151 self.description = description |
| 152 self.dimensions = dimensions |
| 153 self.state_reason = None |
| 154 self.state_value = None |
| 155 self.unit = unit |
| 156 self.alarm_actions = alarm_actions |
| 157 self.insufficient_data_actions = insufficient_data_actions |
| 158 self.ok_actions = ok_actions |
| 159 |
| 160 def __repr__(self): |
| 161 return 'MetricAlarm:%s[%s(%s) %s %s]' % (self.name, self.metric, |
| 162 self.statistic, |
| 163 self.comparison, |
| 164 self.threshold) |
| 165 |
| 166 def startElement(self, name, attrs, connection): |
| 167 if name == 'AlarmActions': |
| 168 self.alarm_actions = ListElement() |
| 169 return self.alarm_actions |
| 170 elif name == 'InsufficientDataActions': |
| 171 self.insufficient_data_actions = ListElement() |
| 172 return self.insufficient_data_actions |
| 173 elif name == 'OKActions': |
| 174 self.ok_actions = ListElement() |
| 175 return self.ok_actions |
| 176 elif name == 'Dimensions': |
| 177 self.dimensions = Dimension() |
| 178 return self.dimensions |
| 179 else: |
| 180 pass |
| 181 |
| 182 def endElement(self, name, value, connection): |
| 183 if name == 'ActionsEnabled': |
| 184 self.actions_enabled = value |
| 185 elif name == 'AlarmArn': |
| 186 self.alarm_arn = value |
| 187 elif name == 'AlarmConfigurationUpdatedTimestamp': |
| 188 self.last_updated = value |
| 189 elif name == 'AlarmDescription': |
| 190 self.description = value |
| 191 elif name == 'AlarmName': |
| 192 self.name = value |
| 193 elif name == 'ComparisonOperator': |
| 194 setattr(self, 'comparison', self._rev_cmp_map[value]) |
| 195 elif name == 'EvaluationPeriods': |
| 196 self.evaluation_periods = int(value) |
| 197 elif name == 'MetricName': |
| 198 self.metric = value |
| 199 elif name == 'Namespace': |
| 200 self.namespace = value |
| 201 elif name == 'Period': |
| 202 self.period = int(value) |
| 203 elif name == 'StateReason': |
| 204 self.state_reason = value |
| 205 elif name == 'StateValue': |
| 206 self.state_value = value |
| 207 elif name == 'Statistic': |
| 208 self.statistic = value |
| 209 elif name == 'Threshold': |
| 210 self.threshold = float(value) |
| 211 elif name == 'Unit': |
| 212 self.unit = value |
| 213 else: |
| 214 setattr(self, name, value) |
| 215 |
| 216 def set_state(self, value, reason, data=None): |
| 217 """ Temporarily sets the state of an alarm. |
| 218 |
| 219 :type value: str |
| 220 :param value: OK | ALARM | INSUFFICIENT_DATA |
| 221 |
| 222 :type reason: str |
| 223 :param reason: Reason alarm set (human readable). |
| 224 |
| 225 :type data: str |
| 226 :param data: Reason data (will be jsonified). |
| 227 """ |
| 228 return self.connection.set_alarm_state(self.name, reason, value, data) |
| 229 |
| 230 def update(self): |
| 231 return self.connection.update_alarm(self) |
| 232 |
| 233 def enable_actions(self): |
| 234 return self.connection.enable_alarm_actions([self.name]) |
| 235 |
| 236 def disable_actions(self): |
| 237 return self.connection.disable_alarm_actions([self.name]) |
| 238 |
| 239 def describe_history(self, start_date=None, end_date=None, max_records=None, |
| 240 history_item_type=None, next_token=None): |
| 241 return self.connection.describe_alarm_history(self.name, start_date, |
| 242 end_date, max_records, |
| 243 history_item_type, |
| 244 next_token) |
| 245 |
| 246 def add_alarm_action(self, action_arn=None): |
| 247 """ |
| 248 Adds an alarm action, represented as an SNS topic, to this alarm. |
| 249 What do do when alarm is triggered. |
| 250 |
| 251 :type action_arn: str |
| 252 :param action_arn: SNS topics to which notification should be |
| 253 sent if the alarm goes to state ALARM. |
| 254 """ |
| 255 if not action_arn: |
| 256 return # Raise exception instead? |
| 257 self.actions_enabled = 'true' |
| 258 self.alarm_actions.append(action_arn) |
| 259 |
| 260 def add_insufficient_data_action(self, action_arn=None): |
| 261 """ |
| 262 Adds an insufficient_data action, represented as an SNS topic, to |
| 263 this alarm. What to do when the insufficient_data state is reached. |
| 264 |
| 265 :type action_arn: str |
| 266 :param action_arn: SNS topics to which notification should be |
| 267 sent if the alarm goes to state INSUFFICIENT_DATA. |
| 268 """ |
| 269 if not action_arn: |
| 270 return |
| 271 self.actions_enabled = 'true' |
| 272 self.insufficient_data_actions.append(action_arn) |
| 273 |
| 274 def add_ok_action(self, action_arn=None): |
| 275 """ |
| 276 Adds an ok action, represented as an SNS topic, to this alarm. What |
| 277 to do when the ok state is reached. |
| 278 |
| 279 :type action_arn: str |
| 280 :param action_arn: SNS topics to which notification should be |
| 281 sent if the alarm goes to state INSUFFICIENT_DATA. |
| 282 """ |
| 283 if not action_arn: |
| 284 return |
| 285 self.actions_enabled = 'true' |
| 286 self.ok_actions.append(action_arn) |
| 287 |
| 288 def delete(self): |
| 289 self.connection.delete_alarms([self.name]) |
| 290 |
| 291 class AlarmHistoryItem(object): |
| 292 def __init__(self, connection=None): |
| 293 self.connection = connection |
| 294 |
| 295 def __repr__(self): |
| 296 return 'AlarmHistory:%s[%s at %s]' % (self.name, self.summary, self.time
stamp) |
| 297 |
| 298 def startElement(self, name, attrs, connection): |
| 299 pass |
| 300 |
| 301 def endElement(self, name, value, connection): |
| 302 if name == 'AlarmName': |
| 303 self.name = value |
| 304 elif name == 'HistoryData': |
| 305 self.data = json.loads(value) |
| 306 elif name == 'HistoryItemType': |
| 307 self.tem_type = value |
| 308 elif name == 'HistorySummary': |
| 309 self.summary = value |
| 310 elif name == 'Timestamp': |
| 311 self.timestamp = datetime.strptime(value, '%Y-%m-%dT%H:%M:%S.%fZ') |
| 312 |
OLD | NEW |