| | |
| | | import oshi.hardware.CentralProcessor; |
| | | import oshi.hardware.GlobalMemory; |
| | | |
| | | /** |
| | | * 日程服务类 |
| | | * @author WWW |
| | | */ |
| | | @Service |
| | | @SuppressWarnings("ALL") |
| | | public class ScheduleService { |
| | | @Autowired |
| | | private ResService resService; |
| | |
| | | |
| | | private static final Log log = LogFactory.getLog(ScheduleService.class); |
| | | |
| | | /** |
| | | * 查询服务器状态 |
| | | */ |
| | | public JSONObject selectServerStatus() throws InterruptedException { |
| | | JSONObject json = new JSONObject(); |
| | | json.put("cpuInfo", selectCpuInfo()); |
| | |
| | | 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()]; |
| | |
| | | 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)); |
| | |
| | | return map; |
| | | } |
| | | |
| | | /** |
| | | * 查询在线用户 |
| | | */ |
| | | public List<JSONObject> selectOnlineUsers() { |
| | | List<JSONObject> list = new ArrayList<JSONObject>(); |
| | | |
| | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 统计在线用户数 |
| | | * @return |
| | | */ |
| | | 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(); |
| | | |
| | |
| | | } |
| | | |
| | | private List<ResEntity> testResStatus(List<ResEntity> resList) { |
| | | List<ResEntity> list = new ArrayList<ResEntity>(); |
| | | List<ResEntity> list = new ArrayList<>(); |
| | | for (ResEntity re : resList) { |
| | | Socket socket = new Socket(); |
| | | try { |
| | | if (StringHelper.isEmpty(re.getTest())) { |
| | | if (StringHelper.isEmpty(re.getUrl())) { |
| | | list.add(re); |
| | | continue; |
| | | } |
| | | |
| | | URI uri = new URI(re.getTest()); |
| | | URI uri = new URI(re.getUrl()); |
| | | SocketAddress add = new InetSocketAddress(uri.getHost(), uri.getPort() == -1 ? 80 : uri.getPort()); |
| | | |
| | | // Ping通地址 |
| | | socket.connect(add, 2000); |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage()); |
| | | // log.error(e.getMessage()) |
| | | list.add(re); |
| | | } finally { |
| | | try { |
| | |
| | | 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(); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 用户登录状态 |
| | | */ |
| | | public JSONObject userLoginCount() { |
| | | List<LoginEntity> list = loginService.selectLoginCounts(); |
| | | if (list == null || list.isEmpty()) { |