#!/usr/bin/env python
#
# Replication FAILOVER_ACTION script
#
# (c) Rocket Software 2014 - All Rights Reserved
# Disclaimer of Warranties. Rocket Software disclaims to the fullest extent authorized by law any and all other warranties, whether express or implied, including, without limitation, any implied warranties
# of  merchantability or fitness for a particular purpose. Without limitation of the foregoing, Rocket Software expressly does not warrant that:
# (a) the software will meet your requirements [or expectations];
# (b) the software or the software content] will be free of bugs, errors, viruses or other defects;
# (c) any results, output, or data provided through or generated by the software will be accurate, up-to-date, complete or reliable;
# (d) the software will be compatible with third party software;
# (e) any errors in the software will be corrected.
#
#   1.0 YSHENG 11/05/14 First Proposed Customer Release
#
####################################################################################################################################

import os
import sys
import socket
import time
import re

# For testing purpose
#os.environ["U2REP_FAILOVER_TIMESTAMP"] = "1416867702"

# Function to return uvhome path
def getuvhome():
	file = open("/.uvhome", "r")
	result = file.read().strip()
	return result

# Script Version
failover_script_version = "1.0"

# Check UVHOME & UVBIN
uvhome = os.getenv("UVHOME")

if uvhome is None:
	uvhome = getuvhome()

uvbin = os.getenv("UVBIN")

if uvbin is None:
	uvbin = uvhome + "/bin"

# Show failover time
dstring = time.strftime("%c")
reportfilename = uvhome + "/RepFailover.log"
scriptrunningname = uvhome + "/RepFailover.running"

if os.path.exists(scriptrunningname):
	print scriptrunningname + " found script already running - exiting"
	exit(5)

# open&update script running file
sr_file = open(scriptrunningname, "wr")
line = "Run Started at : " + dstring + "\n"
sr_file.write(line)
sr_file.close()

# open&update failover log file
fol_file = open(reportfilename, "wr")
#
line = "UniVerse Replication Failover called at: " + dstring + "\n"
fol_file.write(line)
#
line = "Script Version is: " + failover_script_version + "\n"
fol_file.write(line)
#
machinename = socket.gethostname()
line = "Run on machine " + machinename + "\n"
fol_file.write(line)
#

# Show environment variables passed from replication system
line = "U2REP_FAILOVER_TIMESTAMP=" + os.getenv("U2REP_FAILOVER_TIMESTAMP") + "\n"
fol_file.write(line)

# go to uvhome to run queries
os.chdir(uvhome)

# query REP_FAILOVER_LOG in order to get tids
cmd = 'bin/uvsh "SELECT TID FROM REP_FAILOVER_LOG WHERE TIMESTAMP=\'' + os.getenv("U2REP_FAILOVER_TIMESTAMP") + '\' ORDER BY TID COL.SUP COUNT.SUP;"'
tid_list = []
q_result = os.popen(cmd)
lines = q_result.readlines()
for line in lines:
	try:
		tid = int(line)
		if tid not in tid_list:
			tid_list.append(tid)
	except:
		pass

# using tids to query against TRANS_COMMIT_LOG for missing updates
for tid in tid_list:
	sub_cmd = 'bin/uvsh "SELECT B.FNAME, B.ISDICT, B.OP, B.RECID FROM TRANS_COMMIT_LOG A, TRANS_COMMIT_LOG_TP_UPD B WHERE A.@ID=B.@ID AND A.TID=\'' + str(tid) + '\' ORDER BY B.FNAME COL.SUP COUNT.SUP;"'
	subq_result = os.popen(sub_cmd)
	subq_lines = subq_result.readlines()

header = "\nMissing Updates:\nFNAME         ISDICT   OP          RECID\n"
fol_file.write(header)

for subq_line in subq_lines:
	tmp = subq_line.strip()
	if re.match('^[\w._]+', tmp) is not None:
		fol_file.write(tmp + '\n')

# final cleanup before done
fol_file.close()
os.remove(scriptrunningname)

#!/usr/bin/env python
#
# Replication FAILOVER_ACTION script
#
# (c) Rocket Software 2014 - All Rights Reserved
# Disclaimer of Warranties. Rocket Software disclaims to the fullest extent authorized by law any and all other warranties, whether express or implied, including, without limitation, any implied warranties
# of  merchantability or fitness for a particular purpose. Without limitation of the foregoing, Rocket Software expressly does not warrant that:
# (a) the software will meet your requirements [or expectations];
# (b) the software or the software content] will be free of bugs, errors, viruses or other defects;
# (c) any results, output, or data provided through or generated by the software will be accurate, up-to-date, complete or reliable;
# (d) the software will be compatible with third party software;
# (e) any errors in the software will be corrected.
#
#   1.0 YSHENG 11/05/14 First Proposed Customer Release
#
####################################################################################################################################

