Add FREE_send_notification.sh
This commit is contained in:
parent
87eb526eb5
commit
a8e7292561
@ -2,6 +2,7 @@ FROM alpine
|
||||
|
||||
RUN apk add --no-cache \
|
||||
ca-certificates \
|
||||
curl \
|
||||
nagios \
|
||||
nagios-plugins \
|
||||
nagios-plugins-dig \
|
||||
@ -20,6 +21,8 @@ RUN apk add --no-cache \
|
||||
|
||||
USER nagios
|
||||
|
||||
COPY FREE_send_notification.sh /usr/bin/FREE_send_notification.sh
|
||||
|
||||
COPY entrypoint.sh /docker-entrypoint.sh
|
||||
ENTRYPOINT ["/docker-entrypoint.sh"]
|
||||
|
||||
|
171
FREE_send_notification.sh
Executable file
171
FREE_send_notification.sh
Executable file
@ -0,0 +1,171 @@
|
||||
#!/bin/sh
|
||||
|
||||
#
|
||||
# Script d'envoi de notification SMS via l'API Free Mobile
|
||||
# https://github.com/C-Duv/freemobile-smsapi-client
|
||||
#
|
||||
# Auteur: DUVERGIER Claude (http://claude.duvergier.fr)
|
||||
#
|
||||
# Nécessite: sh et wget
|
||||
#
|
||||
# Possible usages: see usage_help()
|
||||
|
||||
set -eu
|
||||
|
||||
readonly PROGNAME=$(basename $0)
|
||||
readonly PROGDIR=$(readlink -m $(dirname $0))
|
||||
|
||||
usage_error () {
|
||||
echo "ERROR: ${1}" >&2
|
||||
echo ""
|
||||
usage_help
|
||||
exit 1
|
||||
}
|
||||
|
||||
usage_help () {
|
||||
echo "Possible usages:"
|
||||
echo "* ${PROGNAME} [options] [message]"
|
||||
echo "* echo \"All your base are belong to us\" | ${PROGNAME} [options]"
|
||||
echo ""
|
||||
echo "Options:"
|
||||
echo "* -c file specify configuration file"
|
||||
echo "* -h display this help"
|
||||
}
|
||||
|
||||
CONFIG_FILE=""
|
||||
while getopts "c:h" option; do
|
||||
case "$option" in
|
||||
c) CONFIG_FILE=${OPTARG} ;;
|
||||
:) usage_error "Invalid arguments" ;;
|
||||
h) usage_help ; exit 0 ;;
|
||||
esac
|
||||
done
|
||||
shift $((OPTIND-1))
|
||||
|
||||
|
||||
##
|
||||
## Configuration système
|
||||
##
|
||||
|
||||
# Caractère de fin de ligne
|
||||
# (http://en.wikipedia.org/wiki/Percent-encoding#Character_data)
|
||||
NEWLINE_CHAR="%0D" # Valeurs possibles : %0A, %0D et %0D%0A
|
||||
|
||||
# URL d'accès à l'API
|
||||
SMSAPI_BASEURL="https://smsapi.free-mobile.fr"
|
||||
|
||||
# Action d'envoi de notification
|
||||
SMSAPI_SEND_ACTION="sendmsg"
|
||||
|
||||
|
||||
##
|
||||
## Configuration utilisateur
|
||||
##
|
||||
|
||||
# Login utilisateur / identifiant Free Mobile (celui utilisé pour accéder à
|
||||
# l'Espace Abonné)
|
||||
USER_LOGIN="$1"
|
||||
shift
|
||||
|
||||
# Clé d'identification (générée et fournie par Free Mobile via l'Espace Abonné,
|
||||
# "Mes Options" : https://mobile.free.fr/moncompte/index.php?page=options)
|
||||
API_KEY="$1"
|
||||
shift
|
||||
|
||||
# Texte qui sera ajouté AVANT chaque message envoyé
|
||||
MESSAGE_HEADER=""
|
||||
|
||||
# Texte qui sera ajouté APRÈS chaque message envoyé
|
||||
MESSAGE_FOOTER=""
|
||||
|
||||
|
||||
##
|
||||
## Fichier de configuration
|
||||
##
|
||||
|
||||
if [ -n "${CONFIG_FILE}" ]; then
|
||||
if [ -e "${CONFIG_FILE}" ]; then
|
||||
. "${CONFIG_FILE}"
|
||||
else
|
||||
echo "ERROR: Configuration file \"${CONFIG_FILE}\" does not exists." >&2
|
||||
exit 2
|
||||
fi
|
||||
else
|
||||
if [ -e "${PROGDIR}/.freemobile-smsapi" ]; then
|
||||
. "${PROGDIR}/.freemobile-smsapi"
|
||||
elif [ -e "${HOME}/.freemobile-smsapi" ]; then
|
||||
. "${HOME}/.freemobile-smsapi"
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
##
|
||||
## Vérifications des paramètres requis
|
||||
##
|
||||
|
||||
if [ -z "${USER_LOGIN}" ] \
|
||||
|| [ -z "${API_KEY}" ] \
|
||||
|| [ -z "${SMSAPI_BASEURL}" ] \
|
||||
|| [ -z "${SMSAPI_SEND_ACTION}" ] \
|
||||
; then
|
||||
echo "ERROR: Either USER_LOGIN, API_KEY, SMSAPI_BASEURL or " \
|
||||
"SMSAPI_SEND_ACTION is not set" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
|
||||
##
|
||||
## Traitement du message
|
||||
##
|
||||
|
||||
MESSAGE_TO_SEND=""
|
||||
if [ $# -ge 1 ] && [ "${1}" ]; then # Message en tant qu'argument de la ligne de commande
|
||||
MESSAGE_TO_SEND="${1}"
|
||||
else # Message lu de STDIN
|
||||
while read line
|
||||
do
|
||||
MESSAGE_TO_SEND="${MESSAGE_TO_SEND}${line}\n"
|
||||
done
|
||||
MESSAGE_TO_SEND=${MESSAGE_TO_SEND%"\n"} # Retire le dernier saut de ligne
|
||||
fi
|
||||
|
||||
# Assemble header, message et footer
|
||||
FINAL_MESSAGE_TO_SEND="${MESSAGE_HEADER}${MESSAGE_TO_SEND}${MESSAGE_FOOTER}"
|
||||
|
||||
##
|
||||
## Appel à l'API (envoi)
|
||||
##
|
||||
|
||||
# echo "Will send the following to ${USER_LOGIN}:" #DEBUG
|
||||
# echo "${FINAL_MESSAGE_TO_SEND}" #DEBUG
|
||||
|
||||
# Particularités de l'appel de curl et la/les options associées :
|
||||
# * Renvoi le code réponse HTTP uniquement :
|
||||
# --write-out "%{http_code}" --silent --output /dev/null
|
||||
#
|
||||
HTTP_STATUS_CODE=$(\
|
||||
curl \
|
||||
--write-out "%{http_code}" \
|
||||
--silent \
|
||||
--output /dev/null \
|
||||
--get "${SMSAPI_BASEURL}/${SMSAPI_SEND_ACTION}" \
|
||||
--data "user=${USER_LOGIN}" \
|
||||
--data "pass=${API_KEY}" \
|
||||
--data-urlencode "msg=${FINAL_MESSAGE_TO_SEND}" \
|
||||
)
|
||||
|
||||
# Codes réponse HTTP possibles
|
||||
# 200 : Le SMS a été envoyé sur votre mobile.
|
||||
# 400 : Un des paramètres obligatoires est manquant.
|
||||
# 402 : Trop de SMS ont été envoyés en trop peu de temps.
|
||||
# 403 : Le service n'est pas activé sur l'espace abonné, ou login / clé
|
||||
# incorrect.
|
||||
# 500 : Erreur côté serveur. Veuillez réessayez ultérieurement.
|
||||
|
||||
if [ "${HTTP_STATUS_CODE}" -eq 200 ]; then
|
||||
# echo "API responded with 200: exiting with 0" #DEBUG
|
||||
exit 0
|
||||
else
|
||||
echo "Error: API responded with ${HTTP_STATUS_CODE}"
|
||||
exit 1
|
||||
fi
|
Loading…
Reference in New Issue
Block a user