Short demo script in Python that monitors the VPN tunnels in Amazon Web Services. It queries the current state every 1.5 seconds in a loop and if the state changes it writes the new state to a log file. Needs the boto library.

import sys
import time
import boto
import boto.vpc
region = “eu-west-1”
# key and keyID can be specified here, or in a config file “””
#access_key_id = “QQQQetc.”
#secret_access_key = “QQQQetc.”
logfile = “vpntun.log”
old_tunnels_state = “”
conn = boto.vpc.connect_to_region(region)
# if access_key_id and secret_access_key are hardcoded in the script,
# comment the line above and uncomment the next one(s)
#conn = boto.vpc.connect_to_region(region, \
#                aws_access_key_id = access_key_id, aws_secret_access_key = secret_access_key)
while True:
    time.sleep(1.5)
    current_tunnels_state = “”
    tunnels = conn.get_all_vpn_connections()[0].tunnels
    # store the state for all tunnels into a string
    for tunnel in tunnels:
        current_tunnels_state = current_tunnels_state + \
            ” | “ + str(tunnel) + ” status is “ + tunnel.status + \
            ” since “ + str(tunnel.last_status_change)
    # if the current state string is different than the previous one,
    # something changed, so write the new state string to a log file
    if current_tunnels_state != old_tunnels_state:
        f = open(logfile, “a”)
        if f is None:
            sys.exit(“Unable to open “ + logfile + ” for writing”)
        f.write(str(time.strftime(“%Y-%m-%dT%H:%M:%S”)) + current_tunnels_state + “\n”)
        f.close()
    old_tunnels_state = current_tunnels_state