import os
import sys
import socket
import time
import re

# For testing purpose
#os.environ["U2REP_FAILOVER_TIMESTAMP"] = "1416867702"

# Function to return uvhome path
def getuvhome():
	file = open("/.uvhome", "r")
	result = file.read().strip()
	return result

# Script Version
failover_script_version = "1.0"

# Check UVHOME & UVBIN
uvhome = os.getenv("UVHOME")

if uvhome is None:
	uvhome = getuvhome()

uvbin = os.getenv("UVBIN")

if uvbin is None:
	uvbin = uvhome + "/bin"

# Show failover time
dstring = time.strftime("%c")
reportfilename = uvhome + "/RepFailover.log"
scriptrunningname = uvhome + "/RepFailover.running"

if os.path.exists(scriptrunningname):
	print scriptrunningname + " found script already running - exiting"
	exit(5)

# open&update script running file
sr_file = open(scriptrunningname, "wr")
line = "Run Started at : " + dstring + "\n"
sr_file.write(line)
sr_file.close()

# open&update failover log file
fol_file = open(reportfilename, "wr")
#
line = "UniVerse Replication Failover called at: " + dstring + "\n"
fol_file.write(line)
#
line = "Script Version is: " + failover_script_version + "\n"
fol_file.write(line)
#
machinename = socket.gethostname()
line = "Run on machine " + machinename + "\n"
fol_file.write(line)
#

# Show environment variables passed from replication system
line = "U2REP_FAILOVER_TIMESTAMP=" + os.getenv("U2REP_FAILOVER_TIMESTAMP") + "\n"
fol_file.write(line)

# go to uvhome to run queries
os.chdir(uvhome)

# query REP_FAILOVER_LOG in order to get tids
cmd = 'bin/uvsh "SELECT TID FROM REP_FAILOVER_LOG WHERE TIMESTAMP=\'' + os.getenv("U2REP_FAILOVER_TIMESTAMP") + '\' ORDER BY TID COL.SUP COUNT.SUP;"'
tid_list = []
q_result = os.popen(cmd)
lines = q_result.readlines()
for line in lines:
	try:
		tid = int(line)
		if tid not in tid_list:
			tid_list.append(tid)
	except:
		pass

# using tids to query against TRANS_COMMIT_LOG for missing updates
for tid in tid_list:
	sub_cmd = 'bin/uvsh "SELECT B.FNAME, B.ISDICT, B.OP, B.RECID FROM TRANS_COMMIT_LOG A, TRANS_COMMIT_LOG_TP_UPD B WHERE A.@ID=B.@ID AND A.TID=\'' + str(tid) + '\' ORDER BY B.FNAME COL.SUP COUNT.SUP;"'
	subq_result = os.popen(sub_cmd)
	subq_lines = subq_result.readlines()

header = "\nMissing Updates:\nFNAME         ISDICT   OP          RECID\n"
fol_file.write(header)

for subq_line in subq_lines:
	tmp = subq_line.strip()
	if re.match('^[\w._]+', tmp) is not None:
		fol_file.write(tmp + '\n')

# final cleanup before done
fol_file.close()
os.remove(scriptrunningname)

#!/usr/bin/env python # # Replication FAILOVER_ACTION script # # (c) Rocket Software 2014 - All Rights Reserved # Disclaimer of Warranties. Rocket Software disclaims to the fullest extent authorized by law any and all other warranties, whether express or implied, including, without limitation, any implied warranties # of  merchantability or fitness for a particular purpose. Without limitation of the foregoing, Rocket Software expressly does not warrant that: # (a) the software will meet your requirements [or expectations]; # (b) the software or the software content] will be free of bugs, errors, viruses or other defects; # (c) any results, output, or data provided through or generated by the software will be accurate, up-to-date, complete or reliable; # (d) the software will be compatible with third party software; # (e) any errors in the software will be corrected. # #   1.0 YSHENG 11/05/14 First Proposed Customer Release # #################################################################################################################################### # For testing purpose #os.environ["U2REP_FAILOVER_TIMESTAMP"] = "1416867702" # Function to return uvhome path # Script Version # Check UVHOME & UVBIN # Show failover time # open&update script running file # open&update failover log file # # # # # Show environment variables passed from replication system # go to uvhome to run queries # query REP_FAILOVER_LOG in order to get tids # using tids to query against TRANS_COMMIT_LOG for missing updates # final cleanup before done