#!/usr/bin/env bash # ============================================================================= # cost-alerts.sh — Nightly check: exits non-zero + prints red warning # if any always-on resources are found. # Designed for cron or quick end-of-day terminal check. # Usage: source .env && bash scripts/cost-alerts.sh # ============================================================================= set -euo pipefail : "${PROJECT_ID:?Set PROJECT_ID}" : "${REGION:?Set REGION}" RED='\033[0;31m' GREEN='\033[0;32m' NC='\033[0m' RISK=0 check() { local LABEL="$1"; local CMD="$2" RESULT=$(eval "$CMD" 2>/dev/null || true) if [[ -n "$RESULT" ]]; then printf "${RED}[!!] %-22s ACTIVE: %s${NC}\n" "$LABEL" "$(echo "$RESULT" | head -3 | tr '\n' ' ')" RISK=1 fi } printf "\n OSVauco Cost Alert Check — $(date '+%Y-%m-%d %H:%M')\n\n" check "Compute VMs" \ "gcloud compute instances list --project=${PROJECT_ID} --format='value(name,status)' | grep RUNNING" check "Cloud SQL" \ "gcloud sql instances list --project=${PROJECT_ID} --format='value(name,state)' | grep RUNNABLE" check "Composer" \ "gcloud composer environments list --project=${PROJECT_ID} --locations=${REGION} --format='value(name)'" check "Memory Bank" \ "gcloud ai reasoning-engines list --project=${PROJECT_ID} --region=${REGION} --format='value(name)'" check "Cloud Run min>0" \ "gcloud run services list --project=${PROJECT_ID} --region=${REGION} --format='value(name)' | xargs -I{} gcloud run services describe {} --project=${PROJECT_ID} --region=${REGION} --format='value(spec.template.metadata.annotations.autoscaling\.knative\.dev/minScale)' 2>/dev/null | grep -v '^$' | grep -v '^0$'" if [[ $RISK -eq 0 ]]; then printf " ${GREEN}[OK] No always-on resources detected. Sleep well.${NC}\n\n" else printf "\n ${RED}Run: bash scripts/cost-audit.sh for full details.${NC}\n\n" exit 1 fi