health_check.sh•3.41 kB
#!/usr/bin/env bash
################################################################################
#
# Script Name: health_check.sh
# ----------------
# Performs health checks on deployed applications or infrastructure components.
#
# @author Nicholas Wilde, 0xb299a622
# @date 03 Sep 2025
# @version 0.1.0
#
################################################################################
set -o errexit
set -o nounset
set -o pipefail
# --- Global Variables ---
CLOUD_PROVIDER=""
RESOURCE_TYPE=""
RESOURCE_NAME=""
CHECK_TYPE=""
ENDPOINT=""
PORT=""
# --- Functions ---
# Function to display script usage
usage() {
echo "Usage: $0 --cloud-provider <provider> --resource-type <type> --resource-name <name> --check-type <type> [--endpoint <url>] [--port <number>]"
echo " --cloud-provider Cloud provider (e.g., aws, azure, gcp)."
echo " --resource-type Type of resource (e.g., ec2-instance, load-balancer, database)."
echo " --resource-name Name of the resource to check."
echo " --check-type Type of health check (e.g., http, tcp, database)."
echo " --endpoint (Optional) URL for HTTP checks."
echo " --port (Optional) Port for TCP checks."
exit 1
}
# Function to parse command-line arguments
parse_args() {
while [[ "$#" -gt 0 ]]; do
case "$1" in
--cloud-provider) CLOUD_PROVIDER="$2"; shift ;;
--resource-type) RESOURCE_TYPE="$2"; shift ;;
--resource-name) RESOURCE_NAME="$2"; shift ;;
--check-type) CHECK_TYPE="$2"; shift ;;
--endpoint) ENDPOINT="$2"; shift ;;
--port) PORT="$2"; shift ;;
-h|--help) usage ;;
*) echo "Unknown parameter passed: $1"; usage ;;
esac
shift
done
# Validate required arguments
if [[ -z "$CLOUD_PROVIDER" || -z "$RESOURCE_TYPE" || -z "$RESOURCE_NAME" || -z "$CHECK_TYPE" ]]; then
echo "Error: Missing required arguments."
usage
fi
# Validate check-type specific arguments
case "$CHECK_TYPE" in
http)
if [[ -z "$ENDPOINT" ]]; then
echo "Error: --endpoint is required for http checks."
usage
fi
;;
tcp)
if [[ -z "$ENDPOINT" || -z "$PORT" ]]; then
echo "Error: --endpoint and --port are required for tcp checks."
usage
fi
;;
database)
# Add validation for database connection parameters if needed
;;
esac
}
# Function to perform HTTP health check
check_http() {
echo "Performing HTTP health check on $ENDPOINT..."
# Example: curl -s -o /dev/null -w "%{http_code}" "$ENDPOINT"
echo "HTTP check complete."
}
# Function to perform TCP health check
check_tcp() {
echo "Performing TCP health check on $ENDPOINT:$PORT..."
# Example: nc -z -w 5 "$ENDPOINT" "$PORT"
echo "TCP check complete."
}
# Function to perform database health check
check_database() {
echo "Performing database health check for $RESOURCE_NAME..."
# Add database specific health check logic here (e.g., psql -c "SELECT 1;")
echo "Database check complete."
}
# --- Main Logic ---
main() {
parse_args "$@"
echo "Starting health check for $RESOURCE_NAME ($RESOURCE_TYPE) on $CLOUD_PROVIDER."
case "$CHECK_TYPE" in
http)
check_http
;;
tcp)
check_tcp
;;
database)
check_database
;;
*)
echo "Error: Unsupported check type: $CHECK_TYPE"
usage
;;
esac
echo "Health check script finished."
}
main "$@"