forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimize-costs.sh
More file actions
executable file
·76 lines (67 loc) · 3.05 KB
/
Copy pathoptimize-costs.sh
File metadata and controls
executable file
·76 lines (67 loc) · 3.05 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
#!/usr/bin/env bash
# Automated cost optimization script
# Detects idle resources, generates right-sizing recommendations, and produces cost reports
set -euo pipefail
REPORT_DIR="${REPORT_DIR:-infrastructure/reports/cost}"
mkdir -p "${REPORT_DIR}"
TIMESTAMP="$(date -u +%Y%m%dT%H%M%SZ)"
REPORT_FILE="${REPORT_DIR}/cost-report-${TIMESTAMP}.json"
log() { echo "[$(date -u +%H:%M:%S)] $*"; }
# ── Idle resource detection ──────────────────────────────────────────────────
detect_idle_resources() {
log "Detecting idle resources..."
bash "$(dirname "$0")/find-idle-resources.sh" 2>/dev/null || true
}
# ── Right-sizing recommendations ─────────────────────────────────────────────
rightsizing_recommendations() {
log "Generating right-sizing recommendations..."
# Check k8s resource requests vs actual usage (requires kubectl + metrics-server)
if command -v kubectl &>/dev/null; then
kubectl top pods --all-namespaces --no-headers 2>/dev/null \
| awk '{print $1,$2,$3,$4}' \
| sort -k3 -rh \
| head -20 || true
else
log "kubectl not available; skipping live right-sizing check"
fi
}
# ── Reserved instance analysis ───────────────────────────────────────────────
reserved_instance_analysis() {
log "Analysing reserved instance opportunities..."
if command -v aws &>/dev/null; then
aws ce get-reservation-coverage \
--time-period Start="$(date -u -d '30 days ago' +%Y-%m-%d 2>/dev/null || date -u -v-30d +%Y-%m-%d)",End="$(date -u +%Y-%m-%d)" \
--granularity MONTHLY 2>/dev/null || log "AWS CE not accessible; skipping RI analysis"
else
log "AWS CLI not available; skipping RI analysis"
fi
}
# ── Spot instance automation ─────────────────────────────────────────────────
spot_instance_check() {
log "Checking spot instance eligibility..."
if command -v kubectl &>/dev/null; then
kubectl get nodes -o json 2>/dev/null \
| grep -o '"node.kubernetes.io/instance-type":"[^"]*"' \
| sort | uniq -c || true
fi
}
# ── Cost report generation ───────────────────────────────────────────────────
generate_report() {
log "Writing cost report to ${REPORT_FILE}..."
jq -n \
--arg ts "${TIMESTAMP}" \
--arg env "${ENVIRONMENT:-unknown}" \
'{
generated_at: $ts,
environment: $env,
sections: ["idle_resources","rightsizing","reserved_instances","spot_instances"],
status: "completed"
}' > "${REPORT_FILE}"
log "Report saved: ${REPORT_FILE}"
}
detect_idle_resources
rightsizing_recommendations
reserved_instance_analysis
spot_instance_check
generate_report
log "Cost optimisation analysis complete."