File: //lib/python2.7/site-packages/lap/xen_ha.py
import socket
import subprocess
try:
from xenapi import XenAPI
except:
import XenAPI
def __run__(params):
xapi = XenAPI.xapi_local()
xapi.login_with_password("", "")
hostname = socket.gethostname()
host_ref = xapi.xenapi.host.get_by_name_label(hostname)
if len(host_ref) > 1:
xapi.xenapi.session.logout()
return [3, "UNKNOWN: Hostname duplicated"]
host = xapi.xenapi.host.get_record(host_ref[0])
if host["license_params"]["enable_xha"].lower() == "false":
xapi.xenapi.session.logout()
return [0, "OK: License Free doesn't have HA"]
pool_ref = xapi.xenapi.pool.get_all()
if len(pool_ref) > 2:
xapi.xenapi.session.logout()
return [3, "UNKNOWN: Host belongs a two or more pools"]
pool = xapi.xenapi.pool.get_record(pool_ref[0])
if not pool["ha_enabled"]:
xapi.xenapi.session.logout()
return [0, "OK: Pool HA Disabled"]
ha_timeout = int(params.get("timeout", 120))
pool_ha_timeout = int(pool["ha_configuration"].get("timeout", 0))
if pool_ha_timeout < ha_timeout:
xapi.xenapi.session.logout()
return [1, "WARNING: Pool HA Wrong Timeout (Timeout=%ss)" % pool_ha_timeout]
sr_space = params.get("sr_space", 50)
sr_space = int(sr_space) * 1024 * 1024
force_disable = params.get("force_disable", "false")
vdi_statefile = xapi.xenapi.VDI.get_record(pool["ha_statefiles"][0])
sr_statefile = xapi.xenapi.SR.get_record(vdi_statefile["SR"])["name_label"]
host_master = xapi.xenapi.host.get_record(pool["master"])["name_label"]
disks_space = subprocess.Popen(["df", "-P"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
disks_space.wait()
disks = disks_space.communicate()
if disks_space.returncode != 0:
xapi.xenapi.session.logout()
return [3, "UNKNOWN: %s"] % disks[1]
avail_space = None
for line in disks[0].splitlines():
if sr_statefile.lower() in line.lower():
avail_space = int(line.split()[3])
break
if avail_space < sr_space:
if force_disable.lower() in ("true", "yes", "y") and hostname.lower() == host_master.lower:
ha_disable = subprocess.Popen(["/usr/sbin/ha", "disable"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
ha_disable.wait()
ha = ha_disable.communicate()
xapi.xenapi.session.logout()
if ha_disable.returncode != 0:
return [2, "CRITICAL: Failed to disable HA (%s)"] % ha[1]
else:
xapi.xenapi.session.logout()
return [2, "CRITICAL: SR Statefile available: %s < %s, Turn off the HA!"] % (avail_space, sr_space)
xapi.xenapi.session.logout()
return [1, "WARNING: HA Enabled"]