whyclxw
2024-07-30 ed04733e15a5304c68ac16c121fd2989c851a57b
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
package com.whyc.service;
 
import cn.hutool.system.oshi.CpuInfo;
import cn.hutool.system.oshi.OshiUtil;
import com.sun.management.OperatingSystemMXBean;
import com.whyc.dto.ShowDTO;
import com.whyc.mapper.ServerStateMapper;
import com.whyc.pojo.ServerState;
import org.springframework.stereotype.Service;
import oshi.hardware.CentralProcessor;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.util.Util;
 
import javax.annotation.Resource;
import java.io.File;
import java.lang.management.ManagementFactory;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
 
@Service
public class ServerStateService {
    @Resource
    private ServerStateMapper mapper;
 
    public ServerState searchAll(){
        ServerState state = mapper.searchAll();
        //内存使用率
        OperatingSystemMXBean osmb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        // 总的物理内存
        double totalMemorySize = osmb.getTotalPhysicalMemorySize();
        totalMemorySize = BigDecimal.valueOf(totalMemorySize/1024/1024/1024*1.0).setScale(1, RoundingMode.CEILING).doubleValue();
        // 剩余的物理内存
        double freeMemorySize = osmb.getFreePhysicalMemorySize();
        freeMemorySize = BigDecimal.valueOf(freeMemorySize/1024/1024/1024*1.0).setScale(1,RoundingMode.CEILING).doubleValue();
 
        int memRate=(int)(BigDecimal.valueOf((1-freeMemorySize*1.0/totalMemorySize)).setScale(2,RoundingMode.CEILING).doubleValue()*100);
 
        /*//磁盘使用率
        Long freeDisk = state.getFree_disc_space()/1024;
        Long totalDisk = state.getTotal_disc_space()/1024;
        int diskRate = 0;
        if(totalDisk!=0) {
            diskRate = (int) (BigDecimal.valueOf((1 - freeDisk * 1.0 / totalDisk)).setScale(2, RoundingMode.CEILING).doubleValue() * 100);
        }else{
            diskRate=0;
        }
        //数据库连接使用率
        Integer currConn = state.getDb_conn_count();
        Integer maxConn = state.getDb_conn_max();
        int connRate = (int)(BigDecimal.valueOf((currConn*1.0/maxConn)).setScale(2,RoundingMode.CEILING).doubleValue()*100);
*/
 
        //获取磁盘容量
        File[] files = File.listRoots();
        long totalDiskSize =0;
        long freeDiskSize=0;
        for (File file:files){
            totalDiskSize+= file.getTotalSpace();
            freeDiskSize+= file.getFreeSpace();
        }
 
        freeDiskSize=freeDiskSize/1024/1024/1024;
        totalDiskSize=totalDiskSize/1024/1024/1024;
        int diskRate = (int)((BigDecimal.valueOf(totalDiskSize-freeDiskSize).divide(BigDecimal.valueOf(totalDiskSize),2, RoundingMode.CEILING)).doubleValue()*100);
 
        //获取连接池数据
        List connInfo = getDBConnectionInfo();
        int maxConn = (int) connInfo.get(0);
        int curConn = (int) connInfo.get(1);
        double connDivide = BigDecimal.valueOf(curConn*1.0/maxConn).setScale(2,RoundingMode.CEILING).doubleValue();
        int connRate = (int) (connDivide*100);
 
        //cpu使用率
        //double cpuLoad = osmb.getSystemCpuLoad();
        CpuInfo cpuInfo = OshiUtil.getCpuInfo();
        double free = cpuInfo.getFree();
        double cpuLoad = 100-free;
        long cpuLoadRate = Math.round(cpuLoad);
        //int cpuLoadRate = (int)(BigDecimal.valueOf(cpuLoad).setScale(2,RoundingMode.CEILING).doubleValue()*100);
 
        state.setFreeMen(freeMemorySize);
        state.setTotalMem(totalMemorySize);
        state.setFreeDiscSpace(freeDiskSize);
        state.setTotalDiscSpace(totalDiskSize);
        state.setDbCOnnCount(curConn);
        state.setDbConnMax(maxConn);
 
        state.setMemRate(memRate);
        state.setDiskRate(diskRate);
        state.setConnRate(connRate);
        state.setServerCpuRate((float) cpuLoadRate);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        state.setNote(sdf.format(new Date()));
        return state;
    }
 
    public List getDBConnectionInfo(){
        ArrayList<Integer> list = new ArrayList<>();
        ShowDTO obj1 = mapper.threadsConnected();
        ShowDTO obj2 = mapper.maxConnections();
        list.add(obj2.getValue());
        list.add(obj1.getValue());
        return list;
    }
 
 
}