Package Products :: Package ZenUtils :: Module ZenDeleteHistory |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenUtils.ZenDeleteHistory

     1  #! /usr/bin/env python
     2  ###########################################################################
     3  #
     4  # This program is part of Zenoss Core, an open source monitoring platform.
     5  # Copyright (C) 2007, Zenoss Inc.
     6  #
     7  # This program is free software; you can redistribute it and/or modify it
     8  # under the terms of the GNU General Public License version 2 as published by
     9  # the Free Software Foundation.
    10  #
    11  # For complete information please visit: http://www.zenoss.com/oss/
    12  #
    13  ###########################################################################
    14
    15  __doc__="""
    16  ZenDeleteHistory
    17  """
    18
    19  from ZenScriptBase import ZenScriptBase
    20
    21
    22 -class ZenDeleteHistory(ZenScriptBase):
    23      """
    24      Delete events from the history table
    25      """
    26
    27 -    def buildOptions(self):
    28          """
    29          Setup the command line options
    30          """
    31          self.parser.add_option('-n', '--numDays',
    32                      dest='numDays', default=None,
    33                      help='Number of days of history to keep')
    34          self.parser.add_option('-d', '--device',
    35                      dest='device', default=None,
    36                      help='Devide id for which to delete events')
    37          ZenScriptBase.buildOptions(self)
    38
    39
    40 -    def deleteHistory(self):
    41          """
    42          Delete historical events.  If device is given then only delete
    43          events for that device.  If numDays is given then only delete
    44          events that are older than that many days.
    45          device and numDays are mutually exclusive.  No real reason for this
    46          other than there is no current need to use both in same call and I
    47          don't want to test the combination.
    48          """
    49          if self.options.numDays:
    50              try:
    51                  self.options.numDays = int(self.options.numDays)
    52              except ValueError:
    53                  raise ValueError('numDays argument must be an integer')
    54
    55          self.connect()
    56
    57          if self.options.device:
    58              statement = 'delete from history '
    59              whereClause = 'where device = "%s"' % self.options.device
    60              reason = 'Deleting events for device %s' % self.options.device
    61              toLog = True
    62          elif self.options.numDays > 0:
    63              statement = ('delete h,j,d from history h '
    64                  'LEFT JOIN log j ON h.evid = j.evid '
    65                  'LEFT JOIN detail d ON h.evid = d.evid ')
    66              whereClause = ('WHERE StateChange < DATE_SUB(NOW(), '
    67                              'INTERVAL %s day)' % self.options.numDays)
    68              reason = ''
    69              toLog = False
    70          else:
    71              return
    72          print '%s%s' % (statement, whereClause)
    73          self.dmd.ZenEventManager.updateEvents(statement, whereClause, reason,
    74                                                  toLog=toLog, table='history')
    75
    76  if __name__ == '__main__':
    77      ZenDeleteHistory().deleteHistory()
    78