Search in sources :

Example 1 with MonitorNotifyLog

use of io.jpom.model.log.MonitorNotifyLog in project Jpom by dromara.

the class MonitorItem method checkNotify.

/**
 * 检查状态
 *
 * @param monitorModel 监控信息
 * @param nodeModel    节点信息
 * @param id           项目id
 * @param copyId       副本id
 * @param runStatus    当前运行状态
 */
private boolean checkNotify(MonitorModel monitorModel, NodeModel nodeModel, String id, String copyId, boolean runStatus) {
    // 获取上次状态
    String projectCopyId = id;
    String copyMsg = StrUtil.EMPTY;
    if (StrUtil.isNotEmpty(copyId)) {
        projectCopyId = StrUtil.format("{}:{}", id, copyId);
        copyMsg = StrUtil.format("副本:{}", copyId);
    }
    boolean pre = this.getPreStatus(monitorModel.getId(), nodeModel.getId(), projectCopyId);
    String title = null;
    String context = null;
    // 查询项目运行状态
    if (runStatus) {
        if (!pre) {
            // 上次是异常状态
            title = StrUtil.format("【{}】节点的【{}】项目{}已经恢复正常运行", nodeModel.getName(), id, copyMsg);
            context = "";
        }
    } else {
        // 
        if (monitorModel.autoRestart()) {
            // 执行重启
            try {
                JsonMessage<String> reJson = NodeForward.requestBySys(nodeModel, NodeUrl.Manage_Restart, "id", id, "copyId", copyId);
                if (reJson.getCode() == HttpStatus.HTTP_OK) {
                    // 重启成功
                    runStatus = true;
                    title = StrUtil.format("【{}】节点的【{}】项目{}已经停止,已经执行重启操作,结果成功", nodeModel.getName(), id, copyMsg);
                } else {
                    title = StrUtil.format("【{}】节点的【{}】项目{}已经停止,已经执行重启操作,结果失败", nodeModel.getName(), id, copyMsg);
                }
                context = "重启结果:" + reJson;
            } catch (Exception e) {
                DefaultSystemLog.getLog().error("执行重启操作", e);
                title = StrUtil.format("【{}】节点的【{}】项目{}已经停止,重启操作异常", nodeModel.getName(), id, copyMsg);
                context = ExceptionUtil.stacktraceToString(e);
            }
        } else {
            title = StrUtil.format("【{}】节点的【{}】项目{}已经没有运行", nodeModel.getName(), id, copyMsg);
            context = "请及时检查";
        }
    }
    if (!pre && !runStatus) {
        // 上一次也是异常,并且当前也是异常
        return false;
    }
    MonitorNotifyLog monitorNotifyLog = new MonitorNotifyLog();
    monitorNotifyLog.setStatus(runStatus);
    monitorNotifyLog.setTitle(title);
    monitorNotifyLog.setContent(context);
    monitorNotifyLog.setCreateTime(System.currentTimeMillis());
    monitorNotifyLog.setNodeId(nodeModel.getId());
    monitorNotifyLog.setProjectId(id);
    monitorNotifyLog.setMonitorId(monitorModel.getId());
    // 
    this.notifyMsg(nodeModel, monitorNotifyLog);
    return runStatus;
}
Also used : MonitorNotifyLog(io.jpom.model.log.MonitorNotifyLog)

Example 2 with MonitorNotifyLog

use of io.jpom.model.log.MonitorNotifyLog in project Jpom by dromara.

the class MonitorItem method reqNodeStatus.

/**
 * 检查节点节点对信息
 *
 * @param nodeModel 节点
 * @param projects  项目
 * @return true 所有项目都正常
 */
