- FastAPI app med 7 bundled module-typer (proxmox, esxi, truenas, splunk, linux, windows, http-json) - Multi-instance support: flere instanser af samme modultype med adskilte assets - Community/Enterprise licensing med admin enterprise-toggle - Capacity forecasting, trends, what-if simulator, alert policies - MCP server + LLM copilot integration - Dockerfile + rune.yaml til Yggdrasil
138 lines
5.2 KiB
Python
138 lines
5.2 KiB
Python
"""Splunk collector — storage, license consumption and index sizes."""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Dict
|
|
|
|
from app import sdk
|
|
|
|
|
|
def _session(config: Dict[str, Any]):
|
|
base = sdk.base_url(config, "https", 8089)
|
|
session = sdk.http_session(config)
|
|
token = str(config.get("token", "")).strip()
|
|
if token:
|
|
session.headers["Authorization"] = f"Bearer {token}"
|
|
elif config.get("username") and config.get("password"):
|
|
session.auth = (config.get("username"), config.get("password"))
|
|
else:
|
|
raise sdk.ModuleError("Configure a Splunk bearer token or username/password")
|
|
return base, session
|
|
|
|
|
|
def _entries(payload: Any):
|
|
if isinstance(payload, dict):
|
|
return payload.get("entry") or []
|
|
return []
|
|
|
|
|
|
def collect(ctx: Dict[str, Any]) -> Dict[str, Any]:
|
|
if sdk.use_demo(ctx, ["host"]):
|
|
return {"assets": sdk.demo_assets(ctx), "notes": "Demo snapshot — running without a Splunk endpoint"}
|
|
|
|
config = ctx["config"]
|
|
base, session = _session(config)
|
|
cluster = config.get("cluster_name") or "Splunk"
|
|
metrics: Dict[str, float] = {}
|
|
capacity: Dict[str, float] = {}
|
|
meta: Dict[str, Any] = {}
|
|
notes = []
|
|
|
|
try:
|
|
info = sdk.get_json(session, f"{base}/services/server/info?output_mode=json")
|
|
content = (_entries(info) or [{}])[0].get("content", {})
|
|
hostname = content.get("host") or config.get("host")
|
|
meta["version"] = content.get("version", "")
|
|
meta["server_name"] = hostname
|
|
cores = content.get("numberOfCores") or content.get("numberOfVirtualCores")
|
|
if cores:
|
|
capacity["cpu_cores"] = float(cores)
|
|
memory_mb = content.get("physicalMemoryMB") or content.get("memory")
|
|
if memory_mb:
|
|
capacity["mem_gb"] = round(float(memory_mb) / 1024, 1)
|
|
except Exception as exc:
|
|
raise sdk.ModuleError(f"Splunk server info failed: {exc}")
|
|
hostname = meta.get("server_name") or "splunk"
|
|
|
|
try:
|
|
partitions = sdk.get_json(session, f"{base}/services/server/status/partitions-space?output_mode=json")
|
|
chosen = None
|
|
for entry in _entries(partitions):
|
|
content = entry.get("content", {})
|
|
if str(content.get("mount_point")) in ("/", "/opt", "/opt/splunk"):
|
|
chosen = content
|
|
if content.get("mount_point") == "/":
|
|
break
|
|
if chosen is None:
|
|
entries = _entries(partitions)
|
|
chosen = entries[0].get("content", {}) if entries else None
|
|
if chosen:
|
|
total_mb = float(chosen.get("capacity") or 0)
|
|
free_mb = float(chosen.get("available") or 0)
|
|
if total_mb > 0:
|
|
used_mb = float(chosen.get("used") or (total_mb - free_mb))
|
|
metrics["storage_pct"] = sdk.percent(used_mb, total_mb)
|
|
metrics["storage_used_gb"] = round(used_mb / 1024, 1)
|
|
metrics["storage_total_gb"] = round(total_mb / 1024, 1)
|
|
capacity["storage_gb"] = round(total_mb / 1024, 1)
|
|
meta["mount"] = chosen.get("mount_point")
|
|
except Exception as exc:
|
|
notes.append(f"partition data unavailable ({exc})")
|
|
|
|
try:
|
|
usage = sdk.get_json(session, f"{base}/services/license/usage?output_mode=json")
|
|
used = 0.0
|
|
quota = 0.0
|
|
for entry in _entries(usage):
|
|
content = entry.get("content", {})
|
|
try:
|
|
used += float(content.get("used") or 0)
|
|
quota += float(content.get("quota") or 0)
|
|
except (TypeError, ValueError):
|
|
continue
|
|
if quota > 0:
|
|
metrics["license_pct"] = sdk.percent(used, quota)
|
|
meta["license_quota_gb"] = round(quota / 1024 ** 3, 1)
|
|
except Exception as exc:
|
|
notes.append(f"license data unavailable ({exc})")
|
|
|
|
try:
|
|
indexes = sdk.get_json(session, f"{base}/services/data/indexes?output_mode=json")
|
|
index_mb = 0.0
|
|
for entry in _entries(indexes):
|
|
try:
|
|
index_mb += float(entry.get("content", {}).get("currentDBSizeMB") or 0)
|
|
except (TypeError, ValueError):
|
|
continue
|
|
if index_mb:
|
|
metrics["index_gb"] = round(index_mb / 1024, 1)
|
|
except Exception as exc:
|
|
notes.append(f"index data unavailable ({exc})")
|
|
|
|
asset = sdk.add_asset(
|
|
external_id=hostname,
|
|
name=hostname,
|
|
type_="appliance",
|
|
metrics=metrics,
|
|
capacity=capacity,
|
|
cluster=cluster,
|
|
tags=["splunk"],
|
|
meta=meta,
|
|
)
|
|
note = f"Collected Splunk telemetry from {base}"
|
|
if notes:
|
|
note += " — " + "; ".join(notes)
|
|
return {"assets": [asset], "notes": note}
|
|
|
|
|
|
def test_connection(ctx: Dict[str, Any]) -> Dict[str, Any]:
|
|
if sdk.use_demo(ctx, ["host"]):
|
|
return {"ok": True, "message": "Demo mode — no Splunk endpoint contacted."}
|
|
base, session = _session(ctx["config"])
|
|
info = sdk.get_json(session, f"{base}/services/server/info?output_mode=json")
|
|
content = (_entries(info) or [{}])[0].get("content", {})
|
|
return {
|
|
"ok": True,
|
|
"message": f"Connected to Splunk {content.get('version', '')} at {base}",
|
|
"details": {"version": content.get("version"), "host": content.get("host")},
|
|
}
|