added hook_api

pull/9/head
Nicolas Roman 4 years ago
parent afab06ee38
commit 8c6c7d5b00
  1. 2
      evomaintenance.cf
  2. 85
      evomaintenance.sh

@ -13,3 +13,5 @@ FULLFROM="John Doe <jdoe@example.com>"
URGENCYFROM=mama.doe@example.com
URGENCYTEL="06.00.00.00.00"
REALM=example.com
API_ENDPOINT=https://example.com/api/
API_KEY=secretkey

@ -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 <<EOTEMPLATE
From: ${FULLFROM}
@ -294,12 +318,14 @@ EVOMAINTMAIL=${EVOMAINTMAIL:-"evomaintenance-$(echo "${HOSTNAME}" | cut -d- -f1)
LOGFILE=${LOGFILE:-"/var/log/evomaintenance.log"}
HOOK_COMMIT=${HOOK_COMMIT:-"1"}
HOOK_DB=${HOOK_DB:-"1"}
HOOK_API=${HOOK_API:-"1"}
HOOK_MAIL=${HOOK_MAIL:-"1"}
DRY_RUN=${DRY_RUN:-"0"}
VERBOSE=${VERBOSE:-"0"}
AUTO=${AUTO:-"0"}
EVOCHECK=${EVOCHECK:-"0"}
GIT_STATUS_MAX_LINES=${GIT_STATUS_MAX_LINES:-20}
API_ENDPOINT=${API_ENDPOINT:-""}
# initialize variables
MESSAGE=""
@ -353,6 +379,14 @@ while :; do
# enable DB hook
HOOK_DB=1
;;
--no-api)
# disable API hook
HOOK_API=0
;;
--api)
# enable API hook
HOOK_API=1
;;
--no-mail)
# disable mail hook
HOOK_MAIL=0
@ -429,6 +463,16 @@ if [ -z "${PSQL_BIN}" ]; then
echo "No \`psql' command has been found, can't save to the database." 2>&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

Loading…
Cancel
Save