You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.5 KiB
72 lines
2.5 KiB
4 months ago
|
# coding=utf-8
|
||
|
"""
|
||
|
@Author: fu-zhe
|
||
|
@FileName: user_cron.py
|
||
|
@DateTime: 2024/5/23 14:19
|
||
|
@Description: 用户相关的定时任务 全量获取,对已有数据进行先删除后写入 周期一天
|
||
|
"""
|
||
|
from __future__ import unicode_literals
|
||
|
|
||
|
import collections
|
||
|
import random,string
|
||
|
import time,datetime
|
||
|
from datetime import datetime, timedelta
|
||
|
import calendar
|
||
|
from uebaMetricsAnalysis.utils.ext_logging import logger
|
||
|
from commandCyberRange.utils.db2json import DBUtils, DBType
|
||
|
from uebaMetricsAnalysis.utils.base_dataclean import entry
|
||
|
|
||
|
JOB_STATUS ={
|
||
|
"RUNNING":1,
|
||
|
"FINISH":2,
|
||
|
"ERROR":3
|
||
|
}
|
||
|
|
||
|
|
||
|
class UserCron:
|
||
|
def generate_job_id():
|
||
|
timestamp = int(time.time() * 1000)
|
||
|
random_letters = ''.join(random.choice(string.ascii_letters) for _ in range(7))
|
||
|
return str(timestamp) + random_letters
|
||
|
|
||
|
#获取 job的执行时间 开始时间-结束时间
|
||
|
def get_job_period(self):
|
||
|
sql = "select job_id, end_time from ueba_clean_jobs order by end_time desc limit 1"
|
||
|
fields=["job_id", "end_time"]
|
||
|
data = DBUtils.transition(fields, sql, DBType.LIST)
|
||
|
start_time = ''
|
||
|
end_time = ''
|
||
|
if len(data)==0:
|
||
|
start_time = datetime.datetime.now() - timedelta(minutes=5)
|
||
|
end_time = datetime.datetime.now()
|
||
|
if len(data)>0:
|
||
|
start_time = data[0].get('end_time')
|
||
|
end_time = data[0].get('end_time') + timedelta(minutes=5)
|
||
|
if end_time > datetime.datetime.now():
|
||
|
return None,None
|
||
|
return start_time,end_time
|
||
|
|
||
|
#每5分钟执行一次
|
||
|
def processing(self):
|
||
|
try:
|
||
|
logger.info("job:开始执行")
|
||
|
start,end=self.get_job_period()
|
||
|
job_id =self.generate_job_id()
|
||
|
DBUtils.insert_job_record(job_id,start,end,JOB_STATUS.get("RUNNING"))
|
||
|
logger.info("job:运行参数:{}".format(start,end))
|
||
|
if start is None or end is None:
|
||
|
logger.info("job:结束时间大于服务器时间不执行")
|
||
|
return
|
||
|
|
||
|
logger.info("job:"+"准备获取es数据")
|
||
|
entry(start,end)
|
||
|
logger.info("job:"+"执行完成")
|
||
|
DBUtils.write_job_status(job_id,JOB_STATUS.get("FINISH"),"")
|
||
|
except Exception ,e:
|
||
|
err_info="定时任务执行失败:".format(str(e), traceback.format_exc())
|
||
|
logger.error(err_info)
|
||
|
DBUtils.write_job_status(job_id,JOB_STATUS.get("ERROR"),err_info)
|
||
|
raise
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
UserCron().processing()
|