harbor run -t openthoughts/tasktrove-exp-rpt-stack-bash-withtests-v2-stack-bash-withtests-9041harbor download openthoughts/tasktrove-exp-rpt-stack-bash-withtests-v2-stack-bash-withtests-9041Implement a health monitoring system for endpoints based on the specifications provided in the verifier script.
Endpoint Health Check: Implement a script named healthcheck.sh that verifies the health of an endpoint passed as a command-line argument. This script should return an exit status:
0 indicates the endpoint is "up" (healthy).1 indicates the endpoint is "down" (unhealthy).Endpoint Management: Implement a script named endpoints.sh that manages the state of the endpoints. It should accept two arguments:
failure-of or repair-of).Endpoint List: Create a file named endpoints.dat that contains a list of endpoints and their current statuses. Each line should have the format <endpoint> <status>, where <endpoint> is the name of the endpoint and <status> is either up or down. Ensure that the initial status of endpoints can be easily set in this file.
The healthcheck.sh script should take one input:
ENDPOINT: A string representing the endpoint to check.The endpoints.sh script should accept two inputs:
ACTION_TYPE: A string that can either be failure-of or repair-of.ENDPOINT: A string representing the endpoint being managed.The endpoints.dat file should initially contain lines representing endpoints and their current states, formatted correctly as specified.
/app/ directory:
/app/healthcheck.sh/app/endpoints.sh/app/endpoints.datEnsure that these scripts are executable and properly handle inputs as described. The solution must conform to the logic inferred from the provided verifier script to ensure it passes successfully.
The following test script will be used to verify your solution:
#!/bin/bash
if [ $# -ne 1 ]
then
echo "Error: Wrong number of arguments."
echo " Usage: ./watchdog.sh ENDPOINT"
exit 1;
fi
ENDPOINT="${1}"
WORKING_DIRECTORY=$(realpath $(dirname "${0}"));
ENDPOINT_LIST="${WORKING_DIRECTORY}/endpoints.dat"
ENDPOINT_MANAGER="${WORKING_DIRECTORY}/endpoints.sh"
HEALTH_CHECK="${WORKING_DIRECTORY}/healthcheck.sh"
log () {
local category="${1}"
local message="${2}";
local timestamp=$(date);
echo "${timestamp} ${category}: ${message}";
}
# Check if the health check script is available
if [ ! -e "${HEALTH_CHECK}" ]
then
log "ERROR" "Could not find '${HEALTH_CHECK}'!"
exit 2;
fi
# Check if the endpoint manager is available
if [ ! -e "${ENDPOINT_MANAGER}" ]
then
log "ERROR" "Could not find the manager script '${ENDPOINT_MANAGER}'";
exit 3;
fi
# Check if the endpoint list is available
if [ ! -e "${ENDPOINT_LIST}" ]
then
log "ERROR" "Could not find the endpoint list '${ENDPOINT_LIST}'";
exit 4;
fi
# Extract the status (up or down) of the the endpoint
previous_status=$(cat "${ENDPOINT_LIST}" \
| grep "${ENDPOINT}" \
| cut -d ' ' -f2)
if [ -z "${previous_status}" ]
then
previous_status='down'
fi
# Invoke the health check
bash "${HEALTH_CHECK}" "${ENDPOINT}"
status=$?;
log "INFO" "Heath check for '${ENDPOINT}' returned '${status}'";
# Compare the previous status with health check result and send the
# appropriate message to the endpoint manager.
if [ $status -ne 0 ]
then
if [ "${previous_status}" = "up" ]
then
log "INFO" "Endpoint '${ENDPOINT}' has failed!"
bash "${ENDPOINT_MANAGER}" failure-of "${ENDPOINT}"
fi
else
if [ "${previous_status}" = "down" ]
then
log "INFO" "Endpoint '${ENDPOINT}' is back!"
bash "${ENDPOINT_MANAGER}" repair-of "${ENDPOINT}"
fi
fi