File: //usr/lib/python2.7/site-packages/lap/check_vpn_ipsec.py
import subprocess
import yaml
import re
def ipsec():
command = "/usr/bin/sudo /usr/sbin/ipsec auto --status"
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
return(out)
def get_ip_vip(ipvip):
ifconfig = subprocess.Popen("/sbin/ifconfig", stdout=subprocess.PIPE)
out, err = ifconfig.communicate()
hasvip = re.search(r"(.*) addr:{0} (.*)".format(ipvip), out)
return(True if hasvip else False)
def ping(ipvip):
command = "/bin/ping -c 1 -W 1 {0}".format(ipvip)
ping = subprocess.call(command.split(), stdout=subprocess.PIPE)
return(True if ping == 0 else False)
def __run__(params):
connection = params.get("connection")
tunel = params.get("tunel")
client = params.get("client")
ipvip = params.get("ipvip")
if not connection or not tunel or not client or not ipvip:
return([2, "Parametros para monitoracao ausentes do YAML, por favor verifique!"])
try:
alive = ping(ipvip)
ipesecstatus = ipsec()
check = re.search(r'(.*): "{0}(.*)" (.*) {1}'.format(connection, tunel), ipesecstatus)
if not get_ip_vip(ipvip):
if not alive:
return([2, "IP VIP Down"])
else:
return([0, "Servidor em Stand by."])
if not check:
return([2, "Error: Problema com o Tunel IPsec LWO x {0}".format(client)])
else:
return([0, "OK: Tunel IPsec LWO Unik x {0}".format(client)])
except Exception, err:
return([2, err])
if __name__ == "__main__":
yamlfile = yaml.load(open("/etc/locaweb/monitoring/unk.yaml"))
options = yamlfile.get("monitoring")[-1]
status = __run__(options)
print status