package com.moon.server.service.all;
|
|
import com.moon.server.entity.all.RedisCacheKey;
|
import com.moon.server.entity.sys.LoginEntity;
|
import com.moon.server.entity.sys.OperateEntity;
|
import com.moon.server.entity.sys.ResEntity;
|
import com.moon.server.entity.sys.UserEntity;
|
import com.moon.server.helper.FileHelper;
|
import com.moon.server.helper.StringHelper;
|
import com.moon.server.service.sys.LoginService;
|
import com.moon.server.service.sys.OperateService;
|
import com.moon.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;
|
|
@Service
|
@SuppressWarnings("ALL")
|
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;
|
}
|
|
public JSONObject selectCpuInfo() throws InterruptedException {
|
SystemInfo systemInfo = new SystemInfo();
|
CentralProcessor processor = systemInfo.getHardware().getProcessor();
|
long[] prevTicks = processor.getSystemCpuLoadTicks();
|
|
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.selectAll();
|
|
return testResStatus(resList);
|
}
|
|
private List<ResEntity> testResStatus(List<ResEntity> resList) {
|
List<ResEntity> list = new ArrayList<>();
|
for (ResEntity re : resList) {
|
Socket socket = new Socket();
|
try {
|
if (StringHelper.isEmpty(re.getUrl())) {
|
list.add(re);
|
continue;
|
}
|
|
URI uri = new URI(re.getUrl());
|
SocketAddress add = new InetSocketAddress(uri.getHost(), uri.getPort() == -1 ? 80 : uri.getPort());
|
|
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.selectAll();
|
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;
|
}
|
}
|