Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions playbooks/robusta_playbooks/bash_enrichments.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import logging
import logging, subprocess
from typing import List

from robusta.api import BaseBlock, BashParams, MarkdownBlock, NodeEvent, PodEvent, RobustaPod, action

from robusta.api import BaseBlock, BashParams, ExecutionBaseEvent, MarkdownBlock, NodeEvent, PodEvent, RobustaPod, action

@action
def pod_bash_enricher(event: PodEvent, params: BashParams):
Expand Down Expand Up @@ -40,3 +38,39 @@ def node_bash_enricher(event: NodeEvent, params: BashParams):
block_list.append(MarkdownBlock(f"Command results for *{params.bash_command}:*"))
block_list.append(MarkdownBlock(exec_result))
event.add_enrichment(block_list)


@action
def generic_bash_enricher(event: ExecutionBaseEvent, params):
"""
Run any bash command on the runner (can include SSH to remote hosts if desired).
Params:
bash_command: List of command args (recommended), or string for shell execution.
"""
bash_command = params.get("bash_command")
if not bash_command:
event.add_enrichment([MarkdownBlock(":warning: `bash_command` param is required!")])
return

try:
if isinstance(bash_command, str):
result = subprocess.run(bash_command, shell=True, capture_output=True, text=True)
else:
result = subprocess.run(bash_command, capture_output=True, text=True)
output = result.stdout.strip()
error = result.stderr.strip()
status = result.returncode
except Exception as e:
output = ""
error = str(e)
status = 1

message = (
f"**Generic Bash Enricher**\n"
f"Command: `{bash_command}`\n"
f"Return code: `{status}`\n"
f"---\n"
f"**STDOUT:**\n```\n{output}\n```\n"
f"**STDERR:**\n```\n{error}\n```"
)
event.add_enrichment([MarkdownBlock(message)])