Search in sources :

Example 1 with MonitorModel

use of io.jpom.model.data.MonitorModel 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 2 with MonitorModel

use of io.jpom.model.data.MonitorModel in project Jpom by dromara.

the class ProjectManageControl method deleteProject.

/**
 * 删除项目
 *
 * @param id id
 * @return json
 */
@PostMapping(value = "deleteProject", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.DEL)
public String deleteProject(@ValidatorItem(value = ValidatorRule.NOT_BLANK) String id, String copyId) {
    NodeModel nodeModel = getNode();
    HttpServletRequest servletRequest = getRequest();
    if (StrUtil.isEmpty(copyId)) {
        // 检查节点分发
        List<OutGivingModel> outGivingModels = outGivingServer.listByWorkspace(servletRequest);
        if (outGivingModels != null) {
            boolean match = outGivingModels.stream().anyMatch(outGivingModel -> outGivingModel.checkContains(nodeModel.getId(), id));
            Assert.state(!match, "当前项目存在节点分发,不能直接删除");
        // for (OutGivingModel outGivingModel : outGivingModels) {
        // if (outGivingModel.checkContains(nodeModel.getId(), id)) {
        // return JsonMessage.getString(405, "当前项目存在节点分发,不能直接删除");
        // }
        // }
        }
        // 
        List<MonitorModel> monitorModels = monitorService.listByWorkspace(servletRequest);
        if (monitorModels != null) {
            boolean match = monitorModels.stream().anyMatch(monitorModel -> monitorModel.checkNodeProject(nodeModel.getId(), id));
            // if (monitorService.checkProject(nodeModel.getId(), id)) {
            // return JsonMessage.getString(405, );
            // }
            Assert.state(!match, "当前项目存在监控项,不能直接删除");
        }
        boolean releaseMethod = buildService.checkReleaseMethod(nodeModel.getId() + StrUtil.COLON + id, servletRequest, BuildReleaseMethod.Project);
        Assert.state(!releaseMethod, "当前项目存在构建项,不能直接删除");
    }
    JsonMessage<Object> request = NodeForward.request(nodeModel, servletRequest, NodeUrl.Manage_DeleteProject);
    if (request.getCode() == HttpStatus.OK.value()) {
        // 
        projectInfoCacheService.syncNode(nodeModel);
    }
    return request.toString();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) NodeModel(io.jpom.model.data.NodeModel) MonitorModel(io.jpom.model.data.MonitorModel) OutGivingModel(io.jpom.model.data.OutGivingModel) PostMapping(org.springframework.web.bind.annotation.PostMapping) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature)

Example 3 with MonitorModel

use of io.jpom.model.data.MonitorModel in project Jpom by dromara.

the class MonitorListController method updateMonitor.

/**
 * 增加或修改监控
 *
 * @param id         id
 * @param name       name
 * @param notifyUser user
 * @return json
 */
@RequestMapping(value = "updateMonitor", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EDIT)
public String updateMonitor(String id, @ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "监控名称不能为空") String name, @ValidatorItem(msg = "请配置监控周期") String execCron, String notifyUser, String webhook) {
    String status = getParameter("status");
    String autoRestart = getParameter("autoRestart");
    JSONArray jsonArray = JSONArray.parseArray(notifyUser);
    // List<String> notifyUsers = jsonArray.toJavaList(String.class);
    List<String> notifyUserList = jsonArray.toJavaList(String.class);
    if (CollUtil.isNotEmpty(notifyUserList)) {
        for (String userId : notifyUserList) {
            Assert.state(userService.exists(new UserModel(userId)), "没有对应的用户:" + userId);
        }
    }
    String projects = getParameter("projects");
    JSONArray projectsArray = JSONArray.parseArray(projects);
    List<MonitorModel.NodeProject> nodeProjects = projectsArray.toJavaList(MonitorModel.NodeProject.class);
    Assert.notEmpty(nodeProjects, "请至少选择一个节点");
    for (MonitorModel.NodeProject nodeProject : nodeProjects) {
        Assert.notEmpty(nodeProject.getProjects(), "请至少选择一个项目");
        for (String project : nodeProject.getProjects()) {
            boolean exists = projectInfoCacheService.exists(nodeProject.getNode(), project);
            Assert.state(exists, "没有对应的项目:" + project);
        }
    }
    // 设置参数
    if (StrUtil.isNotEmpty(webhook)) {
        Validator.validateMatchRegex(RegexPool.URL_HTTP, webhook, "WebHooks 地址不合法");
    }
    Assert.state(CollUtil.isNotEmpty(notifyUserList) || StrUtil.isNotEmpty(webhook), "请选择一位报警联系人或者填写webhook");
    boolean start = "on".equalsIgnoreCase(status);
    MonitorModel monitorModel = monitorService.getByKey(id);
    if (monitorModel == null) {
        monitorModel = new MonitorModel();
    }
    monitorModel.setAutoRestart("on".equalsIgnoreCase(autoRestart));
    monitorModel.setExecCron(this.checkCron(execCron));
    monitorModel.projects(nodeProjects);
    monitorModel.setStatus(start);
    monitorModel.setWebhook(webhook);
    monitorModel.notifyUser(notifyUserList);
    monitorModel.setName(name);
    if (StrUtil.isEmpty(id)) {
        // 添加监控
        monitorService.insert(monitorModel);
        return JsonMessage.getString(200, "添加成功");
    }
    HttpServletRequest request = getRequest();
    monitorService.updateById(monitorModel, request);
    return JsonMessage.getString(200, "修改成功");
}
Also used : UserModel(io.jpom.model.data.UserModel) HttpServletRequest(javax.servlet.http.HttpServletRequest) MonitorModel(io.jpom.model.data.MonitorModel) JSONArray(com.alibaba.fastjson.JSONArray) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with MonitorModel

use of io.jpom.model.data.MonitorModel in project Jpom by dromara.

the class MonitorService method setAlarm.

/**
 * 设置报警状态
 *
 * @param id    监控id
 * @param alarm 状态
 */
public void setAlarm(String id, boolean alarm) {
    MonitorModel monitorModel = new MonitorModel();
    monitorModel.setId(id);
    monitorModel.setAlarm(alarm);
    super.update(monitorModel);
}
Also used : MonitorModel(io.jpom.model.data.MonitorModel)

Example 5 with MonitorModel

use of io.jpom.model.data.MonitorModel in project Jpom by dromara.

the class MonitorService method queryStartingList.

@Override
public List<MonitorModel> queryStartingList() {
    // 关闭监听
    MonitorModel monitorModel = new MonitorModel();
    monitorModel.setStatus(true);
    return super.listByBean(monitorModel);
}
Also used : MonitorModel(io.jpom.model.data.MonitorModel)

Aggregations

MonitorModel (io.jpom.model.data.MonitorModel)5 JSONArray (com.alibaba.fastjson.JSONArray)2 NodeModel (io.jpom.model.data.NodeModel)2 UserModel (io.jpom.model.data.UserModel)2 ClassFeature (io.jpom.permission.ClassFeature)2 Feature (io.jpom.permission.Feature)2 MethodFeature (io.jpom.permission.MethodFeature)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)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 Order (cn.hutool.db.sql.Order)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