1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/plugins/modules/monitoring/datadog/datadog_event.py
patchback[bot] 703bb465c7
Tidy up validate-modules ignores for monitoring modules (#1350) (#1372)
* fixed validation-modules for plugins/modules/monitoring/bigpanda.py

* fixed validation-modules for plugins/modules/monitoring/circonus_annotation.py

* fixed validation-modules for plugins/modules/monitoring/honeybadger_deployment.py

* fixed validation-modules for plugins/modules/monitoring/icinga2_feature.py

* fixed validation-modules for plugins/modules/monitoring/icinga2_host.py

* fixed validation-modules for plugins/modules/monitoring/librato_annotation.py

* fixed validation-modules for plugins/modules/monitoring/logentries.py

* fixed validation-modules for plugins/modules/monitoring/logstash_plugin.py

* fixed validation-modules for plugins/modules/monitoring/newrelic_deployment.py

* fixed validation-modules for plugins/modules/monitoring/pagerduty_alert.py

* fixed validation-modules for plugins/modules/monitoring/pagerduty.py

* fixed validation-modules for plugins/modules/monitoring/pingdom.py

* fixed validation-modules for plugins/modules/monitoring/rollbar_deployment.py

* fixed validation-modules for plugins/modules/monitoring/spectrum_device.py

* fixed validation-modules for plugins/modules/monitoring/stackdriver.py

* fixed validation-modules for plugins/modules/monitoring/statusio_maintenance.py

* fixed validation-modules for plugins/modules/monitoring/uptimerobot.py

* fixed validation-modules for plugins/modules/monitoring/datadog/datadog_event.py

* fixed validation-modules for plugins/modules/monitoring/datadog/datadog_monitor.py

* fixed validation-modules for plugins/modules/monitoring/sensu/sensu_check.py

* fixed validation-modules for plugins/modules/monitoring/sensu/sensu_client.py

* fixed validation-modules for plugins/modules/monitoring/sensu/sensu_handler.py

* fixed validation-modules for plugins/modules/monitoring/sensu/sensu_silence.py

* fixed validation-modules for plugins/modules/monitoring/sensu/sensu_subscription.py

* fixed trailing space

* Enabling validation-modules for monitoring modules

* Added line for 2.9

* Update plugins/modules/monitoring/icinga2_host.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/monitoring/datadog/datadog_event.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Suggestion from PR

* oops, missed the 2.11 ignore file

* Update plugins/modules/monitoring/icinga2_host.py

Co-authored-by: Felix Fontein <felix@fontein.de>

Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit ce5fc7764a)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
2020-11-24 05:33:32 +01:00

167 lines
5.1 KiB
Python

#!/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).
# 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: 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:
- "Artūras `arturaz` Šlajus (@arturaz)"
- "Naoya Nakazawa (@n0ts)"
options:
api_key:
type: str
description: ["Your DataDog API key."]
required: true
app_key:
type: str
description: ["Your DataDog app key."]
required: true
title:
type: str
description: ["The event title."]
required: true
text:
type: str
description: ["The body of the event."]
required: true
date_happened:
type: int
description:
- POSIX timestamp of the event.
- Default value is now.
priority:
type: str
description: ["The priority of the event."]
default: normal
choices: [normal, low]
host:
type: str
description:
- Host name to associate with the event.
- If not specified, it defaults to the remote system's hostname.
tags:
type: list
description: ["Comma separated list of tags to apply to the event."]
alert_type:
type: str
description: ["Type of alert."]
default: info
choices: ['error', 'warning', 'info', 'success']
aggregation_key:
type: str
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 = '''
- name: Post an event with low priority
community.general.datadog_event:
title: Testing from ansible
text: Test
priority: low
api_key: 9775a026f1ca7d1c6c5af9d94d9595a4
app_key: j4JyCYfefWHhgFgiZUqRm63AXHNZQyPGBfJtAzmN
- name: Post an event with several tags
community.general.datadog_event:
title: Testing from ansible
text: Test
api_key: 9775a026f1ca7d1c6c5af9d94d9595a4
app_key: j4JyCYfefWHhgFgiZUqRm63AXHNZQyPGBfJtAzmN
tags: 'aa,bb,#host:{{ inventory_hostname }}'
'''
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
from ansible.module_utils._text import to_native
def main():
module = AnsibleModule(
argument_spec=dict(
api_key=dict(required=True, no_log=True),
app_key=dict(required=True, no_log=True),
title=dict(required=True),
text=dict(required=True),
date_happened=dict(required=False, default=None, type='int'),
priority=dict(
required=False, default='normal', choices=['normal', 'low']
),
host=dict(required=False, default=None),
tags=dict(required=False, default=None, type='list'),
alert_type=dict(
required=False, default='info',
choices=['error', 'warning', 'info', 'success']
),
aggregation_key=dict(required=False, default=None),
validate_certs=dict(default=True, type='bool'),
)
)
# 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'],
'app_key': module.params['app_key']
}
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()