Exemplo n.º 1
0
  def export_ping( self ):
    dInfo = {}
    dInfo[ 'version' ] = DIRAC.version
    dInfo[ 'time' ] = Time.dateTime()
    #Uptime
    try:
      oFD = file( "/proc/uptime" )
      iUptime = long( float( oFD.readline().split()[0].strip() ) )
      oFD.close()
      dInfo[ 'host uptime' ] = iUptime
    except:
      pass
    startTime = self.serviceInfoDict[ 'serviceStartTime' ]
    dInfo[ 'service start time' ] = self.serviceInfoDict[ 'serviceStartTime' ]
    serviceUptime = Time.dateTime() - startTime
    dInfo[ 'service uptime' ] = serviceUptime.days * 3600 + serviceUptime.seconds
    #Load average
    try:
      oFD = file( "/proc/loadavg" )
      sLine = oFD.readline()
      oFD.close()
      dInfo[ 'load' ] = " ".join( sLine.split()[:3] )
    except:
      pass
    dInfo[ 'name' ] = self.serviceInfoDict[ 'serviceName' ]
    stTimes = os.times()
    dInfo[ 'cpu times' ] = { 'user time' : stTimes[0],
                             'system time' : stTimes[1],
                             'children user time' : stTimes[2],
                             'children system time' : stTimes[3],
                             'elapsed real time' : stTimes[4]
                           }

    return S_OK( dInfo )
Exemplo n.º 2
0
  def addLoggingRecord(self,
                       jobID,
                       status='idem',
                       minor='idem',
                       application='idem',
                       date='',
                       source='Unknown'):
    """ Add a new entry to the JobLoggingDB table. One, two or all the three status
        components (major, minor, application) can be specified.
        Optionally the time stamp of the status can
        be provided in a form of a string in a format '%Y-%m-%d %H:%M:%S' or
        as datetime.datetime object. If the time stamp is not provided the current
        UTC time is used.
    """

    event = 'status/minor/app=%s/%s/%s' % (status, minor, application)
    self.gLogger.info("Adding record for job " + str(jobID) + ": '" + event + "' from " + source)

    if not date:
      # Make the UTC datetime string and float
      _date = Time.dateTime()
      epoc = time.mktime(_date.timetuple()) + _date.microsecond / 1000000. - MAGIC_EPOC_NUMBER
      time_order = round(epoc, 3)
    else:
      try:
        if isinstance(date, basestring):
          # The date is provided as a string in UTC
          _date = Time.fromString(date)
          epoc = time.mktime(_date.timetuple()) + _date.microsecond / 1000000. - MAGIC_EPOC_NUMBER
          time_order = round(epoc, 3)
        elif isinstance(date, Time._dateTimeType):
          _date = date
          epoc = time.mktime(_date.timetuple()) + _date.microsecond / 1000000. - \
              MAGIC_EPOC_NUMBER  # pylint: disable=no-member
          time_order = round(epoc, 3)
        else:
          self.gLogger.error('Incorrect date for the logging record')
          _date = Time.dateTime()
          epoc = time.mktime(_date.timetuple()) - MAGIC_EPOC_NUMBER
          time_order = round(epoc, 3)
      except BaseException:
        self.gLogger.exception('Exception while date evaluation')
        _date = Time.dateTime()
        epoc = time.mktime(_date.timetuple()) - MAGIC_EPOC_NUMBER
        time_order = round(epoc, 3)

    cmd = "INSERT INTO LoggingInfo (JobId, Status, MinorStatus, ApplicationStatus, " + \
          "StatusTime, StatusTimeOrder, StatusSource) VALUES (%d,'%s','%s','%s','%s',%f,'%s')" % \
        (int(jobID), status, minor, application[:255],
         str(_date), time_order, source)

    return self._update(cmd)
Exemplo n.º 3
0
  def web_getProxyManagerData( self ):
    sData = self.getSessionData()

    callback = {}

    user = sData["user"]["username"]
    if user == "Anonymous":
      self.finish( {"success":"false", "error":"You are not authorize to access these data"} )
    start, limit, sort, req = self.__request()
    rpcClient = RPCClient( "Framework/ProxyManager" )
    retVal = yield self.threadTask( rpcClient.getContents, req, sort, start, limit )
    gLogger.info( "*!*!*!  RESULT: \n%s" % retVal )
    if not retVal[ 'OK' ]:
      self.finish( {"success":"false", "error":retVal["Message"]} )
    svcData = retVal[ 'Value' ]
    proxies = []
    dnMap = {}
    for record in svcData[ 'Records' ]:
      proxies.append( { 'proxyid': "%s@%s" % ( record[1], record[2] ),
                                  'UserName' : str( record[0] ),
                                  'UserDN' : record[1],
                                  'UserGroup' : record[2],
                                  'ExpirationTime' : str( record[3] ),
                                  'PersistentFlag' : str( record[4] ) } )
    timestamp = Time.dateTime().strftime( "%Y-%m-%d %H:%M [UTC]" )
    data = {"success":"true", "result":proxies, "total":svcData[ 'TotalRecords' ], "date":timestamp}
    self.finish( data )
def getToken( key ):
  '''
    Function that gets the userName from the proxy
  '''

  proxyInfo = getProxyInfo()
  if not proxyInfo[ 'OK' ]:
    error( str( proxyInfo ) )

  if key.lower() == 'owner':
    userName = proxyInfo[ 'Value' ][ 'username' ]
    tokenOwner = S_OK( userName )
    if not tokenOwner[ 'OK' ]:
      error( tokenOwner[ 'Message' ] )
    return tokenOwner[ 'Value' ]

  elif key.lower() == 'expiration':
    expiration = proxyInfo[ 'Value' ][ 'secondsLeft' ]
    tokenExpiration = S_OK( expiration )
    if not tokenExpiration[ 'OK' ]:
      error( tokenExpiration[ 'Message' ] )

    now = Time.dateTime()
    #datetime.datetime.utcnow()
    expirationDate = now + datetime.timedelta( seconds=tokenExpiration['Value'] )
    expirationDate = Time.toString( expirationDate )
    expirationDate = expirationDate.split('.')[0]
    return expirationDate
Exemplo n.º 5
0
 def web_getJobData(self):
   timestamp = Time.dateTime().strftime("%Y-%m-%d %H:%M [UTC]")
   total = 5
   values = []
   values = [{"ExampleId":1,"ExampleValue":"Zoli"},{"ExampleId":2,"ExampleValue":'a'},{"ExampleId":3,"ExampleValue":'aaaa'},{"ExampleId":4,"ExampleValue":'bbbb'},{"ExampleId":5,"ExampleValue":'adsd'}]
   callback = {"success":"true", "result":values, "total":total, "date":timestamp }
   self.finish(callback)
Exemplo n.º 6
0
  def _getHistoryData(self, timeSpan, groupToUse):
    """ Get history data from ElasticSearch Monitoring database

        :param int timeSpan: time span
        :param str groupToUse: requested user group
        :return: dictionary with history data
    """

    monitoringClient = MonitoringClient()

    reportCondition = {'Status': ['Running']}
    if not groupToUse:
      reportGrouping = 'UserGroup'
      reportCondition["grouping"] = ["UserGroup"]
    else:
      reportGrouping = 'User'
      reportCondition["UserGroup"] = groupToUse
      reportCondition["grouping"] = ["User"]

    now = Time.dateTime()
    result = monitoringClient.getReport('WMSHistory', 'AverageNumberOfJobs',
                                        now - datetime.timedelta(seconds=timeSpan), now,
                                        reportCondition, reportGrouping,
                                        {'lastSeconds': timeSpan})
    return result
Exemplo n.º 7
0
  def test_03_api( self ):
    """ DataLoggingDB API

    :param self: self reference
    """
    
    lfns = [ '/Test/00001234/File1', '/Test/00001234/File2' ] 
    fileTuples = tuple( [ ( lfn, "TestStatus", "MinorStatus", Time.toString(), Time.dateTime(), "Somewhere" ) 
                          for lfn in lfns ] )
    
    result = self.__db.addFileRecord( lfns, "TestStatus", date = '2012-04-28 09:49:02.545466' )
    self.assertEqual( result["OK"], True ) 
    self.assertEqual( result["Value"], 2 )
    self.assertEqual( result["lastRowId"], 2 )

    result = self.__db.addFileRecords( fileTuples )
    self.assertEqual( result["OK"], True )

    result = self.__db.getFileLoggingInfo( lfns[0] )
    self.assertEqual( result["OK"], True )
    self.assertEqual( len( result["Value"] ), 2 )

    result = self.__db.getFileLoggingInfo( lfns[1] )
    self.assertEqual( result["OK"], True )
    self.assertEqual( len( result["Value"] ), 2 )

    result = self.__db.getUniqueStates()
    self.assertEqual( result["OK"], True )
    self.assertEqual( result["Value"], [ "TestStatus" ] )

    result = self.__db._update( 'DROP TABLE `%s`' % self.__db.tableName )
    self.assertEqual( result["OK"], True )
