To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit c77178ac authored by Roman Trüb's avatar Roman Trüb

updated slot updater script: removed use of ip_address and added msg if no change detected

parent 13d313ba
......@@ -32,13 +32,15 @@ debug = False
class UpdateSlotAssignThread(threading.Thread):
def __init__(self, observerdata, config, logger, searchtime, maxretries, queue):
threading.Thread.__init__(self)
self.Observerdata = observerdata
self.Config = config
self.Logger = logger
self.Searchtime = searchtime
self.Maxretries = maxretries
self.Queue = queue
self.ObsKey = observerdata[0]
self.ObsHostname = observerdata[1]
self.ObsSerialList = observerdata[2:]
self.Config = config
self.Logger = logger
self.Searchtime = searchtime
self.Maxretries = maxretries
self.Queue = queue
def run(self):
# Get list of ID's for every slot from observer over SSH:
cmd = "tg_serialid.py"
......@@ -46,40 +48,38 @@ class UpdateSlotAssignThread(threading.Thread):
cmd = "%s -s%.1f" %(cmd, self.Searchtime)
if self.Maxretries:
cmd = "%s -m%d" %(cmd, self.Maxretries)
self.Logger.debug("Observer %s: calling %s" %(self.Observerdata[1], cmd))
p = subprocess.Popen(['ssh', 'root@%s' % (self.Observerdata[2]), cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.Logger.debug("Observer %s: calling %s" %(self.ObsHostname, cmd))
p = subprocess.Popen(['ssh', 'root@%s' % (self.ObsHostname), cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
rs = p.communicate()
self.Logger.debug("Observer %s: got response: %s" % (self.Observerdata[1], str(rs)))
self.Logger.debug("Observer %s: got response: %s" % (self.ObsHostname, str(rs)))
# Compare list against values stored on database if ssh command was successful
if (rs[1] != ''):
self.Logger.debug("Observer %s: returned error: %s" % (self.Observerdata[1], str(rs[1])))
self.Logger.debug("Observer %s: returned error: %s" % (self.ObsHostname, str(rs[1])))
slots = rs[0].split('\n')
if ((rs[1] == '') and (len(slots) > 1)):
cmds = []
changes = []
i = 1
for slot in slots[0:4]:
for i, slot in enumerate(slots[0:4]):
s = slot.split(' ')
slotnr = s[0][0]
serialid = s[1]
# If a serial ID was found and if it differs in the database and on the observer, update the database:
if serialid == 'not':
serialid = None
if (serialid != self.Observerdata[2 + i]):
msg = "Observer %s: serial IDs for slot %s differ. Value database: %s, value observer slot: %s" % (self.Observerdata[1], slotnr, self.Observerdata[2 + i], serialid)
if (serialid != self.ObsSerialList[i]):
msg = "Observer %s: serial IDs for slot %s differ. Value database: %s, value observer slot: %s" % (self.ObsHostname, slotnr, self.ObsSerialList[i], serialid)
self.Logger.debug(msg)
changes.append((self.Observerdata[1], slotnr, self.Observerdata[2 + i], serialid))
changes.append((self.ObsHostname, slotnr, self.ObsSerialList[i], serialid))
cmds.append(""" UPDATE `tbl_serv_observer`
SET slot_%s_tg_adapt_list_fk = (
SELECT `serv_tg_adapt_list_key`
FROM `tbl_serv_tg_adapt_list`
WHERE `serialid` = '%s')
SELECT `serv_tg_adapt_list_key`
FROM `tbl_serv_tg_adapt_list`
WHERE `serialid` = '%s')
WHERE `serv_observer_key` = %s;
""" % (i, serialid, self.Observerdata[0]))
i = i + 1
""" % (i+1, serialid, self.ObsKey))
# If any changes need to be done to the database, do so:
if len(cmds) > 0:
try:
......@@ -89,17 +89,17 @@ class UpdateSlotAssignThread(threading.Thread):
raise
try:
for cmd in cmds:
self.Logger.debug("Observer %s: executing SQL: %s" % (self.Observerdata[1], cmd))
self.Logger.debug("Observer %s: executing SQL: %s" % (self.ObsHostname, cmd))
cur.execute(cmd)
cn.commit()
# Finally prepare message to send to admins about the change(s):
msg = ""
sql = """ SELECT `name`
FROM `tbl_serv_tg_adapt_types`
sql = """ SELECT `name`
FROM `tbl_serv_tg_adapt_types`
WHERE `serv_tg_adapt_types_key` = (
SELECT `tg_adapt_types_fk`
FROM `tbl_serv_tg_adapt_list`
SELECT `tg_adapt_types_fk`
FROM `tbl_serv_tg_adapt_list`
WHERE `serialid` = '%s');
"""
for change in changes:
......@@ -111,7 +111,7 @@ class UpdateSlotAssignThread(threading.Thread):
else:
cmd = sql % (change[2])
cur.execute(cmd)
self.Logger.debug("Observer %s: executing SQL: %s" % (self.Observerdata[1], cmd))
self.Logger.debug("Observer %s: executing SQL: %s" % (self.ObsHostname, cmd))
rs = cur.fetchone()
if rs:
old = rs[0]
......@@ -121,7 +121,7 @@ class UpdateSlotAssignThread(threading.Thread):
else:
cmd = sql % (change[3])
cur.execute(cmd)
self.Logger.debug("Observer %s: executing SQL: %s" % (self.Observerdata[1], cmd))
self.Logger.debug("Observer %s: executing SQL: %s" % (self.ObsHostname, cmd))
rs = cur.fetchone()
if rs:
new = rs[0]
......@@ -133,7 +133,7 @@ class UpdateSlotAssignThread(threading.Thread):
else:
msg = msg + "Observer %s: serial IDs for slot %s differ. Old adapter according to database was %s (%s) but detected %s (%s) in slot. Database has been updated accordingly.\n" % (str(change[0]), str(change[1]), str(old), str(change[2]), str(new), str(change[3]))
self.Queue.put(msg)
except MySQLdb.Error, err:
self.Logger.warn(str(err))
sys.exit(errno.EIO)
......@@ -142,7 +142,10 @@ class UpdateSlotAssignThread(threading.Thread):
finally:
cur.close()
cn.close()
else:
msg = "Observer %s: No change detected!\n" % (self.ObsHostname)
self.Queue.put(msg)
return(SUCCESS)
### END UpdateSlotAssignThread
......@@ -156,7 +159,7 @@ class UpdateSlotAssignThread(threading.Thread):
def usage():
print "Usage: %s [--searchtime <float>] [--maxretries <int>] [--debug] [--help] [--version] [--obs <id>] [--develop]" % sys.argv[0]
print "Options:"
print " --searchtime\t\t\tOptional. If set, standard time for waiting for the ID search is overwritten."
print " --searchtime\t\t\tOptional. If set, standard time for waiting for the ID search is overwritten."
print " --maxretries\t\t\tOptional. If set, standard number of retries for reading an ID is overwritten."
print " --debug\t\t\tPrint debug messages to log."
print " --observer\t\t\tUpdate only observer with ID <id>."
......@@ -173,7 +176,7 @@ def usage():
#
##############################################################################
def main(argv):
### Get global variables ###
global debug
threadlist = []
......@@ -182,7 +185,7 @@ def main(argv):
force = False
observer = ""
status = "'online', 'develop'"
# Set timezone to UTC:
os.environ['TZ'] = 'UTC'
time.tzset()
......@@ -191,7 +194,7 @@ def main(argv):
logger = flocklab.get_logger(loggername="flocklab_upd_slot_assign.py", loggerpath=scriptpath)
# Get command line parameters.
try:
try:
opts, args = getopt.getopt(argv, "hvds:m:fo:d", ["help", "version", "debug", "searchtime", "maxretries", "force", "observer", "develop"])
except getopt.GetoptError, err:
print str(err)
......@@ -245,13 +248,13 @@ def main(argv):
logger.warn("Wrong API usage")
usage()
sys.exit(errno.EINVAL)
# Get the config file:
config = flocklab.get_config(configpath=scriptpath)
if not config:
logger.warn("Could not read configuration file. Exiting...")
sys.exit(errno.EAGAIN)
# Check if a test is preparing, running or cleaning up. If yes, exit program.
try:
(cn, cur) = flocklab.connect_to_db(config, logger)
......@@ -268,15 +271,15 @@ def main(argv):
# Get all active observers from the database:
logger.debug("Going to fetch current database status for active observers...")
try:
sql = """ SELECT a.serv_observer_key, a.ethernet_address, a.ip_address, b.serialid AS serialid_1, c.serialid AS serialid_2, d.serialid AS serialid_3, e.serialid AS serialid_4
FROM `tbl_serv_observer` AS a
LEFT JOIN `tbl_serv_tg_adapt_list` AS b
sql = """ SELECT a.serv_observer_key, a.ethernet_address, b.serialid AS serialid_1, c.serialid AS serialid_2, d.serialid AS serialid_3, e.serialid AS serialid_4
FROM `tbl_serv_observer` AS a
LEFT JOIN `tbl_serv_tg_adapt_list` AS b
ON a.slot_1_tg_adapt_list_fk = b.serv_tg_adapt_list_key
LEFT JOIN `tbl_serv_tg_adapt_list` AS c
LEFT JOIN `tbl_serv_tg_adapt_list` AS c
ON a.slot_2_tg_adapt_list_fk = c.serv_tg_adapt_list_key
LEFT JOIN `tbl_serv_tg_adapt_list` AS d
LEFT JOIN `tbl_serv_tg_adapt_list` AS d
ON a.slot_3_tg_adapt_list_fk = d.serv_tg_adapt_list_key
LEFT JOIN `tbl_serv_tg_adapt_list` AS e
LEFT JOIN `tbl_serv_tg_adapt_list` AS e
ON a.slot_4_tg_adapt_list_fk = e.serv_tg_adapt_list_key
WHERE a.status IN (%s) %s
""" % (status, observer)
......@@ -291,15 +294,15 @@ def main(argv):
cn.close()
# Prepare queue which is going to hold the messages returned from the threads:
q = Queue.Queue()
# Start one update thread per observer:
for observer in rs:
logger.debug("Starting thread for %s" % (observer[1]))
# Start one update thread per observer:
for observerdata in rs:
logger.debug("Starting thread for %s" % (observerdata[1]))
try:
t = UpdateSlotAssignThread(observer, config, logger, searchtime, maxretries, q)
t = UpdateSlotAssignThread(observerdata, config, logger, searchtime, maxretries, q)
threadlist.append(t)
t.start()
except:
logger.warn("Error when starting thread for observer %s: %s: %s" % (observer[1], str(sys.exc_info()[0]), str(sys.exc_info()[1])))
logger.warn("Error when starting thread for observer %s: %s: %s" % (observerdata[1], str(sys.exc_info()[0]), str(sys.exc_info()[1])))
continue
# Wait for threads to finish:
logger.debug("Joining threads")
......@@ -332,10 +335,10 @@ def main(argv):
cn.close()
except:
logger.warn("Error when sending change notifications to admin. Line %s: %s: %s" % (str(traceback.tb_lineno(sys.exc_info()[2])), str(sys.exc_info()[0]), str(sys.exc_info()[1])))
logger.debug("Slot assignment updater finished.")
sys.exit(SUCCESS)
### END main()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment