Monitoring Client Connection

Install the msmtp package and create a google app password in order to use gmail as a relay host .

opkg update; opkg install msmtp

#/etc/msmtprc

# Gmail
account        gmail
host           smtp.gmail.com
port           465
auth           on
tls            on
tls_starttls   off
from           <user>@gmail.com
user           <username>
password       <16char_app_pass>

account default : gmail

syslog LOG_MAIL

Modify the script variables and execution period according to your needs.

cat << "EOF" > /etc/email_notification.sh

#!/bin/sh
IP=192.168.1.101  # Host IP address
email_addr="user@example.com" # email address to send the notification to
FAILURE=/tmp/failure

if ! ping -c 5 -W 1 $IP >/dev/null; then
    [ -f "$FAILURE" ] && exit 0
    msg="Host does not respond to ping on $(date)"
    echo -e "To:$email_addr \nSubject:Notification\n\n$msg" | msmtp "$email_addr"
    touch "$FAILURE"
else
    [ -f "$FAILURE" ] || exit 0
    rm -f "$FAILURE"
fi

exit 0
EOF

chmod 755 /etc/email_notification.sh

cat << "EOF" >> /etc/crontabs/root
*/5 * * * * /etc/email_notification.sh
EOF
uci set system.@system[0].cronloglevel="9"
uci commit system
/etc/init.d/cron restart
2 Likes