Debian Codegraph @

ansible_collections/community/general/plugins/modules/datadog_event.py

raw
1 #!/usr/bin/python
2 #
3 # Author: Artūras 'arturaz' Šlajus <[email protected]>
4 # Author: Naoya Nakazawa <[email protected]>
5 #
6 # This module is proudly sponsored by iGeolise (www.igeolise.com) and
7 # Tiny Lab Productions (www.tinylabproductions.com).
8 # Copyright (c) Ansible project
9 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
10 # SPDX-License-Identifier: GPL-3.0-or-later
11
12 from __future__ import annotations
13
14 DOCUMENTATION = r"""
15 module: datadog_event
16 short_description: Posts events to Datadog service
17 description:
18 - Allows to post events to Datadog (www.datadoghq.com) service.
19 - Uses http://docs.datadoghq.com/api/#events API.
20 author:
21 - "Artūras 'arturaz' Šlajus (@arturaz)"
22 - "Naoya Nakazawa (@n0ts)"
23 extends_documentation_fragment:
24 - community.general._attributes
25 attributes:
26 check_mode:
27 support: none
28 diff_mode:
29 support: none
30 options:
31 api_key:
32 type: str
33 description:
34 - Your DataDog API key.
35 required: true
36 app_key:
37 type: str
38 description:
39 - Your DataDog app key.
40 required: true
41 title:
42 type: str
43 description:
44 - The event title.
45 required: true
46 text:
47 type: str
48 description:
49 - The body of the event.
50 required: true
51 date_happened:
52 type: int
53 description:
54 - POSIX timestamp of the event.
55 - Default value is now.
56 priority:
57 type: str
58 description:
59 - The priority of the event.
60 default: normal
61 choices: [normal, low]
62 host:
63 type: str
64 description:
65 - Host name to associate with the event.
66 - If not specified, it defaults to the remote system's hostname.
67 api_host:
68 type: str
69 description:
70 - DataDog API endpoint URL.
71 version_added: '3.3.0'
72 tags:
73 type: list
74 elements: str
75 description:
76 - Comma separated list of tags to apply to the event.
77 alert_type:
78 type: str
79 description:
80 - Type of alert.
81 default: info
82 choices: ['error', 'warning', 'info', 'success']
83 aggregation_key:
84 type: str
85 description:
86 - An arbitrary string to use for aggregation.
87 validate_certs:
88 description:
89 - If V(false), SSL certificates are not validated. This should only be used on personally controlled sites using self-signed
90 certificates.
91 type: bool
92 default: true
93 """
94
95 EXAMPLES = r"""
96 - name: Post an event with low priority
97 community.general.datadog_event:
98 title: Testing from ansible
99 text: Test
100 priority: low
101 api_key: 9775a026f1ca7d1c6c5af9d94d9595a4
102 app_key: j4JyCYfefWHhgFgiZUqRm63AXHNZQyPGBfJtAzmN
103
104 - name: Post an event with several tags
105 community.general.datadog_event:
106 title: Testing from ansible
107 text: Test
108 api_key: 9775a026f1ca7d1c6c5af9d94d9595a4
109 app_key: j4JyCYfefWHhgFgiZUqRm63AXHNZQyPGBfJtAzmN
110 tags: 'aa,bb,#host:{{ inventory_hostname }}'
111
112 - name: Post an event with several tags to another endpoint
113 community.general.datadog_event:
114 title: Testing from ansible
115 text: Test
116 api_key: 9775a026f1ca7d1c6c5af9d94d9595a4
117 app_key: j4JyCYfefWHhgFgiZUqRm63AXHNZQyPGBfJtAzmN
118 api_host: 'https://example.datadoghq.eu'
119 tags:
120 - aa
121 - b
122 - '#host:{{ inventory_hostname }}'
123 """
124
125 import platform
126 import traceback
127
128 # Import Datadog
129 DATADOG_IMP_ERR = None
130 try:
131 from datadog import api, initialize
132
133 HAS_DATADOG = True
134 except Exception:
135 DATADOG_IMP_ERR = traceback.format_exc()
136 HAS_DATADOG = False
137
138 from ansible.module_utils.basic import AnsibleModule, missing_required_lib
139
140
141 def main():
142 module = AnsibleModule(
143 argument_spec=dict(
144 api_key=dict(required=True, no_log=True),
145 app_key=dict(required=True, no_log=True),
146 api_host=dict(type="str"),
147 title=dict(required=True),
148 text=dict(required=True),
149 date_happened=dict(type="int"),
150 priority=dict(default="normal", choices=["normal", "low"]),
151 host=dict(),
152 tags=dict(type="list", elements="str"),
153 alert_type=dict(default="info", choices=["error", "warning", "info", "success"]),
154 aggregation_key=dict(no_log=False),
155 validate_certs=dict(default=True, type="bool"),
156 )
157 )
158
159 # Prepare Datadog
160 if not HAS_DATADOG:
161 module.fail_json(msg=missing_required_lib("datadogpy"), exception=DATADOG_IMP_ERR)
162
163 options = {
164 "api_key": module.params["api_key"],
165 "app_key": module.params["app_key"],
166 }
167 if module.params["api_host"] is not None:
168 options["api_host"] = module.params["api_host"]
169
170 initialize(**options)
171
172 _post_event(module)
173
174
175 def _post_event(module):
176 try:
177 if module.params["host"] is None:
178 module.params["host"] = platform.node().split(".")[0]
179 msg = api.Event.create(
180 title=module.params["title"],
181 text=module.params["text"],
182 host=module.params["host"],
183 tags=module.params["tags"],
184 priority=module.params["priority"],
185 alert_type=module.params["alert_type"],
186 aggregation_key=module.params["aggregation_key"],
187 source_type_name="ansible",
188 )
189 if msg["status"] != "ok":
190 module.fail_json(msg=msg)
191
192 module.exit_json(changed=True, msg=msg)
193 except Exception as e:
194 module.fail_json(msg=f"{e}", exception=traceback.format_exc())
195
196
197 if __name__ == "__main__":
198 main()