OLD | NEW |
1 # Copyright (c) 2006-2009 Mitch Garnaat http://garnaat.org/ | 1 # Copyright (c) 2006-2009 Mitch Garnaat http://garnaat.org/ |
2 # | 2 # |
3 # Permission is hereby granted, free of charge, to any person obtaining a | 3 # Permission is hereby granted, free of charge, to any person obtaining a |
4 # copy of this software and associated documentation files (the | 4 # copy of this software and associated documentation files (the |
5 # "Software"), to deal in the Software without restriction, including | 5 # "Software"), to deal in the Software without restriction, including |
6 # without limitation the rights to use, copy, modify, merge, publish, dis- | 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 | 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- | 8 # persons to whom the Software is furnished to do so, subject to the fol- |
9 # lowing conditions: | 9 # lowing conditions: |
10 # | 10 # |
11 # The above copyright notice and this permission notice shall be included | 11 # The above copyright notice and this permission notice shall be included |
12 # in all copies or substantial portions of the Software. | 12 # in all copies or substantial portions of the Software. |
13 # | 13 # |
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | 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- | 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | 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, | 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, | 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 | 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
20 # IN THE SOFTWARE. | 20 # IN THE SOFTWARE. |
21 # | 21 # |
22 | 22 |
23 class Dimensions(dict): | 23 from boto.ec2.cloudwatch.alarm import MetricAlarm |
| 24 |
| 25 class Dimension(dict): |
24 | 26 |
25 def startElement(self, name, attrs, connection): | 27 def startElement(self, name, attrs, connection): |
26 pass | 28 pass |
27 | 29 |
28 def endElement(self, name, value, connection): | 30 def endElement(self, name, value, connection): |
29 if name == 'Name': | 31 if name == 'Name': |
30 self._name = value | 32 self._name = value |
31 elif name == 'Value': | 33 elif name == 'Value': |
32 self[self._name] = value | 34 if self._name in self: |
33 elif name != 'Dimensions' and name != 'member': | 35 self[self._name].append(value) |
34 self[name] = value | 36 else: |
| 37 self[self._name] = [value] |
| 38 else: |
| 39 setattr(self, name, value) |
35 | 40 |
36 class Metric(object): | 41 class Metric(object): |
37 | 42 |
38 Statistics = ['Minimum', 'Maximum', 'Sum', 'Average', 'SampleCount'] | 43 Statistics = ['Minimum', 'Maximum', 'Sum', 'Average', 'SampleCount'] |
39 Units = ['Seconds', 'Percent', 'Bytes', 'Bits', 'Count', | 44 Units = ['Seconds', 'Microseconds', 'Milliseconds', 'Bytes', 'Kilobytes', |
40 'Bytes/Second', 'Bits/Second', 'Count/Second'] | 45 'Megabytes', 'Gigabytes', 'Terabytes', 'Bits', 'Kilobits', |
| 46 'Megabits', 'Gigabits', 'Terabits', 'Percent', 'Count', |
| 47 'Bytes/Second', 'Kilobytes/Second', 'Megabytes/Second', |
| 48 'Gigabytes/Second', 'Terabytes/Second', 'Bits/Second', |
| 49 'Kilobits/Second', 'Megabits/Second', 'Gigabits/Second', |
| 50 'Terabits/Second', 'Count/Second', None] |
41 | 51 |
42 def __init__(self, connection=None): | 52 def __init__(self, connection=None): |
43 self.connection = connection | 53 self.connection = connection |
44 self.name = None | 54 self.name = None |
45 self.namespace = None | 55 self.namespace = None |
46 self.dimensions = None | 56 self.dimensions = None |
47 | 57 |
48 def __repr__(self): | 58 def __repr__(self): |
49 s = 'Metric:%s' % self.name | 59 return 'Metric:%s' % self.name |
50 if self.dimensions: | |
51 for name,value in self.dimensions.items(): | |
52 s += '(%s,%s)' % (name, value) | |
53 return s | |
54 | 60 |
55 def startElement(self, name, attrs, connection): | 61 def startElement(self, name, attrs, connection): |
56 if name == 'Dimensions': | 62 if name == 'Dimensions': |
57 self.dimensions = Dimensions() | 63 self.dimensions = Dimension() |
58 return self.dimensions | 64 return self.dimensions |
59 | 65 |
60 def endElement(self, name, value, connection): | 66 def endElement(self, name, value, connection): |
61 if name == 'MetricName': | 67 if name == 'MetricName': |
62 self.name = value | 68 self.name = value |
63 elif name == 'Namespace': | 69 elif name == 'Namespace': |
64 self.namespace = value | 70 self.namespace = value |
65 else: | 71 else: |
66 setattr(self, name, value) | 72 setattr(self, name, value) |
67 | 73 |
68 def query(self, start_time, end_time, statistic, unit=None, period=60): | 74 def query(self, start_time, end_time, statistics, unit=None, period=60): |
69 return self.connection.get_metric_statistics(period, start_time, end_tim
e, | 75 if not isinstance(statistics, list): |
70 self.name, self.namespace,
[statistic], | 76 statistics = [statistics] |
71 self.dimensions, unit) | 77 return self.connection.get_metric_statistics(period, |
| 78 start_time, |
| 79 end_time, |
| 80 self.name, |
| 81 self.namespace, |
| 82 statistics, |
| 83 self.dimensions, |
| 84 unit) |
72 | 85 |
73 def describe_alarms(self, period=None, statistic=None, dimensions=None, unit
=None): | 86 def create_alarm(self, name, comparison, threshold, |
| 87 period, evaluation_periods, |
| 88 statistic, enabled=True, description=None, |
| 89 dimensions=None, alarm_actions=None, ok_actions=None, |
| 90 insufficient_data_actions=None, unit=None): |
| 91 if not dimensions: |
| 92 dimensions = self.dimensions |
| 93 alarm = MetricAlarm(self.connection, name, self.name, |
| 94 self.namespace, statistic, comparison, |
| 95 threshold, period, evaluation_periods, |
| 96 unit, description, dimensions, |
| 97 alarm_actions, insufficient_data_actions, |
| 98 ok_actions) |
| 99 if self.connection.put_metric_alarm(alarm): |
| 100 return alarm |
| 101 |
| 102 def describe_alarms(self, period=None, statistic=None, |
| 103 dimensions=None, unit=None): |
74 return self.connection.describe_alarms_for_metric(self.name, | 104 return self.connection.describe_alarms_for_metric(self.name, |
75 self.namespace, | 105 self.namespace, |
76 period, | 106 period, |
77 statistic, | 107 statistic, |
78 dimensions, | 108 dimensions, |
79 unit) | 109 unit) |
80 | 110 |
| 111 |
| 112 |
OLD | NEW |