Search in sources :

Example 6 with NodeModel

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

the class AutoImportLocalNode method findPid.

private static void findPid(String pid) {
    File file = ConfigBean.getInstance().getApplicationJpomInfo(Type.Agent);
    if (!file.exists() || file.isDirectory()) {
        return;
    }
    // 比较进程id
    String json = FileUtil.readString(file, CharsetUtil.CHARSET_UTF_8);
    JpomManifest jpomManifest = JSONObject.parseObject(json, JpomManifest.class);
    if (!pid.equals(String.valueOf(jpomManifest.getPid()))) {
        return;
    }
    // 判断自动授权文件是否存在
    String path = ConfigBean.getInstance().getAgentAutoAuthorizeFile(jpomManifest.getDataPath());
    if (!FileUtil.exist(path)) {
        return;
    }
    json = FileUtil.readString(path, CharsetUtil.CHARSET_UTF_8);
    AgentAutoUser autoUser = JSONObject.parseObject(json, AgentAutoUser.class);
    // 判断授权信息
    // 
    NodeModel nodeModel = new NodeModel();
    nodeModel.setUrl(StrUtil.format("127.0.0.1:{}", jpomManifest.getPort()));
    nodeModel.setName("本机");
    // nodeModel.setProtocol("http");
    // 
    nodeModel.setLoginPwd(autoUser.getAgentPwd());
    nodeModel.setLoginName(autoUser.getAgentName());
    // 
    nodeModel.setOpenStatus(1);
    nodeService.insertNotFill(nodeModel);
    Console.log("Automatically add native node successfully:" + nodeModel.getId());
}
Also used : NodeModel(io.jpom.model.data.NodeModel) JpomManifest(io.jpom.common.JpomManifest) AgentAutoUser(io.jpom.model.system.AgentAutoUser) File(java.io.File)

Example 7 with NodeModel

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

the class BaseProxyHandler method init.

/**
 * 连接成功 初始化
 *
 * @param session    会话
 * @param attributes 属性
 * @throws URISyntaxException 异常
 * @throws IOException        IO
 */
protected void init(WebSocketSession session, Map<String, Object> attributes) throws URISyntaxException, IOException {
    boolean init = (boolean) attributes.getOrDefault("init", false);
    if (init) {
        return;
    }
    NodeModel nodeModel = (NodeModel) attributes.get("nodeInfo");
    UserModel userInfo = (UserModel) attributes.get("userInfo");
    if (nodeModel != null) {
        Object[] parameters = this.getParameters(attributes);
        String url = NodeForward.getSocketUrl(nodeModel, nodeUrl, userInfo, parameters);
        // 连接节点
        ProxySession proxySession = new ProxySession(url, session);
        session.getAttributes().put("proxySession", proxySession);
    }
    attributes.put("init", true);
}
Also used : UserModel(io.jpom.model.data.UserModel) NodeModel(io.jpom.model.data.NodeModel) JSONObject(com.alibaba.fastjson.JSONObject)

Example 8 with NodeModel

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

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

the class NodeStatService method checkList.

private void checkList(List<NodeModel> nodeModels) {
    if (CollUtil.isEmpty(nodeModels)) {
        return;
    }
    nodeModels.forEach(nodeModel -> {
        // 
        nodeModel.setName(nodeModel.getUrl());
        List<NodeModel> modelList = this.getListByUrl(nodeModel.getUrl());
        boolean match = modelList.stream().allMatch(NodeModel::isOpenStatus);
        if (!match) {
            // 节点都关闭
            try {
                BaseServerController.resetInfo(UserModel.EMPTY);
                this.save(modelList, 4, "节点禁用中");
            } finally {
                BaseServerController.removeEmpty();
            }
            return;
        }
        nodeModel.setOpenStatus(1);
        nodeModel.setTimeOut(5);
        // 
        ThreadUtil.execute(() -> {
            try {
                BaseServerController.resetInfo(UserModel.EMPTY);
                JSONObject nodeTopInfo = this.getNodeTopInfo(nodeModel);
                // 
                long timeMillis = SystemClock.now();
                JsonMessage<Object> jsonMessage = NodeForward.requestBySys(nodeModel, NodeUrl.Status, "nodeId", nodeModel.getId());
                int networkTime = (int) (System.currentTimeMillis() - timeMillis);
                JSONObject jsonObject;
                if (jsonMessage.getCode() == 200) {
                    jsonObject = jsonMessage.getData(JSONObject.class);
                } else {
                    // 状态码错
                    jsonObject = new JSONObject();
                    jsonObject.put("status", 3);
                    jsonObject.put("failureMsg", jsonMessage.toString());
                }
                jsonObject.put("networkTime", networkTime);
                if (nodeTopInfo != null) {
                    nodeTopInfo.put("networkTime", networkTime);
                }
                this.save(modelList, nodeTopInfo, jsonObject);
            } catch (AuthorizeException agentException) {
                this.save(modelList, 2, agentException.getMessage());
            } catch (Exception e) {
                this.save(modelList, 1, e.getMessage());
                DefaultSystemLog.getLog().error("获取节点监控信息失败", e);
            } finally {
                BaseServerController.removeEmpty();
            }
        });
    });
}
Also used : NodeModel(io.jpom.model.data.NodeModel) JSONObject(com.alibaba.fastjson.JSONObject) AuthorizeException(io.jpom.system.AuthorizeException) JSONObject(com.alibaba.fastjson.JSONObject) AuthorizeException(io.jpom.system.AuthorizeException)

Example 10 with NodeModel

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

the class MonitorItem method execute.

@Override
public void execute() {
    // 重新查询
    this.monitorModel = monitorService.getByKey(monitorId);
    List<MonitorModel.NodeProject> nodeProjects = monitorModel.projects();
    // 
    List<Boolean> collect = nodeProjects.stream().map(nodeProject -> {
        String nodeId = nodeProject.getNode();
        NodeModel nodeModel = nodeService.getByKey(nodeId);
        if (nodeModel == null) {
            return true;
        }
        return this.reqNodeStatus(nodeModel, nodeProject.getProjects());
    }).filter(aBoolean -> !aBoolean).collect(Collectors.toList());
    boolean allRun = CollUtil.isEmpty(collect);
    // 报警状态
    monitorService.setAlarm(monitorModel.getId(), !allRun);
}
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) NodeModel(io.jpom.model.data.NodeModel)

Aggregations

NodeModel (io.jpom.model.data.NodeModel)64 JSONObject (com.alibaba.fastjson.JSONObject)27 Feature (io.jpom.permission.Feature)23 MethodFeature (io.jpom.permission.MethodFeature)23 ClassFeature (io.jpom.permission.ClassFeature)22 UserModel (io.jpom.model.data.UserModel)17 StrUtil (cn.hutool.core.util.StrUtil)12 NodeService (io.jpom.service.node.NodeService)12 Collectors (java.util.stream.Collectors)12 JsonMessage (cn.jiangzeyin.common.JsonMessage)11 List (java.util.List)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 CollUtil (cn.hutool.core.collection.CollUtil)9 DefaultSystemLog (cn.jiangzeyin.common.DefaultSystemLog)9 NodeForward (io.jpom.common.forward.NodeForward)8 NodeUrl (io.jpom.common.forward.NodeUrl)8 Assert (org.springframework.util.Assert)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 JSONArray (com.alibaba.fastjson.JSONArray)7 BaseServerController (io.jpom.common.BaseServerController)7