最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)獲取服務(wù)器資源(內(nèi)存,負(fù)載,磁盤容量)

 更新時(shí)間:2025年07月21日 10:08:08   作者:大魚>  
這篇文章主要為大家詳細(xì)介紹了如何Java實(shí)現(xiàn)獲取服務(wù)器資源信息,包括內(nèi)存,負(fù)載,磁盤容量等內(nèi)容,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1.說明

我們經(jīng)常通過SSH終端發(fā)送shell命令進(jìn)行服務(wù)器運(yùn)維,從而獲取到服務(wù)器的各種資源,按照這個(gè)思路,我們可以利用Java做一個(gè)定時(shí)任務(wù),定時(shí)采集服務(wù)器資源使用情況,從而實(shí)現(xiàn)服務(wù)器資源的動(dòng)態(tài)呈現(xiàn)。

2.封裝SSH操作方法

首先我們定義SSH連接實(shí)體類。

/**
 * SSH連接
 * @author Mr.Li
 * @date 2023-01-01
 */
public class SshConnection {
    private String username;
    private String password;
    private String hostname;

    public SshConnection(String username, String password, String hostname) {
        this.username = username;
        this.password = password;
        this.hostname = hostname;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getHostname() {
        return hostname;
    }
}

然后封裝SSH命令操作方法

引入Jar包

        <dependency>
            <groupId>org.apache.sshd</groupId>
            <artifactId>sshd-core</artifactId>
            <version>2.8.0</version>
        </dependency>

        <dependency>
            <groupId>net.i2p.crypto</groupId>
            <artifactId>eddsa</artifactId>
            <version>0.3.0</version>
        </dependency>
/**
 * SSH linux操作類
 * @author Mr.Li
 * @date 2023-01-06
 */
@Slf4j
public class SSHLinuxUtils {

    /**
     * 執(zhí)行Shell命令并返回結(jié)果
     * @param conn
     * @param cmd
     * @param timeout
     * @return
     * @throws IOException
     */
    public static SshResponse runCommand(SshConnection conn, String cmd, long timeout) {
        SshClient client = SshClient.setUpDefaultClient();
        try {
            //Open the client
            client.start();
            //Connect to the server
            String hostIp="";
            Integer port=22;
            String [] hostArr=conn.getHostname().split(":");
            if(hostArr.length>1){
                hostIp=hostArr[0];
                port=Integer.parseInt(hostArr[1]);
            }else{
                hostIp=hostArr[0];
            }
            ConnectFuture cf = client.connect(conn.getUsername(), hostIp, port);
            ClientSession session = cf.verify().getSession();
            session.addPasswordIdentity(conn.getPassword());
            session.auth().verify(TimeUnit.SECONDS.toMillis(timeout));
            //Create the exec and channel its output/error streams
            ChannelExec ce = session.createExecChannel(cmd);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ByteArrayOutputStream err = new ByteArrayOutputStream();
            ce.setOut(out);
            ce.setErr(err);
            //Execute and wait
            ce.open();
            Set<ClientChannelEvent> events =
                    ce.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), TimeUnit.SECONDS.toMillis(timeout));
            session.close(false);
            //Check if timed out
            if (events.contains(ClientChannelEvent.TIMEOUT)) {
                log.error(conn.getHostname()+" 命令 "+cmd+ "執(zhí)行超時(shí) "+timeout);
            }
            return new SshResponse(out.toString(), err.toString(), ce.getExitStatus());
        }catch (Exception e){
            log.error("runCommand:cmd:{}",cmd,e);
            return null;
        } finally {
            client.stop();
        }
    }
}

3.執(zhí)行Shell命令

連接服務(wù)器

SshConnection sshConnection = new SshConnection("遠(yuǎn)程登錄服務(wù)器用戶名","遠(yuǎn)程登錄服務(wù)器密碼","遠(yuǎn)程登錄服務(wù)器的IP端口");

以獲取內(nèi)存為例:

//獲取內(nèi)存的命令
String cmd="sudo cat /proc/meminfo";
//執(zhí)行獲取當(dāng)前內(nèi)存的命令
SshResponse sshResponse = SSHLinuxUtils.runCommand(sshConnection,cmd,3);
//其中StdOutput為獲取到的內(nèi)存數(shù)據(jù)
String outPut=sshResponse.getStdOutput();

獲取負(fù)載與磁盤,則只需要經(jīng)命令更換成如下命令即可

//負(fù)載
String cmd="sudo cat /proc/loadavg";
//磁盤
String cmd="sudo df -h";

4.效果呈現(xiàn)

結(jié)合自己的業(yè)務(wù),以及之前介紹的關(guān)于Supervisor監(jiān)控服務(wù)的對接,我們可以完成一個(gè)簡單的服務(wù)運(yùn)維業(yè)務(wù)。

監(jiān)控指標(biāo)

進(jìn)程監(jiān)控

到此這篇關(guān)于Java實(shí)現(xiàn)獲取服務(wù)器資源(內(nèi)存,負(fù)載,磁盤容量)的文章就介紹到這了,更多相關(guān)Java獲取服務(wù)器資源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

和平区| 通城县| 荔波县| 玉林市| 措美县| 镇平县| 隆尧县| 玉树县| 华池县| 太康县| 芷江| 大庆市| 双牌县| 钟祥市| 汉阴县| 九寨沟县| 呼和浩特市| 鄂温| 年辖:市辖区| 三台县| 启东市| 临沭县| 平乐县| 泌阳县| 陵川县| 枣庄市| 榕江县| 陆良县| 佛山市| 常山县| 盐边县| 孝昌县| 沁阳市| 孟连| 遂宁市| 灵璧县| 神木县| 洪雅县| 樟树市| 绥滨县| 油尖旺区|