¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.lf.server.service.all; |
| | | |
| | | import com.lf.server.entity.all.RedisCacheKey; |
| | | import com.lf.server.entity.sys.LoginEntity; |
| | | import com.lf.server.entity.sys.OperateEntity; |
| | | import com.lf.server.entity.sys.ResEntity; |
| | | import com.lf.server.entity.sys.UserEntity; |
| | | import com.lf.server.helper.FileHelper; |
| | | import com.lf.server.helper.StringHelper; |
| | | import com.lf.server.service.sys.LoginService; |
| | | import com.lf.server.service.sys.OperateService; |
| | | import com.lf.server.service.sys.ResService; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.net.InetSocketAddress; |
| | | import java.net.Socket; |
| | | import java.net.SocketAddress; |
| | | import java.net.URI; |
| | | import java.text.DecimalFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import oshi.SystemInfo; |
| | | import oshi.hardware.CentralProcessor; |
| | | import oshi.hardware.GlobalMemory; |
| | | |
| | | /** |
| | | * æ¥ç¨æå¡ç±» |
| | | * @author WWW |
| | | */ |
| | | @Service |
| | | public class ScheduleService { |
| | | @Autowired |
| | | private ResService resService; |
| | | |
| | | @Autowired |
| | | private LoginService loginService; |
| | | |
| | | @Autowired |
| | | private OperateService operateService; |
| | | |
| | | @Autowired |
| | | private RedisTemplate<String, Object> redisTemplate; |
| | | |
| | | private static final Log log = LogFactory.getLog(ScheduleService.class); |
| | | |
| | | /** |
| | | * æ¥è¯¢æå¡å¨ç¶æ |
| | | */ |
| | | public JSONObject selectServerStatus() throws InterruptedException { |
| | | JSONObject json = new JSONObject(); |
| | | json.put("cpuInfo", selectCpuInfo()); |
| | | json.put("memInfo", selectMemInfo()); |
| | | |
| | | return json; |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢Cpuä¿¡æ¯ |
| | | */ |
| | | public JSONObject selectCpuInfo() throws InterruptedException { |
| | | 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 JSONObject selectMemInfo() { |
| | | JSONObject map = new JSONObject(); |
| | | SystemInfo systemInfo = new SystemInfo(); |
| | | GlobalMemory memory = systemInfo.getHardware().getMemory(); |
| | | |
| | | // æ»å
å |
| | | long totalByte = memory.getTotal(); |
| | | |
| | | // å©ä½ |
| | | long surplusByte = memory.getAvailable(); |
| | | map.put("totalMem", FileHelper.formatByte(totalByte)); |
| | | map.put("use", FileHelper.formatByte(totalByte - surplusByte)); |
| | | map.put("remainMem", FileHelper.formatByte(surplusByte)); |
| | | map.put("usage", new DecimalFormat("#.##%").format((totalByte - surplusByte) * 1.0 / totalByte)); |
| | | |
| | | return map; |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢å¨çº¿ç¨æ· |
| | | */ |
| | | public List<JSONObject> selectOnlineUsers() { |
| | | List<JSONObject> list = new ArrayList<JSONObject>(); |
| | | |
| | | Set<String> keys = redisTemplate.keys(RedisCacheKey.signUserKey("*")); |
| | | for (String key : keys) { |
| | | Object obj = redisTemplate.opsForValue().get(key); |
| | | if (obj instanceof UserEntity) { |
| | | UserEntity ue = (UserEntity) obj; |
| | | |
| | | JSONObject map = new JSONObject(); |
| | | map.put("uid", ue.getUid()); |
| | | map.put("uname", ue.getUname()); |
| | | map.put("loginTime", ue.getBak()); |
| | | |
| | | list.add(map); |
| | | } |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢æå¡èµæºç¶æ |
| | | */ |
| | | public List<ResEntity> selectResStatus() { |
| | | List<ResEntity> resList = resService.selectResAll(); |
| | | |
| | | return testResStatus(resList); |
| | | } |
| | | |
| | | private List<ResEntity> testResStatus(List<ResEntity> resList) { |
| | | List<ResEntity> list = new ArrayList<ResEntity>(); |
| | | for (ResEntity re : resList) { |
| | | Socket socket = new Socket(); |
| | | try { |
| | | if (StringHelper.isEmpty(re.getServer())) { |
| | | list.add(re); |
| | | continue; |
| | | } |
| | | |
| | | URI uri = new URI(re.getServer()); |
| | | SocketAddress add = new InetSocketAddress(uri.getHost(), uri.getPort() == -1 ? 80 : uri.getPort()); |
| | | |
| | | // Pingéå°å |
| | | socket.connect(add, 2000); |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage()); |
| | | list.add(re); |
| | | } finally { |
| | | try { |
| | | socket.close(); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * ç»è®¡æå¡èµæºç¶æ |
| | | */ |
| | | public JSONObject countResStatus() { |
| | | List<ResEntity> resList = resService.selectResAll(); |
| | | List<ResEntity> unableList = testResStatus(resList); |
| | | |
| | | JSONObject jsonObject = new JSONObject(); |
| | | |
| | | // èµæºå¯ç¨ |
| | | jsonObject.put("resAbleCount", resList.size() - unableList.size()); |
| | | // èµæºä¸å¯ç¨ |
| | | jsonObject.put("resUnableCount", unableList.size()); |
| | | |
| | | return jsonObject; |
| | | } |
| | | |
| | | /** |
| | | * èµæºæä½ç¶æ |
| | | */ |
| | | public JSONObject operateCount() { |
| | | List<OperateEntity> list = operateService.operateCount(); |
| | | JSONObject jsonObject = new JSONObject(); |
| | | if (list.isEmpty()) { |
| | | return null; |
| | | } else { |
| | | List<JSONObject> lister = new ArrayList<JSONObject>(); |
| | | for (OperateEntity key : list) { |
| | | JSONObject map = new JSONObject(); |
| | | map.put("count", key.getCount()); |
| | | map.put("modular2", key.getModular2()); |
| | | lister.add(map); |
| | | } |
| | | jsonObject.put("operateCount", lister); |
| | | return jsonObject; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * ç¨æ·ç»å½ç¶æ |
| | | */ |
| | | public JSONObject userLoginCount() { |
| | | List<LoginEntity> list = loginService.selectLoginCounts(); |
| | | if (list == null || list.isEmpty()) { |
| | | return null; |
| | | } |
| | | |
| | | JSONObject jsonObject = new JSONObject(); |
| | | List<JSONObject> lister = new ArrayList<JSONObject>(); |
| | | for (LoginEntity key : list) { |
| | | JSONObject map = new JSONObject(); |
| | | map.put("count", key.getCount()); |
| | | map.put("optime", key.getOptime()); |
| | | lister.add(map); |
| | | } |
| | | jsonObject.put("userLoginCount", lister); |
| | | |
| | | return jsonObject; |
| | | } |
| | | } |