forked from Qianlitp/WatchAD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_index.py
48 lines (38 loc) · 1.52 KB
/
delete_index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/python3
# coding: utf-8
# author: 9ian1i https://github.com/Qianlitp
"""
定期删除超过最大保留期限的域控日志索引
"""
import os
import sys
now_path = os.path.abspath(__file__)
home_path = "/".join(now_path.split("/")[:-2])
sys.path.append(home_path)
from tools.database.ElsaticHelper import ElasticHelper
from settings.config import main_config
from settings.database_config import ElasticConfig
from tools.common.common import get_n_days_ago, datetime_to_log_date
from elasticsearch5.exceptions import NotFoundError
from tools.common.Logger import logger
def main():
logger.info("run scheduled task: delete expired index")
es = ElasticHelper()
raw_data_expire = main_config.raw_data_expire
delete_map = {
ElasticConfig.event_log_write_index_prefix: raw_data_expire["dc_log"],
ElasticConfig.traffic_write_index_prefix: raw_data_expire["dc_krb5"],
ElasticConfig.krb5_ticket_write_index_prefix: raw_data_expire["krb5_ticket"],
ElasticConfig.user_activity_write_index_prefix: raw_data_expire["user_activity"]
}
for index_prefix, expire in delete_map.items():
ago = get_n_days_ago(expire)
date = datetime_to_log_date(ago)
index_name = index_prefix + date
try:
es.delete_index(index_name)
logger.info("delete index {name} successfully.".format(name=index_name))
except NotFoundError:
logger.warn("index {name} not found.".format(name=index_name))
if __name__ == '__main__':
main()