From f7fa476de8ba6455b4c3001aac446c8a2a660be1 Mon Sep 17 00:00:00 2001 From: Nicolas Roman Date: Wed, 10 Apr 2019 14:49:02 +0200 Subject: [PATCH] changed error handling for curl and the backend --- evomaintenance.sh | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 6162922..40b3662 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -225,13 +225,13 @@ hook_db() { hook_api() { if [ "${VERBOSE}" = "1" ]; then printf "\n********** API call **************\n" - printf "curl -s -X POST %s -F action=insertEvoMaintenance -F hostname=%s -F userid=%s -F ipaddress=%s -F begin_date=%s -F end_date='now()' -F details=%s" \ - "${API_ENDPOINT}" "${HOSTNAME}" "${USER}" "${IP}" "${BEGIN_DATE}" "${MESSAGE}" + printf "curl -f -s -S -X POST [REDACTED] -k -F api_key=[REDACTED] -F action=insertEvoMaintenance -F hostname=%s -F userid=%s -F ipaddress=%s -F begin_date=%s -F end_date='now()' -F details=%s" \ + "${HOSTNAME}" "${USER}" "${IP}" "${BEGIN_DATE}" "${MESSAGE}" printf "\n***********************************\n" fi if [ "${DRY_RUN}" != "1" ] && [ -x "${CURL_BIN}" ]; then - API_RETURN_STATUS=$(curl -s -o /dev/null -X POST \ + API_RETURN_STATUS=$(curl -f -s -S -X POST \ "${API_ENDPOINT}" -k \ -F api_key="${API_KEY}" \ -F action=insertEvoMaintenance \ @@ -240,13 +240,13 @@ hook_api() { -F ipaddress="${IP}" \ -F begin_date="${BEGIN_DATE}" \ -F end_date='now()' \ - -F details="${MESSAGE}" \ - -w '%{http_code}') + -F details="${MESSAGE}") - if [ "$API_RETURN_STATUS" -eq 200 ]; then - echo "Evomaintenance successfully sent to the API." + # either cURL or the API backend can throw an error, otherwise it returns this JSON response + if [ "$API_RETURN_STATUS" = '{"status":"Ok"}' ]; then + echo "API call OK." else - printf "Error while sending data to the API (code %s).\n" "${API_RETURN_STATUS}" + echo "API call FAILED." fi fi }