13693261870
2025-06-24 8565bd83fcd670ec8379084d600eb97d18037d21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package com.terra.system.service.all;
 
import com.alibaba.fastjson.JSONObject;
import com.terra.system.entity.all.RedisCacheKey;
import com.terra.system.entity.sys.LoginEntity;
import com.terra.system.entity.sys.OperateEntity;
import com.terra.system.entity.sys.ResEntity;
import com.terra.system.entity.sys.UserEntity;
import com.terra.system.helper.FileHelper;
import com.terra.system.helper.StringHelper;
import com.terra.system.service.sys.LoginService;
import com.terra.system.service.sys.OperateService;
import com.terra.system.service.sys.ResService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
 
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;
 
/**
 * 日程服务类
 * @author WWW
 */
@Service
public class ScheduleService {
    @Resource
    private ResService resService;
 
    @Resource
    private LoginService loginService;
 
    @Resource
    private OperateService operateService;
 
    @Resource
    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 int countOnlineUsers() {
        Set<String> keys = redisTemplate.keys(RedisCacheKey.signUserKey("*"));
 
        return null == keys ? 0 : keys.size();
    }
 
    /**
     * 查询服务资源状态
     */
    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;
    }
}