id: wazuh-agent-checkin-rule-health-gate
namespace: company.team
description: |
Wazuh agents that stop checking in and detection rules that stop firing both
mean the fleet went blind, and neither signal ever gated anything. A keyed
webhook accepts the manager state JSON, a WorkingDirectory materializes it
for a Python grade that scores every agent's check-in lag against a threshold
and every rule's 24 hour fire count against an expectation, then a Loop walks
the graded agents for the execution record. Any dark agent or silent rule
pages Slack, an unreadable state pages Slack through its own branch, and the
verdict, the blind agent names, the worst lag and the silent rules are
published as outputs.
triggers:
- id: post_manager_state
type: io.kestra.plugin.core.trigger.Webhook
description: POST the Wazuh manager state JSON here from the manager or a
reporting sidecar after each check-in cycle.
key: "{{ secret('WAZUH_WEBHOOK_KEY') }}"
- id: hourly_health_sweep
type: io.kestra.plugin.core.trigger.Schedule
description: Fallback net, an hourly fleet health sweep, shipped disabled until
an operator switches it on.
cron: "0 * * * *"
disabled: true
inputs:
- id: manager_state
type: JSON
defaults:
manager: prod-wazuh-1
agents:
- name: win-eng-011
status: active
last_checkin_minutes: 4
rules_fired_24h: 12
- name: linux-db-004
status: active
last_checkin_minutes: 96
rules_fired_24h: 3
- name: k8s-edge-007
status: never_connected
last_checkin_minutes: 420
rules_fired_24h: 0
rules:
- name: SQL Injection Attempt
fired_24h: 6
- name: Multiple Authentication Failures
fired_24h: 9
- name: Local Rootkit Detection
fired_24h: 0
description: Wazuh manager state the grade uses when no webhook body is posted,
shaped as a manager name with an agents array and a rules array.
- id: checkin_threshold_minutes
type: INT
defaults: 180
description: Highest check-in lag in minutes an agent may reach before it counts
as blind.
- id: expected_rule_fires
type: INT
defaults: 1
description: Lowest number of times a detection rule must fire in 24 hours
before it counts as silent.
tasks:
# --- 1. manager snapshot grade ---
- id: checkin_workspace
type: io.kestra.plugin.core.flow.WorkingDirectory
description: One shared directory so the shell step that writes the snapshot and
the Python step that grades it read the same file.
tasks:
- id: write_state_snapshot
type: io.kestra.plugin.scripts.shell.Commands
description: Materialize the posted manager state, or the input default on
manual runs, into the shared workspace with a quoted heredoc and size
it for the log.
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
entryPoint: []
containerImage: python:3.12-slim
commands:
- |
set -u
cat > manager_state.json <<'STATE_EOF'
{{ (trigger.body ?? inputs.manager_state) | toJson }}
STATE_EOF
echo "wrote $(wc -c < manager_state.json) bytes of Wazuh manager state to the shared workspace"
- id: grade_checkins
type: io.kestra.plugin.scripts.python.Script
description: Score every agent's check-in lag and status against the threshold,
collect the rules that fired below the expectation, and emit the fleet
verdict through the outputs protocol.
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
containerImage: python:3.12-slim
env:
CHECKIN_THRESHOLD_MINUTES: "{{ inputs.checkin_threshold_minutes }}"
EXPECTED_RULE_FIRES: "{{ inputs.expected_rule_fires }}"
script: |
import json
import os
STATE_FILE = "manager_state.json"
def env_int(name, default):
try:
return int(os.environ.get(name, default))
except (TypeError, ValueError):
return int(default)
CHECKIN_THRESHOLD_MINUTES = env_int("CHECKIN_THRESHOLD_MINUTES", 180)
EXPECTED_RULE_FIRES = env_int("EXPECTED_RULE_FIRES", 1)
def load_state(path):
try:
if not os.path.exists(path) or os.path.getsize(path) == 0:
return None, "manager state file is missing or empty"
with open(path, "r", encoding="utf-8") as handle:
document = json.load(handle)
document = json.loads(document) if isinstance(document, str) else document
if not isinstance(document, dict):
return None, "manager state is not a JSON object"
return document, ""
except ValueError:
return None, "manager state is not valid JSON"
state, problem = load_state(STATE_FILE)
manager = "unknown-manager"
agent_rows = []
blind_names = []
silent_rules = []
worst_lag = 0
agents_checked = 0
error = ""
verdict = "healthy"
if state is None:
verdict = "error"
error = problem
else:
manager = str(state.get("manager") or "unknown-manager")
entries = [row for row in (state.get("agents") or []) if isinstance(row, dict)]
rules = [row for row in (state.get("rules") or []) if isinstance(row, dict)]
if not entries:
verdict = "error"
error = "manager state contains no agents"
else:
for entry in entries:
name = str(entry.get("name") or "unnamed-agent")
status = str(entry.get("status") or "unknown")
try:
lag = int(entry.get("last_checkin_minutes") or 0)
except (TypeError, ValueError):
lag = 0
try:
fired = int(entry.get("rules_fired_24h") or 0)
except (TypeError, ValueError):
fired = 0
blind = status != "active" or lag > CHECKIN_THRESHOLD_MINUTES
agent_rows.append({
"name": name,
"status": status,
"lag_minutes": lag,
"rules_fired_24h": fired,
"blind": blind,
})
if blind:
blind_names.append(name)
worst_lag = max(worst_lag, lag)
for rule in rules:
rule_name = str(rule.get("name") or "unnamed-rule")
try:
fired = int(rule.get("fired_24h") or 0)
except (TypeError, ValueError):
fired = 0
if fired < EXPECTED_RULE_FIRES:
silent_rules.append(rule_name)
agents_checked = len(agent_rows)
verdict = "blind_spots" if (blind_names or silent_rules) else "healthy"
payload = {
"verdict": verdict,
"manager": manager,
"agents_checked": agents_checked,
"agents": agent_rows,
"blind_count": len(blind_names),
"blind_agents": sorted(blind_names),
"worst_lag_minutes": worst_lag,
"silent_rules": sorted(silent_rules),
"silent_rule_count": len(silent_rules),
"rules_expected": EXPECTED_RULE_FIRES,
"error": error,
}
print("manager=%s checked=%d blind=%d silent=%d verdict=%s" % (
manager, agents_checked, len(blind_names), len(silent_rules), verdict))
print("::" + json.dumps({"outputs": payload}) + "::")
# --- 2. per agent record ---
- id: loop_agents
type: io.kestra.plugin.core.flow.Loop
description: Walk every graded agent so the execution history names each lag and
rule contribution even when the gate passes.
values: "{{ outputs.grade_checkins.vars.agents }}"
concurrencyLimit: 4
runIf: "{{ (outputs.grade_checkins.vars.agents | length) > 0 }}"
tasks:
- id: log_agent_state
type: io.kestra.plugin.core.flow.If
description: Split each agent into the blind branch when it stopped checking in,
and the reporting branch otherwise.
condition: "{{ item.value.blind == true }}"
then:
- id: log_blind_agent
type: io.kestra.plugin.core.log.Log
description: Record the dark agent with its status, lag and rule fire count.
message: "blind agent {{ item.value.name }} status {{ item.value.status }} lag
{{ item.value.lag_minutes }}m rules fired {{
item.value.rules_fired_24h }} in 24h"
else:
- id: log_reporting_agent
type: io.kestra.plugin.core.log.Log
description: Record the reporting agent with its status, lag and rule fire
count.
message: "reporting agent {{ item.value.name }} status {{ item.value.status }}
lag {{ item.value.lag_minutes }}m rules fired {{
item.value.rules_fired_24h }} in 24h"
# --- 3. verdict gate ---
- id: gate_fleet_health
type: io.kestra.plugin.core.flow.If
description: Escalate to Slack when any agent went dark or any detection rule
stopped firing, and verify the error branch instead of trusting a parse
failure.
condition: "{{ outputs.grade_checkins.vars.verdict == 'blind_spots' }}"
then:
- id: alert_blind_spots
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Tell the security channel which agents stopped checking in and
which rules went silent, with the worst lag.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"text": "Wazuh fleet blind spot on {{ outputs.grade_checkins.vars.manager }} - {{ outputs.grade_checkins.vars.blind_count }} agent(s) dark ({{ outputs.grade_checkins.vars.blind_agents | join(', ') }}), {{ outputs.grade_checkins.vars.silent_rule_count }} rule(s) silent ({{ outputs.grade_checkins.vars.silent_rules | join(', ') }}), worst lag {{ outputs.grade_checkins.vars.worst_lag_minutes }}m. Execution {{ execution.id }}."
}
else:
- id: check_state_error
type: io.kestra.plugin.core.flow.If
description: Surface an unreadable manager state as its own branch so a broken
payload never reads as a healthy fleet.
condition: "{{ outputs.grade_checkins.vars.verdict == 'error' }}"
then:
- id: alert_unreadable_state
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Tell the security channel the manager state could not be graded and
name the load problem.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"text": "Wazuh check-in grade unreadable for {{ outputs.grade_checkins.vars.manager }} - {{ outputs.grade_checkins.vars.error }}. Execution {{ execution.id }}."
}
else:
- id: log_fleet_healthy
type: io.kestra.plugin.core.log.Log
description: Record the all clear with the agent count, the worst lag and the
rule expectation so the history doubles as evidence.
message: "wazuh fleet healthy - {{ outputs.grade_checkins.vars.agents_checked }}
agent(s) within {{ inputs.checkin_threshold_minutes }}m of the
manager, worst lag {{
outputs.grade_checkins.vars.worst_lag_minutes }}m, every rule
fired at least {{ inputs.expected_rule_fires }} time(s) in 24h"
errors:
- id: notify_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Escalate any failure to the ops channel with the execution url.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{"text": "FAILED: {{ flow.id }} {{ execution.id }}"}
outputs:
- id: verdict
type: STRING
description: healthy when every agent reports and every rule fires, blind_spots
when either signal is dark, error when the state could not be graded.
value: "{{ outputs.grade_checkins.vars.verdict }}"
- id: manager
type: STRING
description: Wazuh manager name the state was graded against, for example prod-wazuh-1.
value: "{{ outputs.grade_checkins.vars.manager }}"
- id: agents_checked
type: INT
description: Number of agents the grade scored, for example 3.
value: "{{ outputs.grade_checkins.vars.agents_checked }}"
- id: blind_count
type: INT
description: Number of agents that are dark, either never connected or lagging
past the threshold.
value: "{{ outputs.grade_checkins.vars.blind_count }}"
- id: blind_agents
type: ARRAY
description: 'Names of the dark agents, for example ["k8s-edge-007"].'
value: "{{ outputs.grade_checkins.vars.blind_agents | toJson }}"
- id: worst_lag_minutes
type: INT
description: Highest check-in lag seen across the fleet in minutes, for example 420.
value: "{{ outputs.grade_checkins.vars.worst_lag_minutes }}"
- id: silent_rules
type: ARRAY
description: 'Detection rules whose 24 hour fire count sits under the
expectation, for example ["Local Rootkit Detection"].'
value: "{{ outputs.grade_checkins.vars.silent_rules | toJson }}"
- id: error
type: STRING
description: Load problem behind an error verdict, empty when the state graded cleanly.
value: "{{ outputs.grade_checkins.vars.error }}"