2022-10-29 20:54:23 +03:00

66 lines
1.7 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
APP_PATH=$(dirname "$0") # relative
APP_PATH=$(cd "$APP_PATH" && pwd) # absolutized and normalized
if [[ -z "$APP_PATH" ]] ; then
# error; for some reason, the path is not accessible
# to the script (e.g. permissions re-evaled after suid)
APP_PATH="/etc/cai-watchdog"
fi
source ${APP_PATH}/inc-icons
SCRIPT_NAME=$0
SERVICE=$1
PROCESS_OR_URI=$2
STATE=$3
case $3 in
online)
SERVICE_OR_PROCESS="Служба"
STATE_STR="доступна"
ICON=${ICON_OK}
;;
running)
SERVICE_OR_PROCESS="Процесс"
STATE_STR="запущен"
ICON=${ICON_OK}
;;
offline)
SERVICE_OR_PROCESS="Служба"
STATE_STR="недоступна"
ICON=${ICON_FAIL}
;;
stopped)
SERVICE_OR_PROCESS="Процесс"
STATE_STR="остановлен"
ICON=${ICON_FAIL}
;;
esac
MESSAGE_TEXT="${ICON} ${SERVICE_OR_PROCESS} ${SERVICE} (${PROCESS_OR_URI}) ${STATE_STR}"
if [ "$#" -ne 3 ]; then
echo "Вы должны передать 3 аргумента - название службы, uri или название процесса и статус. Аргументы с пробелами требуется взять в кавычки"
exit 0
fi
ARGS=$(xargs echo $(perl -anle 's/^[^:]+//g && s/:\s+//g && print' ${APP_PATH}/email.conf) < /dev/null)
set -- $ARGS "$@";
GROUP_ID=$1
BOT_TOKEN=$2
# this 3 checks (if) are not necessary but should be convenient
if [ "${MESSAGE_TEXT}" == "-h" ]; then
echo "Usage: `basename ${SCRIPT_NAME}` \"text message\""
exit 0
fi
if [ -z "${MESSAGE_TEXT}" ]
then
echo "Add message text as second arguments"
exit 0
fi
curl -s --data "text=${MESSAGE_TEXT}" --data "chat_id=$GROUP_ID" 'https://api.telegram.org/bot'$BOT_TOKEN'/sendMessage' > /dev/null