一、目录结构
二、报警策略队列处理
1、入口MonitorServer
import os
import sys if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CrazyMonitor.settings")
from monitor.backends.management import execute_from_command_line
execute_from_command_line(sys.argv)
2、启动调用management
def trigger_watch(self):
'''start to listen triggers'''
trigger_watch = trigger_handler.TriggerHandler(settings)
trigger_watch.start_watching()
3、实例化
1、支持三个参数,启动、停止监控触发器
2、通过反射的形式,
3、监听所有的触发器
4、同一个频道,声明一下就开始接收了
class TriggerHandler(object): def __init__(self,django_settings):
self.django_settings = django_settings
self.redis = redis_conn.redis_conn(self.django_settings)
self.alert_counters ={} #纪录每个action的触发报警次数
'''alert_counters = {
1: {2:{'counter':0,'last_alert':None}, #k 1是主机id, {2:{'counter'}} 2是trigger id
4:{'counter':1,'last_alert':None}}, #k是action id,
#2: {2:0},
}'''
三、打开收音机准备挺广播
1、功能如下
1、打开收音机
2、调频
3、准备接听
4、阻塞有数据的话,就往下走
2、实现代码
def start_watching(self):
'''
start listening and watching the needed to be handled triggers from other process
:return:
''' radio = self.redis.pubsub() #打开收音机
radio.subscribe(self.django_settings.TRIGGER_CHAN) #调频
radio.parse_response() #ready to watch 准备接听
print("\033[43;1m************start listening new triggers**********\033[0m")
self.trigger_count = 0
while True:
msg = radio.parse_response() #阻塞有数据的话,就往下走
self.trigger_consume(msg)
3、调频频道设置
settings配置如下内容
TRIGGER_CHAN = 'trigger_event_channel'