package com.lf.server.service.all;
|
|
import com.lf.server.entity.all.RedisCacheKey;
|
import com.lf.server.entity.sys.ResEntity;
|
import com.lf.server.entity.sys.UsersEntity;
|
import com.lf.server.helper.StringHelper;
|
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 RedisTemplate<String, Object> redisTemplate;
|
|
private static final Log log = LogFactory.getLog(ScheduleService.class);
|
|
/**
|
* 查询服务器状态
|
*
|
* @return
|
* @throws InterruptedException
|
*/
|
public JSONObject selectServerStatus() throws InterruptedException {
|
JSONObject json = new JSONObject();
|
json.put("cpuInfo", selectCpuInfo());
|
json.put("memInfo", selectMemInfo());
|
|
return json;
|
}
|
|
/**
|
* 查询Cpu信息
|
*
|
* @return
|
* @throws InterruptedException
|
*/
|
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;
|
}
|
|
/**
|
* 查询内存信息
|
*
|
* @return
|
*/
|
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", StringHelper.formatByte(totalByte));
|
map.put("use", StringHelper.formatByte(totalByte - surplusByte));
|
map.put("remainMem", StringHelper.formatByte(surplusByte));
|
map.put("usage", new DecimalFormat("#.##%").format((totalByte - surplusByte) * 1.0 / totalByte));
|
|
return map;
|
}
|
|
/**
|
* 查询在线用户
|
*
|
* @return
|
*/
|
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 != null && obj instanceof UsersEntity) {
|
UsersEntity ue = (UsersEntity) 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;
|
}
|
|
/**
|
* 查询服务资源状态
|
*
|
* @return
|
*/
|
public List<ResEntity> selectResStatus() {
|
List<ResEntity> list = new ArrayList<ResEntity>();
|
|
List<ResEntity> resList = resService.selectResAll();
|
for (ResEntity re : resList) {
|
Socket socket = new Socket();
|
try {
|
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.getStackTrace());
|
list.add(re);
|
} finally {
|
try {
|
socket.close();
|
} catch (Exception e) {
|
log.error(e.getStackTrace());
|
}
|
}
|
}
|
|
return list;
|
}
|
}
|