Exemplo n.º 8
0
def getToken(key):
    '''
    Function that gets the userName from the proxy
  '''

    proxyInfo = getProxyInfo()
    if not proxyInfo['OK']:
        error(str(proxyInfo))

    if key.lower() == 'owner':
        userName = proxyInfo['Value']['username']
        tokenOwner = S_OK(userName)
        if not tokenOwner['OK']:
            error(tokenOwner['Message'])
        return tokenOwner['Value']

    elif key.lower() == 'expiration':
        expiration = proxyInfo['Value']['secondsLeft']
        tokenExpiration = S_OK(expiration)
        if not tokenExpiration['OK']:
            error(tokenExpiration['Message'])

        now = Time.dateTime()
        #datetime.datetime.utcnow()
        expirationDate = now + datetime.timedelta(
            seconds=tokenExpiration['Value'])
        expirationDate = Time.toString(expirationDate)
        expirationDate = expirationDate.split('.')[0]
        return expirationDate
Exemplo n.º 9
0
    def export_getCurrentPilotCounters(cls, attrDict={}):
        """ Get pilot counters per Status with attrDict selection. Final statuses are given for
        the last day.
    """

        result = pilotDB.getCounters('PilotAgents', ['Status'],
                                     attrDict,
                                     timeStamp='LastUpdateTime')
        if not result['OK']:
            return result
        last_update = Time.dateTime() - Time.day
        resultDay = pilotDB.getCounters('PilotAgents', ['Status'],
                                        attrDict,
                                        newer=last_update,
                                        timeStamp='LastUpdateTime')
        if not resultDay['OK']:
            return resultDay

        resultDict = {}
        for statusDict, count in result['Value']:
            status = statusDict['Status']
            resultDict[status] = count
            if status in FINAL_STATES:
                resultDict[status] = 0
                for statusDayDict, ccount in resultDay['Value']:
                    if status == statusDayDict['Status']:
                        resultDict[status] = ccount
                    break

        return S_OK(resultDict)
Exemplo n.º 10
0
 def _sendMessage(self, level, msgText, variableText):
     if self.__testLevel(level):
         messageObject = Message(self._systemName, level, Time.dateTime(),
                                 msgText, variableText,
                                 self.__discoverCallingFrame())
         self._processMessage(messageObject)
     return True
Exemplo n.º 11
0
    def getTimings(self, site, period=3600):
        """ Get CPU and wall clock times for the jobs finished in the last hour
    """
        ret = self._escapeString(site)
        if not ret['OK']:
            return ret
        site = ret['Value']

        date = str(Time.dateTime() - Time.second * period)
        req = "SELECT JobID from Jobs WHERE Site=%s and EndExecTime > '%s' " % (
            site, date)
        result = self._query(req)
        jobList = [str(x[0]) for x in result['Value']]
        jobString = ','.join(jobList)

        req = "SELECT SUM(Value) from JobParameters WHERE Name='TotalCPUTime(s)' and JobID in (%s)" % jobString
        result = self._query(req)
        if not result['OK']:
            return result
        cpu = result['Value'][0][0]
        if not cpu:
            cpu = 0.0

        req = "SELECT SUM(Value) from JobParameters WHERE Name='WallClockTime(s)' and JobID in (%s)" % jobString
        result = self._query(req)
        if not result['OK']:
            return result
        wctime = result['Value'][0][0]
        if not wctime:
            wctime = 0.0

        return S_OK({"CPUTime": int(cpu), "WallClockTime": int(wctime)})
Exemplo n.º 12
0
    def clearWaitingPilots(self, condDict):
        """ Clear pilots in the faulty Waiting state
    """

        last_update = Time.dateTime() - MAX_WAITING_STATE_LENGTH * Time.hour
        clearDict = {
            "Status": "Waiting",
            "OwnerDN": condDict["OwnerDN"],
            "OwnerGroup": condDict["OwnerGroup"],
            "GridType": condDict["GridType"],
            "Broker": condDict["Broker"],
        }
        result = self.pilotDB.selectPilots(clearDict, older=last_update)
        if not result["OK"]:
            self.log.warn("Failed to get the Pilot Agents fpr Waiting state")
            return result
        if not result["Value"]:
            return S_OK()
        refList = result["Value"]

        for pilotRef in refList:
            self.log.info("Setting Waiting pilot to Aborted: %s" % pilotRef)
            result = self.pilotDB.setPilotStatus(pilotRef, "Stalled", statusReason="Exceeded max waiting time")

        return S_OK()
Exemplo n.º 13
0
  def _initMonitoring( self ):
    #Init extra bits of monitoring
    self._monitor.setComponentType( MonitoringClient.COMPONENT_SERVICE )
    self._monitor.setComponentName( self._name )
    self._monitor.setComponentLocation( self._cfg.getURL() )
    self._monitor.initialize()
    self._monitor.registerActivity( "Connections", "Connections received", "Framework", "connections", MonitoringClient.OP_RATE )
    self._monitor.registerActivity( "Queries", "Queries served", "Framework", "queries", MonitoringClient.OP_RATE )
    self._monitor.registerActivity( 'CPU', "CPU Usage", 'Framework', "CPU,%", MonitoringClient.OP_MEAN, 600 )
    self._monitor.registerActivity( 'MEM', "Memory Usage", 'Framework', 'Memory,MB', MonitoringClient.OP_MEAN, 600 )
    self._monitor.registerActivity( 'PendingQueries', "Pending queries", 'Framework', 'queries', MonitoringClient.OP_MEAN )
    self._monitor.registerActivity( 'ActiveQueries', "Active queries", 'Framework', 'threads', MonitoringClient.OP_MEAN )
    self._monitor.registerActivity( 'RunningThreads', "Running threads", 'Framework', 'threads', MonitoringClient.OP_MEAN )
    self._monitor.registerActivity( 'MaxFD', "Max File Descriptors", 'Framework', 'fd', MonitoringClient.OP_MEAN )

    self._monitor.setComponentExtraParam( 'DIRACVersion', DIRAC.version )
    self._monitor.setComponentExtraParam( 'platform', DIRAC.getPlatform() )
    self._monitor.setComponentExtraParam( 'startTime', Time.dateTime() )
    for prop in ( ( "__RCSID__", "version" ), ( "__doc__", "description" ) ):
      try:
        value = getattr( self._handler[ 'module' ], prop[0] )
      except Exception as e:
        gLogger.exception( e )
        gLogger.error( "Missing property", prop[0] )
        value = 'unset'
      self._monitor.setComponentExtraParam( prop[1], value )
    for secondaryName in self._cfg.registerAlsoAs():
      gLogger.info( "Registering %s also as %s" % ( self._name, secondaryName ) )
      self._validNames.append( secondaryName )
    return S_OK()
Exemplo n.º 14
0
  def export_getCurrentJobCounters(attrDict=None):
    """ Get job counters per Status with attrDict selection. Final statuses are given for
        the last day.
    """

    if not attrDict:
      attrDict = {}
    result = gJobDB.getCounters('Jobs', ['Status'], attrDict, timeStamp='LastUpdateTime')
    if not result['OK']:
      return result
    last_update = Time.dateTime() - Time.day
    resultDay = gJobDB.getCounters('Jobs', ['Status'], attrDict, newer=last_update,
                                   timeStamp='LastUpdateTime')
    if not resultDay['OK']:
      return resultDay

    resultDict = {}
    for statusDict, count in result['Value']:
      status = statusDict['Status']
      resultDict[status] = count
      if status in FINAL_STATES:
        resultDict[status] = 0
        for statusDayDict, ccount in resultDay['Value']:
          if status == statusDayDict['Status']:
            resultDict[status] = ccount
          break

    return S_OK(resultDict)
