def __backupCurrentConfiguration(self, backupName): configurationFilename = "%s.cfg" % self.getName() configurationFile = os.path.join(DIRAC.rootPath, "etc", configurationFilename) today = Time.date() backupPath = os.path.join(self.getBackupDir(), str(today.year), "%02d" % today.month) mkDir(backupPath) backupFile = os.path.join( backupPath, configurationFilename.replace(".cfg", ".%s.zip" % backupName)) if os.path.isfile(configurationFile): gLogger.info("Making a backup of configuration in %s" % backupFile) try: with zipfile.ZipFile(backupFile, "w", zipfile.ZIP_DEFLATED) as zf: zf.write( configurationFile, "%s.backup.%s" % (os.path.split(configurationFile)[1], backupName)) except Exception: gLogger.exception() gLogger.error("Cannot backup configuration data file", "file %s" % backupFile) else: gLogger.warn("CS data file does not exist", configurationFile)
def __backupCurrentConfiguration( self, backupName ): configurationFilename = "%s.cfg" % self.getName() configurationFile = os.path.join( DIRAC.rootPath, "etc", configurationFilename ) today = Time.date() backupPath = os.path.join( self.getBackupDir(), str( today.year ), "%02d" % today.month ) mkDir(backupPath) backupFile = os.path.join( backupPath, configurationFilename.replace( ".cfg", ".%s.zip" % backupName ) ) if os.path.isfile( configurationFile ): gLogger.info( "Making a backup of configuration in %s" % backupFile ) try: with zipfile.ZipFile( backupFile, "w", zipfile.ZIP_DEFLATED ) as zf: zf.write( configurationFile, "%s.backup.%s" % ( os.path.split( configurationFile )[1], backupName ) ) except Exception: gLogger.exception() gLogger.error( "Cannot backup configuration data file", "file %s" % backupFile ) else: gLogger.warn( "CS data file does not exist", configurationFile )
def __getMessages( self, selectionDict = {}, sortList = [], startItem = 0, maxItems = 0 ): """ """ from re import search if selectionDict.has_key('convertDates'): convertDatesToStrings = selectionDict['convertDates'] del selectionDict['convertDates'] else: convertDatesToStrings = True if convertDatesToStrings: dateField = "DATE_FORMAT(MessageTime, '%Y-%m-%d %H:%i:%s')" else: dateField = 'MessageTime' if selectionDict.has_key('count'): countMessages = selectionDict['count'] del selectionDict['count'] else: countMessages = True if selectionDict.has_key( 'beginDate' ): beginDate = selectionDict['beginDate'] del selectionDict['beginDate'] else: beginDate = None if selectionDict.has_key( 'endDate' ): endDate = selectionDict['endDate'] del selectionDict['endDate'] else: endDate = None if not ( beginDate or endDate ): beginDate= Time.date() - 1 * Time.day if selectionDict.has_key('groupField'): groupField = selectionDict['groupField'] if not selectionDict.has_key( groupField ): groupField = selectionDict.keys()[0] del selectionDict['groupField'] elif countMessages: if selectionDict: groupField = selectionDict.keys()[0] elif sortList: groupField = sortList[0][0] else: groupField = 'FixedTextString' else: groupField = None if selectionDict: fieldList = selectionDict.keys() fieldList.append( dateField ) if not ( selectionDict.has_key( 'LogLevel' ) and selectionDict['LogLevel'] ): selectionDict['LogLevel'] = [ 'ERROR', 'EXCEPT', 'FATAL' ] else: fieldList = [ dateField, 'LogLevel', 'FixedTextString', 'VariableText', 'SystemName', 'SubSystemName', 'OwnerDN', 'OwnerGroup', 'ClientIPNumberString', 'SiteName' ] selectionDict['LogLevel'] = [ 'ERROR', 'EXCEPT', 'FATAL' ] result = LogDB._queryDB( showFieldList = fieldList, condDict = selectionDict, older = endDate, newer = beginDate, count = countMessages, groupColumn = groupField, orderFields = sortList ) if not result['OK']: return result if maxItems: records = result['Value'][ startItem:maxItems + startItem ] else: records = result['Value'][ startItem: ] if not sortList: unOrderedFields = [ ( s[-1], s ) for s in records ] unOrderedFields.sort() records = [ t[1] for t in unOrderedFields ] records.reverse() if countMessages: if 'count(*) as recordCount' in fieldList: fieldList.remove( 'count(*) as recordCount' ) fieldList.append( 'Number of Errors' ) if convertDatesToStrings: for element in fieldList: if search( 'MessageTime',element ): index = fieldList.index( element ) fieldList[index] = 'MessageTime' retValue = { 'ParameterNames': fieldList, 'Records': records , 'TotalRecords': len( result['Value'] ), 'Extras': {}} return S_OK( retValue )
def __getMessages(self, selectionDict={}, sortList=[], startItem=0, maxItems=0): """ """ from re import search if 'convertDates' in selectionDict: convertDatesToStrings = selectionDict['convertDates'] del selectionDict['convertDates'] else: convertDatesToStrings = True if convertDatesToStrings: dateField = "DATE_FORMAT(MessageTime, '%Y-%m-%d %H:%i:%s')" else: dateField = 'MessageTime' if 'count' in selectionDict: countMessages = selectionDict['count'] del selectionDict['count'] else: countMessages = True if 'beginDate' in selectionDict: beginDate = selectionDict['beginDate'] del selectionDict['beginDate'] else: beginDate = None if 'endDate' in selectionDict: endDate = selectionDict['endDate'] del selectionDict['endDate'] else: endDate = None if not (beginDate or endDate): beginDate = Time.date() - 1 * Time.day if 'groupField' in selectionDict: groupField = selectionDict['groupField'] if groupField not in selectionDict: groupField = selectionDict.keys()[0] del selectionDict['groupField'] elif countMessages: if selectionDict: groupField = selectionDict.keys()[0] elif sortList: groupField = sortList[0][0] else: groupField = 'FixedTextString' else: groupField = None if selectionDict: fieldList = selectionDict.keys() fieldList.append(dateField) if not ('LogLevel' in selectionDict and selectionDict['LogLevel']): selectionDict['LogLevel'] = ['ERROR', 'EXCEPT', 'FATAL'] else: fieldList = [ dateField, 'LogLevel', 'FixedTextString', 'VariableText', 'SystemName', 'SubSystemName', 'OwnerDN', 'OwnerGroup', 'ClientIPNumberString', 'SiteName' ] selectionDict['LogLevel'] = ['ERROR', 'EXCEPT', 'FATAL'] result = LogDB._queryDB(showFieldList=fieldList, condDict=selectionDict, older=endDate, newer=beginDate, count=countMessages, groupColumn=groupField, orderFields=sortList) if not result['OK']: return result if maxItems: records = result['Value'][startItem:maxItems + startItem] else: records = result['Value'][startItem:] if not sortList: unOrderedFields = sorted([(s[-1], s) for s in records]) records = [t[1] for t in unOrderedFields] records.reverse() if countMessages: if 'count(*) as recordCount' in fieldList: fieldList.remove('count(*) as recordCount') fieldList.append('Number of Errors') if convertDatesToStrings: for element in fieldList: if search('MessageTime', element): index = fieldList.index(element) fieldList[index] = 'MessageTime' retValue = { 'ParameterNames': fieldList, 'Records': records, 'TotalRecords': len(result['Value']), 'Extras': {} } return S_OK(retValue)