| 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: splunk |
| 9 |
type: notification |
| 10 |
short_description: Sends task result events to Splunk HTTP Event Collector |
| 11 |
author: "Stuart Hirst (!UNKNOWN) <[email protected]>" |
| 12 |
description: |
| 13 |
- This callback plugin sends task results as JSON formatted events to a Splunk HTTP collector. |
| 14 |
- The companion Splunk Monitoring & Diagnostics App is available here U(https://splunkbase.splunk.com/app/4023/). |
| 15 |
- Credit to "Ryan Currah (@ryancurrah)" for original source upon which this is based. |
| 16 |
requirements: |
| 17 |
- Whitelisting this callback plugin |
| 18 |
- 'Create a HTTP Event Collector in Splunk' |
| 19 |
- 'Define the URL and token in C(ansible.cfg)' |
| 20 |
options: |
| 21 |
url: |
| 22 |
description: URL to the Splunk HTTP collector source. |
| 23 |
type: str |
| 24 |
env: |
| 25 |
- name: SPLUNK_URL |
| 26 |
ini: |
| 27 |
- section: callback_splunk |
| 28 |
key: url |
| 29 |
authtoken: |
| 30 |
description: Token to authenticate the connection to the Splunk HTTP collector. |
| 31 |
type: str |
| 32 |
env: |
| 33 |
- name: SPLUNK_AUTHTOKEN |
| 34 |
ini: |
| 35 |
- section: callback_splunk |
| 36 |
key: authtoken |
| 37 |
validate_certs: |
| 38 |
description: Whether to validate certificates for connections to HEC. It is not recommended to set to V(false) except |
| 39 |
when you are sure that nobody can intercept the connection between this plugin and HEC, as setting it to V(false) allows |
| 40 |
man-in-the-middle attacks! |
| 41 |
env: |
| 42 |
- name: SPLUNK_VALIDATE_CERTS |
| 43 |
ini: |
| 44 |
- section: callback_splunk |
| 45 |
key: validate_certs |
| 46 |
type: bool |
| 47 |
default: true |
| 48 |
version_added: '1.0.0' |
| 49 |
include_milliseconds: |
| 50 |
description: Whether to include milliseconds as part of the generated timestamp field in the event sent to the Splunk |
| 51 |
HTTP collector. |
| 52 |
env: |
| 53 |
- name: SPLUNK_INCLUDE_MILLISECONDS |
| 54 |
ini: |
| 55 |
- section: callback_splunk |
| 56 |
key: include_milliseconds |
| 57 |
type: bool |
| 58 |
default: false |
| 59 |
version_added: 2.0.0 |
| 60 |
batch: |
| 61 |
description: |
| 62 |
- Correlation ID which can be set across multiple playbook executions. |
| 63 |
env: |
| 64 |
- name: SPLUNK_BATCH |
| 65 |
ini: |
| 66 |
- section: callback_splunk |
| 67 |
key: batch |
| 68 |
type: str |
| 69 |
version_added: 3.3.0 |
| 70 |
""" |
| 71 |
|
| 72 |
EXAMPLES = r""" |
| 73 |
examples: >- |
| 74 |
To enable, add this to your ansible.cfg file in the defaults block |
| 75 |
[defaults] |
| 76 |
callback_whitelist = community.general.splunk |
| 77 |
Set the environment variable |
| 78 |
export SPLUNK_URL=http://mysplunkinstance.datapaas.io:8088/services/collector/event |
| 79 |
export SPLUNK_AUTHTOKEN=f23blad6-5965-4537-bf69-5b5a545blabla88 |
| 80 |
Set the ansible.cfg variable in the callback_splunk block |
| 81 |
[callback_splunk] |
| 82 |
url = http://mysplunkinstance.datapaas.io:8088/services/collector/event |
| 83 |
authtoken = f23blad6-5965-4537-bf69-5b5a545blabla88 |
| 84 |
""" |
| 85 |
|
| 86 |
import getpass |
| 87 |
import json |
| 88 |
import socket |
| 89 |
import uuid |
| 90 |
from os.path import basename |
| 91 |
|
| 92 |
from ansible.module_utils.ansible_release import __version__ as ansible_version |
| 93 |
from ansible.module_utils.urls import open_url |
| 94 |
from ansible.parsing.ajson import AnsibleJSONEncoder |
| 95 |
from ansible.plugins.callback import CallbackBase |
| 96 |
|
| 97 |
from ansible_collections.community.general.plugins.module_utils._datetime import ( |
| 98 |
now, |
| 99 |
) |
| 100 |
|
| 101 |
|
| 102 |
class SplunkHTTPCollectorSource: |
| 103 |
def __init__(self): |
| 104 |
self.ansible_check_mode = False |
| 105 |
self.ansible_playbook = "" |
| 106 |
self.session = str(uuid.uuid4()) |
| 107 |
self.host = socket.gethostname() |
| 108 |
self.ip_address = socket.gethostbyname(socket.gethostname()) |
| 109 |
self.user = getpass.getuser() |
| 110 |
|
| 111 |
def send_event(self, url, authtoken, validate_certs, include_milliseconds, batch, state, result, runtime): |
| 112 |
if result._task_fields["args"].get("_ansible_check_mode") is True: |
| 113 |
self.ansible_check_mode = True |
| 114 |
|
| 115 |
if result._task._role: |
| 116 |
ansible_role = str(result._task._role) |
| 117 |
else: |
| 118 |
ansible_role = None |
| 119 |
|
| 120 |
if "args" in result._task_fields: |
| 121 |
del result._task_fields["args"] |
| 122 |
|
| 123 |
data = {} |
| 124 |
data["uuid"] = result._task._uuid |
| 125 |
data["session"] = self.session |
| 126 |
if batch is not None: |
| 127 |
data["batch"] = batch |
| 128 |
data["status"] = state |
| 129 |
|
| 130 |
if include_milliseconds: |
| 131 |
time_format = "%Y-%m-%d %H:%M:%S.%f +0000" |
| 132 |
else: |
| 133 |
time_format = "%Y-%m-%d %H:%M:%S +0000" |
| 134 |
|
| 135 |
data["timestamp"] = now().strftime(time_format) |
| 136 |
data["host"] = self.host |
| 137 |
data["ip_address"] = self.ip_address |
| 138 |
data["user"] = self.user |
| 139 |
data["runtime"] = runtime |
| 140 |
data["ansible_version"] = ansible_version |
| 141 |
data["ansible_check_mode"] = self.ansible_check_mode |
| 142 |
data["ansible_host"] = result._host.name |
| 143 |
data["ansible_playbook"] = self.ansible_playbook |
| 144 |
data["ansible_role"] = ansible_role |
| 145 |
data["ansible_task"] = result._task_fields |
| 146 |
data["ansible_result"] = result._result |
| 147 |
|
| 148 |
# This wraps the json payload in and outer json event needed by Splunk |
| 149 |
jsondata = json.dumps({"event": data}, cls=AnsibleJSONEncoder, sort_keys=True) |
| 150 |
|
| 151 |
open_url( |
| 152 |
url, |
| 153 |
jsondata, |
| 154 |
headers={"Content-type": "application/json", "Authorization": f"Splunk {authtoken}"}, |
| 155 |
method="POST", |
| 156 |
validate_certs=validate_certs, |
| 157 |
) |
| 158 |
|
| 159 |
|
| 160 |
class CallbackModule(CallbackBase): |
| 161 |
CALLBACK_VERSION = 2.0 |
| 162 |
CALLBACK_TYPE = "notification" |
| 163 |
CALLBACK_NAME = "community.general.splunk" |
| 164 |
CALLBACK_NEEDS_WHITELIST = True |
| 165 |
|
| 166 |
def __init__(self, display=None): |
| 167 |
super().__init__(display=display) |
| 168 |
self.start_datetimes = {} # Collect task start times |
| 169 |
self.url = None |
| 170 |
self.authtoken = None |
| 171 |
self.validate_certs = None |
| 172 |
self.include_milliseconds = None |
| 173 |
self.batch = None |
| 174 |
self.splunk = SplunkHTTPCollectorSource() |
| 175 |
|
| 176 |
def _runtime(self, result): |
| 177 |
return (now() - self.start_datetimes[result._task._uuid]).total_seconds() |
| 178 |
|
| 179 |
def set_options(self, task_keys=None, var_options=None, direct=None): |
| 180 |
super().set_options(task_keys=task_keys, var_options=var_options, direct=direct) |
| 181 |
|
| 182 |
self.url = self.get_option("url") |
| 183 |
|
| 184 |
if self.url is None: |
| 185 |
self.disabled = True |
| 186 |
self._display.warning( |
| 187 |
"Splunk HTTP collector source URL was " |
| 188 |
"not provided. The Splunk HTTP collector " |
| 189 |
"source URL can be provided using the " |
| 190 |
"`SPLUNK_URL` environment variable or " |
| 191 |
"in the ansible.cfg file." |
| 192 |
) |
| 193 |
|
| 194 |
self.authtoken = self.get_option("authtoken") |
| 195 |
|
| 196 |
if self.authtoken is None: |
| 197 |
self.disabled = True |
| 198 |
self._display.warning( |
| 199 |
"Splunk HTTP collector requires an authentication" |
| 200 |
"token. The Splunk HTTP collector " |
| 201 |
"authentication token can be provided using the " |
| 202 |
"`SPLUNK_AUTHTOKEN` environment variable or " |
| 203 |
"in the ansible.cfg file." |
| 204 |
) |
| 205 |
|
| 206 |
self.validate_certs = self.get_option("validate_certs") |
| 207 |
|
| 208 |
self.include_milliseconds = self.get_option("include_milliseconds") |
| 209 |
|
| 210 |
self.batch = self.get_option("batch") |
| 211 |
|
| 212 |
def v2_playbook_on_start(self, playbook): |
| 213 |
self.splunk.ansible_playbook = basename(playbook._file_name) |
| 214 |
|
| 215 |
def v2_playbook_on_task_start(self, task, is_conditional): |
| 216 |
self.start_datetimes[task._uuid] = now() |
| 217 |
|
| 218 |
def v2_playbook_on_handler_task_start(self, task): |
| 219 |
self.start_datetimes[task._uuid] = now() |
| 220 |
|
| 221 |
def v2_runner_on_ok(self, result, **kwargs): |
| 222 |
self.splunk.send_event( |
| 223 |
self.url, |
| 224 |
self.authtoken, |
| 225 |
self.validate_certs, |
| 226 |
self.include_milliseconds, |
| 227 |
self.batch, |
| 228 |
"OK", |
| 229 |
result, |
| 230 |
self._runtime(result), |
| 231 |
) |
| 232 |
|
| 233 |
def v2_runner_on_skipped(self, result, **kwargs): |
| 234 |
self.splunk.send_event( |
| 235 |
self.url, |
| 236 |
self.authtoken, |
| 237 |
self.validate_certs, |
| 238 |
self.include_milliseconds, |
| 239 |
self.batch, |
| 240 |
"SKIPPED", |
| 241 |
result, |
| 242 |
self._runtime(result), |
| 243 |
) |
| 244 |
|
| 245 |
def v2_runner_on_failed(self, result, **kwargs): |
| 246 |
self.splunk.send_event( |
| 247 |
self.url, |
| 248 |
self.authtoken, |
| 249 |
self.validate_certs, |
| 250 |
self.include_milliseconds, |
| 251 |
self.batch, |
| 252 |
"FAILED", |
| 253 |
result, |
| 254 |
self._runtime(result), |
| 255 |
) |
| 256 |
|
| 257 |
def v2_runner_on_async_failed(self, result, **kwargs): |
| 258 |
self.splunk.send_event( |
| 259 |
self.url, |
| 260 |
self.authtoken, |
| 261 |
self.validate_certs, |
| 262 |
self.include_milliseconds, |
| 263 |
self.batch, |
| 264 |
"FAILED", |
| 265 |
result, |
| 266 |
self._runtime(result), |
| 267 |
) |
| 268 |
|
| 269 |
def v2_runner_on_unreachable(self, result, **kwargs): |
| 270 |
self.splunk.send_event( |
| 271 |
self.url, |
| 272 |
self.authtoken, |
| 273 |
self.validate_certs, |
| 274 |
self.include_milliseconds, |
| 275 |
self.batch, |
| 276 |
"UNREACHABLE", |
| 277 |
result, |
| 278 |
self._runtime(result), |
| 279 |
) |