Exemplo n.º 15
0
  def _getUsageHistoryForTimeSpan( self, timeSpan, groupToUse = "" ):
    reportCondition = { 'Status' : [ 'Running' ] }
    if not groupToUse:
      reportGrouping = 'UserGroup'
    else:
      reportGrouping = 'User'
      reportCondition = { 'UserGroup' : groupToUse }
    now = Time.dateTime()
    result = self.__reportsClient.getReport( 'WMSHistory', 'AverageNumberOfJobs',
                                             now - datetime.timedelta( seconds = timeSpan ), now,
                                             reportCondition, reportGrouping,
                                             { 'lastSeconds' : timeSpan } )
    if not result[ 'OK' ]:
      self.__log.error( "Cannot get history from Accounting", result[ 'Message' ] )
      return result
    data = result['Value'].get( 'data', [] )
    if not data:
      message = "Empty history data from Accounting"
      self.__log.error( message )
      return S_ERROR( message )

    #Map the usernames to DNs
    if groupToUse:
      mappedData = {}
      for userName in data:
        result = CS.getDNForUsername( userName )
        if not result[ 'OK' ]:
          self.__log.error( "User does not have any DN assigned", "%s :%s" % ( userName, result[ 'Message' ] ) )
          continue
        for userDN in result[ 'Value' ]:
          mappedData[ userDN ] = data[ userName ]
      data = mappedData

    return S_OK( data )
Exemplo n.º 16
0
 def execute( self ):
   """ Remove jobs in various status
   """
   #Delete jobs in "Deleted" state
   result = self.removeJobsByStatus( { 'Status' : 'Deleted' } )
   if not result[ 'OK' ]:
     return result
   #Get all the Job types that can be cleaned
   result = self.__getAllowedJobTypes()
   if not result[ 'OK' ]:
     return result
   
   # No jobs in the system subject to removal
   if not result['Value']:
     return S_OK()
   
   baseCond = { 'JobType' : result[ 'Value' ] }
   # Remove jobs with final status
   for status in self.removeStatusDelay:
     delay = self.removeStatusDelay[ status ]
     condDict = dict( baseCond )
     condDict[ 'Status' ] = status
     delTime = str( Time.dateTime() - delay * Time.day )
     result = self.removeJobsByStatus( condDict, delTime )
     if not result['OK']:
       gLogger.warn( 'Failed to remove jobs in status %s' % status )
   return S_OK()
Exemplo n.º 17
0
    def initialize(self, request):
        """ Set default values to attributes and parameters
    """
        if type(request) == NoneType:
            # Set some defaults
            for name in self.attributeNames:
                self.attributes[name] = "Unknown"
            self.attributes["CreationTime"] = str(Time.dateTime())
            self.attributes["Status"] = "New"
            result = getProxyInfo()
            if result["OK"]:
                proxyDict = result["Value"]
                self.attributes["OwnerDN"] = proxyDict["identity"]
                if "group" in proxyDict:
                    self.attributes["OwnerGroup"] = proxyDict["group"]
            self.attributes["DIRACSetup"] = gConfig.getValue("/DIRAC/Setup", "Unknown")
        elif isinstance(request, RequestContainer):
            for attr in self.attributeNames:
                self.attributes[attr] = request.attributes[attr]

        # initialize request from an XML string
        if type(request) in StringTypes:
            for name in self.attributeNames:
                self.attributes[name] = "Unknown"
            self.parseRequest(request)

        # Initialize request from another request
        elif isinstance(request, RequestContainer):
            self.subRequests = copy.deepcopy(request.subrequests)
Exemplo n.º 18
0
 def storeHashTagById( self, userIds, tagName, hashTag = False, connObj = False ):
   """
   Set a data entry for a profile
   """
   if not hashTag:
     hashTag = md5.md5()
     hashTag.update( "%s;%s;%s" % ( Time.dateTime(), userIds, tagName ) )
     hashTag = hashTag.hexdigest()
   hashTagUnescaped = hashTag
   result = self._escapeString( hashTag )
   if not result[ 'OK' ]:
     return result
   hashTag = result[ 'Value' ]
   result = self._escapeString( tagName )
   if not result[ 'OK' ]:
     return result
   tagName = result[ 'Value' ]
   insertSQL = "INSERT INTO `up_HashTags` ( UserId, GroupId, VOId, TagName, HashTag ) VALUES ( %s, %s, %s, %s, %s )" % ( userIds[0], userIds[1], userIds[2], tagName, hashTag )
   result = self._update( insertSQL, conn = connObj )
   if result[ 'OK' ]:
     return S_OK( hashTagUnescaped )
   #If error and not duplicate -> real error
   if result[ 'Message' ].find( "Duplicate entry" ) == -1:
     return result
   updateSQL = "UPDATE `up_HashTags` set HashTag=%s WHERE UserId = %s AND GroupId = %s AND VOId = %s AND TagName = %s" % ( hashTag, userIds[0], userIds[1], userIds[2], tagName )
   result = self._update( updateSQL, conn = connObj )
   if not result[ 'OK' ]:
     return result
   return S_OK( hashTagUnescaped )
Exemplo n.º 19
0
def getToken(key):
    """
    Function that gets the userName from the proxy
  """

    proxyInfo = getProxyInfo()
    if not proxyInfo["OK"]:
        error(str(proxyInfo))

    if key.lower() == "owner":
        userName = proxyInfo["Value"]["username"]
        tokenOwner = S_OK(userName)
        if not tokenOwner["OK"]:
            error(tokenOwner["Message"])
        return tokenOwner["Value"]

    elif key.lower() == "expiration":
        expiration = proxyInfo["Value"]["secondsLeft"]
        tokenExpiration = S_OK(expiration)
        if not tokenExpiration["OK"]:
            error(tokenExpiration["Message"])

        now = Time.dateTime()
        # datetime.datetime.utcnow()
        expirationDate = now + datetime.timedelta(seconds=tokenExpiration["Value"])
        expirationDate = Time.toString(expirationDate)
        expirationDate = expirationDate.split(".")[0]
        return expirationDate
Exemplo n.º 20
0
    def execute(self):
        """ Remove jobs in various status
    """
        #Delete jobs in "Deleted" state
        result = self.removeJobsByStatus({'Status': 'Deleted'})
        if not result['OK']:
            return result
        #Get all the Job types that can be cleaned
        result = self.__getAllowedJobTypes()
        if not result['OK']:
            return result

        # No jobs in the system subject to removal
        if not result['Value']:
            return S_OK()

        baseCond = {'JobType': result['Value']}
        # Remove jobs with final status
        for status in self.removeStatusDelay:
            delay = self.removeStatusDelay[status]
            if delay < 0:
                # Negative delay means don't delete anything...
                continue
            condDict = dict(baseCond)
            if status != 'Any':
                condDict['Status'] = status
            delTime = str(Time.dateTime() - delay * Time.day)
            result = self.removeJobsByStatus(condDict, delTime)
            if not result['OK']:
                gLogger.warn('Failed to remove jobs in status %s' % status)
        return S_OK()
Exemplo n.º 21
0
  def initialize( self, request ):
    """ Set default values to attributes and parameters
    """
    if type( request ) == NoneType:
      # Set some defaults
      for name in self.attributeNames:
        self.attributes[name] = 'Unknown'
      self.attributes['CreationTime'] = str( Time.dateTime() )
      self.attributes['Status'] = "New"
      result = getProxyInfo()
      if result['OK']:
        proxyDict = result[ 'Value' ]
        self.attributes['OwnerDN'] = proxyDict[ 'identity' ]
        if 'group' in proxyDict:
          self.attributes['OwnerGroup'] = proxyDict[ 'group' ]
      self.attributes['DIRACSetup'] = gConfig.getValue( '/DIRAC/Setup', 'Unknown' )
    elif isinstance( request, RequestContainer ):
      for attr in self.attributeNames:
        self.attributes[attr] = request.attributes[attr]

    # initialize request from an XML string
    if type( request ) in StringTypes:
      for name in self.attributeNames:
        self.attributes[name] = 'Unknown'
      self.parseRequest( request )

    # Initialize request from another request
    elif isinstance( request, RequestContainer ):
      self.subRequests = copy.deepcopy( request.subrequests )
Exemplo n.º 22
0
    def storeHashTagById(self, userIds, tagName, hashTag=False):
        """
    Set a data entry for a profile
    """
        if not hashTag:
            hashTag = md5.md5()
            hashTag.update("%s;%s;%s" % (Time.dateTime(), userIds, tagName))
            hashTag = hashTag.hexdigest()

        result = self.insertFields(
            "up_HashTags",
            ["UserId", "GroupId", "VOId", "TagName", "HashTag"],
            [userIds[0], userIds[1], userIds[2], tagName, hashTag],
        )
        if result["OK"]:
            return S_OK(hashTag)
        # If error and not duplicate -> real error
        if result["Message"].find("Duplicate entry") == -1:
            return result
        result = self.updateFields(
            "up_HashTags",
            ["HashTag"],
            [hashTag],
            {"UserId": userIds[0], "GroupId": userIds[1], "VOId": userIds[2], "TagName": tagName},
        )
        if not result["OK"]:
            return result
        return S_OK(hashTag)
Exemplo n.º 23
0
 def submit(self):
   pagestart = time()
   RPC = getRPCClient("WorkloadManagement/JobMonitoring")
   user = str(credentials.getUsername())
   result = RPC.getOwners()
   if result["OK"]:
     defaultGroup = gConfig.getValue("/Registry/DefaultGroup","")
     if defaultGroup == "":
       return {"success":"false","error":"Option /Registry/DefaultGroup is undefined, please set the default group in the CS"}
     group = str(credentials.getSelectedGroup())
     groupProperty = credentials.getProperties(group)
     if user not in result["Value"] and ( "JobAdministrator" or "JobSharing" ) not in groupProperty:
       c.result = {"success":"false","error":"You don't have any jobs in the DIRAC system"}
       return c.result
   else:
     c.result = {"success":"false","error":result["Message"]}
     return c.result
   req = self.__request()
   gLogger.always("getJobPageSummaryWeb(%s,%s,%s,%s)" % (req,globalSort,pageNumber,numberOfJobs))
   result = RPC.getJobPageSummaryWeb(req,globalSort,pageNumber,numberOfJobs)
   gLogger.always(" - REZ: " %result)
   if result["OK"]:
     result = result["Value"]
     gLogger.info("ReS",result)
     if result.has_key("TotalRecords"):
       if  result["TotalRecords"] > 0:
         if result.has_key("ParameterNames") and result.has_key("Records"):
           if len(result["ParameterNames"]) > 0:
             if len(result["Records"]) > 0:
               c.result = []
               jobs = result["Records"]
               head = result["ParameterNames"]
               headLength = len(head)
               for i in jobs:
                 tmp = {}
                 for j in range(0,headLength):
                   tmp[head[j]] = i[j]
                 c.result.append(tmp)
               total = result["TotalRecords"]
               timestamp = Time.dateTime().strftime("%Y-%m-%d %H:%M [UTC]")
               if result.has_key("Extras"):
                 st = self.__dict2string(req)
                 extra = result["Extras"]
                 c.result = {"success":"true","result":c.result,"total":total,"extra":extra,"request":st,"date":timestamp}
               else:
                 c.result = {"success":"true","result":c.result,"total":total,"date":timestamp}
             else:
               c.result = {"success":"false","result":"","error":"There are no data to display"}
           else:
             c.result = {"success":"false","result":"","error":"ParameterNames field is missing"}
         else:
           c.result = {"success":"false","result":"","error":"Data structure is corrupted"}
       else:
         c.result = {"success":"false","result":"","error":"There were no data matching your selection"}
     else:
       c.result = {"success":"false","result":"","error":"Data structure is corrupted"}
   else:
     c.result = {"success":"false","error":result["Message"]}
   gLogger.info("\033[0;31mJOB SUBMIT REQUEST:\033[0m %s" % (time() - pagestart))
   return c.result
Exemplo n.º 24
0
    def _initMonitoring(self):
        # Init extra bits of monitoring
        self._monitor.setComponentType(MonitoringClient.COMPONENT_SERVICE)
        self._monitor.setComponentName(self._name)
        self._monitor.setComponentLocation(self._cfg.getURL())
        self._monitor.initialize()
        self._monitor.registerActivity(
            "Connections", "Connections received", "Framework", "connections", MonitoringClient.OP_RATE
        )
        self._monitor.registerActivity("Queries", "Queries served", "Framework", "queries", MonitoringClient.OP_RATE)
        self._monitor.registerActivity("CPU", "CPU Usage", "Framework", "CPU,%", MonitoringClient.OP_MEAN, 600)
        self._monitor.registerActivity("MEM", "Memory Usage", "Framework", "Memory,MB", MonitoringClient.OP_MEAN, 600)
        self._monitor.registerActivity(
            "PendingQueries", "Pending queries", "Framework", "queries", MonitoringClient.OP_MEAN
        )
        self._monitor.registerActivity(
            "ActiveQueries", "Active queries", "Framework", "threads", MonitoringClient.OP_MEAN
        )
        self._monitor.registerActivity(
            "RunningThreads", "Running threads", "Framework", "threads", MonitoringClient.OP_MEAN
        )
        self._monitor.registerActivity("MaxFD", "Max File Descriptors", "Framework", "fd", MonitoringClient.OP_MEAN)

        self._monitor.setComponentExtraParam("DIRACVersion", DIRAC.version)
        self._monitor.setComponentExtraParam("platform", DIRAC.platform)
        self._monitor.setComponentExtraParam("startTime", Time.dateTime())
        for prop in (("__RCSID__", "version"), ("__doc__", "description")):
            try:
                value = getattr(self._handler["module"], prop[0])
            except Exception, e:
                gLogger.error("Missing %s" % prop[0])
                value = "unset"
            self._monitor.setComponentExtraParam(prop[1], value)
Exemplo n.º 25
0
  def submit( self ):
    RPC = getRPCClient( "Framework/SystemLoggingReport" )
    result = self.__request()
    result = RPC.getGroupedMessages( result, globalSort, pageNumber, numberOfJobs )
    gLogger.info( "- REQUEST:", result )
    if result["OK"]:
      result = result["Value"]
      c.result = []
      jobs = result["Records"]
      if result.has_key( "ParameterNames" ) and result.has_key( "Records" ):
        if len( result["ParameterNames"] ) > 0:
          if len( result["Records"] ) > 0:
            c.result = []
            jobs = result["Records"]
            head = result["ParameterNames"]
            headLength = len( head )
            for i in jobs:
              tmp = {}
              for j in range( 0, headLength ):
                tmp[head[j]] = i[j]
              c.result.append( tmp )
            total = result["TotalRecords"]
            timestamp = Time.dateTime().strftime("%Y-%m-%d %H:%M [UTC]")
#            c.result = {"success":"true", "result":c.result, "total":total,"date":timestamp}
            c.result = {"success":"true", "result":c.result, "total":total}
          else:
            c.result = {"success":"false", "result":"", "error":"There are no data to display"}
        else:
          c.result = {"success":"false", "result":"", "error":"ParameterNames field is missing"}
      else:
        c.result = {"success":"false", "result":"", "error":"Data structure is corrupted"}
    else:
      c.result = {"success":"false", "error":result["Message"]}
    gLogger.info( "\033[0;31mRESULT:\033[0m" )
    return c.result
Exemplo n.º 26
0
 def fatal( self, sMsg, sVarMsg = '' ):
   messageObject = Message( self._systemName,
                            self._logLevels.fatal,
                            Time.dateTime(),
                            sMsg,
                            sVarMsg,
                            self.__discoverCallingFrame() )
   return self.processMessage( messageObject )
Exemplo n.º 27
0
    def setExecutionTime(self, time):
        """ Set the execution time to the current data and time
    """

        if time.lower() == "now":
            self.attributes['ExecutionTime'] = Time.toString(Time.dateTime())
        else:
            self.attributes['ExecutionTime'] = time
  def __setCommiter( self ):

    sessionData = self.getSessionData()

    commiter = "%s@%s - %s" % ( sessionData["user"]["username"],
                                sessionData["user"]["group"],
                                Time.dateTime().strftime( "%Y-%m-%d %H:%M:%S" ) )
    self.__configData[ 'cfgData' ].commiterId = commiter
Exemplo n.º 29
0
  def setExecutionTime(self,time):
    """ Set the execution time to the current data and time
    """

    if time.lower() == "now":
      self.attributes['ExecutionTime'] = Time.toString(Time.dateTime())
    else:
      self.attributes['ExecutionTime'] = time
