Skip to content

Instantly share code, notes, and snippets.

@dialex
Created August 8, 2019 15:05
Show Gist options
  • Save dialex/756f947cae2d4f49e5c5c728c1479ea7 to your computer and use it in GitHub Desktop.
Save dialex/756f947cae2d4f49e5c5c728c1479ea7 to your computer and use it in GitHub Desktop.
Check health of services through APIs
#!/bin/bash
############
# Functions
############
function is_api_healthy() {
local api_endpoint=${1}
local api_name=${2}
local status_code=$(/usr/bin/curl -sL --max-time 5 -w "%{http_code}\n" "$api_endpoint" -o /dev/null)
if [ "$status_code" -ne "200" ]; then
echo "$api_name ❌ [HTTP $status_code] $api_endpoint"
return 1
else
echo "$api_name ✅ healthy"
return 0
fi
}
#########
# Script
#########
# Define your target APIs
services=(1)
declare -a name=(
[1]="service-alerts-subscription"
# (add new endpoints here)
)
declare -a endpoint=(
[1]="https://service-alerts-subscription.apps.dev.eu-central-1.mercury.ORGANISATION.org/api/health"
# (add new endpoints here)
)
echo "Starting healthcheck... 🌡"
# Check if APIs are healthy
total_failures=0
for service in "${services[@]}"; do
is_api_healthy ${endpoint[$service]} ${name[$service]}
num_failure=$?
total_failures+=$num_failure
done
exit $total_failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment