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
|
2019-08-02 02:41:37 +03:00
|
|
|
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-04-16 22:40:15 +04:00
|
|
|
# Copyright (c) 2014 Mozilla Corporation
|
|
|
|
|
2014-12-19 03:54:25 +03:00
|
|
|
# set this to run as a cronjob (after backup has completed)
|
2014-06-19 20:49:50 +04:00
|
|
|
# to regularly remove indexes
|
2014-12-19 03:54:25 +03:00
|
|
|
|
|
|
|
# .conf file will determine what indexes are operated on
|
|
|
|
# Create a starter .conf file with backupDiscover.py
|
2014-04-14 03:18:06 +04:00
|
|
|
|
|
|
|
import sys
|
|
|
|
from datetime import datetime
|
|
|
|
from datetime import date
|
|
|
|
from datetime import timedelta
|
|
|
|
from configlib import getConfig, OptionParser
|
|
|
|
|
2018-10-16 22:45:04 +03:00
|
|
|
from mozdef_util.utilities.toUTC import toUTC
|
2019-04-26 02:25:07 +03:00
|
|
|
from mozdef_util.utilities.logger import logger
|
2018-10-16 22:45:04 +03:00
|
|
|
from mozdef_util.elasticsearch_client import ElasticsearchClient
|
2016-10-24 21:21:52 +03:00
|
|
|
|
2014-04-14 03:18:06 +04:00
|
|
|
|
|
|
|
def esPruneIndexes():
|
2014-06-19 20:49:50 +04:00
|
|
|
logger.debug('started')
|
|
|
|
try:
|
2016-10-24 21:21:52 +03:00
|
|
|
es = ElasticsearchClient((list('{0}'.format(s) for s in options.esservers)))
|
|
|
|
indices = es.get_indices()
|
2014-06-19 20:49:50 +04:00
|
|
|
# do the pruning
|
2018-10-31 02:16:38 +03:00
|
|
|
for (index, dobackup, rotation, pruning) in zip(options.indices, options.dobackup, options.rotation, options.pruning):
|
2014-06-19 20:49:50 +04:00
|
|
|
try:
|
|
|
|
if pruning != '0':
|
|
|
|
index_to_prune = index
|
|
|
|
if rotation == 'daily':
|
2018-12-14 21:49:25 +03:00
|
|
|
idate = date.strftime(toUTC(datetime.now()) - timedelta(days=int(pruning)), '%Y%m%d')
|
2014-06-19 20:49:50 +04:00
|
|
|
index_to_prune += '-%s' % idate
|
|
|
|
elif rotation == 'monthly':
|
2018-12-14 21:49:25 +03:00
|
|
|
idate = date.strftime(datetime.utcnow() - timedelta(days=31 * int(pruning)), '%Y%m')
|
2014-06-19 20:49:50 +04:00
|
|
|
index_to_prune += '-%s' % idate
|
2014-04-14 03:18:06 +04:00
|
|
|
|
2014-06-19 20:49:50 +04:00
|
|
|
if index_to_prune in indices:
|
2017-12-08 21:57:17 +03:00
|
|
|
logger.debug('Deleting index: %s' % index_to_prune)
|
2016-10-24 21:21:52 +03:00
|
|
|
es.delete_index(index_to_prune, True)
|
2014-06-19 20:49:50 +04:00
|
|
|
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:
|
2018-12-14 21:49:25 +03:00
|
|
|
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
|
2018-12-14 21:39:23 +03:00
|
|
|
)
|
2014-06-19 20:49:50 +04:00
|
|
|
# syslog hostname
|
|
|
|
options.sysloghostname = getConfig(
|
|
|
|
'sysloghostname',
|
|
|
|
'localhost',
|
|
|
|
options.configfile
|
2018-12-14 21:39:23 +03:00
|
|
|
)
|
2014-06-19 20:49:50 +04:00
|
|
|
options.syslogport = getConfig(
|
|
|
|
'syslogport',
|
|
|
|
514,
|
|
|
|
options.configfile
|
2018-12-14 21:39:23 +03:00
|
|
|
)
|
2014-04-14 03:18:06 +04:00
|
|
|
options.esservers = list(getConfig(
|
|
|
|
'esservers',
|
|
|
|
'http://localhost:9200',
|
|
|
|
options.configfile).split(',')
|
2018-12-14 21:39:23 +03:00
|
|
|
)
|
2014-06-19 20:49:50 +04:00
|
|
|
options.indices = list(getConfig(
|
|
|
|
'backup_indices',
|
2016-11-29 19:09:31 +03:00
|
|
|
'events,alerts,.kibana',
|
2014-06-19 20:49:50 +04:00
|
|
|
options.configfile).split(',')
|
2018-12-14 21:39:23 +03:00
|
|
|
)
|
2014-06-19 20:49:50 +04:00
|
|
|
options.dobackup = list(getConfig(
|
|
|
|
'backup_dobackup',
|
|
|
|
'1,1,1',
|
|
|
|
options.configfile).split(',')
|
2018-12-14 21:39:23 +03:00
|
|
|
)
|
2014-06-19 20:49:50 +04:00
|
|
|
options.rotation = list(getConfig(
|
|
|
|
'backup_rotation',
|
|
|
|
'daily,monthly,none',
|
|
|
|
options.configfile).split(',')
|
2018-12-14 21:39:23 +03:00
|
|
|
)
|
2014-06-19 20:49:50 +04:00
|
|
|
options.pruning = list(getConfig(
|
|
|
|
'backup_pruning',
|
|
|
|
'20,0,0',
|
|
|
|
options.configfile).split(',')
|
2018-12-14 21:39:23 +03:00
|
|
|
)
|
2014-04-14 03:18:06 +04:00
|
|
|
|
2018-10-31 02:04:55 +03:00
|
|
|
|
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()
|