Localized sending scripts added

Localized sending scripts added
This commit is contained in:
Alexander I. Chebykin 2022-10-29 20:54:23 +03:00
parent 56adae89ec
commit 4036a703c8
10 changed files with 268 additions and 5 deletions

View File

@ -38,7 +38,7 @@ email = E-mail address for notifications
command = Command to send notification
```
In commands You can use fields ```<email>```, ```<subject>```, ```<message>```, ```<service>```, ```<uri>``` and ```<process>```
In commands You can use fields ```<email>```, ```<subject>```, ```<message>```, ```<service>```, ```<uri>```, ```<process>``` and ```<state>```
- ```<email>``` - E-mail address for notifications
- ```<subject>``` - E-mail subject
@ -46,6 +46,7 @@ In commands You can use fields ```<email>```, ```<subject>```, ```<message>```,
- ```<service>``` - Service name
- ```<uri>``` - Service URI (for web services)
- ```<process>``` - Process name (for OS tasks)
- ```<state>``` - 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

View File

@ -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 - <email>
## $2 - <subject>
## $3 - <message>
## $4 - <service>
## $5 - <process>
## $6 - <status> [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"

View File

@ -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!"

View File

@ -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!"

View File

@ -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 - <email>
## $2 - <subject>
## $3 - <message>
## $4 - <service>
## $5 - <uri>
## $6 - <status> [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"

View File

@ -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;

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -321,11 +321,17 @@ fn main() {
.replace("<email>", &tasks[i].email)
.replace("<subject>", &format!("\"\u{2705} Service {} ({}) is online\"", tasks[i].service, tasks[i].uri))
.replace("<message>", &format!("\"\u{2705} Service {} ({}) is online now\"", tasks[i].service, tasks[i].uri))
.replace("<service>", &tasks[i].service)
.replace("<uri>", &tasks[i].uri)
.replace("<state", "online")
} else {
tasks[i].command.to_string()
.replace("<email>", &tasks[i].email)
.replace("<subject>", &format!("\"\u{2705} Process {} ({}) is running\"", tasks[i].service, tasks[i].process))
.replace("<message>", &format!("\"\u{2705} Process {} ({}) is running now\"", tasks[i].service, tasks[i].process))
.replace("<service>", &tasks[i].service)
.replace("<process>", &tasks[i].process)
.replace("<state>", "running")
};
debug_log(format!("execute {}", shell_cmd));
@ -355,6 +361,7 @@ fn main() {
.replace("<message>", &format!("\"\u{274c} Service {} ({}) is offline now\"", tasks[i].service, tasks[i].uri))
.replace("<service>", &tasks[i].service)
.replace("<uri>", &tasks[i].uri)
.replace("<state>", "offline")
} else {
tasks[i].command.to_string()
.replace("<email>", &tasks[i].email)
@ -362,6 +369,7 @@ fn main() {
.replace("<message>", &format!("\"\u{274c} Process {} ({}) is not running now\"", tasks[i].service, tasks[i].process))
.replace("<service>", &tasks[i].service)
.replace("<process>", &tasks[i].process)
.replace("<state>", "stopped")
};
debug_log(format!("execute {}", shell_cmd));