Files
gandalf/scripts/dedup_key_map.py
jaredandClaude Opus 5.5 31207f9533 Thor contract, part 2: ask Thor before host tickets, and send dedup keys
gandalf#66 items 3, 4 and 5.

- thor.py: ThorClient.covering() calls Thor's /api/incidents/covering with
  a 4 s timeout. No config, any error, a timeout or a shadow-only answer
  all mean "not covered" (fail open).
- No metrics from host, planned reboot didn't come back and ping host
  unreachable ask Thor by host, only when every switch port of the host
  is up. A port down is Gandalf's network evidence, so it keeps the ticket.
- No Prometheus data asks with scope=source:prometheus.
- Interface and cluster network tickets never ask: they are network
  evidence, so they are Gandalf's under the contract.
- When Thor names a ticket, Gandalf posts its report there as an
  "Also seen by Gandalf" comment and tracks that ticket for the alert.
- Every ticket sends a dedup_key (ticket_text.dedup_key, scheme in the
  README). scripts/dedup_key_map.py prints the keys for Gandalf's open
  tickets, for Tinker's backfill_dedup_keys.php.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-28 17:45:55 -05:00

40 lines
1.4 KiB
Python

"""Print {ticket_id: dedup_key} for Gandalf's tickets that are still open in Tinker (gandalf#66).
Run in Gandalf's working directory (it reads config.json), then hand the output to Tinker:
sudo -u www-data python3 scripts/dedup_key_map.py > gandalf-keys.json
php scripts/backfill_dedup_keys.php --map=gandalf-keys.json --dry-run # on Tinker
Read only: it changes nothing in Gandalf or Tinker. Run the backfill when the dedup_key
change deploys, so a recurrence matches its open ticket instead of opening a new one.
"""
import json
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import db # noqa: E402
import ticket_text # noqa: E402
from monitor import TicketClient # noqa: E402
def main() -> int:
with open('config.json') as f:
cfg = json.load(f)
open_ids = TicketClient(cfg.get('ticket_api', {})).open_tickets()
if open_ids is None:
print('Could not read the open ticket list from Tinker', file=sys.stderr)
return 1
with db.get_conn() as conn:
with conn.cursor() as cur:
cur.execute('SELECT id, event_type, target_name, target_detail, first_seen, ticket_id '
'FROM network_events WHERE ticket_id IS NOT NULL')
events = cur.fetchall()
print(json.dumps(ticket_text.dedup_key_map(events, open_ids), indent=1, sort_keys=True))
return 0
if __name__ == '__main__':
sys.exit(main())