Exemplo n.º 30
0
 def __getModificator(self):
     rpcClient = getRPCClient(
         gConfig.getValue("/DIRAC/Configuration/MasterServer",
                          "Configuration/Server"))
     commiter = "%s@%s - %s" % (
         credentials.getUsername(), credentials.getSelectedGroup(),
         Time.dateTime().strftime("%Y-%m-%d %H:%M:%S"))
     return Modificator(rpcClient, commiter)
Exemplo n.º 31
0
  def createTask( self, taskName, status, owner, ownerDN, ownerGroup, taskInfo = {} ):
    taskAttrNames = ['TaskName', 'CreationTime', 'UpdateTime', 'Status', 'Owner', 'OwnerDN', 'OwnerGroup', 'Info']
    taskAttrValues = [taskName, Time.dateTime(), Time.dateTime(), status, owner, ownerDN, ownerGroup, json.dumps(taskInfo, separators=(',',':'))]

    result = self.insertFields( 'Task', taskAttrNames, taskAttrValues )
    if not result['OK']:
      self.log.error( 'Can not create new task', result['Message'] )
      return result

    if 'lastRowId' not in result:
      return S_ERROR( 'Failed to retrieve a new ID for task' )

    taskID = int( result['lastRowId'] )

    self.log.info( 'TaskDB: New TaskID served "%s"' % taskID )

    return S_OK( taskID )
Exemplo n.º 32
0
 def __getModificator(self):
     rpcClient = getRPCClient(gConfig.getValue("/DIRAC/Configuration/MasterServer", "Configuration/Server"))
     commiter = "%s@%s - %s" % (
         credentials.getUsername(),
         credentials.getSelectedGroup(),
         Time.dateTime().strftime("%Y-%m-%d %H:%M:%S"),
     )
     return Modificator(rpcClient, commiter)
Exemplo n.º 33
0
    def addLoggingRecord(self, jobID, status="idem", minor="idem", application="idem", date="", source="Unknown"):

        """ Add a new entry to the JobLoggingDB table. One, two or all the three status
        components can be specified. Optionaly the time stamp of the status can
        be provided in a form of a string in a format '%Y-%m-%d %H:%M:%S' or
        as datetime.datetime object. If the time stamp is not provided the current
        UTC time is used.
    """

        event = "status/minor/app=%s/%s/%s" % (status, minor, application)
        self.gLogger.info("Adding record for job " + str(jobID) + ": '" + event + "' from " + source)

        if not date:
            # Make the UTC datetime string and float
            _date = Time.dateTime()
            epoc = time.mktime(_date.timetuple()) + _date.microsecond / 1000000.0 - MAGIC_EPOC_NUMBER
            time_order = round(epoc, 3)
        else:
            try:
                if type(date) in StringTypes:
                    # The date is provided as a string in UTC
                    _date = Time.fromString(date)
                    epoc = time.mktime(_date.timetuple()) + _date.microsecond / 1000000.0 - MAGIC_EPOC_NUMBER
                    time_order = round(epoc, 3)
                elif type(date) == Time._dateTimeType:
                    _date = date
                    epoc = time.mktime(_date.timetuple()) + _date.microsecond / 1000000.0 - MAGIC_EPOC_NUMBER
                    time_order = round(epoc, 3)
                else:
                    self.gLogger.error("Incorrect date for the logging record")
                    _date = Time.dateTime()
                    epoc = time.mktime(_date.timetuple()) - MAGIC_EPOC_NUMBER
                    time_order = round(epoc, 3)
            except:
                self.gLogger.exception("Exception while date evaluation")
                _date = Time.dateTime()
                epoc = time.mktime(_date.timetuple()) - MAGIC_EPOC_NUMBER
                time_order = round(epoc, 3)

        cmd = (
            "INSERT INTO LoggingInfo (JobId, Status, MinorStatus, ApplicationStatus, "
            + "StatusTime, StatusTimeOrder, StatusSource) VALUES (%d,'%s','%s','%s','%s',%f,'%s')"
            % (int(jobID), status, minor, application, str(_date), time_order, source)
        )

        return self._update(cmd)
Exemplo n.º 34
0
    def handleOldPilots(self, connection):
        """
        select all pilots that have not been updated in the last N days and declared them
        Deleted, accounting for them.
        """
        pilotsToAccount = {}
        timeLimitToConsider = Time.toString(Time.dateTime() -
                                            Time.day * self.pilotStalledDays)
        result = self.pilotDB.selectPilots(
            {"Status": PilotStatus.PILOT_TRANSIENT_STATES},
            older=timeLimitToConsider,
            timeStamp="LastUpdateTime")
        if not result["OK"]:
            self.log.error("Failed to get the Pilot Agents")
            return result
        if not result["Value"]:
            return S_OK()

        refList = result["Value"]
        result = self.pilotDB.getPilotInfo(refList)
        if not result["OK"]:
            self.log.error("Failed to get Info for Pilot Agents")
            return result

        pilotsDict = result["Value"]

        for pRef in pilotsDict:
            if pilotsDict[pRef].get("Jobs") and self._checkJobLastUpdateTime(
                    pilotsDict[pRef]["Jobs"], self.pilotStalledDays):
                self.log.debug(
                    "%s should not be deleted since one job of %s is running."
                    % (str(pRef), str(pilotsDict[pRef]["Jobs"])))
                continue
            deletedJobDict = pilotsDict[pRef]
            deletedJobDict["Status"] = PilotStatus.DELETED
            deletedJobDict["StatusDate"] = Time.dateTime()
            pilotsToAccount[pRef] = deletedJobDict
            if len(pilotsToAccount) > 100:
                self.accountPilots(pilotsToAccount, connection)
                self._killPilots(pilotsToAccount)
                pilotsToAccount = {}

        self.accountPilots(pilotsToAccount, connection)
        self._killPilots(pilotsToAccount)

        return S_OK()
Exemplo n.º 35
0
 def showStack( self ):
   messageObject = Message( self._systemName,
                            self._logLevels.debug,
                            Time.dateTime(),
                            "",
                            self.__getStackString(),
                            self.__discoverCallingFrame() )
   self.processMessage( messageObject )
Exemplo n.º 36
0
 def showStack( self ):
   messageObject = Message( self._systemName,
                            self._logLevels.debug,
                            Time.dateTime(),
                            "",
                            self.__getStackString(),
                            self.__discoverCallingFrame() )
   self.processMessage( messageObject )
Exemplo n.º 37
0
    def __setCommiter(self):

        sessionData = self.getSessionData()

        commiter = "%s@%s - %s" % (
            sessionData["user"]["username"], sessionData["user"]["group"],
            Time.dateTime().strftime("%Y-%m-%d %H:%M:%S"))
        self.__configData['cfgData'].commiterId = commiter
Exemplo n.º 38
0
 def fatal( self, sMsg, sVarMsg = '' ):
   messageObject = Message( self._systemName,
                            self._logLevels.fatal,
                            Time.dateTime(),
                            sMsg,
                            sVarMsg,
                            self.__discoverCallingFrame() )
   return self.processMessage( messageObject )
Exemplo n.º 39
0
 def execute(self):
     """ Main execution method
 """
     result = gConfig.getSections("/DIRAC/Setups")
     if not result['OK']:
         return result
     validSetups = result['Value']
     gLogger.info("Valid setups for this cycle are %s" %
                  ", ".join(validSetups))
     #Get the WMS Snapshot!
     result = self.jobDB.getSummarySnapshot(self.__jobDBFields)
     now = Time.dateTime()
     if not result['OK']:
         gLogger.error("Can't the the jobdb summary", result['Message'])
     else:
         values = result['Value'][1]
         for record in values:
             recordSetup = record[0]
             if recordSetup not in validSetups:
                 gLogger.error("Setup %s is not valid" % recordSetup)
                 continue
             if recordSetup not in self.dsClients:
                 gLogger.info("Creating DataStore client for %s" %
                              recordSetup)
                 self.dsClients[recordSetup] = DataStoreClient(
                     setup=recordSetup, retryGraceTime=900)
             record = record[1:]
             rD = {}
             for fV in self.__summaryDefinedFields:
                 rD[fV[0]] = fV[1]
             for iP in range(len(self.__summaryKeyFieldsMapping)):
                 fieldName = self.__summaryKeyFieldsMapping[iP]
                 rD[self.__renameFieldsMapping.get(fieldName,
                                                   fieldName)] = record[iP]
             record = record[len(self.__summaryKeyFieldsMapping):]
             for iP in range(len(self.__summaryValueFieldsMapping)):
                 rD[self.__summaryValueFieldsMapping[iP]] = int(record[iP])
             acWMS = WMSHistory()
             acWMS.setStartTime(now)
             acWMS.setEndTime(now)
             acWMS.setValuesFromDict(rD)
             retVal = acWMS.checkValues()
             if not retVal['OK']:
                 gLogger.error("Invalid accounting record ",
                               "%s -> %s" % (retVal['Message'], rD))
             else:
                 self.dsClients[recordSetup].addRegister(acWMS)
         for setup in self.dsClients:
             gLogger.info("Sending records for setup %s" % setup)
             result = self.dsClients[setup].commit()
             if not result['OK']:
                 gLogger.error(
                     "Couldn't commit wms history for setup %s" % setup,
                     result['Message'])
             else:
                 gLogger.info("Sent %s records for setup %s" %
                              (result['Value'], setup))
     return S_OK()
