Verified Commit 7a6f3241 authored by Fabio Bonfiglio's avatar Fabio Bonfiglio 💾
Browse files

Add Telegram notification support

parent 1960b2fb
......@@ -4,6 +4,8 @@
A simple bash script to check and continuously monitor internet connectivity, to be used on a server or a headless node.
It uses `nslookup` and records probe entries in a `csv` formated file.
Now with [Telegram](https://telegram.org/) notifications support !
## Usage
```sh
$ ./checknet [-h][-q][-h <remoteHost>][-f <fileName>]
......@@ -11,6 +13,7 @@ $ ./checknet [-h][-q][-h <remoteHost>][-f <fileName>]
Option | Description
:-----:|:------------
-h | Display help message and exit.
-t | Telegram chat-ID to use for notifications. See http://t.me/fbo_checknet_bot .
-q | Quiet mode. Doesn't show live log.
-r remoteHost | Use a custom remote host, instead of default `google.com`
-f fileName.csv | Custom destination file. Default is `checknet.csv`
......
......@@ -26,21 +26,25 @@ RUNME=1
DISP=1
REMOTEHOST="google.com"
DESTINATION="checknet.csv"
CHATID=0
STATUS=0
trap "RUNME=0" INT
echo -e "\nchecknet v0.1.0 - by Fabio Bonfiglio"
while getopts hqr:f: option; do
while getopts ht:qr:f: option; do
case "${option}" in
h)
echo -e "\nUsage: $0 [-h][-d][-r][-f <file name>]"
echo -e "\nOptions:\n\n\t-h\t\tDisplay this help message."
echo -e "\t-t\t\tTelegram chat ID to use for notifications. See http://t.me/fbo_checknet_bot"
echo -e "\t-q\t\tQuiet. Don't show live log feed."
echo -e "\t-r remoteHost\tThe remote host name to reach for."
echo -e "\t-f fileName.csv\tIndicate a file path and name where to save csv entries.\n"
exit 0
;;
t) CHATID=${OPTARG};;
q) DISP="0";;
r) REMOTEHOST=${OPTARG};;
f) DESTINATION=${OPTARG};;
......@@ -48,23 +52,38 @@ while getopts hqr:f: option; do
done
nslookup $REMOTEHOST
if [[ "$?" -ne "0" ]]; then
if [ $? -ne 0 ]; then
echo "Invalid remote host name."
exit 1
fi
touch $DESTINATION && echo "Writing logs in $DESTINATION"
if [[ "$DISP" -eq "1" ]]; then
if [ $DISP -eq 1 ]; then
tail -n 0 -f --pid=$$ $DESTINATION &
fi
while [[ "$RUNME" -eq "1" ]]; do
while [ $RUNME -eq 1 ]; do
RES=$(nslookup $REMOTEHOST)
if [[ "$?" -ne "0" ]]; then
echo "$(date --iso-8601=seconds), 0, DOWN" >> $DESTINATION
if [ $? -ne 0 ]; then
if [ $STATUS -ne 0 ]; then
STATUS=0
OUTAGESTART=$(date +%s)
fi
echo "$(date --iso-8601=seconds), $(date +%s), ${STATUS}, DOWN" >> $DESTINATION
else
echo "$(date --iso-8601=seconds), 1, UP" >> $DESTINATION
if [ $STATUS -eq 0 ]; then
STATUS=1
OUTAGEDURATION=$(date +%s)-$OUTAGESTART
if [ $CHATID -ne 0 ]; then
curl -s \
-d chat_id=${CHATID} \
-d text="Network connectivity restored on $(date --iso-8601=seconds). Outage duration: ${OUTAGEDURATION} seconds." \
https://api.telegram.org/bot1196862707:AAHYGqyRzO4mNWrOGBRT6Vz-JXom6e80P9o/sendMessage \
>> /dev/null
fi
fi
echo "$(date --iso-8601=seconds), $(date +%s), ${STATUS}, UP" >> $DESTINATION
fi
sleep 1
done
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment