python3中 filter()
返回的是可迭代对象,python2中 filter()
返回的是过原列表经过函数过滤后的新列表,也就是把原本Py2中的纯列表转为了更省内存的迭代器
对传给render_to_string()函数的字典参数值中,包含了被filter函数过滤后的值,被渲染后,出现了信息缺失
def filter_muted_instance_item(format_alerts):
"""屏蔽掉一个实例指定报警项"""
muted_instance_item = set(
[(i.ip, i.port, i.item) for i in AlertMute.objects.filter(start_time__lte=datetime.datetime.now(),
end_time__gte=datetime.datetime.now())])
filtered_set = filter(lambda x: (x.ip, x.port, x.item) not in muted_instance_item, [format_alert for format_alert in format_alerts])
return filtered_set
# # 定义修饰器 protype
# def host_item_filter(alertor_func):
# @functools.wraps(alertor_func)
# def modifier(*args, **kwargs):
# kwargs['queryset'] = filter_muted_ip(kwargs['queryset'])
# alertor_func(**kwargs)
# return modifier
# 定义修饰器,使用wrapt包简化代码
def item_filter(filter_type='instance_item'):
@wrapt.decorator
def wrapper(wrapped, instance, args, kwargs):
if filter_type == 'ip_item':
kwargs['format_alerts'] = filter_muted_ip(kwargs['format_alerts'])
elif filter_type == 'instance_item':
kwargs['format_alerts'] = filter_muted_instance_item(kwargs['format_alerts'])
return wrapped(*args, **kwargs)
return wrapper
@item_filter(filter_type='instance_item')
def mail_alert(alarm_type=None, format_alerts=None, scan_time=datetime.datetime.now(), **kwargs):
"""
使用预置的邮件模板渲染后发送报警邮件
:param alarm_type: 报警类型
:param format_alerts: 警报结果集
:param scan_time: 警报产生时间
:param kwargs: 杂项参数
:return: 无返回项,程序内直接发送邮件
"""
subject = "报警发送标题"
template = "报警发送预置HTML模板"
if alarm_type == 'dbagent_heartbeat':
template = 'dbAlertAPP/AgentHeartbeatAlarm.html'
subject = 'dbagent心跳报警'
elif:
...........
else:
..........
# 这里注意,被filter修饰器过滤后的元组对象列表变为可以迭代的filter对象,也就是把原本Py2中的纯列表转为了更省内存的迭代器
# 但是渲染器无法识别filter对象,也无法识别把list(iterable_filter)直接带入到参数字典中,需要用表达式转一次
result_list = list(format_alerts)
if kwargs.get("check_map"):
html_string = render_to_string(template, {"results": result_list,
"scanTime": scan_time,
"checkMap": kwargs.get("check_map")
}
)
else:
html_string = render_to_string(template, {"results": result_list,
"scanTime": scan_time,
}
)
try:
send_mail(subject=environment_prefix+subject, message='plain_message', html_message=html_string,
from_email=EMAIL_HOST_USER,
recipient_list=get_recivers(), fail_silently=False)
except Exception as e:
p.error(e)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。