Created
March 1, 2026 20:01
-
-
Save KaiStarkk/25aa34e9f64cd65a223bf58cbd1231c2 to your computer and use it in GitHub Desktop.
Archived monitor-vultr script (removed from variss)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Monitor for orphaned Vultr VPS instances | |
| # Alerts if an instance persists for 2+ consecutive checks (30 min intervals) | |
| # State file tracks instance IDs and their first-seen timestamps | |
| source /root/.matrix/credentials | |
| STATE_FILE="/var/lib/vultr-instance-state" | |
| ALERT_THRESHOLD=2 # Number of consecutive checks before alerting | |
| # Ensure state directory exists | |
| mkdir -p "$(dirname "$STATE_FILE")" | |
| # Get current instances from Vultr API | |
| INSTANCES=$(curl -sfS -H "Authorization: Bearer ${VULTR_API_KEY}" "https://api.vultr.com/v2/instances" 2>/dev/null) | |
| if [ $? -ne 0 ]; then | |
| echo "$(date): Failed to query Vultr API" >> /var/log/vultr-monitor.log | |
| exit 1 | |
| fi | |
| # Extract instance IDs | |
| CURRENT_IDS=$(echo "$INSTANCES" | jq -r ".instances[]?.id // empty" | sort -u) | |
| CURRENT_COUNT=$(echo "$CURRENT_IDS" | grep -c . 2>/dev/null || echo 0) | |
| # Read previous state | |
| declare -A STATE | |
| if [ -f "$STATE_FILE" ]; then | |
| while IFS="=" read -r id count; do | |
| STATE[$id]=$count | |
| done < "$STATE_FILE" | |
| fi | |
| # Process current instances | |
| ALERTS=() | |
| NEW_STATE="" | |
| while IFS= read -r id; do | |
| [ -z "$id" ] && continue | |
| # Get instance details | |
| DETAILS=$(echo "$INSTANCES" | jq -r ".instances[] | select(.id == \"$id\")") | |
| LABEL=$(echo "$DETAILS" | jq -r ".label // \"unnamed\"") | |
| REGION=$(echo "$DETAILS" | jq -r ".region // \"unknown\"") | |
| PLAN=$(echo "$DETAILS" | jq -r ".plan // \"unknown\"") | |
| IP=$(echo "$DETAILS" | jq -r ".main_ip // \"unknown\"") | |
| # Increment count for this instance | |
| PREV_COUNT=${STATE[$id]:-0} | |
| NEW_COUNT=$((PREV_COUNT + 1)) | |
| # Add to new state | |
| NEW_STATE="${NEW_STATE}${id}=${NEW_COUNT}\n" | |
| # Check if we should alert | |
| if [ $NEW_COUNT -ge $ALERT_THRESHOLD ]; then | |
| HOURS=$((NEW_COUNT / 2)) | |
| MINS=$((NEW_COUNT * 30)) | |
| ALERTS+=("⚠️ Vultr instance still running after ${MINS} min: $LABEL ($PLAN in $REGION) - $IP - ID: $id") | |
| fi | |
| done <<< "$CURRENT_IDS" | |
| # Write new state | |
| echo -e "$NEW_STATE" | grep -v "^$" > "$STATE_FILE" | |
| # Send alerts | |
| for alert in "${ALERTS[@]}"; do | |
| /usr/local/bin/matrix-notify "$alert" | |
| echo "$(date): $alert" >> /var/log/vultr-monitor.log | |
| done | |
| # Log summary if instances exist | |
| if [ "$CURRENT_COUNT" -gt 0 ]; then | |
| echo "$(date): $CURRENT_COUNT instance(s) tracked" >> /var/log/vultr-monitor.log | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment