tractatus/scripts/monitoring/monitor-all.sh
TheFlow 0e6be3eaf1 refactor: remove website code and fix critical startup crashes (Phase 8)
CRITICAL FIX: Server would CRASH ON STARTUP (multiple import errors)

REMOVED (2 scripts):
1. scripts/framework-watchdog.js
   - Monitored .claude/session-state.json (OUR Claude Code setup)
   - Monitored .claude/token-checkpoints.json (OUR file structure)
   - Implementers won't have our .claude/ directory

2. scripts/init-db.js
   - Created website collections: blog_posts, media_inquiries, case_submissions
   - Created website collections: resources, moderation_queue, users, citations
   - Created website collections: translations, koha_donations
   - Next steps referenced deleted scripts (npm run seed:admin)

REWRITTEN (2 files):

src/models/index.js (29 lines → 27 lines)
- REMOVED imports: Document, BlogPost, MediaInquiry, CaseSubmission, Resource
- REMOVED imports: ModerationQueue, User (all deleted in Phase 2)
- KEPT imports: AuditLog, DeliberationSession, GovernanceLog, GovernanceRule
- KEPT imports: Precedent, Project, SessionState, VariableValue, VerificationLog
- Result: Only framework models exported

src/server.js (284 lines → 163 lines, 43% reduction)
- REMOVED: Imports to deleted middleware (csrf-protection, response-sanitization)
- REMOVED: Stripe webhook handling (/api/koha/webhook)
- REMOVED: Static file caching (for deleted public/ directory)
- REMOVED: Static file serving (public/ deleted in Phase 6)
- REMOVED: CSRF token endpoint
- REMOVED: Website homepage with "auth, documents, blog, admin" references
- REMOVED: Instruction sync (scripts/sync-instructions-to-db.js reference)
- REMOVED: Hardcoded log path (${process.env.HOME}/var/log/tractatus/...)
- REMOVED: Website-specific security middleware
- KEPT: Security headers, rate limiting, CORS, body parsers
- KEPT: API routes, governance services, MongoDB connections
- RESULT: Clean framework-only server

RESULT: Repository can now start without crashes, all imports resolve

🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-21 22:17:02 +13:00

178 lines
4.2 KiB
Bash
Executable file

#!/bin/bash
#
# Master Monitoring Script
# Orchestrates all monitoring checks for Tractatus production environment
#
# Usage:
# ./monitor-all.sh # Run all monitors
# ./monitor-all.sh --test # Test mode (no alerts)
# ./monitor-all.sh --skip-ssl # Skip SSL check
#
# Exit codes:
# 0 = All checks passed
# 1 = Some warnings
# 2 = Some critical issues
# 3 = Configuration error
set -euo pipefail
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LOG_FILE="/var/log/tractatus/monitoring.log"
ALERT_EMAIL="${ALERT_EMAIL:-}"
# Parse arguments
TEST_MODE=false
SKIP_SSL=false
while [[ $# -gt 0 ]]; do
case $1 in
--test)
TEST_MODE=true
shift
;;
--skip-ssl)
SKIP_SSL=true
shift
;;
*)
echo "Unknown option: $1"
exit 3
;;
esac
done
# Export configuration for child scripts
export ALERT_EMAIL
[[ "$TEST_MODE" == "true" ]] && TEST_FLAG="--test" || TEST_FLAG=""
# Logging function
log() {
local level="$1"
shift
local message="$*"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "[$timestamp] [$level] $message"
if [[ -d "$(dirname "$LOG_FILE")" ]]; then
echo "[$timestamp] [$level] $message" >> "$LOG_FILE"
fi
}
# Run monitoring check
run_check() {
local name="$1"
local script="$2"
shift 2
local args="$@"
log "INFO" "Running $name..."
local exit_code=0
"$SCRIPT_DIR/$script" $args $TEST_FLAG || exit_code=$?
case $exit_code in
0)
log "INFO" "$name: OK ✓"
;;
1)
log "WARN" "$name: Warning"
;;
2)
log "CRITICAL" "$name: Critical"
;;
*)
log "ERROR" "$name: Error (exit code: $exit_code)"
;;
esac
return $exit_code
}
# Main monitoring function
main() {
log "INFO" "=== Starting Tractatus Monitoring Suite ==="
log "INFO" "Timestamp: $(date '+%Y-%m-%d %H:%M:%S %Z')"
log "INFO" "Host: $(hostname)"
[[ "$TEST_MODE" == "true" ]] && log "INFO" "TEST MODE: Alerts suppressed"
local max_severity=0
local checks_run=0
local checks_passed=0
local checks_warned=0
local checks_critical=0
local checks_failed=0
# Health Check
if run_check "Health Check" "health-check.sh"; then
((checks_passed++))
else
local exit_code=$?
[[ $exit_code -eq 1 ]] && ((checks_warned++))
[[ $exit_code -eq 2 ]] && ((checks_critical++))
[[ $exit_code -ge 3 ]] && ((checks_failed++))
[[ $exit_code -gt $max_severity ]] && max_severity=$exit_code
fi
((checks_run++))
# Log Monitor
if run_check "Log Monitor" "log-monitor.sh" --since "5 minutes ago"; then
((checks_passed++))
else
local exit_code=$?
[[ $exit_code -eq 1 ]] && ((checks_warned++))
[[ $exit_code -eq 2 ]] && ((checks_critical++))
[[ $exit_code -ge 3 ]] && ((checks_failed++))
[[ $exit_code -gt $max_severity ]] && max_severity=$exit_code
fi
((checks_run++))
# Disk Monitor
if run_check "Disk Monitor" "disk-monitor.sh"; then
((checks_passed++))
else
local exit_code=$?
[[ $exit_code -eq 1 ]] && ((checks_warned++))
[[ $exit_code -eq 2 ]] && ((checks_critical++))
[[ $exit_code -ge 3 ]] && ((checks_failed++))
[[ $exit_code -gt $max_severity ]] && max_severity=$exit_code
fi
((checks_run++))
# SSL Monitor (optional)
if [[ "$SKIP_SSL" != "true" ]]; then
if run_check "SSL Monitor" "ssl-monitor.sh"; then
((checks_passed++))
else
local exit_code=$?
[[ $exit_code -eq 1 ]] && ((checks_warned++))
[[ $exit_code -eq 2 ]] && ((checks_critical++))
[[ $exit_code -ge 3 ]] && ((checks_failed++))
[[ $exit_code -gt $max_severity ]] && max_severity=$exit_code
fi
((checks_run++))
fi
# Summary
log "INFO" "=== Monitoring Summary ==="
log "INFO" "Checks run: $checks_run"
log "INFO" "Passed: $checks_passed | Warned: $checks_warned | Critical: $checks_critical | Failed: $checks_failed"
if [[ $max_severity -eq 0 ]]; then
log "INFO" "All monitoring checks passed ✓"
elif [[ $max_severity -eq 1 ]]; then
log "WARN" "Some checks returned warnings"
elif [[ $max_severity -eq 2 ]]; then
log "CRITICAL" "Some checks returned critical alerts"
else
log "ERROR" "Some checks failed"
fi
log "INFO" "=== Monitoring Complete ==="
exit $max_severity
}
# Run main function
main