From 046fb79008af80a1877736d25f40e32421d9cc6d Mon Sep 17 00:00:00 2001 From: Guillaume BOURGEOIS Date: Sun, 12 Jul 2026 15:08:51 +0200 Subject: [PATCH] feat(datadog): new Datadog observability integration Datadog API v1/v2, 6 commands: search logs, list/get monitors, submit event, list events. API-key + Application-key auth, stdlib-only. Co-Authored-By: Claude Opus 4.8 (1M context) --- integrations/datadog/manifest.yaml | 88 +++++++++++++++++++ integrations/datadog/scripts/get_monitor.py | 57 ++++++++++++ integrations/datadog/scripts/list_events.py | 58 ++++++++++++ integrations/datadog/scripts/list_monitors.py | 53 +++++++++++ integrations/datadog/scripts/search_logs.py | 66 ++++++++++++++ integrations/datadog/scripts/submit_event.py | 68 ++++++++++++++ .../datadog/scripts/test_connection.py | 52 +++++++++++ 7 files changed, 442 insertions(+) create mode 100644 integrations/datadog/manifest.yaml create mode 100644 integrations/datadog/scripts/get_monitor.py create mode 100644 integrations/datadog/scripts/list_events.py create mode 100644 integrations/datadog/scripts/list_monitors.py create mode 100644 integrations/datadog/scripts/search_logs.py create mode 100644 integrations/datadog/scripts/submit_event.py create mode 100644 integrations/datadog/scripts/test_connection.py diff --git a/integrations/datadog/manifest.yaml b/integrations/datadog/manifest.yaml new file mode 100644 index 0000000..f840b05 --- /dev/null +++ b/integrations/datadog/manifest.yaml @@ -0,0 +1,88 @@ +id: datadog +name: Datadog +version: 1.0.0 +description: "Datadog (API v1/v2) — observability and monitoring: search logs, list and read monitors, submit and list events. API-key + Application-key authentication; stdlib-only, no extra Python dependencies." +changelog: "1.0.0 — Initial release: search logs, list/get monitors, submit event, list events." +category: siem + +# Per-instance configuration. Auth uses the 'DD-API-KEY' and 'DD-APPLICATION-KEY' +# headers. site selects the API host (e.g. datadoghq.com, datadoghq.eu, us5.datadoghq.com). +config_schema: + properties: + api_key: + type: string + description: "Datadog API key" + x-soar-sensitive: true + app_key: + type: string + description: "Datadog Application key" + x-soar-sensitive: true + site: + type: string + description: "Datadog site (default datadoghq.com)" + default: "datadoghq.com" + required: + - api_key + - app_key + +commands: + - id: search_logs + name: datadog-search-logs + description: "Search logs." + risk: read + inputs_schema: + properties: + query: { type: string, description: "Log search query (e.g. 'service:web status:error')" } + from_time: { type: string, description: "From time (e.g. now-1h)" } + to_time: { type: string, description: "To time (e.g. now)" } + limit: { type: number, description: "Max logs (default 50)" } + required: [query] + outputs_schema: { properties: {} } + - id: list_monitors + name: datadog-list-monitors + description: "List monitors." + risk: read + inputs_schema: + properties: + tag: { type: string, description: "Optional monitor tag filter" } + required: [] + outputs_schema: { properties: {} } + - id: get_monitor + name: datadog-get-monitor + description: "Get a single monitor by ID." + risk: read + inputs_schema: + properties: + monitor_id: { type: string, description: "Monitor ID" } + required: [monitor_id] + outputs_schema: { properties: {} } + - id: submit_event + name: datadog-submit-event + description: "Post an event to the Datadog event stream." + inputs_schema: + properties: + title: { type: string, description: "Event title" } + text: { type: string, description: "Event body" } + tags: { type: string, description: "Comma-separated tags" } + alert_type: { type: string, description: "info, warning, error, or success (default info)" } + required: [title, text] + outputs_schema: { properties: {} } + - id: list_events + name: datadog-list-events + description: "List events in a time window." + risk: read + inputs_schema: + properties: + start: { type: number, description: "Start POSIX timestamp (seconds)" } + end: { type: number, description: "End POSIX timestamp (seconds)" } + required: [start, end] + outputs_schema: { properties: {} } + + - id: test_connection + name: datadog-test-connection + description: "Verify the API/app keys (used by the Test button)." + risk: read + inputs_schema: + properties: {} + required: [] + outputs_schema: { properties: {} } diff --git a/integrations/datadog/scripts/get_monitor.py b/integrations/datadog/scripts/get_monitor.py new file mode 100644 index 0000000..39c7199 --- /dev/null +++ b/integrations/datadog/scripts/get_monitor.py @@ -0,0 +1,57 @@ +import json, os, sys, urllib.parse, urllib.request, urllib.error + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def _base(cfg): + return "https://api." + str(cfg.get("site") or "datadoghq.com") + + +def request(method, path, cfg, body=None, params=None): + url = _base(cfg) + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "DD-API-KEY": str(cfg.get("api_key", "")), + "DD-APPLICATION-KEY": str(cfg.get("app_key", "")), + "Accept": "application/json", + } + if data is not None: + headers["Content-Type"] = "application/json" + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +q = lambda v: urllib.parse.quote(str(v), safe="") + + +def main(cfg, inputs): + monitor_id = inputs.get("monitor_id") + if not monitor_id: + raise Exception("monitor_id is required") + return request("GET", "/api/v1/monitor/" + q(monitor_id), cfg) + + +_run(main) diff --git a/integrations/datadog/scripts/list_events.py b/integrations/datadog/scripts/list_events.py new file mode 100644 index 0000000..5a1e812 --- /dev/null +++ b/integrations/datadog/scripts/list_events.py @@ -0,0 +1,58 @@ +import json, os, sys, urllib.parse, urllib.request, urllib.error + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def _base(cfg): + return "https://api." + str(cfg.get("site") or "datadoghq.com") + + +def request(method, path, cfg, body=None, params=None): + url = _base(cfg) + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "DD-API-KEY": str(cfg.get("api_key", "")), + "DD-APPLICATION-KEY": str(cfg.get("app_key", "")), + "Accept": "application/json", + } + if data is not None: + headers["Content-Type"] = "application/json" + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +def main(cfg, inputs): + start = inputs.get("start") + if start in (None, ""): + raise Exception("start is required") + end = inputs.get("end") + if end in (None, ""): + raise Exception("end is required") + params = {"start": int(start), "end": int(end)} + return request("GET", "/api/v1/events", cfg, params=params) + + +_run(main) diff --git a/integrations/datadog/scripts/list_monitors.py b/integrations/datadog/scripts/list_monitors.py new file mode 100644 index 0000000..fe928f1 --- /dev/null +++ b/integrations/datadog/scripts/list_monitors.py @@ -0,0 +1,53 @@ +import json, os, sys, urllib.parse, urllib.request, urllib.error + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def _base(cfg): + return "https://api." + str(cfg.get("site") or "datadoghq.com") + + +def request(method, path, cfg, body=None, params=None): + url = _base(cfg) + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "DD-API-KEY": str(cfg.get("api_key", "")), + "DD-APPLICATION-KEY": str(cfg.get("app_key", "")), + "Accept": "application/json", + } + if data is not None: + headers["Content-Type"] = "application/json" + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +def main(cfg, inputs): + tag = inputs.get("tag") + params = {"monitor_tags": tag} + return request("GET", "/api/v1/monitor", cfg, params=params) + + +_run(main) diff --git a/integrations/datadog/scripts/search_logs.py b/integrations/datadog/scripts/search_logs.py new file mode 100644 index 0000000..99ff359 --- /dev/null +++ b/integrations/datadog/scripts/search_logs.py @@ -0,0 +1,66 @@ +import json, os, sys, urllib.parse, urllib.request, urllib.error + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def _base(cfg): + return "https://api." + str(cfg.get("site") or "datadoghq.com") + + +def request(method, path, cfg, body=None, params=None): + url = _base(cfg) + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "DD-API-KEY": str(cfg.get("api_key", "")), + "DD-APPLICATION-KEY": str(cfg.get("app_key", "")), + "Accept": "application/json", + } + if data is not None: + headers["Content-Type"] = "application/json" + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +def main(cfg, inputs): + query = inputs.get("query") + if not query: + raise Exception("query is required") + from_time = inputs.get("from_time") or "now-1h" + to_time = inputs.get("to_time") or "now" + limit = inputs.get("limit") + body = { + "filter": { + "query": query, + "from": from_time, + "to": to_time, + }, + "page": {"limit": int(limit or 50)}, + "sort": "-timestamp", + } + return request("POST", "/api/v2/logs/events/search", cfg, body=body) + + +_run(main) diff --git a/integrations/datadog/scripts/submit_event.py b/integrations/datadog/scripts/submit_event.py new file mode 100644 index 0000000..043170b --- /dev/null +++ b/integrations/datadog/scripts/submit_event.py @@ -0,0 +1,68 @@ +import json, os, sys, urllib.parse, urllib.request, urllib.error + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def _base(cfg): + return "https://api." + str(cfg.get("site") or "datadoghq.com") + + +def request(method, path, cfg, body=None, params=None): + url = _base(cfg) + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "DD-API-KEY": str(cfg.get("api_key", "")), + "DD-APPLICATION-KEY": str(cfg.get("app_key", "")), + "Accept": "application/json", + } + if data is not None: + headers["Content-Type"] = "application/json" + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +def main(cfg, inputs): + title = inputs.get("title") + if not title: + raise Exception("title is required") + text = inputs.get("text") + if not text: + raise Exception("text is required") + alert_type = inputs.get("alert_type") + body = { + "title": title, + "text": text, + "alert_type": (alert_type or "info"), + } + tags_raw = inputs.get("tags") + if tags_raw: + tags_list = [s.strip() for s in str(tags_raw).split(",") if s.strip()] + if tags_list: + body["tags"] = tags_list + return request("POST", "/api/v1/events", cfg, body=body) + + +_run(main) diff --git a/integrations/datadog/scripts/test_connection.py b/integrations/datadog/scripts/test_connection.py new file mode 100644 index 0000000..67c85da --- /dev/null +++ b/integrations/datadog/scripts/test_connection.py @@ -0,0 +1,52 @@ +import json, os, sys, urllib.parse, urllib.request, urllib.error + + +def _cfg(): + return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}")) + + +def _inputs(): + return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}")) + + +def _base(cfg): + return "https://api." + str(cfg.get("site") or "datadoghq.com") + + +def request(method, path, cfg, body=None, params=None): + url = _base(cfg) + path + if params: + clean = {k: v for k, v in params.items() if v not in (None, "")} + if clean: + url += "?" + urllib.parse.urlencode(clean) + data = json.dumps(body).encode("utf-8") if body is not None else None + headers = { + "DD-API-KEY": str(cfg.get("api_key", "")), + "DD-APPLICATION-KEY": str(cfg.get("app_key", "")), + "Accept": "application/json", + } + if data is not None: + headers["Content-Type"] = "application/json" + req = urllib.request.Request(url, data=data, headers=headers, method=method) + with urllib.request.urlopen(req, timeout=60) as r: + raw = r.read() + return json.loads(raw) if raw else {} + + +def _run(fn): + try: + print(json.dumps(fn(_cfg(), _inputs()))) + except urllib.error.HTTPError as e: + print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")})) + sys.exit(1) + except Exception as e: + print(json.dumps({"error": str(e)})) + sys.exit(1) + + +def main(cfg, inputs): + request("GET", "/api/v1/validate", cfg) + return {"ok": True} + + +_run(main)