-
Notifications
You must be signed in to change notification settings - Fork 1
Hypernova/webmon #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7dfbab3
29e38c3
999f041
4f4932f
da82355
b0bb3ab
611eb77
f89c4e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,72 @@ | ||||||||||||
| #!/bin/bash | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| : "${PUSHGATEWAY_URL:=http://localhost:9091}" | ||||||||||||
| : "${HEALTHCHECK_TIMEOUT:=10}" | ||||||||||||
|
|
||||||||||||
| function log { | ||||||||||||
| echo "[$(date '+%F %T')] [healthcheck] $*" | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if [ -z "${WEB_HEALTHCHECK_ENDPOINTS}" ]; then | ||||||||||||
| log "WEB_HEALTHCHECK_ENDPOINTS not set, skipping health checks" | ||||||||||||
| exit 0 | ||||||||||||
| fi | ||||||||||||
|
|
||||||||||||
| IFS=',' read -ra ENDPOINTS <<< "${WEB_HEALTHCHECK_ENDPOINTS}" | ||||||||||||
|
|
||||||||||||
| if [ ${#ENDPOINTS[@]} -eq 0 ]; then | ||||||||||||
| log "No endpoints configured" | ||||||||||||
| exit 0 | ||||||||||||
| fi | ||||||||||||
|
|
||||||||||||
| log "Checking ${#ENDPOINTS[@]} endpoint(s)..." | ||||||||||||
|
|
||||||||||||
| for endpoint in "${ENDPOINTS[@]}"; do | ||||||||||||
| endpoint=$(echo "$endpoint" | xargs) | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty endpoints aren’t skipped after trim. This can call
Suggested change
|
||||||||||||
|
|
||||||||||||
| endpoint_id=$(echo "$endpoint" | sed 's|https\?://||' | sed 's|[^a-zA-Z0-9]|_|g') | ||||||||||||
|
|
||||||||||||
| log "Checking endpoint: $endpoint (id: $endpoint_id)" | ||||||||||||
|
|
||||||||||||
| http_code=$(curl -o /dev/null -s -w "%{http_code}" -m "$HEALTHCHECK_TIMEOUT" "$endpoint") | ||||||||||||
| curl_exit_code=$? | ||||||||||||
|
|
||||||||||||
| if [ $curl_exit_code -eq 0 ] && [ "$http_code" = "200" ]; then | ||||||||||||
| health_status=1 | ||||||||||||
| log "[OK] $endpoint - OK (200)" | ||||||||||||
| else | ||||||||||||
| health_status=0 | ||||||||||||
| if [ $curl_exit_code -ne 0 ]; then | ||||||||||||
| log "[FAIL] $endpoint - FAILED (curl error: $curl_exit_code)" | ||||||||||||
| else | ||||||||||||
| log "[FAIL] $endpoint - FAILED (HTTP $http_code)" | ||||||||||||
| fi | ||||||||||||
| fi | ||||||||||||
|
|
||||||||||||
| metric_name="web_endpoint_health" | ||||||||||||
|
|
||||||||||||
| metrics_payload="# HELP ${metric_name} Health status of web endpoints (1 = healthy, 0 = unhealthy) | ||||||||||||
| # TYPE ${metric_name} gauge | ||||||||||||
| ${metric_name}{endpoint=\"${endpoint}\",endpoint_id=\"${endpoint_id}\",http_code=\"${http_code}\"} ${health_status} | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Label values in - metrics_payload="# HELP ${metric_name} Health status of web endpoints (1 = healthy, 0 = unhealthy)
-# TYPE ${metric_name} gauge
-${metric_name}{endpoint=\"${endpoint}\",endpoint_id=\"${endpoint_id}\",http_code=\"${http_code}\"} ${health_status}
-"
+ # escape label values per Prometheus exposition format
+ escaped_endpoint=$(printf '%s' "$endpoint" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e ':a;N;s/\n/\\n/;ta')
+ escaped_endpoint_id=$(printf '%s' "$endpoint_id" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e ':a;N;s/\n/\\n/;ta')
+ metrics_payload="# HELP ${metric_name} Health status of web endpoints (1 = healthy, 0 = unhealthy)
+# TYPE ${metric_name} gauge
+${metric_name}{endpoint=\"${escaped_endpoint}\",endpoint_id=\"${escaped_endpoint_id}\",http_code=\"${http_code}\"} ${health_status}
+"
|
||||||||||||
| " | ||||||||||||
|
|
||||||||||||
| push_url="${PUSHGATEWAY_URL}/metrics/job/web_healthcheck/instance/${endpoint_id}" | ||||||||||||
|
|
||||||||||||
| push_response=$(curl -s -w "\nHTTP_CODE:%{http_code}" -X POST -H "Content-Type: text/plain" --data-binary "$metrics_payload" "$push_url" 2>&1) | ||||||||||||
| push_exit_code=$? | ||||||||||||
| push_http_code=$(echo "$push_response" | grep "HTTP_CODE:" | cut -d: -f2) | ||||||||||||
|
|
||||||||||||
| if [ $push_exit_code -eq 0 ] && [ "$push_http_code" = "200" ]; then | ||||||||||||
| log "Metrics pushed for $endpoint_id" | ||||||||||||
| else | ||||||||||||
| log "ERROR: Failed to push metrics for $endpoint_id (HTTP $push_http_code, exit code: $push_exit_code)" | ||||||||||||
| error_message=$(echo "$push_response" | grep -v "HTTP_CODE:") | ||||||||||||
| if [ -n "$error_message" ]; then | ||||||||||||
| log "ERROR: $error_message" | ||||||||||||
| fi | ||||||||||||
| fi | ||||||||||||
| done | ||||||||||||
|
|
||||||||||||
| log "Health check cycle complete" | ||||||||||||
|
|
||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HEALTHCHECK_TIMEOUTisn’t validated. If it’s non‑numeric or out of range,curl -mfails and health is misreported. Consider validating it as a positive integer within a sane range and defaulting when invalid.