Merge pull request 'Localized sending scripts added' (#16) from v1 into master
Reviewed-on: https://www.cainet.info/git/cai/CAI-Watchdog/pulls/16
This commit is contained in:
commit
7e6b3afcdc
25
README.md
25
README.md
@ -38,7 +38,7 @@ email = E-mail address for notifications
|
|||||||
command = Command to send notification
|
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
|
- ```<email>``` - E-mail address for notifications
|
||||||
- ```<subject>``` - E-mail subject
|
- ```<subject>``` - E-mail subject
|
||||||
@ -46,6 +46,7 @@ In commands You can use fields ```<email>```, ```<subject>```, ```<message>```,
|
|||||||
- ```<service>``` - Service name
|
- ```<service>``` - Service name
|
||||||
- ```<uri>``` - Service URI (for web services)
|
- ```<uri>``` - Service URI (for web services)
|
||||||
- ```<process>``` - Process name (for OS tasks)
|
- ```<process>``` - Process name (for OS tasks)
|
||||||
|
- ```<state>``` - Service states are ```online``` and ```offline```, process states are ```running``` and ```stopped```
|
||||||
### Scripts configurations
|
### Scripts configurations
|
||||||
|
|
||||||
#### *nix
|
#### *nix
|
||||||
@ -60,6 +61,18 @@ Configuration file ```/etc/cai-watchdog/email.conf```
|
|||||||
- ```Password:``` - Set e-mail password here
|
- ```Password:``` - Set e-mail password here
|
||||||
- ```SMTP Server:``` - Set SMTP server address here
|
- ```SMTP Server:``` - Set SMTP server address here
|
||||||
- ```Port:``` - Set SMTP port 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
|
##### send-telegram
|
||||||
|
|
||||||
**Usage:** ```send-telegram 'message'```
|
**Usage:** ```send-telegram 'message'```
|
||||||
@ -68,6 +81,16 @@ Configuration file ```/etc/cai-watchdog/telegram.conf```
|
|||||||
|
|
||||||
- ```Group ID:``` - Set Telegram group ID here
|
- ```Group ID:``` - Set Telegram group ID here
|
||||||
- ```Bot token:``` - Set Telegram token 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
|
#### Windows
|
||||||
|
|
||||||
##### send-mail.ps1
|
##### send-mail.ps1
|
||||||
|
|||||||
29
scripts/unix/etc/cai-watchdog/on-process-state-change
Normal file
29
scripts/unix/etc/cai-watchdog/on-process-state-change
Normal 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"
|
||||||
@ -1,4 +1,11 @@
|
|||||||
#!/bin/bash
|
#!/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
|
source ${APP_PATH}/inc-icons
|
||||||
${APP_PATH}/send-telegram "${ICON_INFO} ATTENTION! Watchdog is stopped!"
|
${APP_PATH}/send-telegram "${ICON_INFO} ATTENTION! Watchdog is stopped!"
|
||||||
|
|||||||
@ -1,4 +1,11 @@
|
|||||||
#!/bin/bash
|
#!/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
|
source ${APP_PATH}/inc-icons
|
||||||
${APP_PATH}/send-telegram "${ICON_WARNING} ATTENTION! Watchdog is stopped!"
|
${APP_PATH}/send-telegram "${ICON_WARNING} ATTENTION! Watchdog is stopped!"
|
||||||
|
|||||||
29
scripts/unix/etc/cai-watchdog/on-uri-state-change
Normal file
29
scripts/unix/etc/cai-watchdog/on-uri-state-change
Normal 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"
|
||||||
@ -1,9 +1,17 @@
|
|||||||
#!/bin/bash
|
#!/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
|
rcpt=$1
|
||||||
subj=$2
|
subj=$2
|
||||||
message=$3
|
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 "$@";
|
set -- $ARGS "$@";
|
||||||
|
|
||||||
declare -A email;
|
declare -A email;
|
||||||
|
|||||||
79
scripts/unix/etc/cai-watchdog/send-mail-ru
Normal file
79
scripts/unix/etc/cai-watchdog/send-mail-ru
Normal 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
|
||||||
@ -1,4 +1,12 @@
|
|||||||
#!/bin/bash
|
#!/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
|
SCRIPT_NAME=$0
|
||||||
MESSAGE_TEXT=$1
|
MESSAGE_TEXT=$1
|
||||||
|
|
||||||
@ -7,7 +15,7 @@ if [ "$#" -ne 1 ]; then
|
|||||||
exit 0
|
exit 0
|
||||||
fi
|
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 "$@";
|
set -- $ARGS "$@";
|
||||||
|
|
||||||
GROUP_ID=$1
|
GROUP_ID=$1
|
||||||
|
|||||||
65
scripts/unix/etc/cai-watchdog/send-telegram-ru
Normal file
65
scripts/unix/etc/cai-watchdog/send-telegram-ru
Normal 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
|
||||||
@ -321,11 +321,17 @@ fn main() {
|
|||||||
.replace("<email>", &tasks[i].email)
|
.replace("<email>", &tasks[i].email)
|
||||||
.replace("<subject>", &format!("\"\u{2705} Service {} ({}) is online\"", tasks[i].service, tasks[i].uri))
|
.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("<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 {
|
} else {
|
||||||
tasks[i].command.to_string()
|
tasks[i].command.to_string()
|
||||||
.replace("<email>", &tasks[i].email)
|
.replace("<email>", &tasks[i].email)
|
||||||
.replace("<subject>", &format!("\"\u{2705} Process {} ({}) is running\"", tasks[i].service, tasks[i].process))
|
.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("<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));
|
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("<message>", &format!("\"\u{274c} Service {} ({}) is offline now\"", tasks[i].service, tasks[i].uri))
|
||||||
.replace("<service>", &tasks[i].service)
|
.replace("<service>", &tasks[i].service)
|
||||||
.replace("<uri>", &tasks[i].uri)
|
.replace("<uri>", &tasks[i].uri)
|
||||||
|
.replace("<state>", "offline")
|
||||||
} else {
|
} else {
|
||||||
tasks[i].command.to_string()
|
tasks[i].command.to_string()
|
||||||
.replace("<email>", &tasks[i].email)
|
.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("<message>", &format!("\"\u{274c} Process {} ({}) is not running now\"", tasks[i].service, tasks[i].process))
|
||||||
.replace("<service>", &tasks[i].service)
|
.replace("<service>", &tasks[i].service)
|
||||||
.replace("<process>", &tasks[i].process)
|
.replace("<process>", &tasks[i].process)
|
||||||
|
.replace("<state>", "stopped")
|
||||||
};
|
};
|
||||||
|
|
||||||
debug_log(format!("execute {}", shell_cmd));
|
debug_log(format!("execute {}", shell_cmd));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user