-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromeblackbox.sh
More file actions
executable file
·224 lines (190 loc) · 6.72 KB
/
Copy pathpromeblackbox.sh
File metadata and controls
executable file
·224 lines (190 loc) · 6.72 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env bash
set -euo pipefail
BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
COMPOSE_FILE="${BASE_DIR}/docker-compose.yml"
WRITER_ENV_FILE="${WRITER_ENV_FILE:-.env}"
PROMETHEUS_URL="${PROMETHEUS_URL:-http://127.0.0.1:9090}"
ALERTMANAGER_URL="${ALERTMANAGER_URL:-http://127.0.0.1:9093}"
BLACKBOX_URL="${BLACKBOX_URL:-http://127.0.0.1:9115}"
DEFAULT_TARGET="${TARGET:-db-postgres.example.com:5432}"
PROMETHEUS_IMAGE="${PROMETHEUS_IMAGE:-prom/prometheus:v3.13.0}"
cd "$BASE_DIR"
compose() {
WRITER_ENV_FILE="$WRITER_ENV_FILE" docker compose -f "$COMPOSE_FILE" "$@"
}
usage() {
cat <<'EOF'
Usage: ./promeblackbox.sh COMMAND [OPTIONS]
Commands:
config Validate docker compose config
start Start Alertmanager, Prometheus, Blackbox Exporter, and writer
stop Stop project containers only
restart Restart project containers
status Show container status
logs [service] Follow logs. Optional service: alertmanager, prometheus, blackbox-exporter, blackbox-pg-writer
build-writer Build blackbox-pg-writer image
validate Validate shell scripts and Prometheus config/rules
verify Verify live Prometheus-to-PostgreSQL completeness
reload Reload Prometheus config
targets Show active Prometheus targets
probe [host:port] Run one Blackbox TCP probe
query Query probe_success from Prometheus
pg-schema Run PostgreSQL schema and outage-event migrations
outage-events Create/backfill compact downtime events
storage-guards Apply bounded-growth PostgreSQL maintenance settings
normalize-environment Normalize legacy QA/UAT rows to DEV
normalize-5m Normalize existing raw history and KPI to 5-minute buckets
cleanup-unused Drop tables not used by active dashboards
writer-start Recreate/start writer only
writer-stop Stop writer only
writer-restart Restart writer only
writer-logs Follow writer logs
writer-run-once Run writer once
writer-query Query SQL raw row count and time range
Examples:
./promeblackbox.sh validate
./promeblackbox.sh start
./promeblackbox.sh probe db-postgres.example.com:5432
EOF
}
cmd="${1:-help}"
shift || true
case "$cmd" in
config)
compose config --quiet
;;
start)
compose up -d
compose ps
;;
stop)
compose stop alertmanager prometheus blackbox-exporter blackbox-pg-writer
;;
restart)
compose restart alertmanager prometheus blackbox-exporter blackbox-pg-writer
;;
status)
compose ps
;;
logs)
if [[ $# -gt 0 ]]; then
compose logs -f --tail=200 "$1"
else
compose logs -f --tail=200 alertmanager prometheus blackbox-exporter blackbox-pg-writer
fi
;;
build-writer)
DOCKER_BUILDKIT=0 docker build -t blackbox-pg-writer:latest -f Dockerfile.writer .
;;
validate)
bash -n "$0" scripts/*.sh
for dashboard in grafana/*.json; do
jq -e . "$dashboard" >/dev/null
done
compose config --quiet
docker run --rm --entrypoint promtool \
-v "${BASE_DIR}/prometheus:/etc/prometheus:ro" \
"$PROMETHEUS_IMAGE" check config /etc/prometheus/prometheus.yml
docker run --rm --entrypoint promtool \
-v "${BASE_DIR}/prometheus:/etc/prometheus:ro" \
"$PROMETHEUS_IMAGE" check rules /etc/prometheus/alert-rules.yml
compose run --rm --no-deps --entrypoint /bin/amtool \
alertmanager check-config /etc/alertmanager/alertmanager.yml
;;
verify)
compose run --rm --no-deps \
--entrypoint /workspace/scripts/verify-blackbox-pipeline.sh \
blackbox-pg-writer
;;
reload)
curl -sf -X POST "${PROMETHEUS_URL}/-/reload"
curl -sf -X POST "${ALERTMANAGER_URL}/-/reload"
echo "Prometheus and Alertmanager reload requested."
;;
targets)
curl -sf "${PROMETHEUS_URL}/api/v1/targets?state=active"
;;
probe)
target="${1:-$DEFAULT_TARGET}"
curl -sf "${BLACKBOX_URL}/probe?target=${target}&module=tcp_connect" \
| grep -E "^(probe_success|probe_duration_seconds)"
;;
query)
curl -sfG "${PROMETHEUS_URL}/api/v1/query" \
--data-urlencode 'query=probe_success{job="db-port-availability"}'
;;
pg-schema)
compose run --rm --no-deps \
--entrypoint /workspace/scripts/run-psql.sh \
blackbox-pg-writer \
-f /workspace/sql/001_blackbox_pg_schema.sql \
-f /workspace/sql/008_outage_events.sql \
-f /workspace/sql/012_storage_scalability_guards.sql
;;
outage-events)
compose run --rm --no-deps \
--entrypoint /workspace/scripts/run-psql.sh \
blackbox-pg-writer -f /workspace/sql/008_outage_events.sql
;;
storage-guards)
compose run --rm --no-deps \
--entrypoint /workspace/scripts/run-psql.sh \
blackbox-pg-writer -f /workspace/sql/012_storage_scalability_guards.sql
;;
normalize-environment)
compose run --rm --no-deps \
--entrypoint /workspace/scripts/run-psql.sh \
blackbox-pg-writer -f /workspace/sql/004_normalize_environment.sql
;;
normalize-5m)
if compose ps --status running --services | grep -qx 'blackbox-pg-writer'; then
echo "Stop blackbox-pg-writer before normalizing historical data." >&2
exit 1
fi
compose run --rm --no-deps \
--entrypoint /workspace/scripts/run-psql.sh \
blackbox-pg-writer -f /workspace/sql/006_normalize_history_to_five_minutes.sql
;;
cleanup-unused)
if compose ps --status running --services | grep -qx 'blackbox-pg-writer'; then
echo "Stop blackbox-pg-writer before dropping unused KPI tables." >&2
exit 1
fi
compose run --rm --no-deps \
--entrypoint /workspace/scripts/run-psql.sh \
blackbox-pg-writer -f /workspace/sql/005_kpi_only_cleanup.sql
;;
writer-start)
compose up -d --force-recreate blackbox-pg-writer
;;
writer-stop)
compose stop blackbox-pg-writer
;;
writer-restart)
compose restart blackbox-pg-writer
;;
writer-logs)
compose logs -f --tail=200 blackbox-pg-writer
;;
writer-run-once)
compose run --rm --no-deps \
--entrypoint /workspace/scripts/blackbox-pg-writer.sh \
-e BLACKBOX_RUN_ONCE=true \
blackbox-pg-writer
;;
writer-query)
compose run --rm --no-deps \
--entrypoint /workspace/scripts/run-psql.sh \
blackbox-pg-writer -Atc \
"SELECT count(*) AS rows, min(checked_at), max(checked_at) FROM monitoring.db_port_blackbox_probe_results;"
;;
help|-h|--help)
usage
;;
*)
echo "Unknown command: $cmd" >&2
echo >&2
usage >&2
exit 1
;;
esac