Skip to content

Instantly share code, notes, and snippets.

@mmichaa
Last active May 10, 2023 08:24
Show Gist options
  • Save mmichaa/2aec9cafe3913dd21ed0d82ea64e8e95 to your computer and use it in GitHub Desktop.
Save mmichaa/2aec9cafe3913dd21ed0d82ea64e8e95 to your computer and use it in GitHub Desktop.
heartbeat.sh check for Icinga2
#!/bin/bash
# SEE: https://stackoverflow.com/a/29754866/888294
# More safety, by turning some bugs into errors.
# Without `errexit` you don’t need ! and can replace
# ${PIPESTATUS[0]} with a simple $?, but I prefer safety.
set -o errexit -o pipefail -o noclobber -o nounset
# -allow a command to fail with !’s side effect on errexit
# -use return value from ${PIPESTATUS[0]}, because ! hosed $?
! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
echo 'I’m sorry, `getopt --test` failed in this environment.'
exit 1
fi
LONGOPTS=subdomain:,name:,warning:,critical:
OPTIONS=s:n:w:c:
# -regarding ! and PIPESTATUS see above
# -temporarily store output to be able to check for errors
# -activate quoting/enhanced mode (e.g. by writing out “--options”)
# -pass arguments only via -- "$@" to separate them correctly
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
# e.g. return value is 1
# then getopt has complained about wrong arguments to stdout
exit 2
fi
# read getopt’s output this way to handle the quoting right:
eval set -- "$PARSED"
# handle non-options
if [[ $# -eq 1 ]]; then
echo "$0: -s|--subdomain [SUBDOMAIN] -n|--name [NAME] -w|--warning [WARNING] -c|--critical [CRITICAL]"
exit 4
fi
subdomain="example" name="name" warning="5m" critical="15m"
# now enjoy the options in order and nicely split until we see --
while true; do
case "$1" in
-s|--subdomain)
subdomain="$2"
shift 2
;;
-n|--name)
name="$2"
shift 2
;;
-w|--warning)
warning="$2"
shift 2
;;
-c|--critical)
critical="$2"
shift 2
;;
--)
shift
break
;;
*)
echo "Programming error"
exit 3
;;
esac
done
# handle non-option arguments
if [[ $# -ne 0 ]]; then
echo "$0: -s|--subdomain [SUBDOMAIN] -n|--name [NAME] -w|--warning [WARNING] -c|--critical [CRITICAL]"
exit 4
fi
# echo "subdomain: $subdomain, name: $name, warning: $warning, critical: $critical"
source /opt/heartbeat-sh/setup.sh "$subdomain"
! STATUS=$(/opt/heartbeat-sh/status.sh "$name" --warning "$warning" --error "$critical")
# echo "status: $STATUS"
case "$STATUS" in
OK)
echo "OK - subdomain=$subdomain name=$name"
exit 0
;;
WARNING)
echo "WARNING - subdomain=$subdomain name=$name"
exit 1
;;
ERROR)
echo "CRITICAL - subdomain=$subdomain name=$name"
exit 2
;;
UNKNOWN)
echo "UNKNOWN - subdomain=$subdomain name=$name"
exit 3
;;
esac
object CheckCommand "heartbeat" {
command = [ PluginDir + "/check_heartbeat.sh" ]
arguments += {
"--warning" = {
value = "$heartbeat_warning$"
}
"--critical" = {
value = "$heartbeat_critical$"
}
"--subdomain" = {
value = "$heartbeat_subdomain$"
}
"--name" = {
value = "$heartbeat_name$"
}
}
vars.heartbeat_warning = "5m"
vars.heartbeat_critical = "15m"
vars.heartbeat_subdomain = null
vars.heartbeat_name = null
}
apply Service "heartbeat" {
import "generic-service"
check_interval = 1m
retry_interval = 30s
max_check_attempts = 3
check_command = "heartbeat"
assign where host.vars.agent_type != "ssh" && host.vars.heartbeat_subdomain && host.vars.heartbeat_name
}
#!/bin/bash
# SEE: https://github.com/heartbeat-sh/heartbeat.sh
if [[ -z $HAAS_SUBDOMAIN ]]
then
echo "I don't know what subdomain to ask for your heartbeat status Please run:"
echo "source setup.sh your-subdomain"
exit 1
fi
name="$1"
final_status='UNKNOWN'
URL="https://$HAAS_SUBDOMAIN.heartbeat.sh/heartbeats/txt"
awk_script='{ print $2 }'
if [[ -n "$name" ]]
then
awk_script="{ if ( match(\$1, \"$name\") ) { print \$2 } }"
fi
for status in $(curl -s "$URL" | awk "$awk_script");
do
if [[ "$final_status" == 'UNKNOWN' ]]
then
final_status="$status"
fi
if [[ "$status" == 'WARNING' && "$final_status" == 'OK' ]]
then
final_status='WARNING'
fi
if [[ "$status" == 'ERROR' ]]
then
final_status='ERROR'
fi
done
echo "$final_status"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment