diff --git a/Script/CastorScript.py b/Script/CastorScript.py index fa4101133421fb7266aabe1944b39f1dbfbed5dc..2b1bd3d81e05b45d45d34132e73e7f2d6ce6728c 100644 --- a/Script/CastorScript.py +++ b/Script/CastorScript.py @@ -363,38 +363,68 @@ def krb_tokenexpiration(logger): # So it's safer to always set LC_ALL to a 'default' constant envm = os.environ.copy() envm['LC_ALL'] = 'C' - klist = subprocess.Popen(['klist',], env = envm, stdout=subprocess.PIPE, + + # Define klist_output and returncode for the klist process + # (klist, lists kerberos tokens and their expiration date) + output = None + returnCode = None + + #set a default expiration date to the start of UNIX time + #This is a way to signal that something went wrong + expirationDateTime = datetime.datetime(1970, 1, 1) + + #try to run the klist process (kerberos list) + #catch the exception if it fails and return datetime(1970,1,1) + try: + klist = subprocess.Popen(['klist',], env = envm, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - ret = klist.wait() - out = klist.stdout.read() - logger.debug('klist done. Return code: %d Output: %s' % (ret,out)) + + #we wait for the process to finish + returnCode = klist.wait() + output = klist.stdout.read() + + logger.debug('klist done. Return code: %d Output: %s' % (returnCode,output)) + except Exception as ex: + logger.critical('klist failed: Return code=%s, Output=%s, with exception= %s', str(returnCode), output, str(ex)) + return expirationDateTime - if ret: - logger.critical('klist failed: retcode=%s, output=%s', str(ret), out) - return datetime.datetime(1970, 1, 1) + # if the returncode from the klist process is not 0, something went wrong + if returnCode != 0 or returnCode is None: + logger.critical('klist failed: Return code=%s, Output=%s', str(returnCode), output) + return expirationDateTime + # From the output of klist, we extract the line with the token expiration date + # it is the next line after a line with "Valid" in it. ticket = None - out = out.split('\n') - for idx,l in enumerate(out): + output = output.split('\n') + for idx,l in enumerate(output): + #found the line with "Valid" in it.. if 'Valid' in l: - ticket = out[idx+1] + #and we get the line after! + ticket = output[idx+1] break - + + # If we didn't find a line, we return datetime(1970, 1, 1) if ticket is None: - logger.critical('error parsing klist output: output=%s', out) - return datetime.datetime(1970, 1, 1) + logger.critical('error parsing klist output: output=%s', output) + return expirationDateTime + # we found the line with the date ticket = ticket.split() logger.debug('Ticket expiration: %s' % ticket) + + # and extract only the date into a string + dateString = ' '.join(ticket[2:4]) + + # try to pass it as a datetime object, catch exception + # (but raise it afterwards anyhow, to crash the script (for the .watchdog.sh)) try: - expiration_date = datetime.datetime.strptime(' '.join(ticket[2:4]), - '%m/%d/%y %H:%M:%S') + expirationDateTime = datetime.datetime.strptime(dateString, '%m/%d/%y %H:%M:%S') except ValueError as exc: logger.critical('error converting string to date: ticket="{}", date_string="{}"', - ticket, ' '.join(ticket[2:4])) + ticket, dateString) raise exc - return expiration_date - + return expirationDateTime if __name__ == '__main__': main(config)