зеркало из https://github.com/mozilla/MozDef.git
Resolve events and events-previous alias rotation in single call per alias name.
This commit is contained in:
Родитель
434ead47da
Коммит
f6243e1242
|
@ -54,6 +54,7 @@ def esRotateIndexes():
|
|||
indices = es.get_indices()
|
||||
|
||||
# calc dates for use in index names events-YYYYMMDD, alerts-YYYYMM, etc.
|
||||
pdate_day = date.strftime(toUTC(datetime.now()) - timedelta(days=2), '%Y%m%d')
|
||||
odate_day = date.strftime(toUTC(datetime.now()) - timedelta(days=1), '%Y%m%d')
|
||||
odate_month = date.strftime(toUTC(datetime.now()) - timedelta(days=1), '%Y%m')
|
||||
ndate_day = date.strftime(toUTC(datetime.now()), '%Y%m%d')
|
||||
|
@ -65,9 +66,11 @@ def esRotateIndexes():
|
|||
options.dobackup, options.rotation, options.pruning):
|
||||
try:
|
||||
if rotation != 'none':
|
||||
previndex = index
|
||||
oldindex = index
|
||||
newindex = index
|
||||
if rotation == 'daily':
|
||||
previndex += '-%s' % pdate_day
|
||||
oldindex += '-%s' % odate_day
|
||||
newindex += '-%s' % ndate_day
|
||||
elif rotation == 'monthly':
|
||||
|
@ -81,12 +84,17 @@ def esRotateIndexes():
|
|||
logger.debug('Creating %s index' % newindex)
|
||||
es.create_index(newindex)
|
||||
# set aliases: events to events-YYYYMMDD
|
||||
# and events-previous to events-YYYYMMDD-1 for example
|
||||
# and events-previous to events-YYYYMMDD-1
|
||||
logger.debug('Setting {0} alias to index: {1}'.format(index, newindex))
|
||||
es.create_alias(index, newindex)
|
||||
alias = index
|
||||
index = newindex
|
||||
es.update_alias(oldindex, index, alias)
|
||||
if oldindex in indices:
|
||||
logger.debug('Setting {0}-previous alias to index: {1}'.format(index, oldindex))
|
||||
es.create_alias('%s-previous' % index, oldindex)
|
||||
alias = 'events-previous'
|
||||
index = oldindex
|
||||
oldindex = previndex
|
||||
logger.debug('Setting {0} to index: {1}'.format(alias, index))
|
||||
es.update_alias(oldindex, index, alias)
|
||||
else:
|
||||
logger.debug('Old index %s is missing, do not change %s-previous alias' % (oldindex, index))
|
||||
except Exception as e:
|
||||
|
@ -99,7 +107,7 @@ def esRotateIndexes():
|
|||
current_date = toUTC(datetime.now())
|
||||
for index in options.weekly_rotation_indices:
|
||||
weekly_index_alias = '%s-weekly' % index
|
||||
logger.debug('Trying to realias {0} to indices since {1}'.format(weekly_index_alias, week_ago_str))
|
||||
logger.debug('Trying to re-alias {0} to indices since {1}'.format(weekly_index_alias, week_ago_str))
|
||||
existing_weekly_indices = []
|
||||
for day_obj in daterange(week_ago_date, current_date):
|
||||
day_str = day_obj.strftime('%Y%m%d')
|
||||
|
@ -160,12 +168,23 @@ def initConfig():
|
|||
'20,0,0',
|
||||
options.configfile).split(',')
|
||||
)
|
||||
|
||||
options.weekly_rotation_indices = list(getConfig(
|
||||
'weekly_rotation_indices',
|
||||
'events',
|
||||
options.configfile).split(',')
|
||||
)
|
||||
options.daily_index_alias = list(getConfig(
|
||||
'daily_index_alias',
|
||||
'events',
|
||||
options.configfile)
|
||||
)
|
||||
options.previous_alias = list(getConfig(
|
||||
'previous_alias',
|
||||
'events-previous',
|
||||
options.configfile)
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -176,4 +195,4 @@ if __name__ == '__main__':
|
|||
help="configuration file to use")
|
||||
(options, args) = parser.parse_args()
|
||||
initConfig()
|
||||
esRotateIndexes()
|
||||
esRotateIndexes()
|
|
@ -56,11 +56,21 @@ class ElasticsearchClient():
|
|||
}'''
|
||||
self.es_connection.indices.create(index=index_name, update_all_types='true', body=mapping)
|
||||
|
||||
def create_alias(self, alias_name, index_name):
|
||||
if self.es_connection.indices.exists_alias(index='*', name=alias_name):
|
||||
self.es_connection.indices.delete_alias(index='*', name=alias_name)
|
||||
# def create_alias(self, alias_name, index_name):
|
||||
# if self.es_connection.indices.exists_alias(index='*', name=alias_name):
|
||||
# self.es_connection.indices.delete_alias(index='*', name=alias_name)
|
||||
#
|
||||
# self.es_connection.indices.put_alias(index=index_name, name=alias_name)
|
||||
|
||||
self.es_connection.indices.put_alias(index=index_name, name=alias_name)
|
||||
def update_alias(self, oldindex, index, alias):
|
||||
actions = None
|
||||
if self.es_connection.indices.exists_alias(oldindex, alias):
|
||||
# Add and remove aliases.
|
||||
actions = [
|
||||
{'remove': {'index': oldindex, 'alias': alias}},
|
||||
{'add': {'index': index, 'alias': alias}}
|
||||
]
|
||||
self.es_connection.indices.update_aliases(dict(actions=actions))
|
||||
|
||||
def create_alias_multiple_indices(self, alias_name, indices):
|
||||
if self.es_connection.indices.exists_alias(index='*', name=alias_name):
|
||||
|
|
Загрузка…
Ссылка в новой задаче