一、负责把达到报警条件的trigger进行分析 ,并根据 action 表中的配置来进行报警

1、目录结构

2、功能如下

  1. 1、找到trigger的关联动作,
  2. 2、收到的数据传给trigger_msg就是trigger_data
  3. 3、trigger_id') == None怎么会等于None
  4. 4、每个action都可以直接包含多个主机或主机组

3、实现代码

class ActionHandler(object):
'''
负责把达到报警条件的trigger进行分析 ,并根据 action 表中的配置来进行报警
''' def __init__(self,trigger_data,alert_counter_dic):
self.trigger_data = trigger_data
#self.trigger_process()
self.alert_counter_dic = alert_counter_dic def record_log(self,action_obj,action_operation,host_id,trigger_data):
"""record alert log into DB"""
models.EventLog.objects.create(
event_type = 0,
host_id=host_id,
trigger_id = trigger_data.get('trigger_id'),
log = trigger_data
)

二、报警发送邮件内容

1、实现代码

   def action_email(self,action_obj,action_operation_obj,host_id,trigger_data):
'''
sending alert email to who concerns.
:param action_obj: 触发这个报警的action对象
:param action_operation_obj: 要报警的动作对象
:param host_id: 要报警的目标主机
:param trigger_data: 要报警的数据
:return:
''' print("要发报警的数据:",self.alert_counter_dic[action_obj.id][host_id])
print("action email:",action_operation_obj.action_type,action_operation_obj.notifiers,trigger_data)
notifier_mail_list = [obj.email for obj in action_operation_obj.notifiers.all()]
subject = '级别:%s -- 主机:%s -- 服务:%s' %(trigger_data.get('trigger_id'),
trigger_data.get('host_id'),
trigger_data.get('service_item')) send_mail(
subject,
action_operation_obj.msg_format,
settings.DEFAULT_FROM_EMAIL,
notifier_mail_list,
)

2、邮件截图类似于下图

三、分析trigger并报警

1、为什么tigger里关联了template,template里又关联了主机,那action还要直接关联主机呢?

  那是因为一个trigger可以被多个template关联,这个trigger触发了,不一定是哪个tempalte里的主机导致的

2、功能如下

1、第一次被 触,先初始化一个action counter dic

2、这个主机第一次触发这个action的报警

  你不是触发一次我报一次,我是到了触发时间触发才报警,
3、如果达到报警触发interval次数,就记数+1

4、该报警了

  1. 报完警后更新一下报警时间 ,这样就又重新计算alert interval了
  2. 打印离下次触发报警的时间还有[%s]s" %

3、实现代码

    def trigger_process(self):