Exemplo n.º 40
0
    def handleOldPilots(self, connection):
        """
      select all pilots that have not been updated in the last N days and declared them
      Deleted, accounting for them.
    """
        pilotsToAccount = {}
        timeLimitToConsider = Time.toString(Time.dateTime() -
                                            Time.day * self.pilotStalledDays)
        result = self.pilotDB.selectPilots({'Status': self.queryStateList},
                                           older=timeLimitToConsider,
                                           timeStamp='LastUpdateTime')
        if not result['OK']:
            self.log.error('Failed to get the Pilot Agents')
            return result
        if not result['Value']:
            return S_OK()

        refList = result['Value']
        result = self.pilotDB.getPilotInfo(refList)
        if not result['OK']:
            self.log.error('Failed to get Info for Pilot Agents')
            return result

        pilotsDict = result['Value']

        for pRef in pilotsDict:
            if pilotsDict[pRef].get('Jobs') and self._checkJobLastUpdateTime(
                    pilotsDict[pRef]['Jobs'], self.pilotStalledDays):
                self.log.debug(
                    '%s should not be deleted since one job of %s is running.'
                    % (str(pRef), str(pilotsDict[pRef]['Jobs'])))
                continue
            deletedJobDict = pilotsDict[pRef]
            deletedJobDict['Status'] = 'Deleted'
            deletedJobDict['StatusDate'] = Time.dateTime()
            pilotsToAccount[pRef] = deletedJobDict
            if len(pilotsToAccount) > 100:
                self.accountPilots(pilotsToAccount, connection)
                self._killPilots(pilotsToAccount)
                pilotsToAccount = {}

        self.accountPilots(pilotsToAccount, connection)
        self._killPilots(pilotsToAccount)

        return S_OK()
Exemplo n.º 41
0
  def addFileRecord( self, lfns, status, minor = "Unknown", date = None, source = "Unknown" ):
    """ Add a new entry to the DataLoggingDB table. 

    :warning: Optionally the time stamp of the status can be provided in a form of a string 
    in a format '%Y-%m-%d %H:%M:%S' or as datetime.datetime object. If the time stamp is not 
    provided the current UTC time is used.

    :param self: self reference
    :param list lfns: list of LFNs
    :param str status: status
    :param str minor: additional information
    :param mixed date: date and time
    :param str source: source setting the new status
    """
    self.gLogger.info( "Entering records for %s lfns: %s/%s from source %s" % ( len( lfns ), status, minor, source ) )
    if not date:
      _date = Time.dateTime()
    else:
      if type( date ) in StringTypes:
        _date = Time.fromString( date )
      else:
        _date = date

    try:
      time_order = Time.to2K( _date ) - NEW_MAGIC_EPOCH_2K
    except AttributeError:
      gLogger.error( 'Wrong date argument given using current time stamp' )
      dateNow = Time.dateTime()
      time_order = Time.to2K( dateNow ) - NEW_MAGIC_EPOCH_2K

    inDict = { 'Status': status,
               'MinorStatus': minor,
               'StatusTime': _date,
               'StatusTimeOrder': time_order,
               'Source': source
              }
    result = S_OK( 0 )
    for lfn in lfns:
      inDict['LFN'] = lfn
      res = self.insertFields( self.tableName, inDict = inDict )
      if not res['OK']:
        return res
      result['Value'] += res['Value']
      result['lastRowId'] = res['lastRowId']
    return result
Exemplo n.º 42
0
 def __checkPlotRequest(self, reportRequest):
     #If extraArgs is not there add it
     if 'extraArgs' not in reportRequest:
         reportRequest['extraArgs'] = {}
     if type(reportRequest['extraArgs']
             ) != self.__reportRequestDict['extraArgs']:
         return S_ERROR("Extra args has to be of type %s" %
                        self.__reportRequestDict['extraArgs'])
     reportRequestExtra = reportRequest['extraArgs']
     #Check sliding plots
     if 'lastSeconds' in reportRequestExtra:
         try:
             lastSeconds = long(reportRequestExtra['lastSeconds'])
         except:
             return S_ERROR("lastSeconds key must be a number")
         if lastSeconds < 3600:
             return S_ERROR("lastSeconds must be more than 3600")
         now = Time.dateTime()
         reportRequest['endTime'] = now
         reportRequest['startTime'] = now - datetime.timedelta(
             seconds=lastSeconds)
     else:
         #if enddate is not there, just set it to now
         if not reportRequest.get('endTime', False):
             reportRequest['endTime'] = Time.dateTime()
     #Check keys
     for key in self.__reportRequestDict:
         if not key in reportRequest:
             return S_ERROR('Missing mandatory field %s in plot reques' %
                            key)
         requestKeyType = type(reportRequest[key])
         if key in ('startTime', 'endTime'):
             if requestKeyType not in self.__reportRequestDict[key]:
                 return S_ERROR(
                     "Type mismatch for field %s (%s), required one of %s" %
                     (key, str(requestKeyType),
                      str(self.__reportRequestDict[key])))
             reportRequest[key] = int(Time.toEpoch(reportRequest[key]))
         else:
             if requestKeyType != self.__reportRequestDict[key]:
                 return S_ERROR(
                     "Type mismatch for field %s (%s), required %s" %
                     (key, str(requestKeyType),
                      str(self.__reportRequestDict[key])))
     return S_OK(reportRequest)
Exemplo n.º 43
0
 def getRemainingSecs( self ):
   """
   Get remaining lifetime in secs
   """
   if not self.__valid:
     return S_ERROR( "No certificate loaded" )
   notAfter = self.__certObj.get_not_after()
   remaining = notAfter - Time.dateTime()
   return S_OK( max( 0, remaining.days * 86400 + remaining.seconds ) )
Exemplo n.º 44
0
 def loadCredentials( self ):
   retVal = getProxyInfo()
   if retVal[ 'OK' ]:
     credDict = retVal[ 'Value' ]
     self.commiterId = "%s@%s - %s" % ( credDict[ 'username' ],
                                        credDict[ 'group' ],
                                        Time.dateTime().strftime( "%Y-%m-%d %H:%M:%S" ) )
     return retVal
   return retVal
Exemplo n.º 45
0
 def exception(self, sMsg="", sVarMsg='', lException=False, lExcInfo=False):
     if sVarMsg:
         sVarMsg += "\n%s" % self.__getExceptionString(lException, lExcInfo)
     else:
         sVarMsg = "\n%s" % self.__getExceptionString(lException, lExcInfo)
     messageObject = Message(self._systemName, self._logLevels.exception,
                             Time.dateTime(), sMsg, sVarMsg,
                             self.__discoverCallingFrame())
     return self.processMessage(messageObject)
Exemplo n.º 46
0
 def loadCredentials( self ):
   retVal = getProxyInfo()
   if retVal[ 'OK' ]:
     credDict = retVal[ 'Value' ]
     self.commiterId = "%s@%s - %s" % ( credDict[ 'username' ],
                                        credDict[ 'group' ],
                                        Time.dateTime().strftime( "%Y-%m-%d %H:%M:%S" ) )
     return retVal
   return retVal
Exemplo n.º 47
0
 def setStartTime(self, startTime=False):
     """
 Give a start time for the report
 By default use now
 """
     if not startTime:
         self.startTime = Time.dateTime()
     else:
         self.startTime = startTime
Exemplo n.º 48
0
 def setEndTime(self, endTime=False):
     """
 Give a end time for the report
 By default use now
 """
     if not endTime:
         self.endTime = Time.dateTime()
     else:
         self.endTime = endTime
