2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Author: Artūras 'arturaz' Šlajus <x11@arturaz.net>
|
|
|
|
# Author: Naoya Nakazawa <naoya.n@gmail.com>
|
|
|
|
#
|
|
|
|
# This module is proudly sponsored by iGeolise (www.igeolise.com) and
|
|
|
|
# Tiny Lab Productions (www.tinylabproductions.com).
|
2022-08-05 13:17:19 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: datadog_event
|
|
|
|
short_description: Posts events to Datadog service
|
|
|
|
description:
|
|
|
|
- "Allows to post events to Datadog (www.datadoghq.com) service."
|
|
|
|
- "Uses http://docs.datadoghq.com/api/#events API."
|
|
|
|
author:
|
2022-06-22 22:54:08 +02:00
|
|
|
- "Artūras 'arturaz' Šlajus (@arturaz)"
|
2020-03-09 10:11:07 +01:00
|
|
|
- "Naoya Nakazawa (@n0ts)"
|
|
|
|
options:
|
|
|
|
api_key:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description: ["Your DataDog API key."]
|
|
|
|
required: true
|
|
|
|
app_key:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description: ["Your DataDog app key."]
|
|
|
|
required: true
|
|
|
|
title:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description: ["The event title."]
|
|
|
|
required: true
|
|
|
|
text:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description: ["The body of the event."]
|
|
|
|
required: true
|
|
|
|
date_happened:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: int
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- POSIX timestamp of the event.
|
|
|
|
- Default value is now.
|
|
|
|
priority:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description: ["The priority of the event."]
|
|
|
|
default: normal
|
|
|
|
choices: [normal, low]
|
|
|
|
host:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
|
|
|
description:
|
|
|
|
- Host name to associate with the event.
|
|
|
|
- If not specified, it defaults to the remote system's hostname.
|
2021-06-17 19:05:35 +02:00
|
|
|
api_host:
|
|
|
|
type: str
|
|
|
|
description:
|
|
|
|
- DataDog API endpoint URL.
|
|
|
|
version_added: '3.3.0'
|
2020-03-09 10:11:07 +01:00
|
|
|
tags:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: list
|
2021-02-26 11:37:23 +01:00
|
|
|
elements: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description: ["Comma separated list of tags to apply to the event."]
|
|
|
|
alert_type:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description: ["Type of alert."]
|
|
|
|
default: info
|
|
|
|
choices: ['error', 'warning', 'info', 'success']
|
|
|
|
aggregation_key:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description: ["An arbitrary string to use for aggregation."]
|
|
|
|
validate_certs:
|
|
|
|
description:
|
|
|
|
- If C(no), SSL certificates will not be validated. This should only be used
|
|
|
|
on personally controlled sites using self-signed certificates.
|
|
|
|
type: bool
|
|
|
|
default: 'yes'
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Post an event with low priority
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.datadog_event:
|
2020-03-09 10:11:07 +01:00
|
|
|
title: Testing from ansible
|
|
|
|
text: Test
|
|
|
|
priority: low
|
|
|
|
api_key: 9775a026f1ca7d1c6c5af9d94d9595a4
|
|
|
|
app_key: j4JyCYfefWHhgFgiZUqRm63AXHNZQyPGBfJtAzmN
|
2020-05-15 12:12:41 +02:00
|
|
|
|
|
|
|
- name: Post an event with several tags
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.datadog_event:
|
2020-03-09 10:11:07 +01:00
|
|
|
title: Testing from ansible
|
|
|
|
text: Test
|
|
|
|
api_key: 9775a026f1ca7d1c6c5af9d94d9595a4
|
|
|
|
app_key: j4JyCYfefWHhgFgiZUqRm63AXHNZQyPGBfJtAzmN
|
|
|
|
tags: 'aa,bb,#host:{{ inventory_hostname }}'
|
2021-06-17 19:05:35 +02:00
|
|
|
|
|
|
|
- name: Post an event with several tags to another endpoint
|
|
|
|
community.general.datadog_event:
|
|
|
|
title: Testing from ansible
|
|
|
|
text: Test
|
|
|
|
api_key: 9775a026f1ca7d1c6c5af9d94d9595a4
|
|
|
|
app_key: j4JyCYfefWHhgFgiZUqRm63AXHNZQyPGBfJtAzmN
|
|
|
|
api_host: 'https://example.datadoghq.eu'
|
|
|
|
tags:
|
|
|
|
- aa
|
|
|
|
- b
|
|
|
|
- '#host:{{ inventory_hostname }}'
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
import platform
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
# Import Datadog
|
|
|
|
DATADOG_IMP_ERR = None
|
|
|
|
try:
|
|
|
|
from datadog import initialize, api
|
|
|
|
HAS_DATADOG = True
|
|
|
|
except Exception:
|
|
|
|
DATADOG_IMP_ERR = traceback.format_exc()
|
|
|
|
HAS_DATADOG = False
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
api_key=dict(required=True, no_log=True),
|
|
|
|
app_key=dict(required=True, no_log=True),
|
2021-06-17 19:05:35 +02:00
|
|
|
api_host=dict(type='str'),
|
2020-03-09 10:11:07 +01:00
|
|
|
title=dict(required=True),
|
|
|
|
text=dict(required=True),
|
2021-02-26 11:37:23 +01:00
|
|
|
date_happened=dict(type='int'),
|
|
|
|
priority=dict(default='normal', choices=['normal', 'low']),
|
|
|
|
host=dict(),
|
|
|
|
tags=dict(type='list', elements='str'),
|
|
|
|
alert_type=dict(default='info', choices=['error', 'warning', 'info', 'success']),
|
2021-03-13 13:24:26 +01:00
|
|
|
aggregation_key=dict(no_log=False),
|
2020-06-22 15:56:35 +02:00
|
|
|
validate_certs=dict(default=True, type='bool'),
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Prepare Datadog
|
|
|
|
if not HAS_DATADOG:
|
|
|
|
module.fail_json(msg=missing_required_lib('datadogpy'), exception=DATADOG_IMP_ERR)
|
|
|
|
|
|
|
|
options = {
|
|
|
|
'api_key': module.params['api_key'],
|
2021-06-17 19:05:35 +02:00
|
|
|
'app_key': module.params['app_key'],
|
2020-03-09 10:11:07 +01:00
|
|
|
}
|
2021-06-17 19:05:35 +02:00
|
|
|
if module.params['api_host'] is not None:
|
|
|
|
options['api_host'] = module.params['api_host']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
initialize(**options)
|
|
|
|
|
|
|
|
_post_event(module)
|
|
|
|
|
|
|
|
|
|
|
|
def _post_event(module):
|
|
|
|
try:
|
|
|
|
if module.params['host'] is None:
|
|
|
|
module.params['host'] = platform.node().split('.')[0]
|
|
|
|
msg = api.Event.create(title=module.params['title'],
|
|
|
|
text=module.params['text'],
|
|
|
|
host=module.params['host'],
|
|
|
|
tags=module.params['tags'],
|
|
|
|
priority=module.params['priority'],
|
|
|
|
alert_type=module.params['alert_type'],
|
|
|
|
aggregation_key=module.params['aggregation_key'],
|
|
|
|
source_type_name='ansible')
|
|
|
|
if msg['status'] != 'ok':
|
|
|
|
module.fail_json(msg=msg)
|
|
|
|
|
|
|
|
module.exit_json(changed=True, msg=msg)
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|