From 4036a703c811306bf4b5acb9e60b8e1bb0a7c1bd Mon Sep 17 00:00:00 2001 From: "Alexander I. Chebykin" Date: Sat, 29 Oct 2022 20:54:23 +0300 Subject: [PATCH] Localized sending scripts added Localized sending scripts added --- README.md | 25 +++++- .../etc/cai-watchdog/on-process-state-change | 29 +++++++ scripts/unix/etc/cai-watchdog/on-start | 9 ++- scripts/unix/etc/cai-watchdog/on-stop | 9 ++- .../unix/etc/cai-watchdog/on-uri-state-change | 29 +++++++ scripts/unix/etc/cai-watchdog/send-mail | 10 ++- scripts/unix/etc/cai-watchdog/send-mail-ru | 79 +++++++++++++++++++ scripts/unix/etc/cai-watchdog/send-telegram | 10 ++- .../unix/etc/cai-watchdog/send-telegram-ru | 65 +++++++++++++++ src/main.rs | 8 ++ 10 files changed, 268 insertions(+), 5 deletions(-) create mode 100644 scripts/unix/etc/cai-watchdog/on-process-state-change create mode 100644 scripts/unix/etc/cai-watchdog/on-uri-state-change create mode 100644 scripts/unix/etc/cai-watchdog/send-mail-ru create mode 100644 scripts/unix/etc/cai-watchdog/send-telegram-ru diff --git a/README.md b/README.md index b34a1b8..6c0370f 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ email = E-mail address for notifications command = Command to send notification ``` -In commands You can use fields ``````, ``````, ``````, ``````, `````` and `````` +In commands You can use fields ``````, ``````, ``````, ``````, ``````, `````` and `````` - `````` - E-mail address for notifications - `````` - E-mail subject @@ -46,6 +46,7 @@ In commands You can use fields ``````, ``````, ``````, - `````` - Service name - `````` - Service URI (for web services) - `````` - Process name (for OS tasks) +- `````` - Service states are ```online``` and ```offline```, process states are ```running``` and ```stopped``` ### Scripts configurations #### *nix @@ -60,6 +61,18 @@ Configuration file ```/etc/cai-watchdog/email.conf``` - ```Password:``` - Set e-mail password here - ```SMTP Server:``` - Set SMTP server address here - ```Port:``` - Set SMTP port here + +##### send-mail-[locale] + +**Usage:** ```send-mail-[locale] recipient 'service' 'process-or-uri' 'state'``` + +Configuration file ```/etc/cai-watchdog/email.conf``` + +- ```Username:``` - Set e-mail user name here +- ```Password:``` - Set e-mail password here +- ```SMTP Server:``` - Set SMTP server address here +- ```Port:``` - Set SMTP port here + ##### send-telegram **Usage:** ```send-telegram 'message'``` @@ -68,6 +81,16 @@ Configuration file ```/etc/cai-watchdog/telegram.conf``` - ```Group ID:``` - Set Telegram group ID here - ```Bot token:``` - Set Telegram token here + +##### send-telegram-[locale] + +**Usage:** ```send-telegram-[locale] 'service', 'process-or-uri', 'state'``` + +Configuration file ```/etc/cai-watchdog/telegram.conf``` + +- ```Group ID:``` - Set Telegram group ID here +- ```Bot token:``` - Set Telegram token here + #### Windows ##### send-mail.ps1 diff --git a/scripts/unix/etc/cai-watchdog/on-process-state-change b/scripts/unix/etc/cai-watchdog/on-process-state-change new file mode 100644 index 0000000..20df23b --- /dev/null +++ b/scripts/unix/etc/cai-watchdog/on-process-state-change @@ -0,0 +1,29 @@ +#!/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 + +##### DEFAULT PARAMS ORDER ##### +## $1 - +## $2 - +## $3 - +## $4 - +## $5 - +## $6 - [running | stopped] + +EMAIL=$1 +SUBJECT=$2 +MESSAGE=$3 +SERVICE=$4 +PROCESS=$5 +STATUS=$6 + +## SAMPLE COMMANDS +#${APP_PATH}/send-mail "${EMAIL}", "${SUBJECT}", "$MESSAGE" +#${APP_PATH}/send-telegram-ru "${SERVICE}", "${PROCESS}", "$STATUS" + +${APP_PATH}/send-telegram "$MESSAGE" diff --git a/scripts/unix/etc/cai-watchdog/on-start b/scripts/unix/etc/cai-watchdog/on-start index 0fe159e..ab8faeb 100644 --- a/scripts/unix/etc/cai-watchdog/on-start +++ b/scripts/unix/etc/cai-watchdog/on-start @@ -1,4 +1,11 @@ #!/bin/bash -APP_PATH=/etc/cai-watchdog +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 ${APP_PATH}/send-telegram "${ICON_INFO} ATTENTION! Watchdog is stopped!" diff --git a/scripts/unix/etc/cai-watchdog/on-stop b/scripts/unix/etc/cai-watchdog/on-stop index a006bf9..19d28a7 100644 --- a/scripts/unix/etc/cai-watchdog/on-stop +++ b/scripts/unix/etc/cai-watchdog/on-stop @@ -1,4 +1,11 @@ #!/bin/bash -APP_PATH=/etc/cai-watchdog +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 ${APP_PATH}/send-telegram "${ICON_WARNING} ATTENTION! Watchdog is stopped!" diff --git a/scripts/unix/etc/cai-watchdog/on-uri-state-change b/scripts/unix/etc/cai-watchdog/on-uri-state-change new file mode 100644 index 0000000..064860f --- /dev/null +++ b/scripts/unix/etc/cai-watchdog/on-uri-state-change @@ -0,0 +1,29 @@ +#!/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 + +##### DEFAULT PARAMS ORDER ##### +## $1 - +## $2 - +## $3 - +## $4 - +## $5 - +## $6 - [online | offline] + +EMAIL=$1 +SUBJECT=$2 +MESSAGE=$3 +SERVICE=$4 +URI=$5 +STATUS=$6 + +## SAMPLE COMMANDS +#${APP_PATH}/send-mail "${EMAIL}", "${SUBJECT}", "$MESSAGE" +#${APP_PATH}/send-telegram-ru "${SERVICE}", "${URI}", "$STATUS" + +${APP_PATH}/send-telegram "$MESSAGE" diff --git a/scripts/unix/etc/cai-watchdog/send-mail b/scripts/unix/etc/cai-watchdog/send-mail index 89aa532..dc12d2f 100644 --- a/scripts/unix/etc/cai-watchdog/send-mail +++ b/scripts/unix/etc/cai-watchdog/send-mail @@ -1,9 +1,17 @@ #!/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 + rcpt=$1 subj=$2 message=$3 -ARGS=$(xargs echo $(perl -anle 's/^[^:]+//g && s/:\s+//g && print' /etc/cai-watchdog/email.conf) < /dev/null) +ARGS=$(xargs echo $(perl -anle 's/^[^:]+//g && s/:\s+//g && print' ${APP_PATH}/email.conf) < /dev/null) set -- $ARGS "$@"; declare -A email; diff --git a/scripts/unix/etc/cai-watchdog/send-mail-ru b/scripts/unix/etc/cai-watchdog/send-mail-ru new file mode 100644 index 0000000..4e13489 --- /dev/null +++ b/scripts/unix/etc/cai-watchdog/send-mail-ru @@ -0,0 +1,79 @@ +#!/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 + +RCPT=$1 +SERVICE=$2 +PROCESS_OR_URI=$3 +STATE=$4 + +case $4 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}" + + +#subj=$2 +#message=$3 + +ARGS=$(xargs echo $(perl -anle 's/^[^:]+//g && s/:\s+//g && print' ${APP_PATH}/email.conf) < /dev/null) +set -- $ARGS "$@"; + +declare -A email; +email['user']=$1 +email['pass']=$2 +email['smtp']=$3 +email['port']=$4 + +email_content='From: "Watchdog" <'"${email['user']}"'> +To: "Subscriber" <'"${RCPT}"'> +Subject: '"${MESSAGE_TEXT}"' +Date: '"$(date)"' + +'"${MESSAGE_TEXT}"' +----- +'"${HOSTNAME}"' +'; + + +echo "$email_content" | curl -s \ # -vvv \ + --url "smtps://${email['smtp']}:${email['port']}" \ + --user "${email['user']}:${email['pass']}" \ + --mail-from "${email['user']}" \ + --mail-rcpt "${RCPT}" \ + --upload-file - \ + -T - + + +if [[ $? == 0 ]]; then + echo; + echo 'okay'; +else + echo "curl error code $?"; + man curl | grep "^ \+$? \+" +fi diff --git a/scripts/unix/etc/cai-watchdog/send-telegram b/scripts/unix/etc/cai-watchdog/send-telegram index 7d29a4f..8d97c28 100644 --- a/scripts/unix/etc/cai-watchdog/send-telegram +++ b/scripts/unix/etc/cai-watchdog/send-telegram @@ -1,4 +1,12 @@ #!/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 + SCRIPT_NAME=$0 MESSAGE_TEXT=$1 @@ -7,7 +15,7 @@ if [ "$#" -ne 1 ]; then exit 0 fi -ARGS=$(xargs echo $(perl -anle 's/^[^:]+//g && s/:\s+//g && print' /etc/cai-watchdog/email.conf) < /dev/null) +ARGS=$(xargs echo $(perl -anle 's/^[^:]+//g && s/:\s+//g && print' ${APP_PATH}/email.conf) < /dev/null) set -- $ARGS "$@"; GROUP_ID=$1 diff --git a/scripts/unix/etc/cai-watchdog/send-telegram-ru b/scripts/unix/etc/cai-watchdog/send-telegram-ru new file mode 100644 index 0000000..6ebc37b --- /dev/null +++ b/scripts/unix/etc/cai-watchdog/send-telegram-ru @@ -0,0 +1,65 @@ +#!/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 diff --git a/src/main.rs b/src/main.rs index e62a9da..b9fe6d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -321,11 +321,17 @@ fn main() { .replace("", &tasks[i].email) .replace("", &format!("\"\u{2705} Service {} ({}) is online\"", tasks[i].service, tasks[i].uri)) .replace("", &format!("\"\u{2705} Service {} ({}) is online now\"", tasks[i].service, tasks[i].uri)) + .replace("", &tasks[i].service) + .replace("", &tasks[i].uri) + .replace("", &tasks[i].email) .replace("", &format!("\"\u{2705} Process {} ({}) is running\"", tasks[i].service, tasks[i].process)) .replace("", &format!("\"\u{2705} Process {} ({}) is running now\"", tasks[i].service, tasks[i].process)) + .replace("", &tasks[i].service) + .replace("", &tasks[i].process) + .replace("", "running") }; debug_log(format!("execute {}", shell_cmd)); @@ -355,6 +361,7 @@ fn main() { .replace("", &format!("\"\u{274c} Service {} ({}) is offline now\"", tasks[i].service, tasks[i].uri)) .replace("", &tasks[i].service) .replace("", &tasks[i].uri) + .replace("", "offline") } else { tasks[i].command.to_string() .replace("", &tasks[i].email) @@ -362,6 +369,7 @@ fn main() { .replace("", &format!("\"\u{274c} Process {} ({}) is not running now\"", tasks[i].service, tasks[i].process)) .replace("", &tasks[i].service) .replace("", &tasks[i].process) + .replace("", "stopped") }; debug_log(format!("execute {}", shell_cmd));