-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_quota.sh
More file actions
executable file
·120 lines (99 loc) · 3.21 KB
/
check_quota.sh
File metadata and controls
executable file
·120 lines (99 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
#
# API Cockpit - Unified Quota Check Script
# Queries all configured API providers and reports quota status
#
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LIB_DIR="${SCRIPT_DIR}/lib"
CONFIG_DIR="${SCRIPT_DIR}/config"
LOG_DIR="${SCRIPT_DIR}/logs"
# Load environment variables
if [ -f "${CONFIG_DIR}/.env" ]; then
source "${CONFIG_DIR}/.env"
else
echo "Error: ${CONFIG_DIR}/.env not found. Copy .env.example and configure it."
exit 1
fi
# Ensure log directory exists
mkdir -p "${LOG_DIR}"
# Timestamp for logging
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
LOG_FILE="${LOG_DIR}/quota_check_$(date '+%Y%m%d').log"
# Function to log messages
log() {
echo "[${TIMESTAMP}] $*" | tee -a "${LOG_FILE}"
}
# Function to send Telegram alert
send_telegram_alert() {
local message="$1"
if [ -n "${TELEGRAM_BOT_TOKEN:-}" ] && [ -n "${TELEGRAM_CHAT_ID:-}" ]; then
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-d "chat_id=${TELEGRAM_CHAT_ID}" \
-d "text=${message}" \
-d "parse_mode=Markdown" > /dev/null 2>&1
fi
}
# Function to check quota for a provider
check_provider() {
local provider="$1"
local script="${LIB_DIR}/${provider}.py"
if [ ! -f "${script}" ]; then
log "Warning: ${provider} adapter not found"
return 1
fi
log "Checking ${provider}..."
local result
result=$(python3 "${script}" quota 2>&1)
if [ $? -eq 0 ]; then
echo "${result}"
# Parse quota percentage
local percentage
percentage=$(echo "${result}" | jq -r '.quota.percentage // 0')
# Check thresholds
if (( $(echo "${percentage} >= ${QUOTA_CRITICAL_THRESHOLD:-95}" | bc -l) )); then
local alert="🚨 *CRITICAL*: ${provider} quota at ${percentage}%"
log "${alert}"
send_telegram_alert "${alert}"
elif (( $(echo "${percentage} >= ${QUOTA_WARNING_THRESHOLD:-80}" | bc -l) )); then
local alert="⚠️ *WARNING*: ${provider} quota at ${percentage}%"
log "${alert}"
send_telegram_alert "${alert}"
else
log "${provider} quota OK: ${percentage}%"
fi
else
log "Error checking ${provider}: ${result}"
send_telegram_alert "❌ *ERROR*: Failed to check ${provider} quota"
fi
}
# Main execution
main() {
log "=== Starting quota check ==="
local providers=("antigravity" "codex" "copilot" "windsurf")
local results=()
for provider in "${providers[@]}"; do
result=$(check_provider "${provider}")
if [ -n "${result}" ]; then
results+=("${result}")
fi
done
# Generate summary
log "=== Quota check complete ==="
# Output JSON summary
if [ ${#results[@]} -gt 0 ]; then
echo "{"
echo " \"timestamp\": \"$(date -Iseconds)\","
echo " \"providers\": ["
for i in "${!results[@]}"; do
echo "${results[$i]}"
if [ $i -lt $((${#results[@]} - 1)) ]; then
echo ","
fi
done
echo " ]"
echo "}"
fi
}
# Run main function
main "$@"