Cloud Router Alarms

Cloud Router shall send an alarm message via email, if one or more activated clients are offline.

Prerequisites

Make sure the hostname is configured correctly. If the FQDN is myserver.sub.example.com, /etc/hostname must contain a line like

/etc/hostname
myserver 

and /etc/hosts must contain a line like

/etc/hosts
1.2.3.4  myserver.sub.example.com myserver

Check correct configuration with hostname and hostname -f

Further, we need a Mail Transfer Agent (MTA) such as Exim 4 1) installed on our machine. For convenience reasons we will also install mailutils 2) so we can just call mail from the command line.

apt-get install exim4-daemon-light (or just default-mta)
dpkg-reconfigure exim4-config
apt-get install mailutils

Choose internet site; mail is sent and received directly using SMTP when configuring Exim.

Test in Shell

mail samuel.hess@netmodule.com
Cc:
Subject: Test
Some Body Text
Ctrl + D to send

Implementation

Cloud Router stores the current connections status for OpenVPN clients under /tmp/vpnportal/openvpn_clients Make a file called activated with the activated clients

activated
OPENVPN_CLIENT_1
OPENVPN_CLIENT_2
OPENVPN_CLIENT_3
OPENVPN_CLIENT_4
OPENVPN_CLIENT_5
OPENVPN_CLIENT_6
OPENVPN_CLIENT_7
OPENVPN_CLIENT_8
OPENVPN_CLIENT_9
OPENVPN_CLIENT_10

Run a script like this:

surveyor.sh
# This script checks every $interval if the activated clients are online. 
# If one or more activated clients are offline, an e-mail alarm is sent.
activatedClients=/tmp/vpnportal/openvpn_clients/activated
connectedClients=/tmp/vpnportal/openvpn_clients/connected
interval=60
recipient=alarms@netmodule.com
 
while true; do
    while read client
    do
        grep -q $client $connectedClients
        if [ $? -eq 0 ]
        then
            echo "$client is connected" > /dev/null
        else
            echo "$client is offline"
            echo "$client is offline" >> message2
        fi
    done < $activatedClients
    if [ -f message2 ]
    then
        echo "sending alarm message"
        echo "This alarm message is coming from $(hostname -f) since one or more activated clients are offline:" > message1
        cat message1 message2 | mail -s "Cloud Router Alarm: Client(s) offline" recipient
        rm message*
    fi
    sleep $interval
done