Compare commits

...

4 Commits

6 changed files with 312 additions and 46 deletions

View File

@ -53,3 +53,60 @@ ansible-playbook ./notification.yml
```bash
rm -rf ./notification*
```
## Manually instalation E-Mail notification
**Dependencies:**
>
> - [Python 3](https://www.python.org/downloads/) (tested version 3.9.5 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
> - existing [/usr/local/bin/sendmail.py](https://git.hmp.today/pavel.muhortov/utils#sendmail-py)
```bash
# download sendmail-reboot.sh
sudo wget https://git.hmp.today/pavel.muhortov/notification/raw/branch/master/templates/sendmail-reboot.sh -O /etc/init.d/sendmail-reboot.sh
sudo chmod +x /etc/init.d/sendmail-reboot.sh
sudo update-rc.d sendmail-reboot.sh defaults #debian
sudo chkconfig --add /etc/init.d/sendmail-reboot.sh #rhel
# download sendmail-login.sh
sudo wget https://git.hmp.today/pavel.muhortov/notification/raw/branch/master/templates/sendmail-login.sh -O /etc/profile.d/sendmail-reboot.sh
sudo chmod +x /etc/profile.d/sendmail-login.sh
# edit sendmail.config
sudo tee /usr/local/bin/sendmail.config > /dev/null <<'EOF'
smtp=smtp.host.zone
port=587
from=user@host.zone
pass=pass
dest=user@host.zone
EOF
```
## Manually instalation Telegram notification
**Dependencies:**
>
> - [curl](https://curl.se/download.html) (tested version 7.74.0 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
```bash
# download sendtelegram-reboot.sh
sudo wget https://git.hmp.today/pavel.muhortov/notification/raw/branch/master/templates/sendtelegram-reboot.sh -O /etc/init.d/sendtelegram-reboot.sh
sudo chmod +x /etc/init.d/sendtelegram-reboot.sh
sudo update-rc.d sendtelegram-reboot.sh defaults #debian
sudo chkconfig --add /etc/init.d/sendtelegram-reboot.sh #rhel
# download sendtelegram-login.sh
sudo wget https://git.hmp.today/pavel.muhortov/notification/raw/branch/master/templates/sendtelegram-login.sh -O /etc/profile.d/sendtelegram-login.sh
sudo chmod +x /etc/profile.d/sendtelegram-login.sh
# edit sendtelegram.config
sudo tee /usr/local/bin/sendtelegram.config > /dev/null <<'EOF'
API_URL=https://api.telegram.org/YOURAPIKEY/sendMessage
SILENCE=True
## single chat
CHAT_ID=123456789
## chat topics
CHAT_ID=-100123456789
THRD_ID=123
EOF
```

View File

@ -1,14 +1,48 @@
#! /bin/sh
(
python3 /usr/local/bin/sendmail.py \
-u "$(cat /usr/local/bin/sendmail.config | grep "from=" | cut -d= -f2)" \
-p "$(cat /usr/local/bin/sendmail.config | grep "pass=" | cut -d= -f2)" \
-d "$(cat /usr/local/bin/sendmail.config | grep "dest=" | cut -d= -f2)" \
--smtp "$(cat /usr/local/bin/sendmail.config | grep "smtp=" | cut -d= -f2)" \
--port "$(cat /usr/local/bin/sendmail.config | grep "port=" | cut -d= -f2)" \
--stls "True" \
--subj "[Authorization] $(cat /etc/hostname): user logged in system" \
--text "$(w)" \
>> /dev/null 2>&1 &
TXT_MSG="[Authorization] $(cat /etc/hostname): ${USER}"
SESSION=$(who | grep "${USER}")
if [ -n "${SESSION}" ]; then
TXT_MSG=$(cat <<-END
${TXT_MSG}
--- GDE/SSH:
${SESSION}
END
)
fi
SESSION=$(lsof -a -i -u "${USER}" -sTCP:ESTABLISHED | grep 'vnc' \
| awk '{print $3,$9,$10}')
if [ -n "${SESSION}" ]; then
TXT_MSG=$(cat <<-END
${TXT_MSG}
--- VNC:
${SESSION}
END
)
fi
SESSION=$(ps -ef -u "${USER}" | grep '[s]shd:.*@notty' \
| awk '{$2=$3=$4=$6=$7=""; print $0}')
if [ -n "${SESSION}" ]; then
TXT_MSG=$(cat <<-END
${TXT_MSG}
--- SFTP:
${SESSION}
END
)
fi
(
python3 /usr/local/bin/sendmail.py \
-u "$(grep "from=" /usr/local/bin/sendmail.config | cut -d= -f2)" \
-p "$(grep "pass=" /usr/local/bin/sendmail.config | cut -d= -f2)" \
-d "$(grep "dest=" /usr/local/bin/sendmail.config | cut -d= -f2)" \
--smtp "$(grep "smtp=" /usr/local/bin/sendmail.config | cut -d= -f2)" \
--port "$(grep "port=" /usr/local/bin/sendmail.config | cut -d= -f2)" \
--stls "True" \
--subj "[Authorization] $(cat /etc/hostname): ${USER}" \
--text "${TXT_MSG}" \
>> /dev/null 2>&1 &
)

View File

@ -2,43 +2,116 @@
# chkconfig: 2345 20 80
### BEGIN INIT INFO
# Provides: sendmail-reboot
# Required-Start: $all
# Provides: sendmail-reboot
# Required-Start: $all
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: Sending a message on startup
# Short-Description: Sending a message on startup
### END INIT INFO
start() {
sleep 30
python3 /usr/local/bin/sendmail.py \
--smtp "$(cat /usr/local/bin/sendmail.config | grep "smtp=" | cut -d= -f2)" \
-u "$(cat /usr/local/bin/sendmail.config | grep "from=" | cut -d= -f2)" \
-p "$(cat /usr/local/bin/sendmail.config | grep "pass=" | cut -d= -f2)" \
-d "$(cat /usr/local/bin/sendmail.config | grep "dest=" | cut -d= -f2)" \
--subj "[System States] $(cat /etc/hostname): computer has been rebooted" \
--text "$(w)" \
>> /dev/null 2>&1 &
TXT_MSG="[System States] $(cat /etc/hostname): OS has been rebooted"
REL=$(grep 'PRETTY_NAME' /etc/os-release | cut -d= -f2)
if [ -n "${REL}" ]; then
TXT_MSG=$(cat <<-END
${TXT_MSG}
--- RELEASE:
${REL}
END
)
fi
KER=$(uname -srm)
if [ -n "${KER}" ]; then
TXT_MSG=$(cat <<-END
${TXT_MSG}
--- KERNEL:
${KER}
END
)
fi
NET=$(ip a | grep 'inet' | grep -v -e '127.0.0.1/8' -e '::1/128' \
| awk '{print $2}')
if [ -n "${NET}" ]; then
TXT_MSG=$(cat <<-END
${TXT_MSG}
--- NETWORK:
${NET}
END
)
fi
CPU=$(grep 'model name' /proc/cpuinfo | cut -d':' -f2- | cut -d' ' -f2-)
if [ -n "${CPU}" ]; then
TXT_MSG=$(cat <<-END
${TXT_MSG}
--- CPUs:
${CPU}
END
)
fi
MEM=$(grep /proc/meminfo \
-e 'MemTotal' -e 'MemAvailable' -e 'MemFree' \
-e 'SwapTotal' -e 'SwapFree' \
| numfmt --field 2 --from-unit=Ki --to=iec | sed 's/ kB//g')
if [ -n "${MEM}" ]; then
TXT_MSG=$(cat <<-END
${TXT_MSG}
--- MEMORY:
${MEM}
END
)
fi
DRI=$(LC_ALL=en_US.UTF-8 df -h --output=source,fstype,size,pcent,target \
-x tmpfs -x devtmpfs -x squashfs)
if [ -n "${DRI}" ]; then
TXT_MSG=$(cat <<-END
${TXT_MSG}
--- DRIVES:
${DRI}
END
)
fi
sleep 15
python3 /usr/local/bin/sendmail.py \
--smtp "$(grep "smtp=" /usr/local/bin/sendmail.config | cut -d= -f2)" \
-u "$(grep "from=" /usr/local/bin/sendmail.config | cut -d= -f2)" \
-p "$(grep "pass=" /usr/local/bin/sendmail.config | cut -d= -f2)" \
-d "$(grep "dest=" /usr/local/bin/sendmail.config | cut -d= -f2)" \
--subj "[System States] $(cat /etc/hostname): OS has been rebooted" \
--text "${TXT_MSG}" \
>> /dev/null 2>&1 &
}
stop() {
pkill -f /usr/local/bin/sendmail.py
pkill -f /usr/local/bin/sendmail.py
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
printf "%s\n" "Usage: $0 {start|stop|restart}"
esac
exit 0

View File

@ -1,10 +1,42 @@
#! /bin/sh
API_URL="$(grep "API_URL=" /usr/local/bin/sendtelegram.config | cut -d= -f2)"
SILENCE="$(grep "SILENCE=" /usr/local/bin/sendtelegram.config | cut -d= -f2)"
CHAT_ID="$(grep "CHAT_ID=" /usr/local/bin/sendtelegram.config | cut -d= -f2)"
THRD_ID="$(grep "THRD_ID=" /usr/local/bin/sendtelegram.config | cut -d= -f2)"
TXT_MSG="[Authorization] $(cat /etc/hostname):
$(w -h)"
TXT_MSG="[Authorization] $(cat /etc/hostname): ${USER}"
SESSION=$(who | grep "${USER}")
if [ -n "${SESSION}" ]; then
TXT_MSG=$(cat <<-END
${TXT_MSG}
--- GDE/SSH:
${SESSION}
END
)
fi
SESSION=$(lsof -a -i -u "${USER}" -sTCP:ESTABLISHED | grep 'vnc' \
| awk '{print $3,$9,$10}')
if [ -n "${SESSION}" ]; then
TXT_MSG=$(cat <<-END
${TXT_MSG}
--- VNC:
${SESSION}
END
)
fi
SESSION=$(ps -ef -u "${USER}" | grep '[s]shd:.*@notty' \
| awk '{$2=$3=$4=$6=$7=""; print $0}')
if [ -n "${SESSION}" ]; then
TXT_MSG=$(cat <<-END
${TXT_MSG}
--- SFTP:
${SESSION}
END
)
fi
(
if [ -z "${THRD_ID}" ]; then

View File

@ -15,7 +15,78 @@ start() {
SILENCE="$(grep "SILENCE=" /usr/local/bin/sendtelegram.config | cut -d= -f2)"
CHAT_ID="$(grep "CHAT_ID=" /usr/local/bin/sendtelegram.config | cut -d= -f2)"
THRD_ID="$(grep "THRD_ID=" /usr/local/bin/sendtelegram.config | cut -d= -f2)"
TXT_MSG="[System States] $(cat /etc/hostname): computer has been rebooted"
TXT_MSG="[System States] $(cat /etc/hostname): OS has been rebooted"
REL=$(grep 'PRETTY_NAME' /etc/os-release | cut -d= -f2)
if [ -n "${REL}" ]; then
TXT_MSG=$(cat <<-END
${TXT_MSG}
--- RELEASE:
${REL}
END
)
fi
KER=$(uname -srm)
if [ -n "${KER}" ]; then
TXT_MSG=$(cat <<-END
${TXT_MSG}
--- KERNEL:
${KER}
END
)
fi
NET=$(ip a | grep 'inet' | grep -v -e '127.0.0.1/8' -e '::1/128' \
| awk '{print $2}')
if [ -n "${NET}" ]; then
TXT_MSG=$(cat <<-END
${TXT_MSG}
--- NETWORK:
${NET}
END
)
fi
CPU=$(grep 'model name' /proc/cpuinfo | cut -d':' -f2- | cut -d' ' -f2-)
if [ -n "${CPU}" ]; then
TXT_MSG=$(cat <<-END
${TXT_MSG}
--- CPUs:
${CPU}
END
)
fi
MEM=$(grep /proc/meminfo \
-e 'MemTotal' -e 'MemAvailable' -e 'MemFree' \
-e 'SwapTotal' -e 'SwapFree' \
| numfmt --field 2 --from-unit=Ki --to=iec | sed 's/ kB//g')
if [ -n "${MEM}" ]; then
TXT_MSG=$(cat <<-END
${TXT_MSG}
--- MEMORY:
${MEM}
END
)
fi
DRI=$(LC_ALL=en_US.UTF-8 df -h --output=source,fstype,size,pcent,target \
-x tmpfs -x devtmpfs -x squashfs)
if [ -n "${DRI}" ]; then
TXT_MSG=$(cat <<-END
${TXT_MSG}
--- DRIVES:
${DRI}
END
)
fi
sleep 15
if [ -z "${THRD_ID}" ]; then
@ -35,7 +106,7 @@ start() {
}
stop() {
pkill -f curl -s -o /dev/null -X POST -d chat_id="${CHAT_ID}"
pkill -f "curl -s -o /dev/null -X POST -d chat_id=${CHAT_ID}"
}
case "$1" in

View File

@ -1,4 +1,3 @@
API_KEY={{sendtelegram_api_key}}
API_URL=https://api.telegram.org/bot{{sendtelegram_api_key}}/sendMessage
SILENCE={{sendtelegram_silence}}
CHAT_ID={{sendtelegram_chat_id}}