Skip to content

Commit

Permalink
Merge pull request #84 from hmrodrigues/hooks
Browse files Browse the repository at this point in the history
Added hook for status changes
  • Loading branch information
Cyclenerd authored Jan 27, 2025
2 parents 3ba4c73 + b983b94 commit fb9536e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config-example
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ MY_STATUS_FOOTER='Powered by <a href="https://github.com/Cyclenerd/static_status
# If something has gone wrong and the file has not been deleted automatically, you can delete it.
MY_STATUS_LOCKFILE="/tmp/STATUS_SH_IS_RUNNING.lock"

# Hook to call when a hostname status changes
# The hook script must be executable and receives three arguments:
# 1. New status, either up, down or degraded
# 2. Command used
# 3. Hostname
MY_HOOK_STATUS=""

# Date format for the web page.
# UTC (`-u`) is the default.
# Example: 2021-12-23 12:34:55 UTC
Expand Down
23 changes: 23 additions & 0 deletions status.sh
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ MY_DATE_TIME=${MY_DATE_TIME:-$(date -u "+%Y-%m-%d %H:%M:%S %Z")}
# Avoid semicolons.
# More details can be found in `man date`.

# Hook to call when a hostname status changes
# The hook script must be executable and receives three arguments:
# 1. New status, either up, down or degraded
# 2. Command used
# 3. Hostname
MY_HOOK_STATUS=${MY_HOOK_STATUS:-""}

################################################################################
#### END Configuration Section
################################################################################
Expand Down Expand Up @@ -220,6 +227,8 @@ debug_variables() {
echo "MY_TIMESTAMP: $MY_TIMESTAMP"
echo "MY_DATE_TIME: $MY_DATE_TIME"
echo "MY_LASTRUN_TIME: $MY_LASTRUN_TIME"
echo
echo "MY_HOOK_STATUS: $MY_HOOK_STATUS"
}

# command_exists() tells if a given command exists.
Expand Down Expand Up @@ -429,6 +438,10 @@ function save_downtime() {
printf " %s" "$MY_PORT"
fi
fi

if [[ -x "${MY_HOOK_STATUS}" ]] && ! grep -E "^${MY_COMMAND};${MY_HOSTNAME}" "${MY_HOSTNAME_STATUS_LASTRUN}" &> /dev/null; then
${MY_HOOK_STATUS} "down" "${MY_COMMAND}" "${MY_HOSTNAME}" &> /dev/null
fi
}

# save_degradetime()
Expand All @@ -440,6 +453,10 @@ function save_degradetime() {
if [[ "$BE_LOUD" = "yes" ]] || [[ "$BE_QUIET" = "no" ]]; then
printf "\\n%-5s %-4s %s" "DEGRADED:" "$MY_COMMAND" "$MY_HOSTNAME"
fi

if [[ -x "${MY_HOOK_STATUS}" ]] && ! grep -E "^${MY_COMMAND};${MY_HOSTNAME}" "${MY_HOSTNAME_STATUS_LASTRUN_DEGRADE}" &>/dev/null; then
${MY_HOOK_STATUS} "degraded" "${MY_COMMAND}" "${MY_HOSTNAME}" &> /dev/null
fi
}

# save_availability()
Expand All @@ -460,6 +477,12 @@ function save_availability() {
printf " %s" "$MY_PORT"
fi
fi

if [[ -x "${MY_HOOK_STATUS}" ]]; then
if grep -E "^${MY_COMMAND};${MY_HOSTNAME}" "${MY_HOSTNAME_STATUS_LASTRUN}" &>/dev/null || grep -E "^${MY_COMMAND};${MY_HOSTNAME}" "${MY_HOSTNAME_STATUS_LASTRUN_DEGRADE}" &>/dev/null; then
${MY_HOOK_STATUS} "up" "${MY_COMMAND}" "${MY_HOSTNAME}" &> /dev/null
fi
fi
}

# save_history()
Expand Down

0 comments on commit fb9536e

Please sign in to comment.