From 8c6c7d5b00da007da37f8b44b4aa90d2f2468400 Mon Sep 17 00:00:00 2001 From: Nicolas Roman Date: Thu, 28 Mar 2019 15:46:40 +0100 Subject: [PATCH 1/4] added hook_api --- evomaintenance.cf | 2 ++ evomaintenance.sh | 85 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/evomaintenance.cf b/evomaintenance.cf index f388238..f61d49a 100644 --- a/evomaintenance.cf +++ b/evomaintenance.cf @@ -13,3 +13,5 @@ FULLFROM="John Doe " URGENCYFROM=mama.doe@example.com URGENCYTEL="06.00.00.00.00" REALM=example.com +API_ENDPOINT=https://example.com/api/ +API_KEY=secretkey diff --git a/evomaintenance.sh b/evomaintenance.sh index 723c964..d99c536 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -38,6 +38,8 @@ Options --no-mail disable the mail hook --db enable the database hook (default) --no-db disable the database hook + --api enable the API hook (default) + --no-api disable the API hook --commit enable the commit hook (default) --no-commit disable the commit hook --evocheck enable evocheck execution (default) @@ -149,8 +151,8 @@ get_evocheck() { print_log() { printf "*********** %s ***************\n" "$(get_now)" print_session_data - printf "Hooks : commit=%s db=%s mail=%s\n"\ - "${HOOK_COMMIT}" "${HOOK_DB}" "${HOOK_MAIL}" + printf "Hooks : commit=%s db=%s api=%s mail=%s\n"\ + "${HOOK_COMMIT}" "${HOOK_DB}" "${HOOK_API}" "${HOOK_MAIL}" if [ "${HOOK_MAIL}" = "1" ]; then printf "Mailto : %s\n" "${EVOMAINTMAIL}" fi @@ -220,6 +222,28 @@ hook_db() { fi } +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 "\n***********************************\n" + fi + + if [ "${DRY_RUN}" != "1" ] && [ -x "${CURL_BIN}" ]; then + curl -s -X POST \ + "${API_ENDPOINT}" -k \ + -F api_key="${API_KEY}" \ + -F action=insertEvoMaintenance \ + -F hostname="${HOSTNAME}" \ + -F userid="${USER}" \ + -F ipaddress="${IP}" \ + -F begin_date="${BEGIN_DATE}" \ + -F end_date='now()' \ + -F details="${MESSAGE}" > /dev/null + fi +} + format_mail() { cat <&1 fi +CURL_BIN=$(command -v curl) +readonly CURL_BIN +if [ -z "${CURL_BIN}" ]; then + echo "No \`curl' command has been found, can't call the API." 2>&1 +fi + +if [ -z "${API_ENDPOINT}" ]; then + echo "No API endpoint specified, can't call the API." 2>&1 +fi + EVOCHECK_BIN="/usr/share/scripts/evocheck.sh" GIT_REPOSITORIES="/etc /etc/bind" @@ -489,6 +533,9 @@ if [ "${INTERACTIVE}" = "1" ] && [ "${AUTO}" = "0" ]; then if [ "${HOOK_DB}" = "1" ]; then printf "* save metadata to the database\n" fi + if [ "${HOOK_API}" = "1" ]; then + printf "* send metadata to the API\n" + fi echo "" answer="" @@ -506,6 +553,7 @@ if [ "${INTERACTIVE}" = "1" ] && [ "${AUTO}" = "0" ]; then HOOK_COMMIT=0 HOOK_MAIL=0 HOOK_DB=0 + HOOK_API=0 AUTO=1 break ;; @@ -622,6 +670,36 @@ if [ "${INTERACTIVE}" = "1" ] && [ "${AUTO}" = "0" ]; then ;; esac done + + # API hook + if [ "${HOOK_API}" = "1" ]; then + y="Y"; n="n" + else + y="y"; n="N" + fi + answer="" + while :; do + printf "> Do you want to send the metadata to the API? [%s] " "${y},${n}" + read -r answer + case $answer in + [Yy] ) + hook_api; + break + ;; + [Nn] ) + break + ;; + "" ) + if [ "${HOOK_API}" = "1" ]; then + hook_api + fi + break + ;; + * ) + echo "answer with a valid choice" + ;; + esac + done fi # Log hook @@ -637,6 +715,9 @@ if [ "${INTERACTIVE}" = "0" ] || [ "${AUTO}" = "1" ]; then if [ "${HOOK_DB}" = "1" ]; then hook_db fi + if [ "${HOOK_API}" = "1" ]; then + hook_api + fi fi exit 0 From 5ad41113adcd04e23d407ffc41a74093f04b161b Mon Sep 17 00:00:00 2001 From: Nicolas Roman Date: Wed, 10 Apr 2019 10:45:21 +0200 Subject: [PATCH 2/4] added return code handler for curl --- README.md | 2 ++ evomaintenance.sh | 11 +++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e46d56b..a492f59 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ Options --no-mail disable the mail hook --db enable the database hook (default) --no-db disable the database hook + --api enable the API hook (default) + --no-api disable the API hook --commit enable the commit hook (default) --no-commit disable the commit hook --evocheck enable evocheck execution (default) diff --git a/evomaintenance.sh b/evomaintenance.sh index d99c536..7b580ff 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -231,7 +231,7 @@ hook_api() { fi if [ "${DRY_RUN}" != "1" ] && [ -x "${CURL_BIN}" ]; then - curl -s -X POST \ + API_RETURN_STATUS=$(curl -s -o /dev/null -X POST \ "${API_ENDPOINT}" -k \ -F api_key="${API_KEY}" \ -F action=insertEvoMaintenance \ @@ -240,7 +240,14 @@ hook_api() { -F ipaddress="${IP}" \ -F begin_date="${BEGIN_DATE}" \ -F end_date='now()' \ - -F details="${MESSAGE}" > /dev/null + -F details="${MESSAGE}" \ + -w '%{http_code}') + + if [ "$API_RETURN_STATUS" -eq 200 ]; then + echo "Evomaintenance successfully sent to the API." + else + printf "Error while sending data to the API (code %s).\n" "${API_RETURN_STATUS}" + fi fi } From fe095a20f73e73bd1970fa3b0404844f70e8e903 Mon Sep 17 00:00:00 2001 From: Nicolas Roman Date: Wed, 10 Apr 2019 14:37:14 +0200 Subject: [PATCH 3/4] set --no-db argument to default --- evomaintenance.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/evomaintenance.sh b/evomaintenance.sh index 7b580ff..6162922 100755 --- a/evomaintenance.sh +++ b/evomaintenance.sh @@ -29,15 +29,15 @@ evomaintenance is a program that helps reporting what you've done on a server Usage: evomaintenance or evomaintenance --message="add new host" - or evomaintenance --no-db --no-mail --no-commit + or evomaintenance --no-api --no-mail --no-commit or echo "add new vhost" | evomaintenance Options -m, --message=MESSAGE set the message from the command line --mail enable the mail hook (default) --no-mail disable the mail hook - --db enable the database hook (default) - --no-db disable the database hook + --db enable the database hook + --no-db disable the database hook (default) --api enable the API hook (default) --no-api disable the API hook --commit enable the commit hook (default) @@ -324,7 +324,7 @@ HOSTNAME=${HOSTNAME:-$(get_fqdn)} EVOMAINTMAIL=${EVOMAINTMAIL:-"evomaintenance-$(echo "${HOSTNAME}" | cut -d- -f1)@${REALM}"} LOGFILE=${LOGFILE:-"/var/log/evomaintenance.log"} HOOK_COMMIT=${HOOK_COMMIT:-"1"} -HOOK_DB=${HOOK_DB:-"1"} +HOOK_DB=${HOOK_DB:-"0"} HOOK_API=${HOOK_API:-"1"} HOOK_MAIL=${HOOK_MAIL:-"1"} DRY_RUN=${DRY_RUN:-"0"} From f7fa476de8ba6455b4c3001aac446c8a2a660be1 Mon Sep 17 00:00:00 2001 From: Nicolas Roman Date: Wed, 10 Apr 2019 14:49:02 +0200 Subject: [PATCH 4/4] 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 }