From ab849f796bdc17236a95ea5fe5c166fb8f24a75c Mon Sep 17 00:00:00 2001
From: sws <15810472099@163.com>
Date: 星期六, 26 十一月 2022 16:12:02 +0800
Subject: [PATCH] 1

---
 src/main/java/com/lf/server/service/all/ScheduleService.java |  239 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 239 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/lf/server/service/all/ScheduleService.java b/src/main/java/com/lf/server/service/all/ScheduleService.java
new file mode 100644
index 0000000..7f430f8
--- /dev/null
+++ b/src/main/java/com/lf/server/service/all/ScheduleService.java
@@ -0,0 +1,239 @@
+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;
+    }
+}

--
Gitblit v1.9.3