private boolean reqNodeStatus(NodeModel nodeModel, List<String> projects) {
    if (projects == null || projects.isEmpty()) {
        return true;
    }
    List<Boolean> collect = projects.stream().map(id -> {
        // 
        String title;
        String context;
        try {
            // 查询项目运行状态
            JsonMessage<JSONObject> jsonMessage = NodeForward.requestBySys(nodeModel, NodeUrl.Manage_GetProjectStatus, "id", id, "getCopy", true);
            if (jsonMessage.getCode() == HttpStatus.HTTP_OK) {
                JSONObject jsonObject = jsonMessage.getData();
                int pid = jsonObject.getIntValue("pId");
                boolean runStatus = this.checkNotify(monitorModel, nodeModel, id, null, pid > 0);
                // 检查副本
                List<Boolean> booleanList = null;
                JSONArray copys = jsonObject.getJSONArray("copys");
                if (CollUtil.isNotEmpty(copys)) {
                    booleanList = copys.stream().map(o -> {
                        JSONObject jsonObject1 = (JSONObject) o;
                        String copyId = jsonObject1.getString("copyId");
                        boolean status = jsonObject1.getBooleanValue("status");
                        return MonitorItem.this.checkNotify(monitorModel, nodeModel, id, copyId, status);
                    }).filter(aBoolean -> !aBoolean).collect(Collectors.toList());
                }
                return runStatus && CollUtil.isEmpty(booleanList);
            } else {
                title = StrUtil.format("【{}】节点的状态码异常:{}", nodeModel.getName(), jsonMessage.getCode());
                context = jsonMessage.toString();
            }
        } catch (Exception e) {
            DefaultSystemLog.getLog().error("监控 {} 节点异常 {}", nodeModel.getName(), e.getMessage());
            // 
            title = StrUtil.format("【{}】节点的运行状态异常", nodeModel.getName());
            context = ExceptionUtil.stacktraceToString(e);
        }
        // 获取上次状态
        boolean pre = this.getPreStatus(monitorModel.getId(), nodeModel.getId(), id);
        if (pre) {
            // 上次正常
            MonitorNotifyLog monitorNotifyLog = new MonitorNotifyLog();
            monitorNotifyLog.setStatus(false);
            monitorNotifyLog.setTitle(title);
            monitorNotifyLog.setContent(context);
            monitorNotifyLog.setCreateTime(System.currentTimeMillis());
            monitorNotifyLog.setNodeId(nodeModel.getId());
            monitorNotifyLog.setProjectId(id);
            monitorNotifyLog.setMonitorId(monitorModel.getId());
            // 
            this.notifyMsg(nodeModel, monitorNotifyLog);
        }
        return false;
    }).filter(aBoolean -> !aBoolean).collect(Collectors.toList());
    return CollUtil.isEmpty(collect);
}
Also used : IPlugin(io.jpom.plugin.IPlugin) ExceptionUtil(cn.hutool.core.exceptions.ExceptionUtil) DefaultSystemLog(cn.jiangzeyin.common.DefaultSystemLog) IdUtil(cn.hutool.core.util.IdUtil) MonitorModel(io.jpom.model.data.MonitorModel) HashMap(java.util.HashMap) ProjectInfoCacheService(io.jpom.service.node.ProjectInfoCacheService) Order(cn.hutool.db.sql.Order) JsonMessage(cn.jiangzeyin.common.JsonMessage) JSONArray(com.alibaba.fastjson.JSONArray) ProjectInfoCacheModel(io.jpom.model.node.ProjectInfoCacheModel) NodeService(io.jpom.service.node.NodeService) Map(java.util.Map) PluginFactory(io.jpom.plugin.PluginFactory) NodeForward(io.jpom.common.forward.NodeForward) NodeModel(io.jpom.model.data.NodeModel) MonitorService(io.jpom.service.monitor.MonitorService) Direction(cn.hutool.db.sql.Direction) DbMonitorNotifyLogService(io.jpom.service.dblog.DbMonitorNotifyLogService) SpringUtil(cn.jiangzeyin.common.spring.SpringUtil) Collectors(java.util.stream.Collectors) NodeUrl(io.jpom.common.forward.NodeUrl) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) List(java.util.List) Task(cn.hutool.cron.task.Task) JSONObject(com.alibaba.fastjson.JSONObject) MonitorNotifyLog(io.jpom.model.log.MonitorNotifyLog) ThreadUtil(cn.hutool.core.thread.ThreadUtil) UserModel(io.jpom.model.data.UserModel) UserService(io.jpom.service.user.UserService) HttpStatus(cn.hutool.http.HttpStatus) JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) MonitorNotifyLog(io.jpom.model.log.MonitorNotifyLog)