'''
分析trigger并报警
:return:
'''
print('Action Processing'.center(50,'-')) if self.trigger_data.get('trigger_id') == None: #trigger id == None
print(self.trigger_data)
if self.trigger_data.get('msg'):
print(self.trigger_data.get('msg')) #既然没有trigger id,直接报警给管理 员
else:
print("\033[41;1mInvalid trigger data %s\033[0m" % self.trigger_data) else:#正经的trigger 报警要触发了
print("\033[33;1m%s\033[0m" %self.trigger_data) trigger_id = self.trigger_data.get('trigger_id')
host_id = self.trigger_data.get('host_id')
trigger_obj = models.Trigger.objects.get(id=trigger_id)
actions_set = trigger_obj.action_set.select_related() #找到这个trigger所关联的action list
print("actions_set:",actions_set)
matched_action_list = set() # 一个空集合
for action in actions_set:
#每个action 都 可以直接 包含多个主机或主机组,
# 为什么tigger里关联了template,template里又关联了主机,那action还要直接关联主机呢?
#那是因为一个trigger可以被多个template关联,这个trigger触发了,不一定是哪个tempalte里的主机导致的
for hg in action.host_groups.select_related():
for h in hg.host_set.select_related():
if h.id == host_id:# 这个action适用于此主机
matched_action_list.add(action)
if action.id not in self.alert_counter_dic: #第一次被 触,先初始化一个action counter dic
self.alert_counter_dic[action.id] = {}
print("action, ",id(action))
if h.id not in self.alert_counter_dic[action.id]: # 这个主机第一次触发这个action的报警
self.alert_counter_dic[action.id][h.id] = {'counter': 0, 'last_alert': time.time()}
# self.alert_counter_dic.setdefault(action,{h.id:{'counter':0,'last_alert':time.time()}})
else:
#如果达到报警触发interval次数,就记数+1
if time.time() - self.alert_counter_dic[action.id][h.id]['last_alert'] >= action.interval:
self.alert_counter_dic[action.id][h.id]['counter'] += 1
#self.alert_counter_dic[action.id][h.id]['last_alert'] = time.time() else:
print("没达到alert interval时间,不报警",action.interval,
time.time() - self.alert_counter_dic[action.id][h.id]['last_alert'])
#self.alert_counter_dic.setdefault(action.id,{}) for host in action.hosts.select_related():
if host.id == host_id: # 这个action适用于此主机
matched_action_list.add(action)
if action.id not in self.alert_counter_dic: # 第一次被 触,先初始化一个action counter dic
self.alert_counter_dic[action.id] = {}
if h.id not in self.alert_counter_dic[action.id]: #这个主机第一次触发这个action的报警
self.alert_counter_dic[action.id][h.id] ={'counter': 0, 'last_alert': time.time()}
#self.alert_counter_dic.setdefault(action,{h.id:{'counter':0,'last_alert':time.time()}})
else:
# 如果达到报警触发interval次数,就记数+1
if time.time() - self.alert_counter_dic[action.id][h.id]['last_alert'] >= action.interval:
self.alert_counter_dic[action.id][h.id]['counter'] += 1
#self.alert_counter_dic[action.id][h.id]['last_alert'] = time.time()
else:
print("没达到alert interval时间,不报警", action.interval,
time.time() - self.alert_counter_dic[action.id][h.id]['last_alert']) print("alert_counter_dic:",self.alert_counter_dic)
print("matched_action_list:",matched_action_list)
for action_obj in matched_action_list:#
if time.time() - self.alert_counter_dic[action_obj.id][host_id]['last_alert'] >= action_obj.interval:
#该报警 了
print("该报警了.......",time.time() - self.alert_counter_dic[action_obj.id][host_id]['last_alert'],action_obj.interval)
for action_operation in action_obj.operations.select_related().order_by('-step'):
if action_operation.step > self.alert_counter_dic[action_obj.id][host_id]['counter']:
#就
print("##################alert action:%s" %
action_operation.action_type,action_operation.notifiers) action_func = getattr(self,'action_%s'% action_operation.action_type)
action_func(action_obj,action_operation,host_id,self.trigger_data) #报完警后更新一下报警时间 ,这样就又重新计算alert interval了
self.alert_counter_dic[action_obj.id][host_id]['last_alert'] = time.time()
self.record_log(action_obj,action_operation,host_id,self.trigger_data)
# else:
# print("离下次触发报警的时间还有[%s]s" % )

  

