зеркало из https://github.com/mozilla/MozDef.git
Update rotateIndexes.py cron script
Signed-off-by: Brandon Myers <bmyers@mozilla.com>
This commit is contained in:
Родитель
0a443b8668
Коммит
ac52fc3f70
|
@ -15,13 +15,18 @@
|
||||||
# Create a starter .conf file with backupDiscover.py
|
# Create a starter .conf file with backupDiscover.py
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import pyes
|
|
||||||
import logging
|
import logging
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from datetime import date
|
from datetime import date
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from configlib import getConfig, OptionParser
|
from configlib import getConfig, OptionParser
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../lib'))
|
||||||
|
from utilities.toUTC import toUTC
|
||||||
|
from elasticsearch_client import ElasticsearchClient
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(sys.argv[0])
|
logger = logging.getLogger(sys.argv[0])
|
||||||
logger.level=logging.DEBUG
|
logger.level=logging.DEBUG
|
||||||
|
@ -38,13 +43,15 @@ def esRotateIndexes():
|
||||||
|
|
||||||
logger.debug('started')
|
logger.debug('started')
|
||||||
try:
|
try:
|
||||||
es = pyes.ES((list('{0}'.format(s) for s in options.esservers)))
|
es = ElasticsearchClient((list('{0}'.format(s) for s in options.esservers)))
|
||||||
indices = es.indices.stats()['indices'].keys()
|
|
||||||
|
indices = es.get_indices()
|
||||||
|
|
||||||
# calc dates for use in index names events-YYYYMMDD, alerts-YYYYMM, etc.
|
# calc dates for use in index names events-YYYYMMDD, alerts-YYYYMM, etc.
|
||||||
odate_day = date.strftime(datetime.utcnow()-timedelta(days=1),'%Y%m%d')
|
odate_day = date.strftime(toUTC(datetime.now()) - timedelta(days=1), '%Y%m%d')
|
||||||
odate_month = date.strftime(datetime.utcnow()-timedelta(days=1),'%Y%m')
|
odate_month = date.strftime(toUTC(datetime.now()) - timedelta(days=1), '%Y%m')
|
||||||
ndate_day = date.strftime(datetime.utcnow(),'%Y%m%d')
|
ndate_day = date.strftime(toUTC(datetime.now()), '%Y%m%d')
|
||||||
ndate_month = date.strftime(datetime.utcnow(),'%Y%m')
|
ndate_month = date.strftime(toUTC(datetime.now()), '%Y%m')
|
||||||
|
|
||||||
# examine each index in the .conf file
|
# examine each index in the .conf file
|
||||||
# for rotation settings
|
# for rotation settings
|
||||||
|
@ -66,14 +73,14 @@ def esRotateIndexes():
|
||||||
continue
|
continue
|
||||||
if newindex not in indices:
|
if newindex not in indices:
|
||||||
logger.debug('Creating %s index' % newindex)
|
logger.debug('Creating %s index' % newindex)
|
||||||
es.indices.create_index(newindex)
|
es.create_index(newindex)
|
||||||
# set aliases: events to events-YYYYMMDD
|
# set aliases: events to events-YYYYMMDD
|
||||||
# and events-previous to events-YYYYMMDD-1 for example
|
# and events-previous to events-YYYYMMDD-1 for example
|
||||||
logger.debug('Setting {0} alias to index: {1}'.format(index, newindex))
|
logger.debug('Setting {0} alias to index: {1}'.format(index, newindex))
|
||||||
es.indices.set_alias(index, newindex)
|
es.create_alias(index, newindex)
|
||||||
if oldindex in indices:
|
if oldindex in indices:
|
||||||
logger.debug('Setting {0}-previous alias to index: {1}'.format(index, oldindex))
|
logger.debug('Setting {0}-previous alias to index: {1}'.format(index, oldindex))
|
||||||
es.indices.set_alias('%s-previous' % index, oldindex)
|
es.create_alias('%s-previous' % index, oldindex)
|
||||||
else:
|
else:
|
||||||
logger.debug('Old index %s is missing, do not change %s-previous alias' % (oldindex, index))
|
logger.debug('Old index %s is missing, do not change %s-previous alias' % (oldindex, index))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
Загрузка…
Ссылка в новой задаче