2014-04-14 03:18:06 +04:00
|
|
|
#!/usr/bin/env python
|
2014-04-16 22:40:15 +04:00
|
|
|
|
2014-04-14 03:18:06 +04:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-04-16 22:40:15 +04:00
|
|
|
# Copyright (c) 2014 Mozilla Corporation
|
2014-04-14 03:18:06 +04:00
|
|
|
#
|
2014-04-16 22:40:15 +04:00
|
|
|
# Contributors:
|
|
|
|
# Jeff Bryner jbryner@mozilla.com
|
2014-06-19 20:49:50 +04:00
|
|
|
# Anthony Verez averez@mozilla.com
|
2014-04-16 22:40:15 +04:00
|
|
|
|
2014-06-19 20:49:50 +04:00
|
|
|
# set this to run as a cronjob
|
|
|
|
# to regularly remove indexes
|
2014-04-14 03:18:06 +04:00
|
|
|
# run it after backup of course
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import pyes
|
2014-06-19 20:49:50 +04:00
|
|
|
import logging
|
2014-04-14 03:18:06 +04:00
|
|
|
from datetime import datetime
|
|
|
|
from datetime import date
|
|
|
|
from datetime import timedelta
|
|
|
|
from configlib import getConfig, OptionParser
|
|
|
|
|
|
|
|
|
2014-06-19 20:49:50 +04:00
|
|
|
logger = logging.getLogger(sys.argv[0])
|
|
|
|
logger.level=logging.INFO
|
|
|
|
formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')
|
|
|
|
|
2014-06-19 22:17:27 +04:00
|
|
|
|
2014-04-14 03:18:06 +04:00
|
|
|
def esPruneIndexes():
|
2014-06-19 20:49:50 +04:00
|
|
|
if options.output == 'syslog':
|
|
|
|
logger.addHandler(SysLogHandler(address=(options.sysloghostname, options.syslogport)))
|
|
|
|
else:
|
|
|
|
sh = logging.StreamHandler(sys.stderr)
|
|
|
|
sh.setFormatter(formatter)
|
|
|
|
logger.addHandler(sh)
|
|
|
|
|
|
|
|
logger.debug('started')
|
|
|
|
try:
|
|
|
|
es = pyes.ES((list('{0}'.format(s) for s in options.esservers)))
|
|
|
|
indices = es.indices.stats()['indices'].keys()
|
|
|
|
# do the pruning
|
|
|
|
for (index, dobackup, rotation, pruning) in zip(options.indices,
|
|
|
|
options.dobackup, options.rotation, options.pruning):
|
|
|
|
try:
|
|
|
|
if pruning != '0':
|
|
|
|
index_to_prune = index
|
|
|
|
if rotation == 'daily':
|
|
|
|
idate = date.strftime(datetime.utcnow()-timedelta(days=int(pruning)),'%Y%m%d')
|
|
|
|
index_to_prune += '-%s' % idate
|
|
|
|
elif rotation == 'monthly':
|
|
|
|
idate = date.strftime(datetime.utcnow()-timedelta(days=31*int(pruning)),'%Y%m')
|
|
|
|
index_to_prune += '-%s' % idate
|
2014-04-14 03:18:06 +04:00
|
|
|
|
2014-06-19 20:49:50 +04:00
|
|
|
logger.debug('Deleting index %s...' % index_to_prune)
|
|
|
|
if index_to_prune in indices:
|
|
|
|
es.indices.delete_index(index_to_prune)
|
|
|
|
logger.debug('Deletion successful')
|
|
|
|
else:
|
|
|
|
logger.error('Error deleting index %s, index missing' % index_to_prune)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error("Unhandled exception while deleting %s, terminating: %r" % (index_to_prune, e))
|
2014-04-14 03:18:06 +04:00
|
|
|
|
2014-06-19 20:49:50 +04:00
|
|
|
except Exception as e:
|
|
|
|
logger.error("Unhandled exception, terminating: %r"%e)
|
2014-04-14 03:18:06 +04:00
|
|
|
|
|
|
|
|
|
|
|
def initConfig():
|
2014-06-19 20:49:50 +04:00
|
|
|
# output our log to stdout or syslog
|
|
|
|
options.output = getConfig(
|
|
|
|
'output',
|
|
|
|
'stdout',
|
|
|
|
options.configfile
|
|
|
|
)
|
|
|
|
# syslog hostname
|
|
|
|
options.sysloghostname = getConfig(
|
|
|
|
'sysloghostname',
|
|
|
|
'localhost',
|
|
|
|
options.configfile
|
|
|
|
)
|
|
|
|
options.syslogport = getConfig(
|
|
|
|
'syslogport',
|
|
|
|
514,
|
|
|
|
options.configfile
|
|
|
|
)
|
2014-04-14 03:18:06 +04:00
|
|
|
options.esservers = list(getConfig(
|
|
|
|
'esservers',
|
|
|
|
'http://localhost:9200',
|
|
|
|
options.configfile).split(',')
|
|
|
|
)
|
2014-06-19 20:49:50 +04:00
|
|
|
options.indices = list(getConfig(
|
|
|
|
'backup_indices',
|
|
|
|
'events,alerts,kibana-int',
|
|
|
|
options.configfile).split(',')
|
|
|
|
)
|
|
|
|
options.dobackup = list(getConfig(
|
|
|
|
'backup_dobackup',
|
|
|
|
'1,1,1',
|
|
|
|
options.configfile).split(',')
|
|
|
|
)
|
|
|
|
options.rotation = list(getConfig(
|
|
|
|
'backup_rotation',
|
|
|
|
'daily,monthly,none',
|
|
|
|
options.configfile).split(',')
|
|
|
|
)
|
|
|
|
options.pruning = list(getConfig(
|
|
|
|
'backup_pruning',
|
|
|
|
'20,0,0',
|
|
|
|
options.configfile).split(',')
|
|
|
|
)
|
|
|
|
# aws credentials to use to send files to s3
|
|
|
|
options.aws_access_key_id = getConfig(
|
|
|
|
'aws_access_key_id',
|
|
|
|
'',
|
|
|
|
options.configfile
|
|
|
|
)
|
|
|
|
options.aws_secret_access_key = getConfig(
|
|
|
|
'aws_secret_access_key',
|
|
|
|
'',
|
|
|
|
options.configfile
|
|
|
|
)
|
2014-04-14 03:18:06 +04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = OptionParser()
|
|
|
|
parser.add_option("-c",
|
|
|
|
dest='configfile',
|
2014-06-19 23:14:22 +04:00
|
|
|
default=sys.argv[0].replace('.py', '.conf'),
|
2014-04-14 03:18:06 +04:00
|
|
|
help="configuration file to use")
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
initConfig()
|
|
|
|
esPruneIndexes()
|