浏览代码

系统监控-tenant添加

demon 8 年之前
父节点
当前提交
8e06906f96

+ 8 - 1
hos-broker/src/main/java/com/yihu/hos/broker/common/scheduler/MonitorScheduler.java

@ -15,6 +15,7 @@ import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.annotation.Scheduled;
@ -35,6 +36,9 @@ import java.util.List;
@Component
public class MonitorScheduler {
    @Value("${hos.tenant.name}")
    private String tenant;
    @Autowired
    private MongoOperations mongoOperations;
@ -70,6 +74,7 @@ public class MonitorScheduler {
            MongodbUtil monoEnv = new MongodbUtil(MonitorConstant.MONITOR);
            Document result = null;
            result = new Document();
            result.put("tenant",tenant);
            result.put("create_date", DateUtil.getCurrentString(DateUtil.DEFAULT_YMDHMSDATE_FORMAT));
            result.put("create_time", new Date());
            result.put("host",host);
@ -145,7 +150,8 @@ public class MonitorScheduler {
        DBCollection envCollection = mongoOperations.getCollection(MonitorConstant.HOST);
        BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
                new BasicDBObject[]{
                        new BasicDBObject().append("host", host)});
                        new BasicDBObject().append("host", host),
                        new BasicDBObject().append("tenant", tenant)});
        DBCursor cursor = envCollection.find(queryObject);
        if (cursor.size() < 1) {
@ -153,6 +159,7 @@ public class MonitorScheduler {
                Document result = new Document();
                String host = SigarUtil.getHost();
                String hostName = SigarUtil.getHostName();
                result.put("tenant",tenant);
                result.put("name",  hostName);
                result.put("host",  host);
                monoEnv.insert(MonitorConstant.HOST, result);

+ 2 - 1
hos-broker/src/main/resources/application.yml

@ -51,7 +51,8 @@ hos:
    url: http://localhost:10135
  timer:
      period: 10000
  tenant:
    name: jkzl
---
spring:

+ 13 - 5
src/main/java/com/yihu/hos/monitor/controller/ServerMonitorController.java

@ -12,6 +12,8 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
 * @author HZY
@ -39,6 +41,7 @@ public class ServerMonitorController {
    @ResponseBody
    @ApiOperation(value = "获取服务器使用率", produces = "application/json", notes = "获取服务器硬件使用率")
    public Result usage(
            HttpServletRequest request,
            @ApiParam(name = "host", value = "服务器IP", required = true)
            @RequestParam(value = "host") String host,
            @ApiParam(name = "type", value = "类型", required = true)
@ -47,7 +50,8 @@ public class ServerMonitorController {
            @RequestParam(value = "beginTime") String beginTime,
            @ApiParam(name = "endTime", value = "结束时间", required = true)
            @RequestParam(value = "endTime") String endTime) {
        return monitorService.getMonitorList(host,type, beginTime, endTime);
        HttpSession session = request.getSession();
            return monitorService.getMonitorList(session,host,type, beginTime, endTime);
    }
@ -55,19 +59,22 @@ public class ServerMonitorController {
    @ResponseBody
    @ApiOperation(value = "获取服务器详情信息", produces = "application/json", notes = "获取服务器详情信息")
    public Result detail(
            HttpServletRequest request,
            @ApiParam(name = "host", value = "服务器IP", required = true)
            @RequestParam(value = "host") String host,
            @ApiParam(name = "type", value = "类型", required = true)
            @RequestParam(value = "type") String type,
            @ApiParam(name = "date", value = "时间点", required = true)
            @RequestParam(value = "date") String date) {
        return monitorService.getMonitorDetail(host,type, date);
        HttpSession session = request.getSession();
        return monitorService.getMonitorDetail(session,host,type, date);
    }
    @RequestMapping(value = "/hosts", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取服务器列表", produces = "application/json", notes = "获取服务器列表")
    public Result hosts() {
    public Result hosts(HttpServletRequest request) {
        HttpSession session = request.getSession();
        return monitorService.getHosts();
    }
@ -75,9 +82,10 @@ public class ServerMonitorController {
    @RequestMapping(value = "/trees", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取服务器树列表", produces = "application/json", notes = "获取服务器树列表")
    public Result trees() {
    public Result trees(HttpServletRequest request) {
        try {
            return monitorService.getServerTreeList();
            HttpSession session = request.getSession();
            return monitorService.getServerTreeList(session);
        } catch (Exception e) {
            return Result.error("获取服务树列表失败");
        }

+ 17 - 4
src/main/java/com/yihu/hos/monitor/service/ServerMonitorService.java

@ -1,7 +1,9 @@
package com.yihu.hos.monitor.service;
import com.mongodb.*;
import com.yihu.hos.common.constants.ContextAttributes;
import com.yihu.hos.core.datatype.DateUtil;
import com.yihu.hos.tenant.model.TenantSession;
import com.yihu.hos.web.framework.model.ActionResult;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.model.TreeView;
@ -11,6 +13,7 @@ import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -32,11 +35,13 @@ public class ServerMonitorService {
    @Autowired
    private Mongo mongo;
    public Result getMonitorList(String host, String type, String beginTime, String endTime) {
    public Result getMonitorList(HttpSession session,String host, String type, String beginTime, String endTime) {
        TenantSession tenantSession = (TenantSession)session.getAttribute(ContextAttributes.TENANT_SESSION);
        mongoOperations = new MongoTemplate(mongo, MONITOR);
        DBCollection envCollection = mongoOperations.getCollection(SERVER);
        BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
                new BasicDBObject[]{
                        new BasicDBObject().append("tenant",tenantSession.getTenant()),
                        new BasicDBObject().append("host",host),
                        new BasicDBObject().append("type",type),
                        new BasicDBObject().append("create_time",
@ -57,11 +62,13 @@ public class ServerMonitorService {
        return actionResult;
    }
    public Result getMonitorDetail(String host, String type, String date) {
    public Result getMonitorDetail(HttpSession session,String host, String type, String date) {
        TenantSession tenantSession = (TenantSession)session.getAttribute(ContextAttributes.TENANT_SESSION);
        mongoOperations = new MongoTemplate(mongo, MONITOR);
        DBCollection envCollection = mongoOperations.getCollection(SERVER);
        BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
                new BasicDBObject[]{
                        new BasicDBObject().append("tenant",tenantSession.getTenant()),
                        new BasicDBObject().append("host", host),
                        new BasicDBObject().append("type", type),
                        new BasicDBObject().append("create_time",new BasicDBObject().append(QueryOperators.LTE, DateUtil.toTimestamp(date)))});
@ -104,11 +111,17 @@ public class ServerMonitorService {
     * @return
     * @throws Exception
     */
    public Result getServerTreeList() throws Exception {
    public Result getServerTreeList(HttpSession session) throws Exception {
        List<TreeView> treeList = new ArrayList<>();
        TenantSession tenantSession = (TenantSession)session.getAttribute(ContextAttributes.TENANT_SESSION);
        mongoOperations = new MongoTemplate(mongo, MONITOR);
        DBCollection envCollection = mongoOperations.getCollection(HOST);
        DBCursor cursor = envCollection.find();
        //查询条件
        BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND,
                new BasicDBObject[]{
                        new BasicDBObject().append("tenant",tenantSession.getTenant())});
        DBCursor cursor = envCollection.find(queryObject);
        while(cursor.hasNext()) {
            DBObject dbObject = cursor.next();

+ 7 - 0
src/main/java/com/yihu/hos/system/service/SystemManager.java

@ -1,5 +1,7 @@
package com.yihu.hos.system.service;
import com.yihu.hos.common.constants.ContextAttributes;
import com.yihu.hos.tenant.model.TenantSession;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.system.model.SystemUser;
import com.yihu.hos.system.service.intf.ISystemManager;
@ -25,6 +27,7 @@ public class SystemManager implements ISystemManager {
    @Override
    public  Result loginAction(HttpSession session,String user,String password) throws Exception
    {
        TenantSession tenantSession = (TenantSession)session.getAttribute(ContextAttributes.TENANT_SESSION);
        //特殊账户
        if(user.equals("admin") && password.equals("JKZL"))
        {
@ -32,6 +35,8 @@ public class SystemManager implements ISystemManager {
            userInfo.setLoginCode("admin");
            userInfo.setUserName("管理员");
            session.setAttribute("userInfo",userInfo);
            tenantSession.setUserCode("admin");
            session.setAttribute(ContextAttributes.TENANT_SESSION, tenantSession);
            return Result.success("登录成功!");
        }
@ -46,6 +51,8 @@ public class SystemManager implements ISystemManager {
            String userPassword = userInfo.getPassword();
            if(userPassword.equals(DigestUtils.md5Hex(password + saltValue)))
            {
                tenantSession.setUserCode(user);
                session.setAttribute(ContextAttributes.TENANT_SESSION, tenantSession);
                session.setAttribute("userInfo",userInfo);
                return Result.success("登录成功!");
            }

+ 11 - 2
src/main/java/com/yihu/hos/tenant/model/TenantSession.java

@ -9,12 +9,13 @@ public class TenantSession {
    private String userCode;
    private String token;
    private String schema;
    private String tenant;
    public TenantSession() {
    }
    public TenantSession(String userCode, String schema) {
        this.userCode = userCode;
    public TenantSession(String tenant, String schema) {
        this.tenant = tenant;
        this.schema = schema;
        token = UUID.randomUUID().toString();
    }
@ -48,4 +49,12 @@ public class TenantSession {
    public void setToken(String token) {
        this.token = token;
    }
    public String getTenant() {
        return tenant;
    }
    public void setTenant(String tenant) {
        this.tenant = tenant;
    }
}