持续定时加载报警策略
()
| 8 | |
| 9 | //持续定时加载报警策略 |
| 10 | func (c *Controller) loadStrategiesForever() { |
| 11 | var wg sync.WaitGroup |
| 12 | for { |
| 13 | // 获取所有产品线 |
| 14 | products := mydb.GetProducts() |
| 15 | // 更新产品线告警队列 |
| 16 | c.refreshQueue(products) |
| 17 | for { |
| 18 | if len(c.nodePool.Nodes) != 0 { |
| 19 | break |
| 20 | } |
| 21 | lg.Warn("no inspector connected, do not generate task, retry after 1 seconds") |
| 22 | time.Sleep(time.Second) |
| 23 | } |
| 24 | |
| 25 | for _, product := range products { |
| 26 | // 根据产品线 id 获取策略 |
| 27 | strategies := mydb.GetStrategies(product.ID) |
| 28 | wg.Add(1) |
| 29 | go func(strategies []*types.Strategy) { |
| 30 | defer wg.Done() |
| 31 | for _, strategy := range strategies { |
| 32 | if strategy.Enable == false { |
| 33 | lg.Info("strategy %s is not enabled, skipped it.", strategy.Name) |
| 34 | continue |
| 35 | } |
| 36 | // 根据策略 id 获取 trigger |
| 37 | triggers := mydb.GetTriggersByStrategyID(strategy.ID) |
| 38 | //如果没有 trigger 则忽略 |
| 39 | if len(triggers) == 0 { |
| 40 | lg.Warn("strategy %s has no trigger, skipped it.", strategy.Name) |
| 41 | continue |
| 42 | } |
| 43 | // 生成 AlarmTask |
| 44 | c.processSingleStrategy(strategy, triggers) |
| 45 | } |
| 46 | }(strategies) |
| 47 | } |
| 48 | wg.Wait() |
| 49 | time.Sleep(time.Second * time.Duration(GlobalConfig.LOAD_STRATEGIES_INTERVAL)) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func (c *Controller) processSingleStrategy(strategy *types.Strategy, triggers map[string]*types.Trigger) { |
| 54 | globalHosts := make([]*types.Host, 0) |
no test coverage detected