Example 3 with MonitorNotifyLog

use of io.jpom.model.log.MonitorNotifyLog in project Jpom by dromara.

the class MonitorItem method getPreStatus.

/**
 * 获取上次是否也为异常状态
 *
 * @param monitorId 监控id
 * @param nodeId    节点id
 * @param projectId 项目id
 * @return true 为正常状态,false 异常状态
 */
private boolean getPreStatus(String monitorId, String nodeId, String projectId) {
    // 检查是否已经触发通知
    MonitorNotifyLog monitorNotifyLog = new MonitorNotifyLog();
    monitorNotifyLog.setNodeId(nodeId);
    monitorNotifyLog.setProjectId(projectId);
    monitorNotifyLog.setMonitorId(monitorId);
    List<MonitorNotifyLog> queryList = dbMonitorNotifyLogService.queryList(monitorNotifyLog, 1, new Order("createTime", Direction.DESC));
    MonitorNotifyLog entity1 = CollUtil.getFirst(queryList);
    return entity1 == null || entity1.status();
}
Also used : Order(cn.hutool.db.sql.Order) MonitorNotifyLog(io.jpom.model.log.MonitorNotifyLog)

Example 4 with MonitorNotifyLog

use of io.jpom.model.log.MonitorNotifyLog in project Jpom by dromara.

the class DbMonitorNotifyLogService method updateStatus.

/**
 * 修改执行结果
 *
 * @param logId    通知id
 * @param status   状态
 * @param errorMsg 错误消息
 */
public void updateStatus(String logId, boolean status, String errorMsg) {
    MonitorNotifyLog monitorNotifyLog = new MonitorNotifyLog();
    monitorNotifyLog.setId(logId);
    monitorNotifyLog.setNotifyStatus(status);
    monitorNotifyLog.setNotifyError(errorMsg);
    super.update(monitorNotifyLog);
// Entity entity = new Entity();
// entity.set("notifyStatus", status);
// if (errorMsg != null) {
// entity.set("notifyError", errorMsg);
// }
// //
// Entity where = new Entity();
// where.set("logId", logId);
// super.update(entity, where);
}
Also used : MonitorNotifyLog(io.jpom.model.log.MonitorNotifyLog)

Aggregations

MonitorNotifyLog (io.jpom.model.log.MonitorNotifyLog)4 Order (cn.hutool.db.sql.Order)2 CollUtil (cn.hutool.core.collection.CollUtil)1 ExceptionUtil (cn.hutool.core.exceptions.ExceptionUtil)1 ThreadUtil (cn.hutool.core.thread.ThreadUtil)1 IdUtil (cn.hutool.core.util.IdUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 Task (cn.hutool.cron.task.Task)1 Direction (cn.hutool.db.sql.Direction)1 HttpStatus (cn.hutool.http.HttpStatus)1 DefaultSystemLog (cn.jiangzeyin.common.DefaultSystemLog)1 JsonMessage (cn.jiangzeyin.common.JsonMessage)1 SpringUtil (cn.jiangzeyin.common.spring.SpringUtil)1 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 NodeForward (io.jpom.common.forward.NodeForward)1 NodeUrl (io.jpom.common.forward.NodeUrl)1 MonitorModel (io.jpom.model.data.MonitorModel)1 NodeModel (io.jpom.model.data.NodeModel)1 UserModel (io.jpom.model.data.UserModel)1