| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
from __future__ import absolute_import, division, print_function |
| 19 |
|
| 20 |
__metaclass__ = type |
| 21 |
|
| 22 |
DOCUMENTATION = """ |
| 23 |
--- |
| 24 |
module: grafana_notification_channel |
| 25 |
notes: |
| 26 |
- Notification channels are replaced by contact points starting Grafana 8.3 and this module is currently not able to manage contact points. |
| 27 |
- The module will report execution as successful since Grafana maintains backward compatibility with previous alert management, but |
| 28 |
- nothing will be visible in the contact points if new alerting mechanism is enabled. |
| 29 |
author: |
| 30 |
- Aliaksandr Mianzhynski (@amenzhinsky) |
| 31 |
- Rémi REY (@rrey) |
| 32 |
version_added: "1.1.0" |
| 33 |
short_description: Manage Grafana Notification Channels |
| 34 |
description: |
| 35 |
- Create/Update/Delete Grafana Notification Channels via API. |
| 36 |
deprecated: |
| 37 |
removed_in: "3.0.0" |
| 38 |
why: Legacy alerting is removed in Grafana version 11. |
| 39 |
alternative: Use M(community.grafana.grafana_contact_point) instead. |
| 40 |
options: |
| 41 |
org_id: |
| 42 |
description: |
| 43 |
- The Grafana Organisation ID where the dashboard will be imported / exported. |
| 44 |
- Not used when I(grafana_api_key) is set, because the grafana_api_key only belongs to one organisation.. |
| 45 |
default: 1 |
| 46 |
type: int |
| 47 |
state: |
| 48 |
type: str |
| 49 |
default: present |
| 50 |
choices: |
| 51 |
- present |
| 52 |
- absent |
| 53 |
description: |
| 54 |
- Status of the notification channel. |
| 55 |
uid: |
| 56 |
type: str |
| 57 |
description: |
| 58 |
- The channel unique identifier. |
| 59 |
name: |
| 60 |
type: str |
| 61 |
description: |
| 62 |
- The name of the notification channel. |
| 63 |
- Required when I(state) is C(present). |
| 64 |
type: |
| 65 |
type: str |
| 66 |
choices: |
| 67 |
- dingding |
| 68 |
- discord |
| 69 |
- email |
| 70 |
- googlechat |
| 71 |
- hipchat |
| 72 |
- kafka |
| 73 |
- line |
| 74 |
- teams |
| 75 |
- opsgenie |
| 76 |
- pagerduty |
| 77 |
- prometheus |
| 78 |
- pushover |
| 79 |
- sensu |
| 80 |
- slack |
| 81 |
- telegram |
| 82 |
- threema |
| 83 |
- victorops |
| 84 |
- webhook |
| 85 |
description: |
| 86 |
- The channel notification type. |
| 87 |
- Required when I(state) is C(present). |
| 88 |
is_default: |
| 89 |
type: bool |
| 90 |
default: false |
| 91 |
description: |
| 92 |
- Use this channel for all alerts. |
| 93 |
include_image: |
| 94 |
type: bool |
| 95 |
default: false |
| 96 |
description: |
| 97 |
- Capture a visualization image and attach it to notifications. |
| 98 |
disable_resolve_message: |
| 99 |
type: bool |
| 100 |
default: false |
| 101 |
description: |
| 102 |
- Disable the resolve message. |
| 103 |
skip_version_check: |
| 104 |
description: |
| 105 |
- Skip Grafana version check and try to reach api endpoint anyway. |
| 106 |
type: bool |
| 107 |
default: false |
| 108 |
reminder_frequency: |
| 109 |
type: str |
| 110 |
description: |
| 111 |
- Additional notifications interval for triggered alerts. |
| 112 |
- For example C(15m). |
| 113 |
dingding_url: |
| 114 |
type: str |
| 115 |
description: |
| 116 |
- DingDing webhook URL. |
| 117 |
dingding_message_type: |
| 118 |
type: list |
| 119 |
elements: str |
| 120 |
choices: |
| 121 |
- link |
| 122 |
- action_card |
| 123 |
description: |
| 124 |
- DingDing message type. |
| 125 |
discord_url: |
| 126 |
type: str |
| 127 |
description: |
| 128 |
- Discord webhook URL. |
| 129 |
discord_message_content: |
| 130 |
type: str |
| 131 |
description: |
| 132 |
- Overrides message content. |
| 133 |
email_addresses: |
| 134 |
type: list |
| 135 |
elements: str |
| 136 |
description: |
| 137 |
- List of recipients. |
| 138 |
email_single: |
| 139 |
type: bool |
| 140 |
description: |
| 141 |
- Send single email to all recipients. |
| 142 |
googlechat_url: |
| 143 |
type: str |
| 144 |
description: |
| 145 |
- Google Hangouts webhook URL. |
| 146 |
hipchat_url: |
| 147 |
type: str |
| 148 |
description: |
| 149 |
- HipChat webhook URL. |
| 150 |
hipchat_api_key: |
| 151 |
type: str |
| 152 |
description: |
| 153 |
- HipChat API key. |
| 154 |
hipchat_room_id: |
| 155 |
type: str |
| 156 |
description: |
| 157 |
- HipChat room ID. |
| 158 |
kafka_url: |
| 159 |
type: str |
| 160 |
description: |
| 161 |
- Kafka REST proxy URL. |
| 162 |
kafka_topic: |
| 163 |
type: str |
| 164 |
description: |
| 165 |
- Kafka topic name. |
| 166 |
line_token: |
| 167 |
type: str |
| 168 |
description: |
| 169 |
- LINE token. |
| 170 |
teams_url: |
| 171 |
type: str |
| 172 |
description: |
| 173 |
- Microsoft Teams webhook URL. |
| 174 |
opsgenie_url: |
| 175 |
type: str |
| 176 |
description: |
| 177 |
- OpsGenie webhook URL. |
| 178 |
opsgenie_api_key: |
| 179 |
type: str |
| 180 |
description: |
| 181 |
- OpsGenie API key. |
| 182 |
opsgenie_auto_close: |
| 183 |
type: bool |
| 184 |
description: |
| 185 |
- Automatically close alerts in OpsGenie once the alert goes back to ok. |
| 186 |
opsgenie_override_priority: |
| 187 |
type: bool |
| 188 |
description: |
| 189 |
- Allow the alert priority to be set using the og_priority tag. |
| 190 |
pagerduty_integration_key: |
| 191 |
type: str |
| 192 |
description: |
| 193 |
- PagerDuty integration key. |
| 194 |
pagerduty_severity: |
| 195 |
type: list |
| 196 |
elements: str |
| 197 |
choices: |
| 198 |
- critical |
| 199 |
- error |
| 200 |
- warning |
| 201 |
- info |
| 202 |
description: |
| 203 |
- Alert severity in PagerDuty. |
| 204 |
pagerduty_auto_resolve: |
| 205 |
type: bool |
| 206 |
description: |
| 207 |
- Resolve incidents in PagerDuty once the alert goes back to ok. |
| 208 |
pagerduty_message_in_details: |
| 209 |
type: bool |
| 210 |
description: |
| 211 |
- Move the alert message from the PD summary into the custom details. |
| 212 |
- This changes the custom details object and may break event rules you have configured. |
| 213 |
prometheus_url: |
| 214 |
type: str |
| 215 |
description: |
| 216 |
- Prometheus API URL. |
| 217 |
prometheus_username: |
| 218 |
type: str |
| 219 |
description: |
| 220 |
- Prometheus username. |
| 221 |
prometheus_password: |
| 222 |
type: str |
| 223 |
description: |
| 224 |
- Prometheus password. |
| 225 |
pushover_api_token: |
| 226 |
type: str |
| 227 |
description: |
| 228 |
- Pushover API token. |
| 229 |
pushover_user_key: |
| 230 |
type: str |
| 231 |
description: |
| 232 |
- Pushover user key. |
| 233 |
pushover_devices: |
| 234 |
type: list |
| 235 |
elements: str |
| 236 |
description: |
| 237 |
- Devices list in Pushover. |
| 238 |
pushover_priority: |
| 239 |
type: list |
| 240 |
elements: str |
| 241 |
choices: |
| 242 |
- emergency |
| 243 |
- high |
| 244 |
- normal |
| 245 |
- low |
| 246 |
- lowest |
| 247 |
description: |
| 248 |
- Alert priority in Pushover. |
| 249 |
pushover_retry: |
| 250 |
type: int |
| 251 |
description: |
| 252 |
- Retry in C(n) minutes. |
| 253 |
- Only when priority is C(emergency). |
| 254 |
pushover_expire: |
| 255 |
type: int |
| 256 |
description: |
| 257 |
- Expire alert in C(n) minutes. |
| 258 |
- Only when priority is C(emergency). |
| 259 |
pushover_alert_sound: |
| 260 |
type: str |
| 261 |
description: |
| 262 |
- L(Alert sound in Pushover,https://pushover.net/api#sounds) |
| 263 |
pushover_ok_sound: |
| 264 |
type: str |
| 265 |
description: |
| 266 |
- L(OK sound in Pushover,https://pushover.net/api#sounds) |
| 267 |
sensu_url: |
| 268 |
type: str |
| 269 |
description: |
| 270 |
- Sensu webhook URL. |
| 271 |
sensu_source: |
| 272 |
type: str |
| 273 |
description: |
| 274 |
- Source in Sensu. |
| 275 |
sensu_handler: |
| 276 |
type: str |
| 277 |
description: |
| 278 |
- Sensu handler name. |
| 279 |
sensu_username: |
| 280 |
type: str |
| 281 |
description: |
| 282 |
- Sensu user. |
| 283 |
sensu_password: |
| 284 |
type: str |
| 285 |
description: |
| 286 |
- Sensu password. |
| 287 |
slack_url: |
| 288 |
type: str |
| 289 |
description: |
| 290 |
- Slack webhook URL. |
| 291 |
slack_recipient: |
| 292 |
type: str |
| 293 |
description: |
| 294 |
- Override default Slack channel or user. |
| 295 |
slack_username: |
| 296 |
type: str |
| 297 |
description: |
| 298 |
- Set the username for the bot's message. |
| 299 |
slack_icon_emoji: |
| 300 |
type: str |
| 301 |
description: |
| 302 |
- An emoji to use for the bot's message. |
| 303 |
slack_icon_url: |
| 304 |
type: str |
| 305 |
description: |
| 306 |
- URL to an image to use as the icon for the bot's message |
| 307 |
slack_mention_users: |
| 308 |
type: list |
| 309 |
elements: str |
| 310 |
description: |
| 311 |
- Mention users list. |
| 312 |
slack_mention_groups: |
| 313 |
type: list |
| 314 |
elements: str |
| 315 |
description: |
| 316 |
- Mention groups list. |
| 317 |
slack_mention_channel: |
| 318 |
type: list |
| 319 |
elements: str |
| 320 |
choices: |
| 321 |
- here |
| 322 |
- channel |
| 323 |
description: |
| 324 |
- Mention whole channel or just active members. |
| 325 |
slack_token: |
| 326 |
type: str |
| 327 |
description: |
| 328 |
- Slack token. |
| 329 |
telegram_bot_token: |
| 330 |
type: str |
| 331 |
description: |
| 332 |
- Telegram bot token; |
| 333 |
telegram_chat_id: |
| 334 |
type: str |
| 335 |
description: |
| 336 |
- Telegram chat id. |
| 337 |
threema_gateway_id: |
| 338 |
type: str |
| 339 |
description: |
| 340 |
- 8 character Threema Gateway ID (starting with a *). |
| 341 |
threema_recipient_id: |
| 342 |
type: str |
| 343 |
description: |
| 344 |
- 8 character Threema ID that should receive the alerts. |
| 345 |
threema_api_secret: |
| 346 |
type: str |
| 347 |
description: |
| 348 |
- Threema Gateway API secret. |
| 349 |
victorops_url: |
| 350 |
type: str |
| 351 |
description: |
| 352 |
- VictorOps webhook URL. |
| 353 |
victorops_auto_resolve: |
| 354 |
type: bool |
| 355 |
description: |
| 356 |
- Resolve incidents in VictorOps once the alert goes back to ok. |
| 357 |
webhook_url: |
| 358 |
type: str |
| 359 |
description: |
| 360 |
- Webhook URL |
| 361 |
webhook_username: |
| 362 |
type: str |
| 363 |
description: |
| 364 |
- Webhook username. |
| 365 |
webhook_password: |
| 366 |
type: str |
| 367 |
description: |
| 368 |
- Webhook password. |
| 369 |
webhook_http_method: |
| 370 |
type: list |
| 371 |
elements: str |
| 372 |
choices: |
| 373 |
- POST |
| 374 |
- PUT |
| 375 |
description: |
| 376 |
- Webhook HTTP verb to use. |
| 377 |
|
| 378 |
extends_documentation_fragment: |
| 379 |
- community.grafana.basic_auth |
| 380 |
- community.grafana.api_key |
| 381 |
""" |
| 382 |
|
| 383 |
|
| 384 |
EXAMPLES = """ |
| 385 |
- name: Create slack notification channel |
| 386 |
register: result |
| 387 |
grafana_notification_channel: |
| 388 |
uid: slack |
| 389 |
name: slack |
| 390 |
type: slack |
| 391 |
slack_url: https://hooks.slack.com/services/xxx/yyy/zzz |
| 392 |
grafana_url: "{{ grafana_url }}" |
| 393 |
grafana_user: "{{ grafana_username }}" |
| 394 |
grafana_password: "{{ grafana_password}}" |
| 395 |
|
| 396 |
- name: Delete slack notification channel |
| 397 |
register: result |
| 398 |
grafana_notification_channel: |
| 399 |
state: absent |
| 400 |
uid: slack |
| 401 |
grafana_url: "{{ grafana_url }}" |
| 402 |
grafana_user: "{{ grafana_username }}" |
| 403 |
grafana_password: "{{ grafana_password}}" |
| 404 |
""" |
| 405 |
|
| 406 |
RETURN = """ |
| 407 |
notification_channel: |
| 408 |
description: Notification channel created or updated by the module. |
| 409 |
returned: changed |
| 410 |
type: dict |
| 411 |
sample: | |
| 412 |
{ |
| 413 |
"created": "2020-11-10T21:10:19.675308051+03:00", |
| 414 |
"disableResolveMessage": false, |
| 415 |
"frequency": "", |
| 416 |
"id": 37, |
| 417 |
"isDefault": false, |
| 418 |
"name": "Oops", |
| 419 |
"secureFields": {}, |
| 420 |
"sendReminder": false, |
| 421 |
"settings": { |
| 422 |
"uploadImage": false, |
| 423 |
"url": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" |
| 424 |
}, |
| 425 |
"type": "slack", |
| 426 |
"uid": "slack-oops", |
| 427 |
"updated": "2020-11-10T21:10:19.675308112+03:00" |
| 428 |
} |
| 429 |
""" |
| 430 |
|
| 431 |
import json |
| 432 |
|
| 433 |
from ansible.module_utils.basic import AnsibleModule |
| 434 |
from ansible.module_utils.urls import fetch_url |
| 435 |
from ansible.module_utils._text import to_text |
| 436 |
from ansible_collections.community.grafana.plugins.module_utils.base import ( |
| 437 |
grafana_argument_spec, |
| 438 |
clean_url, |
| 439 |
parse_grafana_version, |
| 440 |
) |
| 441 |
from ansible.module_utils.urls import basic_auth_header |
| 442 |
|
| 443 |
|
| 444 |
class GrafanaAPIException(Exception): |
| 445 |
pass |
| 446 |
|
| 447 |
|
| 448 |
def dingding_channel_payload(data, payload): |
| 449 |
payload["settings"]["url"] = data["dingding_url"] |
| 450 |
if data.get("dingding_message_type"): |
| 451 |
payload["settings"]["msgType"] = { |
| 452 |
"link": "link", |
| 453 |
"action_card": "actionCard", |
| 454 |
}[data["dingding_message_type"]] |
| 455 |
|
| 456 |
|
| 457 |
def discord_channel_payload(data, payload): |
| 458 |
payload["settings"]["url"] = data["discord_url"] |
| 459 |
if data.get("discord_message_content"): |
| 460 |
payload["settings"]["content"] = data["discord_message_content"] |
| 461 |
|
| 462 |
|
| 463 |
def email_channel_payload(data, payload): |
| 464 |
payload["settings"]["addresses"] = ";".join(data["email_addresses"]) |
| 465 |
if data.get("email_single"): |
| 466 |
payload["settings"]["singleEmail"] = data["email_single"] |
| 467 |
|
| 468 |
|
| 469 |
def hipchat_channel_payload(data, payload): |
| 470 |
payload["settings"]["url"] = data["hipchat_url"] |
| 471 |
if data.get("hipchat_api_key"): |
| 472 |
payload["settings"]["apiKey"] = data["hipchat_api_key"] |
| 473 |
if data.get("hipchat_room_id"): |
| 474 |
payload["settings"]["roomid"] = data["hipchat_room_id"] |
| 475 |
|
| 476 |
|
| 477 |
def pagerduty_channel_payload(data, payload): |
| 478 |
payload["settings"]["integrationKey"] = data["pagerduty_integration_key"] |
| 479 |
if data.get("pagerduty_severity"): |
| 480 |
payload["settings"]["severity"] = data["pagerduty_severity"] |
| 481 |
if data.get("pagerduty_auto_resolve"): |
| 482 |
payload["settings"]["autoResolve"] = data["pagerduty_auto_resolve"] |
| 483 |
if data.get("pagerduty_message_in_details"): |
| 484 |
payload["settings"]["messageInDetails"] = data["pagerduty_message_in_details"] |
| 485 |
|
| 486 |
|
| 487 |
def prometheus_channel_payload(data, payload): |
| 488 |
payload["type"] = "prometheus-alertmanager" |
| 489 |
payload["settings"]["url"] = data["prometheus_url"] |
| 490 |
if data.get("prometheus_username"): |
| 491 |
payload["settings"]["basicAuthUser"] = data["prometheus_username"] |
| 492 |
if data.get("prometheus_password"): |
| 493 |
payload["settings"]["basicAuthPassword"] = data["prometheus_password"] |
| 494 |
|
| 495 |
|
| 496 |
def pushover_channel_payload(data, payload): |
| 497 |
payload["settings"]["apiToken"] = data["pushover_api_token"] |
| 498 |
payload["settings"]["userKey"] = data["pushover_user_key"] |
| 499 |
if data.get("pushover_devices"): |
| 500 |
payload["settings"]["device"] = ";".join(data["pushover_devices"]) |
| 501 |
if data.get("pushover_priority"): |
| 502 |
payload["settings"]["priority"] = { |
| 503 |
"emergency": "2", |
| 504 |
"high": "1", |
| 505 |
"normal": "0", |
| 506 |
"low": "-1", |
| 507 |
"lowest": "-2", |
| 508 |
}[data["pushover_priority"]] |
| 509 |
if data.get("pushover_retry"): |
| 510 |
payload["settings"]["retry"] = str(data["pushover_retry"]) |
| 511 |
if data.get("pushover_expire"): |
| 512 |
payload["settings"]["expire"] = str(data["pushover_expire"]) |
| 513 |
if data.get("pushover_alert_sound"): |
| 514 |
payload["settings"]["sound"] = data["pushover_alert_sound"] |
| 515 |
if data.get("pushover_ok_sound"): |
| 516 |
payload["settings"]["okSound"] = data["pushover_ok_sound"] |
| 517 |
|
| 518 |
|
| 519 |
def sensu_channel_payload(data, payload): |
| 520 |
payload["settings"]["url"] = data["sensu_url"] |
| 521 |
if data.get("sensu_source"): |
| 522 |
payload["settings"]["source"] = data["sensu_source"] |
| 523 |
if data.get("sensu_handler"): |
| 524 |
payload["settings"]["handler"] = data["sensu_handler"] |
| 525 |
if data.get("sensu_username"): |
| 526 |
payload["settings"]["username"] = data["sensu_username"] |
| 527 |
if data.get("sensu_password"): |
| 528 |
payload["settings"]["password"] = data["sensu_password"] |
| 529 |
|
| 530 |
|
| 531 |
def slack_channel_payload(data, payload): |
| 532 |
payload["settings"]["url"] = data["slack_url"] |
| 533 |
if data.get("slack_recipient"): |
| 534 |
payload["settings"]["recipient"] = data["slack_recipient"] |
| 535 |
if data.get("slack_username"): |
| 536 |
payload["settings"]["username"] = data["slack_username"] |
| 537 |
if data.get("slack_icon_emoji"): |
| 538 |
payload["settings"]["iconEmoji"] = data["slack_icon_emoji"] |
| 539 |
if data.get("slack_icon_url"): |
| 540 |
payload["settings"]["iconUrl"] = data["slack_icon_url"] |
| 541 |
if data.get("slack_mention_users"): |
| 542 |
payload["settings"]["mentionUsers"] = ",".join(data["slack_mention_users"]) |
| 543 |
if data.get("slack_mention_groups"): |
| 544 |
payload["settings"]["mentionGroups"] = ",".join(data["slack_mention_groups"]) |
| 545 |
if data.get("slack_mention_channel"): |
| 546 |
payload["settings"]["mentionChannel"] = data["slack_mention_channel"] |
| 547 |
if data.get("slack_token"): |
| 548 |
payload["settings"]["token"] = data["slack_token"] |
| 549 |
|
| 550 |
|
| 551 |
def webhook_channel_payload(data, payload): |
| 552 |
payload["settings"]["url"] = data["webhook_url"] |
| 553 |
if data.get("webhook_http_method"): |
| 554 |
payload["settings"]["httpMethod"] = data["webhook_http_method"] |
| 555 |
if data.get("webhook_username"): |
| 556 |
payload["settings"]["username"] = data["webhook_username"] |
| 557 |
if data.get("webhook_password"): |
| 558 |
payload["settings"]["password"] = data["webhook_password"] |
| 559 |
|
| 560 |
|
| 561 |
def grafana_notification_channel_payload(data): |
| 562 |
payload = { |
| 563 |
"uid": data["uid"], |
| 564 |
"name": data["name"], |
| 565 |
"type": data["type"], |
| 566 |
"isDefault": data["is_default"], |
| 567 |
"disableResolveMessage": data["disable_resolve_message"], |
| 568 |
"settings": {"uploadImage": data["include_image"]}, |
| 569 |
} |
| 570 |
|
| 571 |
if data.get("reminder_frequency"): |
| 572 |
payload["sendReminder"] = True |
| 573 |
payload["frequency"] = data["reminder_frequency"] |
| 574 |
|
| 575 |
if data["type"] == "dingding": |
| 576 |
dingding_channel_payload(data, payload) |
| 577 |
elif data["type"] == "discord": |
| 578 |
discord_channel_payload(data, payload) |
| 579 |
elif data["type"] == "email": |
| 580 |
email_channel_payload(data, payload) |
| 581 |
elif data["type"] == "googlechat": |
| 582 |
payload["settings"]["url"] = data["googlechat_url"] |
| 583 |
elif data["type"] == "hipchat": |
| 584 |
hipchat_channel_payload(data, payload) |
| 585 |
elif data["type"] == "kafka": |
| 586 |
payload["settings"]["kafkaRestProxy"] = data["kafka_url"] |
| 587 |
payload["settings"]["kafkaTopic"] = data["kafka_topic"] |
| 588 |
elif data["type"] == "line": |
| 589 |
payload["settings"]["token"] = data["line_token"] |
| 590 |
elif data["type"] == "teams": |
| 591 |
payload["settings"]["url"] = data["teams_url"] |
| 592 |
elif data["type"] == "opsgenie": |
| 593 |
payload["settings"]["apiUrl"] = data["opsgenie_url"] |
| 594 |
payload["settings"]["apiKey"] = data["opsgenie_api_key"] |
| 595 |
elif data["type"] == "pagerduty": |
| 596 |
pagerduty_channel_payload(data, payload) |
| 597 |
elif data["type"] == "prometheus": |
| 598 |
prometheus_channel_payload(data, payload) |
| 599 |
elif data["type"] == "pushover": |
| 600 |
pushover_channel_payload(data, payload) |
| 601 |
elif data["type"] == "sensu": |
| 602 |
sensu_channel_payload(data, payload) |
| 603 |
elif data["type"] == "slack": |
| 604 |
slack_channel_payload(data, payload) |
| 605 |
elif data["type"] == "telegram": |
| 606 |
payload["settings"]["bottoken"] = data["telegram_bot_token"] |
| 607 |
payload["settings"]["chatid"] = data["telegram_chat_id"] |
| 608 |
elif data["type"] == "threema": |
| 609 |
payload["settings"]["gateway_id"] = data["threema_gateway_id"] |
| 610 |
payload["settings"]["recipient_id"] = data["threema_recipient_id"] |
| 611 |
payload["settings"]["api_secret"] = data["threema_api_secret"] |
| 612 |
elif data["type"] == "victorops": |
| 613 |
payload["settings"]["url"] = data["victorops_url"] |
| 614 |
if data.get("victorops_auto_resolve"): |
| 615 |
payload["settings"]["autoResolve"] = data["victorops_auto_resolve"] |
| 616 |
elif data["type"] == "webhook": |
| 617 |
webhook_channel_payload(data, payload) |
| 618 |
return payload |
| 619 |
|
| 620 |
|
| 621 |
class GrafanaNotificationChannelInterface(object): |
| 622 |
def __init__(self, module): |
| 623 |
self._module = module |
| 624 |
# {{{ Authentication header |
| 625 |
self.headers = {"Content-Type": "application/json"} |
| 626 |
if module.params.get("grafana_api_key", None): |
| 627 |
self.headers["Authorization"] = ( |
| 628 |
"Bearer %s" % module.params["grafana_api_key"] |
| 629 |
) |
| 630 |
else: |
| 631 |
self.headers["Authorization"] = basic_auth_header( |
| 632 |
module.params["url_username"], module.params["url_password"] |
| 633 |
) |
| 634 |
# }}} |
| 635 |
self.grafana_url = clean_url(module.params.get("url")) |
| 636 |
if module.params.get("skip_version_check") is False: |
| 637 |
try: |
| 638 |
grafana_version = self.get_version() |
| 639 |
except GrafanaAPIException as e: |
| 640 |
self._module.fail_json(failed=True, msg=to_text(e)) |
| 641 |
if grafana_version["major"] >= 11: |
| 642 |
self._module.fail_json( |
| 643 |
failed=True, |
| 644 |
msg="Legacy Alerting API is available up to Grafana v10", |
| 645 |
) |
| 646 |
|
| 647 |
def get_version(self): |
| 648 |
r, info = fetch_url( |
| 649 |
self._module, |
| 650 |
"%s/api/health" % self.grafana_url, |
| 651 |
headers=self.headers, |
| 652 |
method="GET", |
| 653 |
) |
| 654 |
if info["status"] == 200: |
| 655 |
version = json.loads(to_text(r.read())).get("version") |
| 656 |
if version is not None: |
| 657 |
return parse_grafana_version(version) |
| 658 |
else: |
| 659 |
raise GrafanaAPIException("Failed to retrieve version: %s" % info) |
| 660 |
|
| 661 |
def grafana_switch_organisation(self, grafana_url, org_id): |
| 662 |
r, info = fetch_url( |
| 663 |
self._module, |
| 664 |
"%s/api/user/using/%s" % (grafana_url, org_id), |
| 665 |
headers=self.headers, |
| 666 |
method="POST", |
| 667 |
) |
| 668 |
if info["status"] != 200: |
| 669 |
raise GrafanaAPIException( |
| 670 |
"Unable to switch to organization %s : %s" % (org_id, info) |
| 671 |
) |
| 672 |
|
| 673 |
def grafana_create_notification_channel(self, data, payload): |
| 674 |
r, info = fetch_url( |
| 675 |
self._module, |
| 676 |
"%s/api/alert-notifications" % data["url"], |
| 677 |
data=json.dumps(payload), |
| 678 |
headers=self.headers, |
| 679 |
method="POST", |
| 680 |
) |
| 681 |
if info["status"] == 200: |
| 682 |
return { |
| 683 |
"state": "present", |
| 684 |
"changed": True, |
| 685 |
"channel": json.loads(to_text(r.read())), |
| 686 |
} |
| 687 |
else: |
| 688 |
raise GrafanaAPIException( |
| 689 |
"Unable to create notification channel: %s" % info |
| 690 |
) |
| 691 |
|
| 692 |
def grafana_update_notification_channel(self, data, payload, before): |
| 693 |
r, info = fetch_url( |
| 694 |
self._module, |
| 695 |
"%s/api/alert-notifications/uid/%s" % (data["url"], data["uid"]), |
| 696 |
data=json.dumps(payload), |
| 697 |
headers=self.headers, |
| 698 |
method="PUT", |
| 699 |
) |
| 700 |
if info["status"] == 200: |
| 701 |
del before["created"] |
| 702 |
del before["updated"] |
| 703 |
|
| 704 |
channel = json.loads(to_text(r.read())) |
| 705 |
after = channel.copy() |
| 706 |
del after["created"] |
| 707 |
del after["updated"] |
| 708 |
|
| 709 |
if before == after: |
| 710 |
return { |
| 711 |
"changed": False, |
| 712 |
"channel": channel, |
| 713 |
} |
| 714 |
else: |
| 715 |
return { |
| 716 |
"changed": True, |
| 717 |
"diff": { |
| 718 |
"before": before, |
| 719 |
"after": after, |
| 720 |
}, |
| 721 |
"channel": channel, |
| 722 |
} |
| 723 |
else: |
| 724 |
raise GrafanaAPIException( |
| 725 |
"Unable to update notification channel %s : %s" % (data["uid"], info) |
| 726 |
) |
| 727 |
|
| 728 |
def grafana_create_or_update_notification_channel(self, data): |
| 729 |
payload = grafana_notification_channel_payload(data) |
| 730 |
r, info = fetch_url( |
| 731 |
self._module, |
| 732 |
"%s/api/alert-notifications/uid/%s" % (data["url"], data["uid"]), |
| 733 |
headers=self.headers, |
| 734 |
) |
| 735 |
if info["status"] == 200: |
| 736 |
before = json.loads(to_text(r.read())) |
| 737 |
return self.grafana_update_notification_channel(data, payload, before) |
| 738 |
elif info["status"] == 404: |
| 739 |
return self.grafana_create_notification_channel(data, payload) |
| 740 |
else: |
| 741 |
raise GrafanaAPIException( |
| 742 |
"Unable to get notification channel %s : %s" % (data["uid"], info) |
| 743 |
) |
| 744 |
|
| 745 |
def grafana_delete_notification_channel(self, data): |
| 746 |
r, info = fetch_url( |
| 747 |
self._module, |
| 748 |
"%s/api/alert-notifications/uid/%s" % (data["url"], data["uid"]), |
| 749 |
headers=self.headers, |
| 750 |
method="DELETE", |
| 751 |
) |
| 752 |
if info["status"] == 200: |
| 753 |
return {"state": "absent", "changed": True} |
| 754 |
elif info["status"] == 404: |
| 755 |
return {"changed": False} |
| 756 |
else: |
| 757 |
raise GrafanaAPIException( |
| 758 |
"Unable to delete notification channel %s : %s" % (data["uid"], info) |
| 759 |
) |
| 760 |
|
| 761 |
|
| 762 |
def main(): |
| 763 |
argument_spec = grafana_argument_spec() |
| 764 |
argument_spec.update( |
| 765 |
org_id=dict(type="int", default=1), |
| 766 |
uid=dict(type="str"), |
| 767 |
name=dict(type="str"), |
| 768 |
type=dict( |
| 769 |
type="str", |
| 770 |
choices=[ |
| 771 |
"dingding", |
| 772 |
"discord", |
| 773 |
"email", |
| 774 |
"googlechat", |
| 775 |
"hipchat", |
| 776 |
"kafka", |
| 777 |
"line", |
| 778 |
"teams", |
| 779 |
"opsgenie", |
| 780 |
"pagerduty", |
| 781 |
"prometheus", |
| 782 |
"pushover", |
| 783 |
"sensu", |
| 784 |
"slack", |
| 785 |
"telegram", |
| 786 |
"threema", |
| 787 |
"victorops", |
| 788 |
"webhook", |
| 789 |
], |
| 790 |
), |
| 791 |
is_default=dict(type="bool", default=False), |
| 792 |
include_image=dict(type="bool", default=False), |
| 793 |
disable_resolve_message=dict(type="bool", default=False), |
| 794 |
skip_version_check=dict(type="bool", default=False), |
| 795 |
reminder_frequency=dict(type="str"), |
| 796 |
dingding_url=dict(type="str"), |
| 797 |
dingding_message_type=dict( |
| 798 |
type="list", elements="str", choices=["link", "action_card"] |
| 799 |
), |
| 800 |
discord_url=dict(type="str"), |
| 801 |
discord_message_content=dict(type="str"), |
| 802 |
email_addresses=dict(type="list", elements="str"), |
| 803 |
email_single=dict(type="bool"), |
| 804 |
googlechat_url=dict(type="str"), |
| 805 |
hipchat_url=dict(type="str"), |
| 806 |
hipchat_api_key=dict(type="str", no_log=True), |
| 807 |
hipchat_room_id=dict(type="str"), |
| 808 |
kafka_url=dict(type="str"), |
| 809 |
kafka_topic=dict(type="str"), |
| 810 |
line_token=dict(type="str", no_log=True), |
| 811 |
teams_url=dict(type="str"), |
| 812 |
opsgenie_url=dict(type="str"), |
| 813 |
opsgenie_api_key=dict(type="str", no_log=True), |
| 814 |
opsgenie_auto_close=dict(type="bool"), |
| 815 |
opsgenie_override_priority=dict(type="bool"), |
| 816 |
pagerduty_integration_key=dict(type="str", no_log=True), |
| 817 |
pagerduty_severity=dict( |
| 818 |
type="list", |
| 819 |
elements="str", |
| 820 |
choices=["critical", "error", "warning", "info"], |
| 821 |
), |
| 822 |
pagerduty_auto_resolve=dict(type="bool"), |
| 823 |
pagerduty_message_in_details=dict(type="bool"), |
| 824 |
prometheus_url=dict(type="str"), |
| 825 |
prometheus_username=dict(type="str"), |
| 826 |
prometheus_password=dict(type="str", no_log=True), |
| 827 |
pushover_api_token=dict(type="str", no_log=True), |
| 828 |
pushover_user_key=dict(type="str", no_log=True), |
| 829 |
pushover_devices=dict(type="list", elements="str"), |
| 830 |
pushover_priority=dict( |
| 831 |
type="list", |
| 832 |
elements="str", |
| 833 |
choices=["emergency", "high", "normal", "low", "lowest"], |
| 834 |
), |
| 835 |
pushover_retry=dict(type="int"), # TODO: only when priority==emergency |
| 836 |
pushover_expire=dict(type="int"), # TODO: only when priority==emergency |
| 837 |
pushover_alert_sound=dict(type="str"), # TODO: add sound choices |
| 838 |
pushover_ok_sound=dict(type="str"), # TODO: add sound choices |
| 839 |
sensu_url=dict(type="str"), |
| 840 |
sensu_source=dict(type="str"), |
| 841 |
sensu_handler=dict(type="str"), |
| 842 |
sensu_username=dict(type="str"), |
| 843 |
sensu_password=dict(type="str", no_log=True), |
| 844 |
slack_url=dict(type="str", no_log=True), |
| 845 |
slack_recipient=dict(type="str"), |
| 846 |
slack_username=dict(type="str"), |
| 847 |
slack_icon_emoji=dict(type="str"), |
| 848 |
slack_icon_url=dict(type="str"), |
| 849 |
slack_mention_users=dict(type="list", elements="str"), |
| 850 |
slack_mention_groups=dict(type="list", elements="str"), |
| 851 |
slack_mention_channel=dict( |
| 852 |
type="list", elements="str", choices=["here", "channel"] |
| 853 |
), |
| 854 |
slack_token=dict(type="str", no_log=True), |
| 855 |
telegram_bot_token=dict(type="str", no_log=True), |
| 856 |
telegram_chat_id=dict(type="str"), |
| 857 |
threema_gateway_id=dict(type="str"), |
| 858 |
threema_recipient_id=dict(type="str"), |
| 859 |
threema_api_secret=dict(type="str", no_log=True), |
| 860 |
victorops_url=dict(type="str"), |
| 861 |
victorops_auto_resolve=dict(type="bool"), |
| 862 |
webhook_url=dict(type="str"), |
| 863 |
webhook_username=dict(type="str"), |
| 864 |
webhook_password=dict(type="str", no_log=True), |
| 865 |
webhook_http_method=dict(type="list", elements="str", choices=["POST", "PUT"]), |
| 866 |
) |
| 867 |
|
| 868 |
module = AnsibleModule( |
| 869 |
argument_spec=argument_spec, |
| 870 |
supports_check_mode=False, |
| 871 |
required_together=[ |
| 872 |
["url_username", "url_password", "org_id"], |
| 873 |
["prometheus_username", "prometheus_password"], |
| 874 |
["sensu_username", "sensu_password"], |
| 875 |
], |
| 876 |
mutually_exclusive=[["url_username", "grafana_api_key"]], |
| 877 |
required_if=[ |
| 878 |
["state", "present", ["name", "type"]], |
| 879 |
["type", "dingding", ["dingding_url"]], |
| 880 |
["type", "discord", ["discord_url"]], |
| 881 |
["type", "email", ["email_addresses"]], |
| 882 |
["type", "googlechat", ["googlechat_url"]], |
| 883 |
["type", "hipchat", ["hipchat_url"]], |
| 884 |
["type", "kafka", ["kafka_url", "kafka_topic"]], |
| 885 |
["type", "line", ["line_token"]], |
| 886 |
["type", "teams", ["teams_url"]], |
| 887 |
["type", "opsgenie", ["opsgenie_url", "opsgenie_api_key"]], |
| 888 |
["type", "pagerduty", ["pagerduty_integration_key"]], |
| 889 |
["type", "prometheus", ["prometheus_url"]], |
| 890 |
["type", "pushover", ["pushover_api_token", "pushover_user_key"]], |
| 891 |
["type", "sensu", ["sensu_url"]], |
| 892 |
["type", "slack", ["slack_url"]], |
| 893 |
["type", "telegram", ["telegram_bot_token", "telegram_chat_id"]], |
| 894 |
[ |
| 895 |
"type", |
| 896 |
"threema", |
| 897 |
["threema_gateway_id", "threema_recipient_id", "threema_api_secret"], |
| 898 |
], |
| 899 |
["type", "victorops", ["victorops_url"]], |
| 900 |
["type", "webhook", ["webhook_url"]], |
| 901 |
], |
| 902 |
) |
| 903 |
|
| 904 |
module.params["url"] = clean_url(module.params["url"]) |
| 905 |
alert_channel_iface = GrafanaNotificationChannelInterface(module) |
| 906 |
|
| 907 |
if module.params["state"] == "present": |
| 908 |
result = alert_channel_iface.grafana_create_or_update_notification_channel( |
| 909 |
module.params |
| 910 |
) |
| 911 |
module.exit_json(failed=False, **result) |
| 912 |
else: |
| 913 |
result = alert_channel_iface.grafana_delete_notification_channel(module.params) |
| 914 |
module.exit_json(failed=False, **result) |
| 915 |
|
| 916 |
|
| 917 |
if __name__ == "__main__": |
| 918 |
main() |