#!/usr/bin/env python
# -*- coding: utf8 -*-
"""
diagnose du rvp
"""

from commands import getstatusoutput
from os.path import isfile
from creole import config
from creole.client import CreoleClient
from zephir.monitor.agents.rvp import parse_ipsec_statusall


dico_eole = CreoleClient().get_creole()
install_rvp = dico_eole['install_rvp']
zephir_client_noaction = dico_eole.get('zephir_client_noaction')
statusall_err, ret = getstatusoutput("ipsec statusall")
tunnels, total, up, down, idle_threads = parse_ipsec_statusall(zephir_client_noaction, ret)
cmd = [". /usr/lib/eole/diagnose.sh"]
cmd.append('EchoGras "*** Réseau virtuel privé"')

# test du RVP si configuré
if install_rvp == "oui":
    if dico_eole.get('eole_module') == 'sphynx':
        cmd_line = "ip route | grep default | grep -q src"
        err, ret_route = getstatusoutput(cmd_line)
        if err == 0 or dico_eole['activer_haute_dispo'] == 'non':
            cmd.append('printf ".  %${len_pf}s =>" "VPN"')
            if statusall_err == 0:
                if "0 up, 0 connecting" in ret:
                    cmd.append('EchoOrange " Aucun tunnel actif"')
                else:
                    cmd.append('EchoVert " Ok"')
            else:
                cmd.append('EchoRouge " Erreur"')
            cmd.append('TestService ARV $(CreoleGet adresse_ip_eth0):$(CreoleGet arv_port)')
        else:
            cmd.append('printf ".  %${len_pf}s =>" ""')
            cmd.append('EchoBleu "  Node inactif"')
    else:
        TEST_RVP = "/usr/share/eole/test-rvp"
        if isfile(TEST_RVP):
            if statusall_err == 0:
                if "0 up, 0 connecting" in ret:
                    cmd.append('printf ".  %${len_pf}s =>" "RVP"')
                    cmd.append('EchoOrange " Aucun tunnel actif"')
                else:
                    for tunnel in tunnels:
                        if tunnel['child_name'] != '':
                            tested_ip = tunnel['dst'].split('--')
                            if len(tested_ip) > 1:
                                for ip in tested_ip:
                                    if "injoignable" in ip:
                                        cmd_line = 'printf ".  %${len_pf}s => " "RVP '
                                        cmd_line += str(ip.split('injoignable')[0].strip())\
                                                    +'"'+'\nEchoRouge " Erreur"'
                                        if cmd_line not in cmd:
                                            cmd.append(cmd_line)
                                    elif "joignable" in ip:
                                        cmd_line = 'printf ".  %${len_pf}s => " "RVP '
                                        cmd_line += str(ip.split('joignable')[0].strip())\
                                                    +'"'+'\nEchoVert " OK"'
                                        if cmd_line not in cmd:
                                            cmd.append(cmd_line)
            else:
                cmd.append('printf ".  %${len_pf}s => " "RVP"')
                cmd.append('EchoRouge " Erreur"')
        else:
            cmd.append('NoConfig "RVP"')
        cmd.append('echo')
else:
    cmd.append('Inactif "RVP"')
cmd_string = "\n".join(cmd)
err, ret = getstatusoutput(cmd_string)
print ret
exit(0)
