package com.wgcloud.controller; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.github.pagehelper.PageInfo; import com.wgcloud.entity.CpuTemperatures; import com.wgcloud.entity.DeskIo; import com.wgcloud.entity.DiskState; import com.wgcloud.entity.DiskSmart; import com.wgcloud.service.*; import com.wgcloud.util.ResDataUtils; import com.wgcloud.util.TokenUtils; import com.wgcloud.util.staticvar.StaticKeys; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @version v3.3 * @ClassName:SystemInfoOpenController.java * @author: http://www.wgstart.com * @date: 2021年4月27日 * @Description: 数据开放接口,获取主机各种监控数据,磁盘、磁盘io、cpu温度、系统负载、cpu数据、内存数据、网络流量数据 * @Copyright: 2019-2021 wgcloud. All rights reserved. */ @Controller @RequestMapping("/systemInfoOpen") public class SystemInfoOpenController { private static final Logger logger = LoggerFactory.getLogger(SystemInfoOpenController.class); @Resource private LogInfoService logInfoService; @Resource private CpuStateService cpuStateService; @Resource private DiskStateService diskStateService; @Resource private DiskSmartService diskSmartService; @Resource private DeskIoService deskIoService; @Resource private CpuTemperaturesService cpuTemperaturesService; @Resource private MemStateService memStateService; @Resource private NetIoStateService netIoStateService; @Resource private SysLoadStateService sysLoadStateService; @Autowired private TokenUtils tokenUtils; /** * agent查询根据ip查询磁盘smar列表 * * @param paramBean * @return */ @ResponseBody @RequestMapping(value = "agentDiskSmartList") public String agentDiskSmartList(@RequestBody String paramBean) { JSONObject agentJsonObject = (JSONObject) JSONUtil.parse(paramBean); if (!tokenUtils.checkAgentToken(agentJsonObject)) { logger.error(StaticKeys.TOKEN_ERROR); return ResDataUtils.resetErrorJson(StaticKeys.TOKEN_ERROR); } Map params = new HashMap(); if (null == agentJsonObject.get("hostname") || StringUtils.isEmpty(agentJsonObject.get("hostname").toString())) { return ResDataUtils.resetErrorJson(StaticKeys.REQUIRE_PARAM_ERROR); } params.put("hostname", agentJsonObject.get("hostname").toString()); try { List diskSmarts = diskSmartService.selectAllByParams(params); return ResDataUtils.resetSuccessJson(diskSmarts); } catch (Exception e) { logger.error("agent查询根据ip查询磁盘smar列表错误", e); logInfoService.save("agent查询根据ip查询磁盘smar列表错误", e.toString(), StaticKeys.LOG_XTCZ); return ResDataUtils.resetErrorJson(e.toString()); } } /** * agent查询根据ip查询磁盘列表 * * @param paramBean * @return */ @ResponseBody @RequestMapping(value = "agentDeskStatesList") public String agentDeskStatesList(@RequestBody String paramBean) { JSONObject agentJsonObject = (JSONObject) JSONUtil.parse(paramBean); if (!tokenUtils.checkAgentToken(agentJsonObject)) { logger.error(StaticKeys.TOKEN_ERROR); return ResDataUtils.resetErrorJson(StaticKeys.TOKEN_ERROR); } Map params = new HashMap(); if (null == agentJsonObject.get("hostname") || StringUtils.isEmpty(agentJsonObject.get("hostname").toString())) { return ResDataUtils.resetErrorJson(StaticKeys.REQUIRE_PARAM_ERROR); } params.put("hostname", agentJsonObject.get("hostname").toString()); try { List deskStates = diskStateService.selectAllByParams(params); return ResDataUtils.resetSuccessJson(deskStates); } catch (Exception e) { logger.error("agent查询根据ip查询磁盘列表错误", e); logInfoService.save("agent查询根据ip查询磁盘列表错误", e.toString(), StaticKeys.LOG_XTCZ); return ResDataUtils.resetErrorJson(e.toString()); } } /** * agent查询根据ip查询磁盘io列表 * * @param paramBean * @return */ @ResponseBody @RequestMapping(value = "agentDiskIosList") public String agentDiskIosList(@RequestBody String paramBean) { JSONObject agentJsonObject = (JSONObject) JSONUtil.parse(paramBean); if (!tokenUtils.checkAgentToken(agentJsonObject)) { logger.error(StaticKeys.TOKEN_ERROR); return ResDataUtils.resetErrorJson(StaticKeys.TOKEN_ERROR); } Map params = new HashMap(); if (null == agentJsonObject.get("hostname") || StringUtils.isEmpty(agentJsonObject.get("hostname").toString())) { return ResDataUtils.resetErrorJson(StaticKeys.REQUIRE_PARAM_ERROR); } params.put("hostname", agentJsonObject.get("hostname").toString()); try { List deskIos = deskIoService.selectAllByParams(params); return ResDataUtils.resetSuccessJson(deskIos); } catch (Exception e) { logger.error("agent查询根据ip查询磁盘io列表错误", e); logInfoService.save("agent查询根据ip查询磁盘io列表错误", e.toString(), StaticKeys.LOG_XTCZ); return ResDataUtils.resetErrorJson(e.toString()); } } /** * agent查询根据ip查询cpu温度列表 * * @param paramBean * @return */ @ResponseBody @RequestMapping(value = "agentCpuTemperaturesList") public String agentCpuTemperaturesList(@RequestBody String paramBean) { JSONObject agentJsonObject = (JSONObject) JSONUtil.parse(paramBean); if (!tokenUtils.checkAgentToken(agentJsonObject)) { logger.error(StaticKeys.TOKEN_ERROR); return ResDataUtils.resetErrorJson(StaticKeys.TOKEN_ERROR); } Map params = new HashMap(); if (null == agentJsonObject.get("hostname") || StringUtils.isEmpty(agentJsonObject.get("hostname").toString())) { return ResDataUtils.resetErrorJson(StaticKeys.REQUIRE_PARAM_ERROR); } params.put("hostname", agentJsonObject.get("hostname").toString()); try { List cpuTemperatures = cpuTemperaturesService.selectAllByParams(params); return ResDataUtils.resetSuccessJson(cpuTemperatures); } catch (Exception e) { logger.error("agent查询根据ip查询cpu温度列表错误", e); logInfoService.save("agent查询根据ip查询cpu温度列表错误", e.toString(), StaticKeys.LOG_XTCZ); return ResDataUtils.resetErrorJson(e.toString()); } } /** * agent查询根据ip查询网络流量列表 * * @param paramBean * @return */ @ResponseBody @RequestMapping(value = "agentNetIoStatesList") public String agentNetIoStatesList(@RequestBody String paramBean) { JSONObject agentJsonObject = (JSONObject) JSONUtil.parse(paramBean); if (!tokenUtils.checkAgentToken(agentJsonObject)) { logger.error(StaticKeys.TOKEN_ERROR); return ResDataUtils.resetErrorJson(StaticKeys.TOKEN_ERROR); } Map params = new HashMap(); if (null == agentJsonObject.get("hostname") || StringUtils.isEmpty(agentJsonObject.get("hostname").toString())) { return ResDataUtils.resetErrorJson(StaticKeys.REQUIRE_PARAM_ERROR); } params.put("hostname", agentJsonObject.get("hostname").toString()); if (null != agentJsonObject.get("startTime")) { params.put("startTime", agentJsonObject.get("startTime").toString()); } if (null != agentJsonObject.get("endTime")) { params.put("endTime", agentJsonObject.get("endTime").toString()); } try { PageInfo netIoStates = netIoStateService.selectByParams(params, agentJsonObject.getInt("page"), agentJsonObject.getInt("pageSize")); return ResDataUtils.resetSuccessJson(netIoStates); } catch (Exception e) { logger.error("agent查询根据ip查询系统负载列表错误", e); logInfoService.save("agent查询根据ip查询系统负载列表错误", e.toString(), StaticKeys.LOG_XTCZ); return ResDataUtils.resetErrorJson(e.toString()); } } /** * agent查询根据ip查询系统cpu状态监控数据列表 * * @param paramBean * @return */ @ResponseBody @RequestMapping(value = "agentCpuStatesList") public String agentCpuStatesList(@RequestBody String paramBean) { JSONObject agentJsonObject = (JSONObject) JSONUtil.parse(paramBean); if (!tokenUtils.checkAgentToken(agentJsonObject)) { logger.error(StaticKeys.TOKEN_ERROR); return ResDataUtils.resetErrorJson(StaticKeys.TOKEN_ERROR); } Map params = new HashMap(); if (null == agentJsonObject.get("hostname") || StringUtils.isEmpty(agentJsonObject.get("hostname").toString())) { return ResDataUtils.resetErrorJson(StaticKeys.REQUIRE_PARAM_ERROR); } params.put("hostname", agentJsonObject.get("hostname").toString()); if (null != agentJsonObject.get("startTime")) { params.put("startTime", agentJsonObject.get("startTime").toString()); } if (null != agentJsonObject.get("endTime")) { params.put("endTime", agentJsonObject.get("endTime").toString()); } try { PageInfo cpuStates = cpuStateService.selectByParams(params, agentJsonObject.getInt("page"), agentJsonObject.getInt("pageSize")); return ResDataUtils.resetSuccessJson(cpuStates); } catch (Exception e) { logger.error("agent查询根据ip查询系统cpu监控数据列表错误", e); logInfoService.save("agent查询根据ip查询系统cpu监控数据列表错误", e.toString(), StaticKeys.LOG_XTCZ); return ResDataUtils.resetErrorJson(e.toString()); } } /** * agent查询根据ip查询系统内存状态监控数据列表 * * @param paramBean * @return */ @ResponseBody @RequestMapping(value = "agentMemStatesList") public String agentMemStatesList(@RequestBody String paramBean) { JSONObject agentJsonObject = (JSONObject) JSONUtil.parse(paramBean); if (!tokenUtils.checkAgentToken(agentJsonObject)) { logger.error(StaticKeys.TOKEN_ERROR); return ResDataUtils.resetErrorJson(StaticKeys.TOKEN_ERROR); } Map params = new HashMap(); if (null == agentJsonObject.get("hostname") || StringUtils.isEmpty(agentJsonObject.get("hostname").toString())) { return ResDataUtils.resetErrorJson(StaticKeys.REQUIRE_PARAM_ERROR); } params.put("hostname", agentJsonObject.get("hostname").toString()); if (null != agentJsonObject.get("startTime")) { params.put("startTime", agentJsonObject.get("startTime").toString()); } if (null != agentJsonObject.get("endTime")) { params.put("endTime", agentJsonObject.get("endTime").toString()); } try { PageInfo cpuStates = memStateService.selectByParams(params, agentJsonObject.getInt("page"), agentJsonObject.getInt("pageSize")); return ResDataUtils.resetSuccessJson(cpuStates); } catch (Exception e) { logger.error("agent查询根据ip查询系统内存监控数据列表错误", e); logInfoService.save("agent查询根据ip查询系统内存监控数据列表错误", e.toString(), StaticKeys.LOG_XTCZ); return ResDataUtils.resetErrorJson(e.toString()); } } /** * agent查询根据ip查询系统负载监控数据列表 * * @param paramBean * @return */ @ResponseBody @RequestMapping(value = "agentSysLoadStatesList") public String agentSysLoadStatesList(@RequestBody String paramBean) { JSONObject agentJsonObject = (JSONObject) JSONUtil.parse(paramBean); if (!tokenUtils.checkAgentToken(agentJsonObject)) { logger.error(StaticKeys.TOKEN_ERROR); return ResDataUtils.resetErrorJson(StaticKeys.TOKEN_ERROR); } Map params = new HashMap(); if (null == agentJsonObject.get("hostname") || StringUtils.isEmpty(agentJsonObject.get("hostname").toString())) { return ResDataUtils.resetErrorJson(StaticKeys.REQUIRE_PARAM_ERROR); } params.put("hostname", agentJsonObject.get("hostname").toString()); if (null != agentJsonObject.get("startTime")) { params.put("startTime", agentJsonObject.get("startTime").toString()); } if (null != agentJsonObject.get("endTime")) { params.put("endTime", agentJsonObject.get("endTime").toString()); } try { PageInfo cpuStates = sysLoadStateService.selectByParams(params, agentJsonObject.getInt("page"), agentJsonObject.getInt("pageSize")); return ResDataUtils.resetSuccessJson(cpuStates); } catch (Exception e) { logger.error("aagent查询根据ip查询系统负载监控数据列表错误", e); logInfoService.save("agent查询根据ip查询系统负载监控数据列表错误", e.toString(), StaticKeys.LOG_XTCZ); return ResDataUtils.resetErrorJson(e.toString()); } } }