Exemplo n.º 49
0
  def __checkPlotRequest(self, reportRequest):
    """
    It check the plot attributes. We have to make sure that all attributes which are needed are provided.

    :param dict reportRequest: contains the plot attributes.

    """
    # If extraArgs is not there add it
    if 'extraArgs' not in reportRequest:
      reportRequest['extraArgs'] = {}
    if not isinstance(reportRequest['extraArgs'], self.__reportRequestDict['extraArgs']):
      return S_ERROR("Extra args has to be of type %s" % self.__reportRequestDict['extraArgs'])
    reportRequestExtra = reportRequest['extraArgs']

    # Check sliding plots
    if 'lastSeconds' in reportRequestExtra:
      try:
        lastSeconds = int(reportRequestExtra['lastSeconds'])
      except ValueError:
        gLogger.error("lastSeconds key must be a number")
        return S_ERROR("Value Error")
      if lastSeconds < 3600:
        return S_ERROR("lastSeconds must be more than 3600")
      now = Time.dateTime()  # this is an UTC time
      reportRequest['endTime'] = now
      reportRequest['startTime'] = now - datetime.timedelta(seconds=lastSeconds)
    else:
      # if end date is not there, just set it to now
      if not reportRequest.get('endTime'):
        # check the existence of the endTime it can be present and empty
        reportRequest['endTime'] = Time.dateTime()
    # Check keys
    for key in self.__reportRequestDict:
      if key not in reportRequest:
        return S_ERROR('Missing mandatory field %s in plot request' % key)

      if not isinstance(reportRequest[key], self.__reportRequestDict[key]):
        return S_ERROR("Type mismatch for field %s (%s), required one of %s" % (key,
                                                                                str(type(reportRequest[key])),
                                                                                str(self.__reportRequestDict[key])))
      if key in ('startTime', 'endTime'):
        reportRequest[key] = int(Time.toEpoch(reportRequest[key]))

    return S_OK(reportRequest)
Exemplo n.º 50
0
    def execute(self):
        """ Main execution method
    """
        # Get the WMS Snapshot!
        result = JobDB().getSummarySnapshot(self.__jobDBFields)
        now = Time.dateTime()
        if not result['OK']:
            self.log.error(
                "Can't get the JobDB summary",
                "%s: won't commit at this cycle" % result['Message'])
            return S_ERROR()

        # Now we try to commit
        values = result['Value'][1]

        self.log.info("Start sending records")
        for record in values:
            record = record[1:]
            rD = {}
            for fV in self.__summaryDefinedFields:
                rD[fV[0]] = fV[1]
            for iP in range(len(self.__summaryKeyFieldsMapping)):
                fieldName = self.__summaryKeyFieldsMapping[iP]
                rD[self.__renameFieldsMapping.get(fieldName,
                                                  fieldName)] = record[iP]
            record = record[len(self.__summaryKeyFieldsMapping):]
            for iP in range(len(self.__summaryValueFieldsMapping)):
                rD[self.__summaryValueFieldsMapping[iP]] = int(record[iP])

            for backend in self.datastores:
                if backend.lower() == 'monitoring':
                    rD['timestamp'] = int(Time.toEpoch(now))
                    self.datastores['Monitoring'].addRecord(rD)

                elif backend.lower() == 'accounting':
                    acWMS = WMSHistory()
                    acWMS.setStartTime(now)
                    acWMS.setEndTime(now)
                    acWMS.setValuesFromDict(rD)
                    retVal = acWMS.checkValues()
                    if not retVal['OK']:
                        self.log.error("Invalid accounting record ",
                                       "%s -> %s" % (retVal['Message'], rD))
                    else:
                        self.datastores['Accounting'].addRegister(acWMS)

        for backend, datastore in self.datastores.items():
            self.log.info("Committing to %s backend" % backend)
            result = datastore.commit()
            if not result['OK']:
                self.log.error("Couldn't commit WMS history to %s" % backend,
                               result['Message'])
                return S_ERROR()
            self.log.verbose("Done committing to %s backend" % backend)

        return S_OK()
Exemplo n.º 51
0
 def debug(self, sMsg, sVarMsg=''):
     # In case of S_ERROR structure make full string representation
     if isReturnStructure(sMsg):
         sMsg = reprReturnErrorStructure(sMsg, full=True)
     if isReturnStructure(sVarMsg):
         sVarMsg = reprReturnErrorStructure(sVarMsg, full=True)
     messageObject = Message(self._systemName, self._logLevels.debug,
                             Time.dateTime(), sMsg, sVarMsg,
                             self.__discoverCallingFrame())
     return self.processMessage(messageObject)
Exemplo n.º 52
0
  def addFileRecord( self, lfns, status, minor = "Unknown", date = None, source = "Unknown" ):
    """ Add a new entry to the DataLoggingDB table. 

    :warning: Optionally the time stamp of the status can be provided in a form of a string 
    in a format '%Y-%m-%d %H:%M:%S' or as datetime.datetime object. If the time stamp is not 
    provided the current UTC time is used.

    :param self: self reference
    :param list lfns: list of LFNs
    :param str status: status
    :param str minor: additional information
    :param mixed date: date and time
    :param str source: source setting the new status
    """
    self.gLogger.info( "Entering records for %s lfns: %s/%s from source %s" % ( len( lfns ), status, minor, source ) )
    _date = date
    if not date:
      _date = Time.dateTime()
    if type( date ) in StringTypes:
      _date = Time.fromString( date )

    try:
      time_order = Time.to2K( _date ) - NEW_MAGIC_EPOCH_2K
    except AttributeError:
      gLogger.error( 'Wrong date argument given using current time stamp' )
      date = Time.dateTime()
      time_order = Time.to2K( date ) - NEW_MAGIC_EPOCH_2K

    inDict = { 'Status': status,
               'MinorStatus': minor,
               'StatusTime': date,
               'StatusTimeOrder': time_order,
               'Source': source
              }
    result = S_OK( 0 )
    for lfn in lfns:
      inDict['LFN'] = lfn
      res = self.insertFields( self.tableName, inDict = inDict )
      if not res['OK']:
        return res
      result['Value'] += res['Value']
      result['lastRowId'] = res['lastRowId']
    return result
Exemplo n.º 53
0
    def _initMonitoring(self):
        if not self.activityMonitoring:
            # Init extra bits of monitoring
            self._monitor.setComponentType(MonitoringClient.COMPONENT_SERVICE)
            self._monitor.setComponentName(self._name)
            self._monitor.setComponentLocation(self._cfg.getURL())
            self._monitor.initialize()
            self._monitor.registerActivity("Connections",
                                           "Connections received", "Framework",
                                           "connections",
                                           MonitoringClient.OP_RATE)
            self._monitor.registerActivity("Queries", "Queries served",
                                           "Framework", "queries",
                                           MonitoringClient.OP_RATE)
            self._monitor.registerActivity("CPU", "CPU Usage", "Framework",
                                           "CPU,%", MonitoringClient.OP_MEAN,
                                           600)
            self._monitor.registerActivity("MEM", "Memory Usage", "Framework",
                                           "Memory,MB",
                                           MonitoringClient.OP_MEAN, 600)
            self._monitor.registerActivity("PendingQueries", "Pending queries",
                                           "Framework", "queries",
                                           MonitoringClient.OP_MEAN)
            self._monitor.registerActivity("ActiveQueries", "Active queries",
                                           "Framework", "threads",
                                           MonitoringClient.OP_MEAN)
            self._monitor.registerActivity("RunningThreads", "Running threads",
                                           "Framework", "threads",
                                           MonitoringClient.OP_MEAN)
            self._monitor.registerActivity("MaxFD", "Max File Descriptors",
                                           "Framework", "fd",
                                           MonitoringClient.OP_MEAN)

            self._monitor.setComponentExtraParam("DIRACVersion", DIRAC.version)
            self._monitor.setComponentExtraParam("platform",
                                                 DIRAC.getPlatform())
            self._monitor.setComponentExtraParam("startTime", Time.dateTime())
            props = [("__doc__", "description")]
            if six.PY2:
                props += [("__RCSID__", "version")]
            for prop in props:
                try:
                    value = getattr(self._handler["module"], prop[0])
                except Exception as e:
                    gLogger.exception(e)
                    gLogger.error("Missing property", prop[0])
                    value = "unset"
                self._monitor.setComponentExtraParam(prop[1], value)

        for secondaryName in self._cfg.registerAlsoAs():
            gLogger.info("Registering %s also as %s" %
                         (self._name, secondaryName))
            self._validNames.append(secondaryName)

        return S_OK()
