diff --git a/Script/CastorScript.py b/Script/CastorScript.py index 929340352fc91e5428f1dce561bb47f04938e9f9..592475ee04e1a71af1a359314787b20b42656e6e 100644 --- a/Script/CastorScript.py +++ b/Script/CastorScript.py @@ -357,68 +357,38 @@ 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' - - # 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, + klist = subprocess.Popen(['klist',], env = envm, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - - #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 + ret = klist.wait() + out = klist.stdout.read() + logger.debug('klist done. Return code: %d Output: %s' % (ret,out)) - # 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 + if ret: + logger.critical('klist failed: retcode=%s, output=%s', str(ret), out) + return datetime.datetime(1970, 1, 1) - # 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 - output = output.split('\n') - for idx,l in enumerate(output): - #found the line with "Valid" in it.. + out = out.split('\n') + for idx,l in enumerate(out): if 'Valid' in l: - #and we get the line after! - ticket = output[idx+1] + ticket = out[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', output) - return expirationDateTime + logger.critical('error parsing klist output: output=%s', out) + return datetime.datetime(1970, 1, 1) - # 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: - expirationDateTime = datetime.datetime.strptime(dateString, '%m/%d/%y %H:%M:%S') + expiration_date = datetime.datetime.strptime(' '.join(ticket[2:4]), + '%m/%d/%y %H:%M:%S') except ValueError as exc: logger.critical('error converting string to date: ticket="{}", date_string="{}"', - ticket, dateString) + ticket, ' '.join(ticket[2:4])) raise exc - return expirationDateTime + return expiration_date + if __name__ == '__main__': main(config)