cur = conn.cursor()
try:
runsql = "select ha_groupid,primary_ip from ha_config"
cur.execute(runsql)
res = cur.fetchone()
while res :
t = CMonitorHost(res[0], res[1], True, "db01", g_hauser, g_hapass)
t.start()
res = cur.fetchone()
runsql = "select ha_groupid,standby_ip from ha_config"
cur.execute(runsql)
res = cur.fetchone()
while res :
t = CMonitorHost(res[0], res[1], False, "db01", g_hauser, g_hapass)
t.start()
res = cur.fetchone()
# 檢查是否要切換
while g_running:
try:
runsql = "SELECT ha_groupid, extract(epoch from (now() - standby_hearttime)) shtime from ha_config where extract(epoch from (now() - primary_hearttime)) > %d and current_hostid=primary_hostid" % (g_switch_timedelay)
cur.execute(runsql)
res = cur.fetchone()
while res :
if res[1] < g_switch_timedelay:
errlog("ERROR: ha_groupid(%d) switch to standby database!" % (res[0]))
cur2 = conn.cursor()
runsql = "update ha_config set current_hostid=standby_hostid where ha_groupid=%s" % (res[0])
cur2.execute("update ha_config set current_hostid=standby_hostid where ha_groupid=%s", (res[0],))
cur2.close()
else:
errlog("ERROR: ha_groupid(%d) primary and standby all failed!!!" % (res[0]))
res = cur.fetchone()
runsql = "SELECT ha_groupid, extract(epoch from (now() - primary_hearttime)) shtime from ha_config where extract(epoch from (now() - standby_hearttime)) > %d and current_hostid=standby_hostid" % (g_switch_timedelay)
cur.execute(runsql)
res = cur.fetchone()
while res :
if res[1] < g_switch_timedelay:
errlog("ERROR: ha_groupid: %d switch to primary database!" % (res[0]))
cur2 = conn.cursor()
runsql = "update ha_config set current_hostid=primary_hostid where ha_groupid=%s" % (res[0])
cur2.execute("update ha_config set current_hostid=primary_hostid where ha_groupid=%s", (res[0],))
cur2.close()
else:
errlog("ERROR: ha_groupid: %d primary and standby all failed!!!" % (res[0]))
res = cur.fetchone()