Exemplo n.º 54
0
  def web_getPilotSummaryData(self):
    RPC = RPCClient("WorkloadManagement/WMSAdministrator", timeout = 600 )
    callback = {}
    req = self.__request()

    result = yield self.threadTask(RPC.getPilotSummaryWeb, req, self.globalSort , self.pageNumber, self.numberOfJobs)

    if not result["OK"]:
      self.finish({"success":"false", "result":[], "total":0, "error":result["Message"]})
      return

    result = result["Value"]

    if not result.has_key("TotalRecords"):
      self.finish({"success":"false", "result":[], "total":-1, "error":"Data structure is corrupted"})
      return


    if not (result["TotalRecords"] > 0):
      self.finish({"success":"false", "result":[], "total":0, "error":"There were no data matching your selection"})
      return


    if not (result.has_key("ParameterNames") and result.has_key("Records")):
      self.finish({"success":"false", "result":[], "total":-1, "error":"Data structure is corrupted"})
      return

    if not (len(result["ParameterNames"]) > 0):
      self.finish({"success":"false", "result":[], "total":-1, "error":"ParameterNames field is missing"})
      return

    if not (len(result["Records"]) > 0):
      self.finish({"success":"false", "result":[], "total":0, "Message":"There are no data to display"})
      return

    callback = []
    jobs = result["Records"]
    head = result["ParameterNames"]
    headLength = len(head)

    for i in jobs:
      tmp = {}
      for j in range(0,headLength):
        tmp[head[j]] = i[j]
      callback.append(tmp)
    total = result["TotalRecords"]
    total = result["TotalRecords"]
    timestamp = Time.dateTime().strftime("%Y-%m-%d %H:%M [UTC]")
    if result.has_key("Extras"):
      st = self.__dict2string({})
      extra = result["Extras"]
      callback = {"success":"true", "result":callback, "total":total, "extra":extra, "request":st, "date":timestamp }
    else:
      callback = {"success":"true", "result":callback, "total":total, "date":timestamp}
    self.finish(callback)
Exemplo n.º 55
0
    def __checkPlotRequest(self, reportRequest):
        # If extraArgs is not there add it
        if "extraArgs" not in reportRequest:
            reportRequest["extraArgs"] = {}
        if not isinstance(reportRequest["extraArgs"],
                          self.__reportRequestDict["extraArgs"]):
            return S_ERROR("Extra args has to be of type %s" %
                           self.__reportRequestDict["extraArgs"])
        reportRequestExtra = reportRequest["extraArgs"]
        # Check sliding plots
        if "lastSeconds" in reportRequestExtra:
            try:
                lastSeconds = int(reportRequestExtra["lastSeconds"])
            except ValueError:
                gLogger.error("lastSeconds key must be a number")
                return S_ERROR("Value Error")
            if lastSeconds < 3600:
                return S_ERROR("lastSeconds must be more than 3600")
            now = Time.dateTime()
            reportRequest["endTime"] = now
            reportRequest["startTime"] = now - datetime.timedelta(
                seconds=lastSeconds)
        else:
            # if enddate is not there, just set it to now
            if not reportRequest.get("endTime", False):
                reportRequest["endTime"] = Time.dateTime()
        # Check keys
        for key in self.__reportRequestDict:
            if key not in reportRequest:
                return S_ERROR("Missing mandatory field %s in plot reques" %
                               key)

            if not isinstance(reportRequest[key],
                              self.__reportRequestDict[key]):
                return S_ERROR(
                    "Type mismatch for field %s (%s), required one of %s" %
                    (key, str(type(reportRequest[key])),
                     str(self.__reportRequestDict[key])))
            if key in ("startTime", "endTime"):
                reportRequest[key] = int(Time.toEpoch(reportRequest[key]))

        return S_OK(reportRequest)
Exemplo n.º 56
0
 def loadCredentials(self):
     retVal = getProxyInfo()
     if retVal["OK"]:
         credDict = retVal["Value"]
         self.commiterId = "%s@%s - %s" % (
             credDict["username"],
             credDict["group"],
             Time.dateTime().strftime("%Y-%m-%d %H:%M:%S"),
         )
         return retVal
     return retVal
Exemplo n.º 57
0
    def web_getResourceSummaryData(self):
        '''This method returns the data required to fill the grid.
    '''

        requestParams = self.__requestParams()
        gLogger.info(requestParams)

        pub = RPCClient('ResourceStatus/Publisher')

        elementStatuses = yield self.threadTask(
            pub.getElementStatuses, 'Resource', requestParams['name'],
            requestParams['elementType'], requestParams['statusType'],
            requestParams['status'], requestParams['tokenOwner'])
        if not elementStatuses['OK']:
            self.finish({
                'success': 'false',
                'error': elementStatuses['Message']
            })

        elementTree = collections.defaultdict(list)

        for element in elementStatuses['Value']:

            elementDict = dict(zip(elementStatuses['Columns'], element))

            elementDict['DateEffective'] = str(elementDict['DateEffective'])
            elementDict['LastCheckTime'] = str(elementDict['LastCheckTime'])
            elementDict['TokenExpiration'] = str(
                elementDict['TokenExpiration'])

            elementTree[elementDict['Name']].append(elementDict)

        elementList = []

        for elementValues in elementTree.values():

            if len(elementValues) == 1:
                elementList.append(elementValues[0])
            else:

                elementList.append(self.combine(elementValues))

        rssMachine = RSSMachine(None)

        yield self.threadTask(rssMachine.orderPolicyResults, elementList)

        timestamp = Time.dateTime().strftime("%Y-%m-%d %H:%M [UTC]")

        self.finish({
            'success': 'true',
            'result': elementList,
            'total': len(elementList),
            "date": timestamp
        })
Exemplo n.º 58
0
 def __init__( self, serviceName ):
   self._name = serviceName
   self._startTime = Time.dateTime()
   self._cfg = ServiceConfiguration( serviceName )
   self._validNames = [ self._name ]
   self._monitor = MonitoringClient()
   self.__monitorLastStatsUpdate = time.time()
   self._stats = { 'queries' : 0, 'connections' : 0 }
   self._authMgr = AuthManager( "%s/Authorization" % self._cfg.getServicePath() )
   self._transportPool = getGlobalTransportPool()
   self.__cloneId = 0
Exemplo n.º 59
0
 def __addFoundDefinedComponent(self, compDictList):
     cD = self.walkSet(self.__requiredSet, compDictList[0])
     dbD = self.walkSet(self.__dbSet, compDictList[0])
     now = Time.dateTime()
     unmatched = compDictList
     for dbComp in dbD:
         if 'Status' not in dbComp:
             self.__setStatus(dbComp, 'OK')
             if dbComp['Type'] == "service":
                 if 'Port' not in dbComp:
                     self.__setStatus(dbComp, 'Error',
                                      "Port is not defined")
                 elif dbComp['Port'] not in [
                         compDict['Port'] for compDict in compDictList
                         if 'Port' in compDict
                 ]:
                     self.__setStatus(
                         compDict, 'Error',
                         "Port (%s) is different that specified in the CS" %
                         dbComp['Port'])
             elapsed = now - dbComp['LastHeartbeat']
             elapsed = elapsed.days * 86400 + elapsed.seconds
             if elapsed > self.__maxSecsSinceHeartbeat:
                 self.__setStatus(
                     dbComp, "Error",
                     "Last heartbeat was received at %s (%s secs ago)" %
                     (dbComp['LastHeartbeat'], elapsed))
         cD.append(dbComp)
         #See if we have a perfect match
         newUnmatched = []
         for unmatchedComp in unmatched:
             perfectMatch = True
             for field in unmatchedComp:
                 if field in ('Status', 'Message'):
                     continue
                 if field not in dbComp:
                     perfectMatch = False
                     continue
                 if field == 'Host':
                     result = Network.checkHostsMatch(
                         unmatchedComp[field], dbComp[field])
                     if not result['OK'] or not result['Value']:
                         perfectMatch = False
                 else:
                     if unmatchedComp[field] != dbComp[field]:
                         perfectMatch = False
             if not perfectMatch:
                 newUnmatched.append(unmatchedComp)
         unmatched = newUnmatched
     for unmatchedComp in unmatched:
         self.__setStatus(unmatchedComp, "Error",
                          "There is no component up with this properties")
         cD.append(unmatchedComp)