package com.landtool.lanbase.modules.sys.controller; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.function.BiConsumer; import java.util.function.Consumer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; import com.landtool.lanbase.common.utils.RedisUtils; import com.landtool.lanbase.modules.sys.service.MonitorService; import com.landtool.lanbase.modules.sys.service.impl.MonitorServiceImpl; import oshi.SystemInfo; import oshi.hardware.CentralProcessor; import oshi.hardware.GlobalMemory; @RequestMapping("/api/monitor") @Controller @CrossOrigin public class SecModController { @Autowired private MonitorService monitor; @Autowired public RedisUtils redisUtils; /** * 用户在线人数统计 */ @RequestMapping("/onlineUser") public @ResponseBody String onlineUser() { ConcurrentHashMap info = redisUtils.get("onlineuser", ConcurrentHashMap.class); JSONObject json = new JSONObject(); Map col=new HashMap<>(); // 查询在线用户信息 info.forEach(new BiConsumer() { @Override public void accept(String t, Double u) { col.put(t, new Date(u.longValue()) ); } }); json.put("count", col.size()); json.put("onlineUser", col); return JSONObject.toJSONStringWithDateFormat(json, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue); } /** *查询异常系统 */ @RequestMapping("/queryErrorSystem") public @ResponseBody String queryErrorSystem(Integer page,Integer limit) { JSONObject json = new JSONObject(); Map param=new HashMap<>(); param.put("page", page); param.put("limit", limit); List list=monitor.queryProblemSystem(param); json.put("count", list.size()); json.put("errorsystem", list); return JSONObject.toJSONStringWithDateFormat(json, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue); } /** * 查询用户访问行为 */ @RequestMapping("/queryUserTrace") public @ResponseBody String queryUserTrace(String username,Integer page,Integer limit) { JSONObject json = new JSONObject(); Map param=new HashMap<>(); param.put("username",username); param.put("page", page); param.put("limit", limit); List list=monitor.queryUserTrace(param); json.put("total", list.size()); json.put("data", list); return JSONObject.toJSONStringWithDateFormat(json, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue); } /** * 用户在线人数查询 */ @RequestMapping("/queryonlineUser") public @ResponseBody String queryonlineUser() { if(!redisUtils.exists("onlineuser")){ return "not exist"; }else{ ConcurrentHashMap info = redisUtils.get("onlineuser", ConcurrentHashMap.class); ConcurrentHashMap vkinfo=new ConcurrentHashMap<>(); List list = new ArrayList(); Consumer consumer = (str) -> { JSONObject json=JSON.parseObject(info.get(str).toString()); vkinfo.put(json.getString("username"), json.getLong("time")); list.add(json.getString("username")); }; info.forEachKey(info.size(), consumer); List personlist = new ArrayList<>(); if(!list.isEmpty()){ personlist = monitor.queryUsers(list); } JSONObject json = new JSONObject(); // 查询在线用户信息 json.put("count", info.size()); personlist.forEach(new Consumer() { @Override public void accept(Map t) { /** modified by qufangxu**/ Long times = (Long) vkinfo.get((String)t.get("loginname")); t.put("time", new Date( times.longValue() )); } }); json.put("onlineUser", personlist); return JSONObject.toJSONStringWithDateFormat(json, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue); } } /** * 用户行为分析 */ @RequestMapping("/analysisUser") public @ResponseBody String analysisUserAction(Integer userid,Integer day,String startdate,String enddate, String username) { //分析 Map map=new HashMap(); map.put("day",day); map.put("startdate",startdate); map.put("enddate",enddate); map.put("userid",userid ); map.put("username",username ); List list=monitor.queryUserops(map); return JSONObject.toJSONString(list, SerializerFeature.WriteMapNullValue); } public String serverstatus() throws InterruptedException { JSONObject json=new JSONObject(); json.put("cpuinfo", printlnCpuInfo()); json.put("meminfo", MemInfo()); return JSONObject.toJSONStringWithDateFormat(json, "yyyy-MM-dd HH:mm:ss", SerializerFeature.PrettyFormat); } private static JSONObject printlnCpuInfo() throws InterruptedException { //map.put("----------------cpu信息----------------"); SystemInfo systemInfo = new SystemInfo(); CentralProcessor processor = systemInfo.getHardware().getProcessor(); long[] prevTicks = processor.getSystemCpuLoadTicks(); // 睡眠1s TimeUnit.SECONDS.sleep(1); long[] ticks = processor.getSystemCpuLoadTicks(); long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()]; long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()]; long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()]; long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()]; long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()]; long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()]; long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()]; long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()]; long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal; JSONObject map=new JSONObject(); map.put("cpuCore", processor.getLogicalProcessorCount()); map.put("cpuSysUsage", new DecimalFormat("#.##%").format(cSys * 1.0 / totalCpu)); map.put("cpuUserUsage", new DecimalFormat("#.##%").format(user * 1.0 / totalCpu)); map.put("cpuRtcWaitUsage", new DecimalFormat("#.##%").format(iowait * 1.0 / totalCpu)); map.put("cpuRtcUseUsage", new DecimalFormat("#.##%").format(1.0-(idle * 1.0 / totalCpu))); return map; } public static JSONObject MemInfo(){ JSONObject map=new JSONObject(); SystemInfo systemInfo = new SystemInfo(); GlobalMemory memory = systemInfo.getHardware().getMemory(); //总内存 long totalByte = memory.getTotal(); //剩余 long acaliableByte = memory.getAvailable(); map.put("totalmem", formatByte(totalByte)); map.put("use", formatByte(totalByte-acaliableByte)); map.put("remainmem", formatByte(acaliableByte)); map.put("usage", new DecimalFormat("#.##%").format((totalByte-acaliableByte)*1.0/totalByte)); return map; } public static String formatByte(long byteNumber){ //换算单位 double FORMAT = 1024.0; double kbNumber = byteNumber/FORMAT; if(kbNumber