#!/usr/bin/python
|
|
# Copyright: Ansible Project
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
from __future__ import absolute_import, division, print_function
|
|
__metaclass__ = type
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
---
|
|
module: cloudwatchlogs_put_metric_data
|
|
version_added: 1.3.0
|
|
author:
|
|
- "Markus Bergholz (@markuman)"
|
|
short_description: Put custom metric data to cloudwatch
|
|
description:
|
|
- Put custom metric data to cloudwatch.
|
|
requirements:
|
|
- boto3
|
|
- botocore
|
|
options:
|
|
|
|
extends_documentation_fragment:
|
|
- amazon.aws.aws
|
|
- amazon.aws.ec2
|
|
|
|
'''
|
|
|
|
EXAMPLES = '''
|
|
- name: put metric data
|
|
community.aws.cloudwatchlogs_put_metric_data:
|
|
namespace: sitespeed.io
|
|
metric_data:
|
|
- metric_name: sitespeed.io
|
|
dimensions:
|
|
- name: website
|
|
value: www.lekker.de
|
|
value: 1879
|
|
unit: Milliseconds
|
|
|
|
'''
|
|
|
|
RETURN = """
|
|
|
|
|
|
"""
|
|
from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule, is_boto3_error_code, get_boto3_client_method_parameters
|
|
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict
|
|
|
|
try:
|
|
from botocore.exceptions import ClientError, BotoCoreError, WaiterError
|
|
except ImportError:
|
|
pass # caught by AnsibleAWSModule
|
|
|
|
from datetime import datetime
|
|
|
|
def put_metric_data(cw, params):
|
|
dt = datetime.utcnow()
|
|
|
|
metric_data = list()
|
|
for md in params.get('metric_data'):
|
|
data = dict()
|
|
data['MetricName'] = md.get('metric_name')
|
|
data['Value'] = float(md.get('value'))
|
|
data['Unit'] = md.get('unit') or 'None'
|
|
data['Dimensions'] = list()
|
|
data['Timestamp'] = md.get('timestamp') or dt
|
|
for item in md.get('dimensions'):
|
|
d = dict()
|
|
d['Name'] = item.get('name')
|
|
d['Value'] = item.get('value')
|
|
data['Dimensions'].append(d)
|
|
metric_data.append(data)
|
|
|
|
|
|
response = cw.put_metric_data(
|
|
Namespace=params.get('namespace'),
|
|
MetricData=metric_data
|
|
)
|
|
if response['ResponseMetadata']['HTTPStatusCode'] == 200:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
|
|
def main():
|
|
|
|
arg_spec = dict(
|
|
namespace=dict(type='str', required=True),
|
|
metric_data=dict(type='list', required=True)
|
|
)
|
|
|
|
|
|
module = AnsibleAWSModule(
|
|
argument_spec=arg_spec,
|
|
supports_check_mode=False
|
|
)
|
|
|
|
cw = module.client('cloudwatch')
|
|
change = put_metric_data(cw, module.params)
|
|
|
|
|
|
module.exit_json(changed=change, metric_data=None)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|