Operations bot
cheshmhayash bot serve is a long-running daemon. It runs the operations commands on a schedule, posts the results to chat, and lets a person approve a cleanup with one click. It writes every action to an audit log.
The bot does what a person does with consumer scan and consumer apply, with the same safety rules:
- Auto-delete is opt-in. A scan proposes a manifest. Nothing is deleted until a person approves it, unless a schedule has
auto_delete: true. - Every deletion re-checks the consumer against the live cluster just before it deletes it.
- Protected consumers are never deleted, whatever the manifest says.
Configuration
Section titled “Configuration”The bot reads the ops config (--ops-config, default ~/.config/cheshmhayash/ops.yaml) and NATS contexts from ~/.config/nats/context/.
bot: schedules: # kind defaults to consumer-stale: the only kind that makes a manifest # and an approve link. The other kinds are reports. - name: daily cron: "0 3 * * *" context: prod-a peer_context: prod-b min_pending: 10000 min_idle: 24h - name: ghost-peers kind: peer-check # peers offline in every Raft group cron: "*/30 * * * *" context: prod-a - name: unbounded-streams kind: stream-unlimited # streams with no retention limit cron: "0 6 * * *" context: prod-a - name: replication kind: stream-report # streams with fewer than 2 replicas cron: "0 7 * * *" context: prod-a - name: nightly-sweep cron: "0 4 * * *" context: prod-a min_pending: 10000 min_idle: 168h auto_delete: true # delete, do not propose; still re-checks and protects
notify: - mattermost://chat.example.com/hooks/abc-xyz?channel=nats-ops # - slack://hooks.slack.com/services/T.../B.../... # - webhook://hooks.example.com/n8n/abc # JSON: {title, body, manifest_id, link} # - stdout://
store: file:///var/lib/cheshmhayash/manifests audit_log: /var/lib/cheshmhayash/audit.jsonl
http: listen: ":8080" base_url: https://nats-bot.example.com # the public URL in chat links
signing_key: change-me-to-32-random-bytes
# Optional: send the part of each manifest that a team owns to that team. # Owners get visibility only; the approve link goes to the global notify list. owners: - name: orders streams: [orders, orders-dlq] consumer_prefix: [orders-] notify: - mattermost://chat.example.com/hooks/def-uvw?channel=orders-oncall
# Optional: listen in a Mattermost channel (see "Chat transports"). mattermost: enabled: false server: https://chat.example.com token_file: /etc/cheshmhayash/mattermost-token team: platform channel: nats-ops trigger: "!cheshmhayash"Environment variables that start with CHESHMHAYASH_OPS_, with __ between sections, override the file. For example: CHESHMHAYASH_OPS_DEFAULTS__MIN_PENDING=10000.
Schedule kinds
Section titled “Schedule kinds”| Kind | What it does |
|---|---|
consumer-stale (default) |
Scans for stale consumers. Stores a manifest and posts a signed approve link. |
peer-check |
Reports ghost peers. |
stream-unlimited |
Reports streams with no retention limit. |
stream-report |
Reports streams with fewer than two replicas, with a placement-skew summary. |
The report kinds never delete anything: cheshmhayash never deletes a stream or removes a peer.
The approval flow
Section titled “The approval flow”- A
consumer-staleschedule runs, or someone typesscan <ctx>in chat. - The bot stores the manifest and posts a summary with a link:
https://<base_url>/approve/<id>?token=<hmac>. - A person opens the link. A
GETshows a plain-text preview of what would be deleted. - The person confirms. A
POSTre-checks every consumer against the live cluster, applies the protect rules, deletes the rest, and returns a summary.
The token is an HMAC-SHA256 of the manifest ID with signing_key. A leaked link can approve only the one manifest it was made for.
Chat commands
Section titled “Chat commands”Both chat transports share one set of commands:
list list stored manifest IDsshow <id> preview a manifestclusters list the NATS contexts this bot can reachstreams [ctx] list streams (one context, or all)stream <ctx> <name> one stream in detaillast <ctx> <stream> [sub] metadata of the last message on a subjectconsumers <ctx> <stream> all consumers of a stream, unfilteredusage [ctx] total footprint and the largest streamscluster <ctx> connected server, peers and accountscan <ctx> [stream] scan now; replies with a signed approve linkhelp this listChat transports
Section titled “Chat transports”Pick one by whether your chat server can reach the bot.
Push (slash command). The chat server sends a POST to /slash on the bot, so it needs a route to the bot. Set up a /cheshmhayash slash command in Mattermost or Slack with signing_key as its token:
/cheshmhayash list/cheshmhayash scan prod-aPull (WebSocket). The bot opens an outbound WebSocket to Mattermost with a bot-account token. It watches one channel for messages that start with trigger and answers over REST. Use it when the chat server cannot reach the bot:
!cheshmhayash list!cheshmhayash scan prod-aThe bot account must be a member of the team and of the channel. If Mattermost goes down, the listener retries with backoff, and the scheduler keeps running.
HTTP endpoints
Section titled “HTTP endpoints”| Method | Path | Purpose |
|---|---|---|
GET |
/healthz |
{"status":"ok"} for probes. |
GET |
/metrics |
Prometheus metrics: scan duration and candidates, apply outcomes, approval latency, build info. |
GET |
/manifest/{id} |
The stored manifest as YAML. |
POST |
/slash |
The slash-command handler. Needs the token. |
GET |
/approve/{id}?token=... |
A preview of what would be deleted. |
POST |
/approve/{id}?token=... |
Re-checks and applies. Returns a JSON summary. |
Audit log
Section titled “Audit log”Each scan, preview and apply is one JSON line in audit_log. The bot opens the file in append mode and holds no lock, so you can rotate it with logrotate.
On Kubernetes
Section titled “On Kubernetes”Set bot.enabled: true in the Helm chart. It adds a single-replica Deployment with the Recreate strategy, a PVC for the manifest store and audit log, a Service, and optionally a ServiceMonitor, Ingress or HTTPProxy, and NetworkPolicy. NATS contexts come from bot.natsContexts (inline JSON, an existing Secret, or rendered from env at start).