| 1 |
# Copyright (c) Ansible Project |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
name: sumologic |
| 9 |
type: notification |
| 10 |
short_description: Sends task result events to Sumologic |
| 11 |
author: "Ryan Currah (@ryancurrah)" |
| 12 |
description: |
| 13 |
- This callback plugin sends task results as JSON formatted events to a Sumologic HTTP collector source. |
| 14 |
requirements: |
| 15 |
- Whitelisting this callback plugin |
| 16 |
- 'Create a HTTP collector source in Sumologic and specify a custom timestamp format of V(yyyy-MM-dd HH:mm:ss ZZZZ) and |
| 17 |
a custom timestamp locator of V("timestamp": "(.*\)")' |
| 18 |
options: |
| 19 |
url: |
| 20 |
description: URL to the Sumologic HTTP collector source. |
| 21 |
type: str |
| 22 |
env: |
| 23 |
- name: SUMOLOGIC_URL |
| 24 |
ini: |
| 25 |
- section: callback_sumologic |
| 26 |
key: url |
| 27 |
""" |
| 28 |
|
| 29 |
EXAMPLES = r""" |
| 30 |
examples: |- |
| 31 |
To enable, add this to your ansible.cfg file in the defaults block |
| 32 |
[defaults] |
| 33 |
callback_whitelist = community.general.sumologic |
| 34 |
|
| 35 |
Set the environment variable |
| 36 |
export SUMOLOGIC_URL=https://endpoint1.collection.us2.sumologic.com/receiver/v1/http/R8moSv1d8EW9LAUFZJ6dbxCFxwLH6kfCdcBfddlfxCbLuL-BN5twcTpMk__pYy_cDmp== |
| 37 |
|
| 38 |
Set the ansible.cfg variable in the callback_sumologic block |
| 39 |
[callback_sumologic] |
| 40 |
url = https://endpoint1.collection.us2.sumologic.com/receiver/v1/http/R8moSv1d8EW9LAUFZJ6dbxCFxwLH6kfCdcBfddlfxCbLuL-BN5twcTpMk__pYy_cDmp== |
| 41 |
""" |
| 42 |
|
| 43 |
import getpass |
| 44 |
import json |
| 45 |
import socket |
| 46 |
import uuid |
| 47 |
from os.path import basename |
| 48 |
|
| 49 |
from ansible.module_utils.ansible_release import __version__ as ansible_version |
| 50 |
from ansible.module_utils.urls import open_url |
| 51 |
from ansible.parsing.ajson import AnsibleJSONEncoder |
| 52 |
from ansible.plugins.callback import CallbackBase |
| 53 |
|
| 54 |
from ansible_collections.community.general.plugins.module_utils._datetime import ( |
| 55 |
now, |
| 56 |
) |
| 57 |
|
| 58 |
|
| 59 |
class SumologicHTTPCollectorSource: |
| 60 |
def __init__(self): |
| 61 |
self.ansible_check_mode = False |
| 62 |
self.ansible_playbook = "" |
| 63 |
self.session = str(uuid.uuid4()) |
| 64 |
self.host = socket.gethostname() |
| 65 |
self.ip_address = socket.gethostbyname(socket.gethostname()) |
| 66 |
self.user = getpass.getuser() |
| 67 |
|
| 68 |
def send_event(self, url, state, result, runtime): |
| 69 |
if result._task_fields["args"].get("_ansible_check_mode") is True: |
| 70 |
self.ansible_check_mode = True |
| 71 |
|
| 72 |
if result._task._role: |
| 73 |
ansible_role = str(result._task._role) |
| 74 |
else: |
| 75 |
ansible_role = None |
| 76 |
|
| 77 |
if "args" in result._task_fields: |
| 78 |
del result._task_fields["args"] |
| 79 |
|
| 80 |
data = {} |
| 81 |
data["uuid"] = result._task._uuid |
| 82 |
data["session"] = self.session |
| 83 |
data["status"] = state |
| 84 |
data["timestamp"] = now().strftime("%Y-%m-%d %H:%M:%S +0000") |
| 85 |
data["host"] = self.host |
| 86 |
data["ip_address"] = self.ip_address |
| 87 |
data["user"] = self.user |
| 88 |
data["runtime"] = runtime |
| 89 |
data["ansible_version"] = ansible_version |
| 90 |
data["ansible_check_mode"] = self.ansible_check_mode |
| 91 |
data["ansible_host"] = result._host.name |
| 92 |
data["ansible_playbook"] = self.ansible_playbook |
| 93 |
data["ansible_role"] = ansible_role |
| 94 |
data["ansible_task"] = result._task_fields |
| 95 |
data["ansible_result"] = result._result |
| 96 |
|
| 97 |
open_url( |
| 98 |
url, |
| 99 |
data=json.dumps(data, cls=AnsibleJSONEncoder, sort_keys=True), |
| 100 |
headers={"Content-type": "application/json", "X-Sumo-Host": data["ansible_host"]}, |
| 101 |
method="POST", |
| 102 |
) |
| 103 |
|
| 104 |
|
| 105 |
class CallbackModule(CallbackBase): |
| 106 |
CALLBACK_VERSION = 2.0 |
| 107 |
CALLBACK_TYPE = "notification" |
| 108 |
CALLBACK_NAME = "community.general.sumologic" |
| 109 |
CALLBACK_NEEDS_WHITELIST = True |
| 110 |
|
| 111 |
def __init__(self, display=None): |
| 112 |
super().__init__(display=display) |
| 113 |
self.start_datetimes = {} # Collect task start times |
| 114 |
self.url = None |
| 115 |
self.sumologic = SumologicHTTPCollectorSource() |
| 116 |
|
| 117 |
def _runtime(self, result): |
| 118 |
return (now() - self.start_datetimes[result._task._uuid]).total_seconds() |
| 119 |
|
| 120 |
def set_options(self, task_keys=None, var_options=None, direct=None): |
| 121 |
super().set_options(task_keys=task_keys, var_options=var_options, direct=direct) |
| 122 |
|
| 123 |
self.url = self.get_option("url") |
| 124 |
|
| 125 |
if self.url is None: |
| 126 |
self.disabled = True |
| 127 |
self._display.warning( |
| 128 |
"Sumologic HTTP collector source URL was " |
| 129 |
"not provided. The Sumologic HTTP collector " |
| 130 |
"source URL can be provided using the " |
| 131 |
"`SUMOLOGIC_URL` environment variable or " |
| 132 |
"in the ansible.cfg file." |
| 133 |
) |
| 134 |
|
| 135 |
def v2_playbook_on_start(self, playbook): |
| 136 |
self.sumologic.ansible_playbook = basename(playbook._file_name) |
| 137 |
|
| 138 |
def v2_playbook_on_task_start(self, task, is_conditional): |
| 139 |
self.start_datetimes[task._uuid] = now() |
| 140 |
|
| 141 |
def v2_playbook_on_handler_task_start(self, task): |
| 142 |
self.start_datetimes[task._uuid] = now() |
| 143 |
|
| 144 |
def v2_runner_on_ok(self, result, **kwargs): |
| 145 |
self.sumologic.send_event(self.url, "OK", result, self._runtime(result)) |
| 146 |
|
| 147 |
def v2_runner_on_skipped(self, result, **kwargs): |
| 148 |
self.sumologic.send_event(self.url, "SKIPPED", result, self._runtime(result)) |
| 149 |
|
| 150 |
def v2_runner_on_failed(self, result, **kwargs): |
| 151 |
self.sumologic.send_event(self.url, "FAILED", result, self._runtime(result)) |
| 152 |
|
| 153 |
def runner_on_async_failed(self, result, **kwargs): |
| 154 |
self.sumologic.send_event(self.url, "FAILED", result, self._runtime(result)) |
| 155 |
|
| 156 |
def v2_runner_on_unreachable(self, result, **kwargs): |
| 157 |
self.sumologic.send_event(self.url, "UNREACHABLE", result, self._runtime(result)) |