暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

CAT源码分析(三)——CAT服务器之AlertMachine

贰级天災 2018-09-22
578

和作为JobMachine时相同,当CAT服务器是AlertMachine时,也额外启动了其它的线程。主要有两类,一类是告警,一类是刷新存储的告警信息。

先看第一类:


一、告警线程


        if (serverConfigManager.isAlertMachine()) {
            TransactionAlert transactionAlert = ctx.lookup(TransactionAlert.class);
            EventAlert eventAlert = ctx.lookup(EventAlert.class);
            BusinessAlert metricAlert = ctx.lookup(BusinessAlert.class);
            ExceptionAlert exceptionAlert = ctx.lookup(ExceptionAlert.class);
            HeartbeatAlert heartbeatAlert = ctx.lookup(HeartbeatAlert.class);
            Threads.forGroup("cat").start(metricAlert);
            Threads.forGroup("cat").start(exceptionAlert);
            Threads.forGroup("cat").start(heartbeatAlert);
            Threads.forGroup("cat").start(transactionAlert);
            Threads.forGroup("cat").start(eventAlert);
        }
复制


可以看到启动了各种类型的告警(注释了的部分已手动去除)。

下面以transactionAlert为例:


    public void run() {        boolean active = TimeHelper.sleepToNextMinute();        while (active) {
            Transaction t = Cat.newTransaction("AlertTransaction", TimeHelper.getMinuteStr());            long current = System.currentTimeMillis();            try {
                MonitorRules monitorRules = m_ruleConfigManager.getMonitorRules();
                Map<String, Rule> rules = monitorRules.getRules();                for (Entry<String, Rule> entry : rules.entrySet()) {                    try {
                        processRule(entry.getValue());
                    } catch (Exception e) {
                        Cat.logError(e);
                    }
                }
                t.setStatus(Transaction.SUCCESS);
            } catch (Exception e) {
                t.setStatus(e);
                Cat.logError(e);
            } finally {
                t.complete();
            }            long duration = System.currentTimeMillis() - current;            try {                if (duration < DURATION) {
                    Thread.sleep(DURATION - duration);
                }
            } catch (InterruptedException e) {
                active = false;
            }
        }
    }
复制



可以看到,线程每分钟执行一次。先重新获取一下监控规则,然后对新规则进行处理。

规则的配置地址:http://localhost:8080/cat/s/config?op=transactionRule。


处理过程:


    private void processRule(Rule rule) {
        List<String> fields = Splitters.by(";").split(rule.getId());
        String domain = fields.get(0);
        String type = fields.get(1);
        String name = fields.get(2);
        String monitor = fields.get(3);
        List<AlertResultEntity> alertResults = computeAlertForRule(domain, type, name, monitor, rule.getConfigs());        for (AlertResultEntity alertResult : alertResults) {
            AlertEntity entity = new AlertEntity();
            entity.setDate(alertResult.getAlertTime()).setContent(alertResult.getContent())
                  .setLevel(alertResult.getAlertLevel());
            entity.setMetric(type + "-" + name + "-" + monitor).setType(getName()).setGroup(domain);
            m_sendManager.addAlert(entity);
        }
    }
复制



计算出要告警的信息,并将结果添加的告警管理器里面,由告警器统一发送。


二、刷新存储告警信息


        public void run() {            long current = TimeHelper.getCurrentMinute().getTime();
            Date start = new Date(current + (1 - StorageConstants.DEFAULT_MINUTE_COUNT) * TimeHelper.ONE_MINUTE);
            Date end = new Date(current + TimeHelper.ONE_MINUTE - TimeHelper.ONE_SECOND);
            Transaction t = Cat.newTransaction("ReloadTask", "StorageAlertRecover");            try {                try {
                    buildAlertInfos(start, end, StorageConstants.SQL_TYPE);
                    buildAlertInfos(start, end, StorageConstants.CACHE_TYPE);
                } catch (DalNotFoundException e) {                    // ignore
                } catch (Exception e) {
                    Cat.logError(e);
                }
                t.setStatus(Transaction.SUCCESS);
            } catch (Exception e) {
                t.setStatus(e);
                Cat.logError(e);
            } finally {
                t.complete();
            }
        }
复制


其实就是简单的启动时从数据库取一下告警信息


文章转载自贰级天災,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论