def export_getDownTimesWeb(self, selectDict, sortList, startItem, maxItems): """ get down times as registered with the policies. Calls :meth:`DIRAC.ResourceStatusSystem.DB.ResourceStatusDB.ResourceStatusDB.getDownTimesWeb` :Parameters: `selectDict` { 'Granularity':'Site', 'Resource', or a list with both 'Severity':'OUTAGE', 'AT_RISK', or a list with both } `sortList` [] (no sorting provided) `startItem` `maxItems` :return: { 'OK': XX, 'rpcStub': XX, 'getDownTimesWeb', ({}, [], X, X)), Value': { 'ParameterNames': ['Granularity', 'Name', 'Severity', 'When'], 'Records': [[], [], ...] 'TotalRecords': X, 'Extras': {}, } } """ try: gLogger.info("ResourceManagementHandler.getDownTimesWeb: Attempting to get down times list") try: try: granularity = selectDict['Granularity'] except KeyError: granularity = [] if not isinstance(granularity, list): granularity = [granularity] commands = [] if granularity == []: commands = ['DTEverySites', 'DTEveryResources'] elif 'Site' in granularity: commands.append('DTEverySites') elif 'Resource' in granularity: commands.append('DTEveryResources') try: severity = selectDict['Severity'] except KeyError: severity = [] if not isinstance(severity, list): severity = [severity] if severity == []: severity = ['AT_RISK', 'OUTAGE'] res = rmDB.getClientsCacheStuff(['Name', 'Opt_ID', 'Value', 'Result', 'CommandName'], commandName = commands) records = [] if not ( res == () ): made_IDs = [] for dt_tuple in res: considered_ID = dt_tuple[1] if considered_ID not in made_IDs: name = dt_tuple[0] if dt_tuple[4] == 'DTEverySites': granularity = 'Site' elif dt_tuple[4] == 'DTEveryResources': granularity = 'Resource' toTake = ['Severity', 'StartDate', 'EndDate', 'Description'] for dt_t in res: if considered_ID == dt_t[1]: if toTake != []: if dt_t[2] in toTake: if dt_t[2] == 'Severity': sev = dt_t[3] toTake.remove('Severity') if dt_t[2] == 'StartDate': startDate = dt_t[3] toTake.remove('StartDate') if dt_t[2] == 'EndDate': endDate = dt_t[3] toTake.remove('EndDate') if dt_t[2] == 'Description': description = dt_t[3] toTake.remove('Description') now = datetime.datetime.utcnow().replace(microsecond = 0, second = 0) startDate_datetime = datetime.datetime.strptime(startDate, '%Y-%m-%d %H:%M') endDate_datetime = datetime.datetime.strptime(endDate, '%Y-%m-%d %H:%M') if endDate_datetime < now: when = 'Finished' else: if startDate_datetime < now: when = 'OnGoing' else: hours = str(convertTime(startDate_datetime - now, 'hours')) when = 'In ' + hours + ' hours.' if sev in severity: records.append([ considered_ID, granularity, name, sev, when, startDate, endDate, description ]) made_IDs.append(considered_ID) # adding downtime links to the GOC DB page in Extras DT_links = [] for record in records: DT_link = rmDB.getClientsCacheStuff(['Result'], opt_ID = record[0], value = 'Link') DT_link = DT_link[0][0] DT_links.append({ record[0] : DT_link } ) paramNames = ['ID', 'Granularity', 'Name', 'Severity', 'When', 'Start', 'End', 'Description'] finalDict = {} finalDict['TotalRecords'] = len(records) finalDict['ParameterNames'] = paramNames # Return all the records if maxItems == 0 or the specified number otherwise if maxItems: finalDict['Records'] = records[startItem:startItem+maxItems] else: finalDict['Records'] = records finalDict['Extras'] = DT_links except RSSDBException, x: gLogger.error(whoRaised(x)) except RSSException, x: gLogger.error(whoRaised(x))
def doCommand(self): """ Returns DT info that are cached. :attr:`args`: - args[0]: string: should be a ValidRes - args[1]: string should be the name of the ValidRes - args[2]: string: optional, number of hours in which the down time is starting """ super(DTInfo_Cached_Command, self).doCommand() granularity = self.args[0] name = self.args[1] if self.client is None: from DIRAC.ResourceStatusSystem.Client.ResourceManagementClient import ResourceManagementClient self.client = ResourceManagementClient(timeout=self.timeout) now = datetime.datetime.utcnow().replace(microsecond=0, second=0) try: if granularity in ('Site', 'Sites'): commandName = 'DTEverySites' elif self.args[0] in ('Resource', 'Resources'): commandName = 'DTEveryResources' res = self.client.getCachedIDs(name, commandName) if res['OK']: res = res['Value'] else: res = [] if len(res) == 0: return {'Result': {'DT': None}} if len(res) > 1: #there's more than one DT dt_ID_startingSoon = res[0] startSTR_startingSoon = self.client.getCachedResult( name, commandName, 'StartDate', dt_ID_startingSoon)['Value'][0] start_datetime_startingSoon = datetime.datetime(*time.strptime( startSTR_startingSoon, "%Y-%m-%d %H:%M")[0:5]) endSTR_startingSoon = self.client.getCachedResult( name, commandName, 'EndDate', dt_ID_startingSoon)['Value'][0] end_datetime_startingSoon = datetime.datetime( *time.strptime(endSTR_startingSoon, "%Y-%m-%d %H:%M")[0:5]) if start_datetime_startingSoon < now: if end_datetime_startingSoon > now: #ongoing downtime found! DT_ID = dt_ID_startingSoon try: DT_ID except: for dt_ID in res[1:]: #looking for an ongoing one startSTR = self.client.getCachedResult( name, commandName, 'StartDate', dt_ID)['Value'][0] start_datetime = datetime.datetime( *time.strptime(startSTR, "%Y-%m-%d %H:%M")[0:5]) endSTR = self.client.getCachedResult( name, commandName, 'EndDate', dt_ID)['Value'][0] end_datetime = datetime.datetime( *time.strptime(endSTR, "%Y-%m-%d %H:%M")[0:5]) if start_datetime < now: if end_datetime > now: #ongoing downtime found! DT_ID = dt_ID break if start_datetime < start_datetime_startingSoon: #the DT starts before the former considered one dt_ID_startingSoon = dt_ID try: DT_ID except: #if I'm here, there's no OnGoing DT DT_ID = dt_ID_startingSoon else: DT_ID = res[0] DT_dict_result = {} endSTR = self.client.getCachedResult(name, commandName, 'EndDate', DT_ID)['Value'][0] end_datetime = datetime.datetime( *time.strptime(endSTR, "%Y-%m-%d %H:%M")[0:5]) if end_datetime < now: return {'Result': {'DT': None}} DT_dict_result['EndDate'] = endSTR DT_dict_result['DT'] = self.client.getCachedResult( name, commandName, 'Severity', DT_ID)['Value'][0] DT_dict_result['StartDate'] = self.client.getCachedResult( name, commandName, 'StartDate', DT_ID)['Value'][0] DT_dict_result['Description'] = self.client.getCachedResult( name, commandName, 'Description', DT_ID)['Value'][0] DT_dict_result['Link'] = self.client.getCachedResult( name, commandName, 'Link', DT_ID)['Value'][0] startSTR = self.client.getCachedResult(name, commandName, 'StartDate', DT_ID)['Value'][0] start_datetime = datetime.datetime( *time.strptime(startSTR, "%Y-%m-%d %H:%M")[0:5]) if start_datetime > now: try: self.args[2] diff = convertTime(start_datetime - now, 'hours') if diff > self.args[2]: return {'Result': {'DT': None}} DT_dict_result['DT'] = DT_dict_result['DT'] + " in " + str( diff) + ' hours' except: # Searching only for onGoing DT, got future ones return {'Result': {'DT': None}} return {'Result': DT_dict_result} except urllib2.URLError: gLogger.error("GOCDB timed out for " + self.args[0] + " " + self.args[1]) return {'Result': 'Unknown'} except: gLogger.exception("Exception when calling GOCDBClient for " + self.args[0] + " " + self.args[1]) return {'Result': 'Unknown'}
def export_getDownTimesWeb(self, selectDict, sortList, startItem, maxItems): """ get down times as registered with the policies. Calls :meth:`DIRAC.ResourceStatusSystem.DB.ResourceStatusDB.ResourceStatusDB.getDownTimesWeb` :Parameters: `selectDict` { 'Granularity':'Site', 'Resource', or a list with both 'Severity':'OUTAGE', 'AT_RISK', or a list with both } `sortList` [] (no sorting provided) `startItem` `maxItems` :return: { 'OK': XX, 'rpcStub': XX, 'getDownTimesWeb', ({}, [], X, X)), Value': { 'ParameterNames': ['Granularity', 'Name', 'Severity', 'When'], 'Records': [[], [], ...] 'TotalRecords': X, 'Extras': {}, } } """ try: gLogger.info( "ResourceManagementHandler.getDownTimesWeb: Attempting to get down times list" ) try: try: granularity = selectDict['Granularity'] except KeyError: granularity = [] if not isinstance(granularity, list): granularity = [granularity] commands = [] if granularity == []: commands = ['DTEverySites', 'DTEveryResources'] elif 'Site' in granularity: commands.append('DTEverySites') elif 'Resource' in granularity: commands.append('DTEveryResources') try: severity = selectDict['Severity'] except KeyError: severity = [] if not isinstance(severity, list): severity = [severity] if severity == []: severity = ['AT_RISK', 'OUTAGE'] res = rmDB.getClientsCacheStuff( ['Name', 'Opt_ID', 'Value', 'Result', 'CommandName'], commandName=commands) records = [] if not (res == ()): made_IDs = [] for dt_tuple in res: considered_ID = dt_tuple[1] if considered_ID not in made_IDs: name = dt_tuple[0] if dt_tuple[4] == 'DTEverySites': granularity = 'Site' elif dt_tuple[4] == 'DTEveryResources': granularity = 'Resource' toTake = [ 'Severity', 'StartDate', 'EndDate', 'Description' ] for dt_t in res: if considered_ID == dt_t[1]: if toTake != []: if dt_t[2] in toTake: if dt_t[2] == 'Severity': sev = dt_t[3] toTake.remove('Severity') if dt_t[2] == 'StartDate': startDate = dt_t[3] toTake.remove('StartDate') if dt_t[2] == 'EndDate': endDate = dt_t[3] toTake.remove('EndDate') if dt_t[2] == 'Description': description = dt_t[3] toTake.remove('Description') now = datetime.datetime.utcnow().replace( microsecond=0, second=0) startDate_datetime = datetime.datetime.strptime( startDate, '%Y-%m-%d %H:%M') endDate_datetime = datetime.datetime.strptime( endDate, '%Y-%m-%d %H:%M') if endDate_datetime < now: when = 'Finished' else: if startDate_datetime < now: when = 'OnGoing' else: hours = str( convertTime(startDate_datetime - now, 'hours')) when = 'In ' + hours + ' hours.' if sev in severity: records.append([ considered_ID, granularity, name, sev, when, startDate, endDate, description ]) made_IDs.append(considered_ID) # adding downtime links to the GOC DB page in Extras DT_links = [] for record in records: DT_link = rmDB.getClientsCacheStuff(['Result'], opt_ID=record[0], value='Link') DT_link = DT_link[0][0] DT_links.append({record[0]: DT_link}) paramNames = [ 'ID', 'Granularity', 'Name', 'Severity', 'When', 'Start', 'End', 'Description' ] finalDict = {} finalDict['TotalRecords'] = len(records) finalDict['ParameterNames'] = paramNames # Return all the records if maxItems == 0 or the specified number otherwise if maxItems: finalDict['Records'] = records[startItem:startItem + maxItems] else: finalDict['Records'] = records finalDict['Extras'] = DT_links except RSSDBException, x: gLogger.error(whoRaised(x)) except RSSException, x: gLogger.error(whoRaised(x))
def doCommand(self): """ Returns DT info that are cached. :attr:`args`: - args[0]: string: should be a ValidElement - args[1]: string should be the name of the ValidElement - args[2]: string: optional, number of hours in which the down time is starting """ timeFormat = "%Y-%m-%d %H:%M" super(DTInfo_Cached_Command, self).doCommand() self.APIs = initAPIs( self.__APIs__, self.APIs ) try: granularity = self.args[0] name = self.args[1] now = datetime.utcnow().replace( microsecond = 0, second = 0 ) if granularity == 'Site': commandName = 'DTEverySites' elif granularity == 'Resource': commandName = 'DTEveryResources' meta = { 'columns': 'opt_ID' } res = self.APIs[ 'ResourceManagementClient' ].getClientCache( name = name, commandName = commandName, meta = meta ) if not res[ 'OK' ]: return { 'Result' : res } res = res[ 'Value' ] #CachedResult clientDict = { 'name' : name, 'commandName' : commandName, 'value' : None, 'opt_ID' : None, 'meta' : { 'columns' : 'Result' } } if len(res) > 1: #there's more than one DT dt_ID_startingSoon = res[0] clientDict[ 'value' ] = 'StartDate' clientDict[ 'optID' ] = dt_ID_startingSoon clientDict[ 'meta' ] = { 'columns' : 'Result' } startSTR_startingSoon = self.APIs[ 'ResourceManagementClient' ].getClientCache( **clientDict )[ 'Value' ] if startSTR_startingSoon: startSTR_startingSoon = startSTR_startingSoon[0][0] clientDict[ 'value' ] = 'EndDate' clientDict[ 'optID' ] = dt_ID_startingSoon clientDict[ 'meta' ] = { 'columns' : 'Result' } endSTR_startingSoon = self.APIs[ 'ResourceManagementClient' ].getClientCache( **clientDict )[ 'Value' ] if endSTR_startingSoon: endSTR_startingSoon = endSTR_startingSoon[0][0] start_datetime_startingSoon = datetime.strptime(startSTR_startingSoon, timeFormat ) end_datetime_startingSoon = datetime.strptime(endSTR_startingSoon, timeFormat ) DT_ID = None if start_datetime_startingSoon < now: if end_datetime_startingSoon > now: #ongoing downtime found! DT_ID = dt_ID_startingSoon if DT_ID is None: for dt_ID in res[1:]: #looking for an ongoing one clientDict[ 'value' ] = 'StartDate' clientDict[ 'optID' ] = dt_ID clientDict[ 'meta' ] = { 'columns' : 'Result' } startSTR = self.APIs[ 'ResourceManagementClient' ].getClientCache( **clientDict )[ 'Value' ] if startSTR: startSTR = startSTR[0][0] clientDict[ 'value' ] = 'EndDate' clientDict[ 'optID' ] = dt_ID clientDict[ 'meta' ] = { 'columns' : 'Result' } endSTR = self.APIs[ 'ResourceManagementClient' ].getClientCache( **clientDict )[ 'Value' ] if endSTR: endSTR = endSTR[0][0] start_datetime = datetime.strptime( startSTR, timeFormat ) end_datetime = datetime.strptime( endSTR, timeFormat ) if start_datetime < now: if end_datetime > now: #ongoing downtime found! DT_ID = dt_ID break if start_datetime < start_datetime_startingSoon: #the DT starts before the former considered one dt_ID_startingSoon = dt_ID if DT_ID is None: #if I'm here, there's no OnGoing DT DT_ID = dt_ID_startingSoon else: DT_ID = res[0] DT_dict_result = {} clientDict[ 'value' ] = 'EndDate' clientDict[ 'optID' ] = DT_ID clientDict[ 'meta' ] = { 'columns' : 'Result' } endSTR = self.APIs[ 'ResourceManagementClient' ].getClientCache( **clientDict )[ 'Value' ] if endSTR: endSTR = endSTR[0][0] end_datetime = datetime.strptime( endSTR, timeFormat ) if end_datetime < now: return { 'Result' : S_OK( { 'DT' : None } ) } DT_dict_result['EndDate'] = endSTR clientDict[ 'value' ] = 'Severity' clientDict[ 'optID' ] = DT_ID clientDict[ 'meta' ] = { 'columns' : 'Result' } DT_dict_result['DT'] = self.APIs[ 'ResourceManagementClient' ].getClientCache( **clientDict )[ 'Value' ] if DT_dict_result['DT']: DT_dict_result['DT'] = DT_dict_result['DT'][0][0] clientDict[ 'value' ] = 'StartDate' clientDict[ 'optID' ] = DT_ID clientDict[ 'meta' ] = { 'columns' : 'Result' } DT_dict_result['StartDate'] = self.APIs[ 'ResourceManagementClient' ].getClientCache( **clientDict )[ 'Value' ] if DT_dict_result['StartDate']: DT_dict_result['StartDate'] = DT_dict_result['StartDate'][0][0] clientDict[ 'value' ] = 'Description' clientDict[ 'optID' ] = DT_ID clientDict[ 'meta' ] = { 'columns' : 'Result' } DT_dict_result['Description'] = self.APIs[ 'ResourceManagementClient' ].getClientCache( **clientDict )[ 'Value' ] if DT_dict_result['Description']: DT_dict_result['Description'] = DT_dict_result['Description'][0][0] clientDict[ 'value' ] = 'Link' clientDict[ 'optID' ] = DT_ID clientDict[ 'meta' ] = { 'columns' : 'Result' } DT_dict_result['Link'] = self.APIs[ 'ResourceManagementClient' ].getClientCache( **clientDict )[ 'Value' ] if DT_dict_result['Link']: DT_dict_result['Link'] = DT_dict_result['Link'][0][0] start_datetime = datetime.strptime( DT_dict_result['StartDate'], timeFormat ) if start_datetime > now: self.args[2] diff = convertTime(start_datetime - now, 'hours') if diff > self.args[2]: return { 'Result': S_OK( { 'DT' : None } ) } DT_dict_result['DT'] = DT_dict_result['DT'] + " in " + str(diff) + ' hours' res = S_OK( DT_dict_result ) except Exception, e: _msg = '%s (%s): %s' % ( self.__class__.__name__, self.args, e ) gLogger.exception( _msg ) return { 'Result' : S_ERROR( _msg ) }
def doCommand(self): """ Return getStatus from GOC DB Client :attr:`args`: - args[0]: string: should be a ValidRes - args[1]: string: should be the name of the ValidRes - args[2]: string: optional, number of hours in which the down time is starting """ super(GOCDBStatus_Command, self).doCommand() if self.client is None: # use standard GOC DB Client from DIRAC.Core.LCG.GOCDBClient import GOCDBClient self.client = GOCDBClient() granularity = self.args[0] name = self.args[1] try: hours = self.args[2] except IndexError: hours = None if granularity in ('Site', 'Sites'): name = getGOCSiteName(name) if not name['OK']: raise RSSException, name['Message'] name = name['Value'] try: res = self.client.getStatus(granularity, name, None, hours, self.timeout) if not res['OK']: return {'Result': 'Unknown'} res = res['Value'] if res is None or res == {}: return {'Result': {'DT': None}} DT_dict_result = {} now = datetime.datetime.utcnow().replace(microsecond=0, second=0) if len(res) > 1: #there's more than one DT for dt_ID in res: #looking for an ongoing one startSTR = res[dt_ID]['FORMATED_START_DATE'] start_datetime = datetime.datetime( *time.strptime(startSTR, "%Y-%m-%d %H:%M")[0:5]) if start_datetime < now: resDT = res[dt_ID] break try: resDT except: #if I'm here, there's no OnGoing DT resDT = res[res.keys()[0]] res = resDT else: res = res[res.keys()[0]] DT_dict_result['DT'] = res['SEVERITY'] DT_dict_result['EndDate'] = res['FORMATED_END_DATE'] startSTR = res['FORMATED_START_DATE'] start_datetime = datetime.datetime( *time.strptime(startSTR, "%Y-%m-%d %H:%M")[0:5]) if start_datetime > now: diff = convertTime(start_datetime - now, 'hours') DT_dict_result['DT'] = DT_dict_result['DT'] + " in " + str( diff) + ' hours' return {'Result': DT_dict_result} except urllib2.URLError: gLogger.error("GOCDB timed out for " + granularity + " " + name) return {'Result': 'Unknown'} except: gLogger.exception("Exception when calling GOCDBClient for " + granularity + " " + name) return {'Result': 'Unknown'}
def doCommand(self): """ Return getStatus from GOC DB Client :attr:`args`: - args[0]: string: should be a ValidElement - args[1]: string: should be the name of the ValidElement - args[2]: string: optional, number of hours in which the down time is starting """ timeFormat = "%Y-%m-%d %H:%M" super(GOCDBStatus_Command, self).doCommand() self.APIs = initAPIs( self.__APIs__, self.APIs ) try: granularity = self.args[0] name = self.args[1] if len( self.args ) > 2: hours = self.args[2] else: hours = None if granularity == 'Site': name = getGOCSiteName( name )[ 'Value' ] res = self.APIs[ 'GOCDBClient' ].getStatus( granularity, name, None, hours ) if not res['OK']: return { 'Result' : res } res = res['Value'] if res is None or res == {}: return { 'Result' : S_OK( { 'DT' : None } ) } DT_dict_result = {} now = datetime.utcnow().replace( microsecond = 0, second = 0 ) if len( res ) > 1: #there's more than one DT resDT = None for dt_ID in res: #looking for an ongoing one startSTR = res[ dt_ID ][ 'FORMATED_START_DATE' ] start_datetime = datetime.strptime( startSTR, timeFormat ) if start_datetime < now: resDT = res[ dt_ID ] break #if I'm here, there's no OnGoing DT if resDT is None: resDT = res[res.keys()[0]] res = resDT else: res = res[res.keys()[0]] DT_dict_result['DT'] = res['SEVERITY'] DT_dict_result['EndDate'] = res['FORMATED_END_DATE'] startSTR = res['FORMATED_START_DATE'] start_datetime = datetime.strptime( startSTR, timeFormat ) if start_datetime > now: diff = convertTime( start_datetime - now, 'hours' ) DT_dict_result[ 'DT' ] = DT_dict_result['DT'] + " in " + str( diff ) + ' hours' res = S_OK( DT_dict_result ) except Exception, e: _msg = '%s (%s): %s' % ( self.__class__.__name__, self.args, e ) gLogger.exception( _msg ) return { 'Result' : S_ERROR( _msg ) }
def doCommand(self): """ Returns DT info that are cached. :attr:`args`: - args[0]: string: should be a ValidElement - args[1]: string should be the name of the ValidElement - args[2]: string: optional, number of hours in which the down time is starting """ timeFormat = "%Y-%m-%d %H:%M" super(DTInfo_Cached_Command, self).doCommand() self.APIs = initAPIs(self.__APIs__, self.APIs) try: granularity = self.args[0] name = self.args[1] now = datetime.utcnow().replace(microsecond=0, second=0) if granularity == 'Site': commandName = 'DTEverySites' elif granularity == 'Resource': commandName = 'DTEveryResources' meta = {'columns': 'opt_ID'} res = self.APIs['ResourceManagementClient'].getClientCache( name=name, commandName=commandName, meta=meta) if not res['OK']: return {'Result': res} res = res['Value'] #CachedResult clientDict = { 'name': name, 'commandName': commandName, 'value': None, 'opt_ID': None, 'meta': { 'columns': 'Result' } } if len(res) > 1: #there's more than one DT dt_ID_startingSoon = res[0] clientDict['value'] = 'StartDate' clientDict['optID'] = dt_ID_startingSoon clientDict['meta'] = {'columns': 'Result'} startSTR_startingSoon = self.APIs[ 'ResourceManagementClient'].getClientCache( **clientDict)['Value'] if startSTR_startingSoon: startSTR_startingSoon = startSTR_startingSoon[0][0] clientDict['value'] = 'EndDate' clientDict['optID'] = dt_ID_startingSoon clientDict['meta'] = {'columns': 'Result'} endSTR_startingSoon = self.APIs[ 'ResourceManagementClient'].getClientCache( **clientDict)['Value'] if endSTR_startingSoon: endSTR_startingSoon = endSTR_startingSoon[0][0] start_datetime_startingSoon = datetime.strptime( startSTR_startingSoon, timeFormat) end_datetime_startingSoon = datetime.strptime( endSTR_startingSoon, timeFormat) DT_ID = None if start_datetime_startingSoon < now: if end_datetime_startingSoon > now: #ongoing downtime found! DT_ID = dt_ID_startingSoon if DT_ID is None: for dt_ID in res[1:]: #looking for an ongoing one clientDict['value'] = 'StartDate' clientDict['optID'] = dt_ID clientDict['meta'] = {'columns': 'Result'} startSTR = self.APIs[ 'ResourceManagementClient'].getClientCache( **clientDict)['Value'] if startSTR: startSTR = startSTR[0][0] clientDict['value'] = 'EndDate' clientDict['optID'] = dt_ID clientDict['meta'] = {'columns': 'Result'} endSTR = self.APIs[ 'ResourceManagementClient'].getClientCache( **clientDict)['Value'] if endSTR: endSTR = endSTR[0][0] start_datetime = datetime.strptime( startSTR, timeFormat) end_datetime = datetime.strptime(endSTR, timeFormat) if start_datetime < now: if end_datetime > now: #ongoing downtime found! DT_ID = dt_ID break if start_datetime < start_datetime_startingSoon: #the DT starts before the former considered one dt_ID_startingSoon = dt_ID if DT_ID is None: #if I'm here, there's no OnGoing DT DT_ID = dt_ID_startingSoon else: DT_ID = res[0] DT_dict_result = {} clientDict['value'] = 'EndDate' clientDict['optID'] = DT_ID clientDict['meta'] = {'columns': 'Result'} endSTR = self.APIs['ResourceManagementClient'].getClientCache( **clientDict)['Value'] if endSTR: endSTR = endSTR[0][0] end_datetime = datetime.strptime(endSTR, timeFormat) if end_datetime < now: return {'Result': S_OK({'DT': None})} DT_dict_result['EndDate'] = endSTR clientDict['value'] = 'Severity' clientDict['optID'] = DT_ID clientDict['meta'] = {'columns': 'Result'} DT_dict_result['DT'] = self.APIs[ 'ResourceManagementClient'].getClientCache( **clientDict)['Value'] if DT_dict_result['DT']: DT_dict_result['DT'] = DT_dict_result['DT'][0][0] clientDict['value'] = 'StartDate' clientDict['optID'] = DT_ID clientDict['meta'] = {'columns': 'Result'} DT_dict_result['StartDate'] = self.APIs[ 'ResourceManagementClient'].getClientCache( **clientDict)['Value'] if DT_dict_result['StartDate']: DT_dict_result['StartDate'] = DT_dict_result['StartDate'][0][0] clientDict['value'] = 'Description' clientDict['optID'] = DT_ID clientDict['meta'] = {'columns': 'Result'} DT_dict_result['Description'] = self.APIs[ 'ResourceManagementClient'].getClientCache( **clientDict)['Value'] if DT_dict_result['Description']: DT_dict_result['Description'] = DT_dict_result['Description'][ 0][0] clientDict['value'] = 'Link' clientDict['optID'] = DT_ID clientDict['meta'] = {'columns': 'Result'} DT_dict_result['Link'] = self.APIs[ 'ResourceManagementClient'].getClientCache( **clientDict)['Value'] if DT_dict_result['Link']: DT_dict_result['Link'] = DT_dict_result['Link'][0][0] start_datetime = datetime.strptime(DT_dict_result['StartDate'], timeFormat) if start_datetime > now: self.args[2] diff = convertTime(start_datetime - now, 'hours') if diff > self.args[2]: return {'Result': S_OK({'DT': None})} DT_dict_result['DT'] = DT_dict_result['DT'] + " in " + str( diff) + ' hours' res = S_OK(DT_dict_result) except Exception, e: _msg = '%s (%s): %s' % (self.__class__.__name__, self.args, e) gLogger.exception(_msg) return {'Result': S_ERROR(_msg)}
def doCommand(self): """ Return getStatus from GOC DB Client :attr:`args`: - args[0]: string: should be a ValidElement - args[1]: string: should be the name of the ValidElement - args[2]: string: optional, number of hours in which the down time is starting """ timeFormat = "%Y-%m-%d %H:%M" super(GOCDBStatus_Command, self).doCommand() self.APIs = initAPIs(self.__APIs__, self.APIs) try: granularity = self.args[0] name = self.args[1] if len(self.args) > 2: hours = self.args[2] else: hours = None if granularity == 'Site': name = getGOCSiteName(name)['Value'] res = self.APIs['GOCDBClient'].getStatus(granularity, name, None, hours) if not res['OK']: return {'Result': res} res = res['Value'] if res is None or res == {}: return {'Result': S_OK({'DT': None})} DT_dict_result = {} now = datetime.utcnow().replace(microsecond=0, second=0) if len(res) > 1: #there's more than one DT resDT = None for dt_ID in res: #looking for an ongoing one startSTR = res[dt_ID]['FORMATED_START_DATE'] start_datetime = datetime.strptime(startSTR, timeFormat) if start_datetime < now: resDT = res[dt_ID] break #if I'm here, there's no OnGoing DT if resDT is None: resDT = res[res.keys()[0]] res = resDT else: res = res[res.keys()[0]] DT_dict_result['DT'] = res['SEVERITY'] DT_dict_result['EndDate'] = res['FORMATED_END_DATE'] startSTR = res['FORMATED_START_DATE'] start_datetime = datetime.strptime(startSTR, timeFormat) if start_datetime > now: diff = convertTime(start_datetime - now, 'hours') DT_dict_result['DT'] = DT_dict_result['DT'] + " in " + str( diff) + ' hours' res = S_OK(DT_dict_result) except Exception, e: _msg = '%s (%s): %s' % (self.__class__.__name__, self.args, e) gLogger.exception(_msg) return {'Result': S_ERROR(_msg)}
def doCommand(self): """ Returns DT info that are cached. :attr:`args`: - args[0]: string: should be a ValidRes - args[1]: string should be the name of the ValidRes - args[2]: string: optional, number of hours in which the down time is starting """ super(DTInfo_Cached_Command, self).doCommand() granularity = self.args[0] name = self.args[1] if self.client is None: from DIRAC.ResourceStatusSystem.Client.ResourceManagementClient import ResourceManagementClient self.client = ResourceManagementClient( timeout = self.timeout ) now = datetime.datetime.utcnow().replace( microsecond = 0, second = 0 ) try: if granularity in ('Site', 'Sites'): commandName = 'DTEverySites' elif self.args[0] in ('Resource', 'Resources'): commandName = 'DTEveryResources' res = self.client.getCachedIDs( name, commandName ) if res[ 'OK' ]: res = res[ 'Value' ] else: res = [] if len(res) == 0: return {'Result':{'DT':None}} if len(res) > 1: #there's more than one DT dt_ID_startingSoon = res[0] startSTR_startingSoon = self.client.getCachedResult(name, commandName, 'StartDate', dt_ID_startingSoon)[ 'Value' ][0] start_datetime_startingSoon = datetime.datetime( *time.strptime(startSTR_startingSoon, "%Y-%m-%d %H:%M")[0:5] ) endSTR_startingSoon = self.client.getCachedResult(name, commandName, 'EndDate', dt_ID_startingSoon)[ 'Value' ][0] end_datetime_startingSoon = datetime.datetime( *time.strptime(endSTR_startingSoon, "%Y-%m-%d %H:%M")[0:5] ) if start_datetime_startingSoon < now: if end_datetime_startingSoon > now: #ongoing downtime found! DT_ID = dt_ID_startingSoon try: DT_ID except: for dt_ID in res[1:]: #looking for an ongoing one startSTR = self.client.getCachedResult(name, commandName, 'StartDate', dt_ID)['Value'][0] start_datetime = datetime.datetime( *time.strptime(startSTR, "%Y-%m-%d %H:%M")[0:5] ) endSTR = self.client.getCachedResult(name, commandName, 'EndDate', dt_ID)['Value'][0] end_datetime = datetime.datetime( *time.strptime(endSTR, "%Y-%m-%d %H:%M")[0:5] ) if start_datetime < now: if end_datetime > now: #ongoing downtime found! DT_ID = dt_ID break if start_datetime < start_datetime_startingSoon: #the DT starts before the former considered one dt_ID_startingSoon = dt_ID try: DT_ID except: #if I'm here, there's no OnGoing DT DT_ID = dt_ID_startingSoon else: DT_ID = res[0] DT_dict_result = {} endSTR = self.client.getCachedResult(name, commandName, 'EndDate', DT_ID)['Value'][0] end_datetime = datetime.datetime( *time.strptime(endSTR, "%Y-%m-%d %H:%M")[0:5] ) if end_datetime < now: return {'Result': {'DT':None}} DT_dict_result['EndDate'] = endSTR DT_dict_result['DT'] = self.client.getCachedResult(name, commandName, 'Severity', DT_ID)['Value'][0] DT_dict_result['StartDate'] = self.client.getCachedResult(name, commandName, 'StartDate', DT_ID)['Value'][0] DT_dict_result['Description'] = self.client.getCachedResult(name, commandName, 'Description', DT_ID)['Value'][0] DT_dict_result['Link'] = self.client.getCachedResult(name, commandName, 'Link', DT_ID)['Value'][0] startSTR = self.client.getCachedResult(name, commandName, 'StartDate', DT_ID)['Value'][0] start_datetime = datetime.datetime( *time.strptime(startSTR, "%Y-%m-%d %H:%M")[0:5] ) if start_datetime > now: try: self.args[2] diff = convertTime(start_datetime - now, 'hours') if diff > self.args[2]: return {'Result': {'DT':None}} DT_dict_result['DT'] = DT_dict_result['DT'] + " in " + str(diff) + ' hours' except: # Searching only for onGoing DT, got future ones return {'Result': {'DT':None}} return {'Result':DT_dict_result} except urllib2.URLError: gLogger.error("GOCDB timed out for " + self.args[0] + " " + self.args[1] ) return {'Result':'Unknown'} except: gLogger.exception("Exception when calling GOCDBClient for " + self.args[0] + " " + self.args[1] ) return {'Result':'Unknown'}
def doCommand(self): """ Return getStatus from GOC DB Client :attr:`args`: - args[0]: string: should be a ValidRes - args[1]: string: should be the name of the ValidRes - args[2]: string: optional, number of hours in which the down time is starting """ super(GOCDBStatus_Command, self).doCommand() if self.client is None: # use standard GOC DB Client from DIRAC.Core.LCG.GOCDBClient import GOCDBClient self.client = GOCDBClient() granularity = self.args[0] name = self.args[1] try: hours = self.args[2] except IndexError: hours = None if granularity in ('Site', 'Sites'): name = getGOCSiteName(name) if not name['OK']: raise RSSException, name['Message'] name = name['Value'] try: res = self.client.getStatus(granularity, name, None, hours, self.timeout) if not res['OK']: return {'Result':'Unknown'} res = res['Value'] if res is None or res == {}: return {'Result':{'DT':None}} DT_dict_result = {} now = datetime.datetime.utcnow().replace(microsecond = 0, second = 0) if len(res) > 1: #there's more than one DT for dt_ID in res: #looking for an ongoing one startSTR = res[dt_ID]['FORMATED_START_DATE'] start_datetime = datetime.datetime( *time.strptime(startSTR, "%Y-%m-%d %H:%M")[0:5] ) if start_datetime < now: resDT = res[dt_ID] break try: resDT except: #if I'm here, there's no OnGoing DT resDT = res[res.keys()[0]] res = resDT else: res = res[res.keys()[0]] DT_dict_result['DT'] = res['SEVERITY'] DT_dict_result['EndDate'] = res['FORMATED_END_DATE'] startSTR = res['FORMATED_START_DATE'] start_datetime = datetime.datetime( *time.strptime(startSTR, "%Y-%m-%d %H:%M")[0:5] ) if start_datetime > now: diff = convertTime(start_datetime - now, 'hours') DT_dict_result['DT'] = DT_dict_result['DT'] + " in " + str(diff) + ' hours' return {'Result':DT_dict_result} except urllib2.URLError: gLogger.error("GOCDB timed out for " + granularity + " " + name ) return {'Result':'Unknown'} except: gLogger.exception("Exception when calling GOCDBClient for " + granularity + " " + name ) return {'Result':'Unknown'}