2014-03-18 01:32:55 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = """
|
2014-03-19 22:26:59 +01:00
|
|
|
module: ec2_metric_alarm
|
2014-03-20 18:22:03 +01:00
|
|
|
short_description: "Create/update or delete AWS Cloudwatch 'metric alarms'"
|
2014-03-18 01:32:55 +01:00
|
|
|
description:
|
|
|
|
- Can create or delete AWS metric alarms
|
|
|
|
- Metrics you wish to alarm on must already exist
|
|
|
|
version_added: "1.6"
|
2015-06-15 20:41:22 +02:00
|
|
|
author: "Zacharie Eakin (@zeekin)"
|
2014-03-18 01:32:55 +01:00
|
|
|
options:
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- register or deregister the alarm
|
|
|
|
required: true
|
|
|
|
choices: ['present', 'absent']
|
|
|
|
name:
|
2015-05-04 13:54:03 +02:00
|
|
|
description:
|
2014-03-18 01:32:55 +01:00
|
|
|
- Unique name for the alarm
|
|
|
|
required: true
|
|
|
|
metric:
|
|
|
|
description:
|
|
|
|
- Name of the monitored metric (e.g. CPUUtilization)
|
|
|
|
- Metric must already exist
|
|
|
|
required: false
|
|
|
|
namespace:
|
|
|
|
description:
|
2014-09-05 22:09:29 +02:00
|
|
|
- Name of the appropriate namespace ('AWS/EC2', 'System/Linux', etc.), which determines the category it will appear under in cloudwatch
|
2014-03-18 01:32:55 +01:00
|
|
|
required: false
|
|
|
|
statistic:
|
|
|
|
description:
|
|
|
|
- Operation applied to the metric
|
|
|
|
- Works in conjunction with period and evaluation_periods to determine the comparison value
|
|
|
|
required: false
|
|
|
|
options: ['SampleCount','Average','Sum','Minimum','Maximum']
|
|
|
|
comparison:
|
|
|
|
description:
|
|
|
|
- Determines how the threshold value is compared
|
|
|
|
required: false
|
|
|
|
options: ['<=','<','>','>=']
|
2014-05-08 17:51:07 +02:00
|
|
|
threshold:
|
|
|
|
description:
|
2014-03-18 01:32:55 +01:00
|
|
|
- Sets the min/max bound for triggering the alarm
|
|
|
|
required: false
|
|
|
|
period:
|
|
|
|
description:
|
|
|
|
- The time (in seconds) between metric evaluations
|
|
|
|
required: false
|
|
|
|
evaluation_periods:
|
|
|
|
description:
|
|
|
|
- The number of times in which the metric is evaluated before final calculation
|
|
|
|
required: false
|
2014-05-08 17:51:07 +02:00
|
|
|
unit:
|
2014-03-18 01:32:55 +01:00
|
|
|
description:
|
|
|
|
- The threshold's unit of measurement
|
|
|
|
required: false
|
|
|
|
options: ['Seconds','Microseconds','Milliseconds','Bytes','Kilobytes','Megabytes','Gigabytes','Terabytes','Bits','Kilobits','Megabits','Gigabits','Terabits','Percent','Count','Bytes/Second','Kilobytes/Second','Megabytes/Second','Gigabytes/Second','Terabytes/Second','Bits/Second','Kilobits/Second','Megabits/Second','Gigabits/Second','Terabits/Second','Count/Second','None']
|
|
|
|
description:
|
|
|
|
description:
|
2015-05-04 13:54:03 +02:00
|
|
|
- A longer description of the alarm
|
2014-03-18 01:32:55 +01:00
|
|
|
required: false
|
|
|
|
dimensions:
|
|
|
|
description:
|
|
|
|
- Describes to what the alarm is applied
|
|
|
|
required: false
|
|
|
|
alarm_actions:
|
2014-05-08 17:51:07 +02:00
|
|
|
description:
|
2014-03-18 01:32:55 +01:00
|
|
|
- A list of the names action(s) taken when the alarm is in the 'alarm' status
|
|
|
|
required: false
|
|
|
|
insufficient_data_actions:
|
|
|
|
description:
|
|
|
|
- A list of the names of action(s) to take when the alarm is in the 'insufficient_data' status
|
|
|
|
required: false
|
|
|
|
ok_actions:
|
|
|
|
description:
|
|
|
|
- A list of the names of action(s) to take when the alarm is in the 'ok' status
|
|
|
|
required: false
|
2014-04-09 08:43:55 +02:00
|
|
|
extends_documentation_fragment: aws
|
2014-03-18 01:32:55 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: create alarm
|
2014-03-19 22:26:59 +01:00
|
|
|
ec2_metric_alarm:
|
2014-03-18 01:32:55 +01:00
|
|
|
state: present
|
|
|
|
region: ap-southeast-2
|
|
|
|
name: "cpu-low"
|
|
|
|
metric: "CPUUtilization"
|
|
|
|
namespace: "AWS/EC2"
|
|
|
|
statistic: Average
|
|
|
|
comparison: "<="
|
|
|
|
threshold: 5.0
|
|
|
|
period: 300
|
|
|
|
evaluation_periods: 3
|
|
|
|
unit: "Percent"
|
|
|
|
description: "This will alarm when a bamboo slave's cpu usage average is lower than 5% for 15 minutes "
|
|
|
|
dimensions: {'InstanceId':'i-XXX'}
|
|
|
|
alarm_actions: ["action1","action2"]
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.ec2 import *
|
|
|
|
|
|
|
|
try:
|
|
|
|
import boto.ec2.cloudwatch
|
|
|
|
from boto.ec2.cloudwatch import CloudWatchConnection, MetricAlarm
|
|
|
|
from boto.exception import BotoServerError
|
2015-04-02 01:16:54 +02:00
|
|
|
HAS_BOTO = True
|
2014-03-18 01:32:55 +01:00
|
|
|
except ImportError:
|
2015-04-02 01:16:54 +02:00
|
|
|
HAS_BOTO = False
|
2014-03-18 01:32:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
def create_metric_alarm(connection, module):
|
2014-05-08 17:51:07 +02:00
|
|
|
|
2014-03-18 01:32:55 +01:00
|
|
|
name = module.params.get('name')
|
|
|
|
metric = module.params.get('metric')
|
|
|
|
namespace = module.params.get('namespace')
|
|
|
|
statistic = module.params.get('statistic')
|
|
|
|
comparison = module.params.get('comparison')
|
|
|
|
threshold = module.params.get('threshold')
|
|
|
|
period = module.params.get('period')
|
|
|
|
evaluation_periods = module.params.get('evaluation_periods')
|
|
|
|
unit = module.params.get('unit')
|
|
|
|
description = module.params.get('description')
|
|
|
|
dimensions = module.params.get('dimensions')
|
|
|
|
alarm_actions = module.params.get('alarm_actions')
|
|
|
|
insufficient_data_actions = module.params.get('insufficient_data_actions')
|
|
|
|
ok_actions = module.params.get('ok_actions')
|
|
|
|
|
|
|
|
alarms = connection.describe_alarms(alarm_names=[name])
|
2014-05-08 17:51:07 +02:00
|
|
|
|
2014-03-18 01:32:55 +01:00
|
|
|
if not alarms:
|
|
|
|
|
|
|
|
alm = MetricAlarm(
|
|
|
|
name=name,
|
2014-05-08 17:51:07 +02:00
|
|
|
metric=metric,
|
2014-03-18 01:32:55 +01:00
|
|
|
namespace=namespace,
|
|
|
|
statistic=statistic,
|
|
|
|
comparison=comparison,
|
|
|
|
threshold=threshold,
|
|
|
|
period=period,
|
|
|
|
evaluation_periods=evaluation_periods,
|
|
|
|
unit=unit,
|
|
|
|
description=description,
|
|
|
|
dimensions=dimensions,
|
|
|
|
alarm_actions=alarm_actions,
|
|
|
|
insufficient_data_actions=insufficient_data_actions,
|
|
|
|
ok_actions=ok_actions
|
|
|
|
)
|
2014-05-08 17:51:07 +02:00
|
|
|
try:
|
2014-03-18 01:32:55 +01:00
|
|
|
connection.create_alarm(alm)
|
2014-05-08 17:51:07 +02:00
|
|
|
changed = True
|
|
|
|
alarms = connection.describe_alarms(alarm_names=[name])
|
2014-03-18 01:32:55 +01:00
|
|
|
except BotoServerError, e:
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
else:
|
|
|
|
alarm = alarms[0]
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
for attr in ('comparison','metric','namespace','statistic','threshold','period','evaluation_periods','unit','description'):
|
|
|
|
if getattr(alarm, attr) != module.params.get(attr):
|
|
|
|
changed = True
|
|
|
|
setattr(alarm, attr, module.params.get(attr))
|
|
|
|
#this is to deal with a current bug where you cannot assign '<=>' to the comparator when modifying an existing alarm
|
|
|
|
comparison = alarm.comparison
|
|
|
|
comparisons = {'<=' : 'LessThanOrEqualToThreshold', '<' : 'LessThanThreshold', '>=' : 'GreaterThanOrEqualToThreshold', '>' : 'GreaterThanThreshold'}
|
|
|
|
alarm.comparison = comparisons[comparison]
|
|
|
|
|
|
|
|
dim1 = module.params.get('dimensions')
|
|
|
|
dim2 = alarm.dimensions
|
2014-05-08 17:51:07 +02:00
|
|
|
|
2014-03-18 01:32:55 +01:00
|
|
|
for keys in dim1:
|
|
|
|
if not isinstance(dim1[keys], list):
|
|
|
|
dim1[keys] = [dim1[keys]]
|
2015-01-29 22:36:35 +01:00
|
|
|
if keys not in dim2 or dim1[keys] != dim2[keys]:
|
2014-03-18 01:32:55 +01:00
|
|
|
changed=True
|
2014-05-08 17:51:07 +02:00
|
|
|
setattr(alarm, 'dimensions', dim1)
|
2014-03-18 01:32:55 +01:00
|
|
|
|
2014-05-08 17:51:07 +02:00
|
|
|
for attr in ('alarm_actions','insufficient_data_actions','ok_actions'):
|
2014-03-18 01:32:55 +01:00
|
|
|
action = module.params.get(attr) or []
|
|
|
|
if getattr(alarm, attr) != action:
|
|
|
|
changed = True
|
|
|
|
setattr(alarm, attr, module.params.get(attr))
|
2014-05-08 17:51:07 +02:00
|
|
|
|
2014-03-18 01:32:55 +01:00
|
|
|
try:
|
|
|
|
if changed:
|
|
|
|
connection.create_alarm(alarm)
|
|
|
|
except BotoServerError, e:
|
|
|
|
module.fail_json(msg=str(e))
|
2014-05-08 17:51:07 +02:00
|
|
|
result = alarms[0]
|
|
|
|
module.exit_json(changed=changed, name=result.name,
|
|
|
|
actions_enabled=result.actions_enabled,
|
|
|
|
alarm_actions=result.alarm_actions,
|
|
|
|
alarm_arn=result.alarm_arn,
|
|
|
|
comparison=result.comparison,
|
|
|
|
description=result.description,
|
|
|
|
dimensions=result.dimensions,
|
|
|
|
evaluation_periods=result.evaluation_periods,
|
|
|
|
insufficient_data_actions=result.insufficient_data_actions,
|
|
|
|
last_updated=result.last_updated,
|
|
|
|
metric=result.metric,
|
|
|
|
namespace=result.namespace,
|
|
|
|
ok_actions=result.ok_actions,
|
|
|
|
period=result.period,
|
|
|
|
state_reason=result.state_reason,
|
|
|
|
state_value=result.state_value,
|
|
|
|
statistic=result.statistic,
|
|
|
|
threshold=result.threshold,
|
|
|
|
unit=result.unit)
|
2014-03-18 01:32:55 +01:00
|
|
|
|
|
|
|
def delete_metric_alarm(connection, module):
|
|
|
|
name = module.params.get('name')
|
|
|
|
|
|
|
|
alarms = connection.describe_alarms(alarm_names=[name])
|
2014-05-08 17:51:07 +02:00
|
|
|
|
2014-03-18 01:32:55 +01:00
|
|
|
if alarms:
|
|
|
|
try:
|
|
|
|
connection.delete_alarms([name])
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
except BotoServerError, e:
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
else:
|
|
|
|
module.exit_json(changed=False)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
argument_spec = ec2_argument_spec()
|
|
|
|
argument_spec.update(
|
|
|
|
dict(
|
|
|
|
name=dict(required=True, type='str'),
|
|
|
|
metric=dict(type='str'),
|
2014-09-05 22:09:29 +02:00
|
|
|
namespace=dict(type='str'),
|
|
|
|
statistic=dict(type='str', choices=['SampleCount', 'Average', 'Sum', 'Minimum', 'Maximum']),
|
2014-03-18 01:32:55 +01:00
|
|
|
comparison=dict(type='str', choices=['<=', '<', '>', '>=']),
|
|
|
|
threshold=dict(type='float'),
|
|
|
|
period=dict(type='int'),
|
|
|
|
unit=dict(type='str', choices=['Seconds', 'Microseconds', 'Milliseconds', 'Bytes', 'Kilobytes', 'Megabytes', 'Gigabytes', 'Terabytes', 'Bits', 'Kilobits', 'Megabits', 'Gigabits', 'Terabits', 'Percent', 'Count', 'Bytes/Second', 'Kilobytes/Second', 'Megabytes/Second', 'Gigabytes/Second', 'Terabytes/Second', 'Bits/Second', 'Kilobits/Second', 'Megabits/Second', 'Gigabits/Second', 'Terabits/Second', 'Count/Second', 'None']),
|
|
|
|
evaluation_periods=dict(type='int'),
|
|
|
|
description=dict(type='str'),
|
|
|
|
dimensions=dict(type='dict'),
|
|
|
|
alarm_actions=dict(type='list'),
|
|
|
|
insufficient_data_actions=dict(type='list'),
|
|
|
|
ok_actions=dict(type='list'),
|
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
2015-04-27 18:38:57 +02:00
|
|
|
region=dict(aliases=['aws_region', 'ec2_region']),
|
2014-03-18 01:32:55 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec)
|
|
|
|
|
2015-04-02 01:16:54 +02:00
|
|
|
if not HAS_BOTO:
|
|
|
|
module.fail_json(msg='boto required for this module')
|
|
|
|
|
2014-03-18 01:32:55 +01:00
|
|
|
state = module.params.get('state')
|
|
|
|
|
|
|
|
region, ec2_url, aws_connect_params = get_aws_connection_info(module)
|
|
|
|
try:
|
|
|
|
connection = connect_to_aws(boto.ec2.cloudwatch, region, **aws_connect_params)
|
2014-11-01 03:44:44 +01:00
|
|
|
except (boto.exception.NoAuthHandlerFound, StandardError), e:
|
2014-03-18 01:32:55 +01:00
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
create_metric_alarm(connection, module)
|
|
|
|
elif state == 'absent':
|
|
|
|
delete_metric_alarm(connection, module)
|
|
|
|
|
|
|
|
main()
|