作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 elastalert 中有一条规则,如果两小时内没有付款,则会发送通知。
我还有一个匹配增强功能,每晚从 0:00 到 8:00 AM 都会删除这些通知:
from elastalert.enhancements import BaseEnhancement, DropMatchException
import datetime
import time
import sys
def datetime_from_utc_to_local(utc_datetime):
now_timestamp = time.time()
offset = datetime.datetime.fromtimestamp(now_timestamp) - datetime.datetime.utcfromtimestamp(now_timestamp)
return utc_datetime + offset
class DropFrom00To06(BaseEnhancement):
def process(self, match):
dateformat = "%Y-%m-%dT%H:%M:%S"
exceptional_dateformat = "%Y-%m-%dT%H:%M:%SZ"
timestamp = match['@timestamp'].split(".")[0]
try:
timestamp = datetime.datetime.strptime(timestamp, dateformat)
except ValueError:
timestamp = datetime.datetime.strptime(timestamp, exceptional_dateformat)
except:
print("Unexpected error:", sys.exc_info()[0])
raise
timestamp = datetime_from_utc_to_local(timestamp)
timePart = timestamp.time()
if timePart >= datetime.time(00, 00) and timePart <= datetime.time(8, 00):
raise DropMatchException()
最佳答案
解决方案是这样的:
from elastalert.enhancements import BaseEnhancement, DropMatchException
import datetime
import time
import sys
def datetime_from_utc_to_local(utc_datetime):
now_timestamp = time.time()
offset = datetime.datetime.fromtimestamp(now_timestamp) - datetime.datetime.utcfromtimestamp(now_timestamp)
return utc_datetime + offset
class DropFrom00To06(BaseEnhancement):
def process(self, match):
dateformat = "%Y-%m-%dT%H:%M:%S"
exceptional_dateformat = "%Y-%m-%dT%H:%M:%SZ"
timestamp = match['@timestamp'].split(".")[0]
try:
timestamp = datetime.datetime.strptime(timestamp, dateformat)
except ValueError:
timestamp = datetime.datetime.strptime(timestamp, exceptional_dateformat)
except:
print("Unexpected error:", sys.exc_info()[0])
raise
timestamp = datetime_from_utc_to_local(timestamp)
timePart = timestamp.time()
d = timestamp.date()
day = d.weekday()
elif day == 6 and timePart >= datetime.time(00, 00) and timePart <= datetime.time(10, 00):
raise DropMatchException()
elif timePart >= datetime.time(00, 00) and timePart <= datetime.time(8, 00):
raise DropMatchException()
关于elasticsearch - 由于假阳性病例,周日早上放松 elastalert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45922608/
我是一名优秀的程序员,十分优秀!