分布式监控系统开发【day38】:报警模块解析(六)的更多相关文章

  1. Python之路,Day20 - 分布式监控系统开发

    Python之路,Day20 - 分布式监控系统开发   本节内容 为什么要做监控? 常用监控系统设计讨论 监控系统架构设计 监控表结构设计 为什么要做监控? –熟悉IT监控系统的设计原理 –开发一个 ...

  2. 分布式监控系统开发【day37】:需求讨论(一)

    本节内容 为什么要做监控? 常用监控系统设计讨论 监控需求讨论 如何实现监控服务器的水平扩展? 监控系统架构设计 一.为什么要做监控? 熟悉IT监控系统的设计原理 开发一个简版的类Zabbix监控系统 ...

  3. Python之分布式监控系统开发

    为什么要做监控? –熟悉IT监控系统的设计原理 –开发一个简版的类Zabbix监控系统 –掌握自动化开发项目的程序设计思路及架构解藕原则 常用监控系统设计讨论 Zabbix Nagios 监控系统需求 ...

  4. day26 分布式监控系统开发

    本节内容 为什么要做监控? 常用监控系统设计讨论 监控系统架构设计 监控表结构设计 为什么要做监控? –熟悉IT监控系统的设计原理 –开发一个简版的类Zabbix监控系统 –掌握自动化开发项目的程序设 ...

  5. 分布式监控系统开发【day38】:报警阈值程序逻辑解析(三)

    一.需求讨论 1.请问如何解决延迟问题 1000台机器,每1分钟循环一次但是刚好第一次循环第一秒刚处理完了,结果还没等到第二分钟又出问题,你那必须等到第二次循环,假如我这个服务很重要必须实时知道,每次 ...

  6. 分布式监控系统开发【day38】:主机存活检测程序解析(七)

    一.目录结构 二.入口 1.文件MonitorServer.py import os import sys if __name__ == "__main__": os.enviro ...

  7. 分布式监控系统开发【day38】:报警自动升级代码解析及测试(八)

    一.报警自动升级代码解析 发送邮件代码 def action_email(self,action_obj,action_operation_obj,host_id,trigger_data): ''' ...

  8. 分布式监控系统开发【day38】:监控trigger表结构设计(一)

    一.需求讨论 1.zabbix触发器的模板截图 1.zabbix2.4.7 2.zabbix3.0 2.模板与触发器关联的好处 好处就是可以批量处理,比如我说我有1000机器都要监控cpu.内存.IO ...

  9. 分布式监控系统开发【day38】:报警策略队列处理(五)

    一.目录结构 二.报警策略队列处理 1.入口MonitorServer import os import sys if __name__ == "__main__": os.env ...

随机推荐

  1. Swift JSON字符串和字典以及数组的互转

    1.JSONString转换为字典 // JSONString转换为字典 func getDictionaryFromJSONString(jsonString:String) ->NSDict ...

  2. Flex builder4.6激活【转】

    方法一: 1.到Adobe官网下载FlashBuilder 4.6 http://download.adobe.com/pub/adobe/flex/win/FlashBuilder_4_6_LS10 ...

  3. MySql 学习之路-高级2

    目录: 1.约束 2.ALTER TABLE 3.VIEW 1.约束 说明:SQL约束用于规定表中的数据规则,如果存在违反约束的数据行为,行为会被约束终止,约束可以在建表是规定,也可以在建表后规定,通 ...

  4. docker容器日志收集方案(方案一 filebeat+本地日志收集)

    filebeat不用多说就是扫描本地磁盘日志文件,读取文件内容然后远程传输. docker容器日志默认记录方式为 json-file 就是将日志以json格式记录在磁盘上 格式如下: { " ...

  5. 用 PLSQL 创建新用户及导入 dmp

    一.创建表空间 在导入 dmp 文件之前,你要在数据库里面给它分配一片存储它的地方(表空间). 如果我们知道需要导入的数据库的表空间直接创建就可以,如果不不知道,也没有关系,我们可以用 txt 打开 ...

  6. 模块简介:(random)(xml,json,pickle,shelve)(time,datetime)(os,sys)(shutil)(pyYamal,configparser)(hashlib)

    Random模块: #!/usr/bin/env python #_*_encoding: utf-8_*_ import random print (random.random()) #0.6445 ...

  7. 【Linux基础】判断当前机器是虚拟机还是物理机

    1.使用dmidecode命令查看(root权限) DMI (Desktop Management Interface, DMI)的主要组成部分是Management InformationForma ...

  8. flink window的early计算

    Tumbing Windows:滚动窗口,窗口之间时间点不重叠.它是按照固定的时间,或固定的事件个数划分的,分别可以叫做滚动时间窗口和滚动事件窗口.Sliding Windows:滑动窗口,窗口之间时 ...

  9. RPC是什么?

    初学微服务,一点会问RPC是什么,通常网上的资料会说,是一种协议,然后说得很复杂,一堆概念,拜托,我只是想知道RPC是什么,而不是  怎么实现怎么做. RPC就是想实现函数调用模式的网络化,A服务(微 ...

  10. WEB框架-Django框架学习-预备知识

    今日份整理,终于开始整个阶段学习的后期了,今日开始学习Django的框架,加油,你是最胖的! 1.web基础知识 1.1 web应用 Web应用程序是一种可以通过Web访问的应用程序,程序的最大好处是 ...