#!/usr/bin/env python
import sys
import csha

csha.log.debug('HA ha_query_liveset')

try:
    liveness = {}
    ismaster = {}
    uuid = {}
    liveset = ""

    sf = csha.Statefile()
    myhostuuid = csha.util.get_current_host_uuid()

    master = sf.get_master()
    master_uuid = master.uuid

    pool_state_invalid = sf.get_invalid()
    if pool_state_invalid:
        csha.util.call(
            "HA", ["/usr/libexec/xapi/cluster-stack/corosync/ha_stop_daemon"])
        sys.exit(14)

    hosts = sf.read_all_hosts()
    nodeids = []

    for nodeid in hosts.keys():
        host_uuid = hosts[nodeid].uuid
        if host_uuid:
            nodeids.append(nodeid)
            uuid[nodeid] = host_uuid
            liveness[nodeid] = "FALSE"
            if master_uuid == host_uuid:
                ismaster[nodeid] = "TRUE"
            else:
                ismaster[nodeid] = "FALSE"

    dlm_output = csha.util.call("HA", ["dlm_tool", "status"])
    dlm_output_lines = dlm_output.splitlines()

    for line in dlm_output_lines:
        if line.startswith("node "):
            parts = line.split(" ")
            if parts[2] == 'M':
                hostid = int(parts[1])
                if hosts[hostid].online:
                    liveness[hostid] = "TRUE"
                    liveset += (" " + uuid[hostid])
            elif parts[2] == 'X':
                hostid = int(parts[1])
                if hosts[hostid].online:
                    liveness[hostid] = "FALSE"

except Exception:
    csha.log.error("HA ha_query_liveset error", exc_info=True)
    print "HA pool state invalid"
    sys.exit(0)

out = """<?xml version="1.0" encoding="UTF-8"?>
<ha_liveset_info version="1.0">
  <status>Online</status>
  <localhost>
    <HostID>%s</HostID>
  </localhost>""" % myhostuuid

for nodeid in nodeids:
    out += """
  <host>
    <HostID>%s</HostID>
    <liveness>%s</liveness>
    <master>%s</master>
    <statefile_access>%s</statefile_access>
    <statefile_corrupted>FALSE</statefile_corrupted>
    <excluded>FALSE</excluded>
  </host>""" % (uuid[nodeid], liveness[nodeid],
                ismaster[nodeid], liveness[nodeid])

out += """
  <raw_status_on_local_host>
    <statefile_latency>1</statefile_latency>
    <statefile_latency_max>1</statefile_latency_max>
    <statefile_latency_min>0</statefile_latency_min>
    <heartbeat_latency>7006</heartbeat_latency>
    <heartbeat_latency_max>7006</heartbeat_latency_max>
    <heartbeat_latency_min>7006</heartbeat_latency_min>
    <Xapi_healthcheck_latency>27</Xapi_healthcheck_latency>
    <Xapi_healthcheck_latency_max>-1</Xapi_healthcheck_latency_max>
    <Xapi_healthcheck_latency_min>-1</Xapi_healthcheck_latency_min>"""

for nodeid in nodeids:
    if liveness[nodeid] == "TRUE":
        out += """
    <host_raw_data>
      <HostID>%s</HostID>
      <time_since_last_update_on_statefile>7304</time_since_last_update_on_statefile>
      <time_since_last_heartbeat>3839</time_since_last_heartbeat>
      <time_since_xapi_restart_first_attempted>-1</time_since_xapi_restart_first_attempted>
      <xapi_error_string></xapi_error_string>
      <heartbeat_active_list_on_heartbeat>
          %s
      </heartbeat_active_list_on_heartbeat>
      <heartbeat_active_list_on_statefile>
          %s
      </heartbeat_active_list_on_statefile>
      <statefile_active_list_on_heartbeat>
          %s
      </statefile_active_list_on_heartbeat>
      <statefile_active_list_on_statefile>
          %s
      </statefile_active_list_on_statefile>
    </host_raw_data>""" % (uuid[nodeid], liveset, liveset, liveset, liveset)
    else:
        out += """
    <host_raw_data>
      <HostID>%s</HostID>
      <time_since_last_update_on_statefile>7304</time_since_last_update_on_statefile>
      <time_since_last_heartbeat>3839</time_since_last_heartbeat>
      <time_since_xapi_restart_first_attempted>-1</time_since_xapi_restart_first_attempted>
      <xapi_error_string></xapi_error_string>
      <heartbeat_active_list_on_heartbeat>
      </heartbeat_active_list_on_heartbeat>
      <heartbeat_active_list_on_statefile>
      </heartbeat_active_list_on_statefile>
      <statefile_active_list_on_heartbeat>
      </statefile_active_list_on_heartbeat>
      <statefile_active_list_on_statefile>
      </statefile_active_list_on_statefile>
    </host_raw_data>""" % (uuid[nodeid])

out += """
  </raw_status_on_local_host>
  <timeout>
    <T1>60000</T1>
    <T2>60000</T2>
    <T3>120000</T3>
    <Wh>60000</Wh>
    <Ws>75000</Ws>
  </timeout>
  <warning_on_local_host>
    <statefile_lost>FALSE</statefile_lost>
    <heartbeat_approaching_timeout>FALSE</heartbeat_approaching_timeout>
    <statefile_approaching_timeout>FALSE</statefile_approaching_timeout>
    <Xapi_healthcheck_approaching_timeout>FALSE</Xapi_healthcheck_approaching_timeout>
    <network_bonding_error>FALSE</network_bonding_error>
  </warning_on_local_host>
</ha_liveset_info>"""

print out
