All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taotao.boot.monitor.strategy.DefaultWarnStrategy Maven / Gradle / Ivy

/*
 * Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.taotao.boot.monitor.strategy;

import com.taotao.boot.common.utils.common.PropertyUtils;
import com.taotao.boot.common.utils.lang.StringUtils;
import com.taotao.boot.monitor.collect.task.IOCollectTask;
import com.taotao.boot.monitor.model.Report;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * DefaultWarnStrategy
 *
 * @author shuigedeng
 * @version 2021.9
 * @since 2021-09-10 17:02:08
 */
public class DefaultWarnStrategy implements WarnStrategy {

    protected static int maxCacheSize = 3;
    protected static List cacheReports = Collections.synchronizedList(new ArrayList<>(maxCacheSize + 2));
    protected Rule.RulesAnalyzer rulesAnalyzer;
    protected WarnTemplate warnTemplate;

    public Rule.RulesAnalyzer getRulesAnalyzer() {
        return rulesAnalyzer;
    }

    public WarnTemplate getWarnTemplate() {
        return warnTemplate;
    }

    public DefaultWarnStrategy(WarnTemplate warnTemplate, Rule.RulesAnalyzer rulesAnalyzer) {
        this.warnTemplate = warnTemplate;
        this.rulesAnalyzer = rulesAnalyzer;

        setDefaultStrategy();
    }

    /**
     * setDefaultStrategy
     *
     * @since 2021-09-10 17:02:40
     */
    public void setDefaultStrategy() {
        rulesAnalyzer.registerRules(
                "cpu.process", PropertyUtils.getPropertyCache("ttc.monitor.strategy.cpu.process", "[>0.7]"));
        rulesAnalyzer.registerRules(
                "cpu.system", PropertyUtils.getPropertyCache("ttc.monitor.strategy.cpu.system", "[>0.7]"));
        rulesAnalyzer.registerRules(
                "io.current.dir.usable.size",
                PropertyUtils.getPropertyCache("ttc.monitor.strategy.io.current.dir.usable.size", "[<500]"));
        rulesAnalyzer.registerRules(
                "memery.jvm.max",
                PropertyUtils.getPropertyCache("ttc.monitor.strategy.memery.jvm.max", "[<256]"));
        rulesAnalyzer.registerRules(
                "memery.system.free",
                PropertyUtils.getPropertyCache("ttc.monitor.strategy.memery.system.free", "[<256]"));
        rulesAnalyzer.registerRules(
                "thread.deadlocked.count",
                PropertyUtils.getPropertyCache("ttc.monitor.strategy.thread.deadlocked.count", "[>10]"));
        rulesAnalyzer.registerRules(
                "thread.total",
                PropertyUtils.getPropertyCache("ttc.monitor.strategy.thread.total", "[>1000]"));
        rulesAnalyzer.registerRules(
                "tomcat.threadPool.pool.size.count",
                PropertyUtils.getPropertyCache(
                        "ttc.monitor.strategy.tomcat.threadPool.pool.size.count", "[>1000]"));
        rulesAnalyzer.registerRules(
                "tomcat.threadPool.active.count",
                PropertyUtils.getPropertyCache(
                        "ttc.monitor.strategy.tomcat.threadPool.active.count", "[>200]"));
        rulesAnalyzer.registerRules(
                "tomcat.threadPool.queue.size",
                PropertyUtils.getPropertyCache("ttc.monitor.strategy.tomcat.threadPool.queue.size", "[>50]"));

        if (rulesAnalyzer.getRules("io.current.dir.usable.size") != null) {
            // 设置报警回调
            rulesAnalyzer.getRules("io.current.dir.usable.size").forEach(c -> {
                if (c.getType() == Rule.RuleType.less) {
                    c.setHitCallBack((value) -> IOCollectTask.clearLog());
                }
            });
        }
    }

    /**
     * analyse
     *
     * @param report report
     * @return {@link Report }
     * @since 2021-09-10 17:02:44
     */
    @Override
    public Report analyse(Report report) {
        while (cacheReports.size() > maxCacheSize) {
            cacheReports.remove(0);
        }

        cacheReports.add(report);
        Report avgReport = report.avgReport(cacheReports);
        return rulesAnalyzer.analyse(avgReport);
    }

    /**
     * analyseText
     *
     * @param report report
     * @return {@link String }
     * @since 2021-09-10 17:02:49
     */
    @Override
    public String analyseText(Report report) {
        Report r = analyse(report);
        StringBuilder warn = new StringBuilder();

        r.eachReport((filed, item) -> {
            if (item.isWarn()) {
                warn.append(warnTemplate.getWarnContent(filed, item.getDesc(), item.getValue(), item.getRule()));
            }
            return item;
        });

        String warnInfo = warn.toString();
        if (StringUtils.isEmpty(warnInfo)) {
            return "";
        }
        return warnInfo;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy