Example #1
0
    def execute(self):
        """ The main agent execution method
    """
        limitDate = toString(dateTime() - self.period)
        limitDate = limitDate[:limitDate.find('.')]

        commonString = 'FROM MessageRepository WHERE messageTime <'
        cmd = "SELECT count(*) %s '%s'" % (commonString, limitDate)
        result = self.SystemLoggingDB._query(cmd)
        if not result['OK']:
            return result
        recordsToErase = result['Value'][0][0]

        if recordsToErase == 0:
            self.log.info('No records to erase')
            return S_OK('No records to erase')
        else:
            cmd = "DELETE LOW_PRIORITY %s '%s'" % (commonString, limitDate)
            result = self.SystemLoggingDB._update(cmd)
            if not result['OK']:
                self.log.error('Could not erase the requested records',
                               'those older than %s' % limitDate)
                return result
            else:
                self.log.info('%s records have been erased' % recordsToErase)
                return result
  def execute(self):
    """ The main agent execution method
    """
    limitDate = toString( dateTime() - self.period )
    limitDate = limitDate[:limitDate.find('.')]

    commonString = 'FROM MessageRepository WHERE messageTime <'
    cmd = "SELECT count(*) %s '%s'" % ( commonString, limitDate )
    result = self.SystemLoggingDB._query( cmd )
    if not result['OK']: 
      return result
    recordsToErase=result['Value'][0][0]

    if recordsToErase == 0:
      self.log.info('No records to erase')
      return S_OK('No records to erase')
    else:
      cmd = "DELETE LOW_PRIORITY %s '%s'" % ( commonString, limitDate )
      result =  self.SystemLoggingDB._update( cmd )
      if not result['OK']:
        self.log.error( 'Could not erase the requested records',
                        'those older than %s' % limitDate )
        return result
      else:
        self.log.info('%s records have been erased' % recordsToErase )
        return result
  def execute( self ):
    """ The main agent execution method
    """
    limitDate = date() - self._period
    tableList = [ "MessageRepository", "FixedTextMessages", "Systems",
                  "SubSystems" ]
    columnsList = [ "SystemName", "SubSystemName", "count(*) as entries",
                    "FixedTextString" ]
    cmd = "SELECT " + ', '.join( columnsList ) + " FROM " \
          + " NATURAL JOIN ".join( tableList ) \
          + " WHERE MessageTime > '%s'" % limitDate \
          + " GROUP BY FixedTextString HAVING entries > %s" % self._threshold \
          + " ORDER BY entries DESC LIMIT %i;" % self._limit

    result = self.systemLoggingDB._query( cmd )
    if not result['OK']:
      return result

    messageList = result['Value']

    if messageList == 'None' or messageList == ():
      self.log.warn( 'The DB query returned an empty result' )
      return S_OK()

    mailBody = '\n'
    for message in messageList:
      mailBody = mailBody + "Count: " + str( message[2] ) + "\tError: '"\
                 + message[3] + "'\tSystem: '" + message[0]\
                 + "'\tSubsystem: '" + message[1] + "'\n"

    mailBody = mailBody + "\n\n-------------------------------------------------------\n"\
               + "Please do not reply to this mail. It was automatically\n"\
               + "generated by a Dirac Agent.\n"

    result = self.systemLoggingDB._getDataFromAgentTable( self.agentName )
    self.log.debug( result )
    if not result['OK']:
      errorString = "Could not get the date when the last mail was sent"
      self.log.error( errorString )
      return S_ERROR( errorString )
    else:
      if len( result['Value'] ):
        self.log.debug( "date value: %s" % fromString( result['Value'][0][0][1:-1] ) )
        lastMailSentDate = fromString( result['Value'][0][0][1:-1] )
      else:
        lastMailSentDate = limitDate - 1 * day
        result = self.systemLoggingDB._insertDataIntoAgentTable( self.agentName, lastMailSentDate )
        if not result['OK']:
          errorString = "Could not insert data into the DB"
          self.log.error( errorString, result['Message'] )
          return S_ERROR( errorString + ": " + result['Message'] )

    self.log.debug( "limitDate: %s\t" % limitDate \
                   + "lastMailSentDate: %s\n" % lastMailSentDate )
    if lastMailSentDate > limitDate:
      self.log.info( "The previous report was sent less "\
                     + " than %s days ago" % self.__days )
      return S_OK()

    dateSent = toString( date() )
    self.log.info( "The list with the top errors has been sent" )

    result = self.systemLoggingDB._insertDataIntoAgentTable( self.agentName, dateSent )
    if not result['OK']:
      errorString = "Could not insert data into the DB"
      self.log.error( errorString, result['Message'] )
      return S_ERROR( errorString + ": " + result['Message'] )

    result = self.notification.sendMail( self._mailAddress, self._subject,
                                         mailBody )
    if not result[ 'OK' ]:
      self.log.warn( "The notification could not be sent" )
      return S_OK()

    return S_OK( "The list with the top errors has been sent" )
  def execute( self ):
    """ The main agent execution method
    """
    limitDate = date() - self._period
    tableList = [ "MessageRepository", "FixedTextMessages", "Systems",
                  "SubSystems" ]
    columnsList = [ "SystemName", "SubSystemName", "count(*) as entries",
                    "FixedTextString" ]
    cmd = "SELECT " + ', '.join( columnsList ) + " FROM " \
          + " NATURAL JOIN ".join( tableList ) \
          + " WHERE MessageTime > '%s'" % limitDate \
          + " GROUP BY FixedTextString HAVING entries > %s" % self._threshold \
          + " ORDER BY entries DESC LIMIT %i;" % self._limit

    result = self.systemLoggingDB._query( cmd )
    if not result['OK']:
      return result

    messageList = result['Value']

    if messageList == 'None' or messageList == ():
      self.log.warn( 'The DB query returned an empty result' )
      return S_OK()

    mailBody = '\n'
    for message in messageList:
      mailBody = mailBody + "Count: " + str( message[2] ) + "\tError: '"\
                 + message[3] + "'\tSystem: '" + message[0]\
                 + "'\tSubsystem: '" + message[1] + "'\n"

    mailBody = mailBody + "\n\n-------------------------------------------------------\n"\
               + "Please do not reply to this mail. It was automatically\n"\
               + "generated by a Dirac Agent.\n"

    result = self.systemLoggingDB._getDataFromAgentTable( self.agentName )
    self.log.debug( result )
    if not result['OK']:
      errorString = "Could not get the date when the last mail was sent"
      self.log.error( errorString )
      return S_ERROR( errorString )
    else:
      if len( result['Value'] ):
        self.log.debug( "date value: %s" % fromString( result['Value'][0][0][1:-1] ) )
        lastMailSentDate = fromString( result['Value'][0][0][1:-1] )
      else:
        lastMailSentDate = limitDate - 1 * day
        result = self.systemLoggingDB._insertDataIntoAgentTable( self.agentName, lastMailSentDate )
        if not result['OK']:
          errorString = "Could not insert data into the DB"
          self.log.error( errorString, result['Message'] )
          return S_ERROR( errorString + ": " + result['Message'] )

    self.log.debug( "limitDate: %s\t" % limitDate \
                   + "lastMailSentDate: %s\n" % lastMailSentDate )
    if lastMailSentDate > limitDate:
      self.log.info( "The previous report was sent less "\
                     + " than %s days ago" % self.__days )
      return S_OK()

    dateSent = toString( date() )
    self.log.info( "The list with the top errors has been sent" )

    result = self.systemLoggingDB._insertDataIntoAgentTable( self.agentName, dateSent )
    if not result['OK']:
      errorString = "Could not insert data into the DB"
      self.log.error( errorString, result['Message'] )
      return S_ERROR( errorString + ": " + result['Message'] )

    result = self.notification.sendMail( self._mailAddress, self._subject,
                                         mailBody )
    if not result[ 'OK' ]:
      self.log.warn( "The notification could not be sent" )
      return S_OK()

    return S_OK( "The list with the top errors has been sent" )