Verified Commit 661c7150 authored by Fabio Bonfiglio's avatar Fabio Bonfiglio 💾
Browse files

v0.1.2

- Logging and recording outage in separate files.
- Telegram notifications indicating sart time and duration, as well as 
hostname of the probe
- Now with markdown !
parent 92487316
......@@ -2,7 +2,7 @@
[![GNU AGPLv3](https://img.shields.io/badge/license-AGPLv3-blue)](https://www.gnu.org/licenses/agpl-3.0.txt)
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.
It uses `nslookup` and records probe entries in a `.log` file, and outages in a `.csv` formated file.
Now with [Telegram](https://telegram.org/) notifications support !
......@@ -35,4 +35,4 @@ $ ps -C checknet.sh
## TODO
- [x] First implementation
- [ ] Verify csv extension if other file name is specified
- [ ] Test with `ping` and a regexp to parse timing, insteat of just `nslookup`
- [ ] (Test with `ping` and a regexp to parse timing, instead of just `nslookup`)
......@@ -22,17 +22,20 @@
# TODO : try to implement with `ping -c 1 google.com | sed -rn "2s/^.+time=([\d\.]+)/\1/pm"` to obtain response time.
VERSION="0.1.2"
RUNME=1
DISP=1
REMOTEHOST="google.com"
LOGFILE="checknet.log"
DESTINATION="checknet.csv"
CHATID=0
STATUS=0
OUTAGESTART=$(date +%s)
OUTAGESTART=0
STARTTIME=$(date --iso-8601=seconds)
trap "RUNME=0" INT
echo -e "\nchecknet v0.1.0 - by Fabio Bonfiglio"
echo -e "\nchecknet v${VERSION} - by Fabio Bonfiglio"
while getopts ht:qr:f: option; do
case "${option}" in
......@@ -58,10 +61,12 @@ if [ $? -ne 0 ]; then
exit 1
fi
touch $DESTINATION && echo "Writing logs in $DESTINATION"
touch $LOGFILE && echo "Writing logs in $LOGFILE"
touch $DESTINATION && echo "Recording outages in $DESTINATION"
echo "Outage detected time, Start timestamp, Duration, End timestamp" >> $DESTINATION
if [ $DISP -eq 1 ]; then
tail -n 0 -f --pid=$$ $DESTINATION &
tail -n 0 -f --pid=$$ $LOGFILE &
fi
while [ $RUNME -eq 1 ]; do
......@@ -70,21 +75,35 @@ while [ $RUNME -eq 1 ]; do
if [ $STATUS -ne 0 ]; then
STATUS=0
OUTAGESTART=$(date +%s)
STARTTIME=$(date --iso-8601=seconds)
fi
echo "$(date --iso-8601=seconds), $(date +%s), ${STATUS}, DOWN" >> $DESTINATION
echo "$(date --iso-8601=seconds), $(date +%s), ${STATUS}, DOWN" >> $LOGFILE
else
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
if [ $OUTAGESTART -ne 0 ]; then
OUTAGEDURATION=$(( $(date +%s) - ${OUTAGESTART} ))
echo "${STARTTIME}, ${OUTAGESTART}, ${OUTAGEDURATION}, $(date +%s)" >> $DESTINATION
if [ $CHATID -ne 0 ]; then
curl -s \
-d chat_id=${CHATID} \
-d text="\`$(hostname)\`: *Network connectivity outage* starting ${STARTTIME} (\`${OUTAGESTART}\`), and connection restored at $(date --iso-8601=seconds) (duration: ${OUTAGEDURATION} seconds)." \
-d parse_mode="Markdown" \
https://api.telegram.org/bot1196862707:AAHYGqyRzO4mNWrOGBRT6Vz-JXom6e80P9o/sendMessage \
>> /dev/null
fi
else
if [ $CHATID -ne 0 ]; then
curl -s \
-d chat_id=${CHATID} \
-d text="(\`$(whoami)\`) Starting network connectivity monitoring from \`$(hostname)\` at $(date --iso-8601=seconds)." \
-d parse_mode="Markdown" \
https://api.telegram.org/bot1196862707:AAHYGqyRzO4mNWrOGBRT6Vz-JXom6e80P9o/sendMessage \
>> /dev/null
fi
fi
fi
echo "$(date --iso-8601=seconds), $(date +%s), ${STATUS}, UP" >> $DESTINATION
echo "$(date --iso-8601=seconds), $(date +%s), ${STATUS}, UP" >> $LOGFILE
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