当前位置:   article > 正文

Java 获取 Linux服务器主机名称、内网ip、cpu使用率、内存使用率、磁盘使用率、JVM使用率

Java 获取 Linux服务器主机名称、内网ip、cpu使用率、内存使用率、磁盘使用率、JVM使用率

下面的代码直接打包带走使用
1、pom文件依赖

 <dependency>
     <groupId>com.jcraft</groupId>
     <artifactId>jsch</artifactId>
     <version>0.1.55</version> <!-- 请根据实际情况检查最新版本 -->
 </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

2、代码

package com.xffy.order.controller;

import com.jcraft.jsch.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class LinuxServerInfo {

    public static void main(String[] args) {
        String host = "10.15.111.15";
        int port = 22;
        String username = "zhang";
        String password = "123456";

        try {
            // 创建JSch对象
            JSch jsch = new JSch();

            // 创建会话
            Session session = jsch.getSession(username, host, port);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);

            // 连接
            session.connect();

            // 获取服务器信息
            String serverName = executeCommand(session, "hostname");
            String internalIP = executeCommand(session, "hostname -I | awk '{print $1}'");
            String cpuUsage = executeCommand(session, "top -bn1 | grep 'Cpu(s)' | awk '{print $2 + $4}'");
            String memoryUsage = executeCommand(session, "free | awk 'NR==2 {print $3/$2*100}'");
            String diskUsage = executeCommand(session, "df -h | awk '$NF==\"/\"{print $5}'");
            double jvmUsage = getJVMUsage();
            String jvmUsageStr = String.format("%.0f", jvmUsage)  + "%";

            // 打印结果
            System.out.println("Server Name: " + serverName);
            System.out.println("Internal IP: " + internalIP);
            System.out.println("CPU Usage: " + cpuUsage + "%");
            System.out.println("Memory Usage: " + memoryUsage + "%");
            System.out.println("Disk Usage: " + diskUsage);
            System.out.println("JVM Usage: " + jvmUsageStr);

            // 关闭会话
            session.disconnect();

        } catch (JSchException e) {
            e.printStackTrace();
        }
    }

    // 执行Shell命令并获取输出
    private static String executeCommand(Session session, String command) throws JSchException {
        StringBuilder output = new StringBuilder();

        try {
            // 创建通道
            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(command);

            // 获取输入流
            InputStream inputStream = channel.getInputStream();

            // 连接通道
            channel.connect();

            // 读取命令输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;

            while ((line = reader.readLine()) != null) {
                output.append(line).append("\n");
            }

            // 关闭通道
            channel.disconnect();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return output.toString().trim();
    }

    // 获取JVM使用率
    private static double getJVMUsage() {
        Runtime runtime = Runtime.getRuntime();
        double totalMemory = runtime.totalMemory();
        double freeMemory = runtime.freeMemory();
        double maxMemory = runtime.maxMemory();

        double jvmUsage = ((totalMemory - freeMemory) / maxMemory) * 100;
        return jvmUsage;
    }
}

  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/64982
推荐阅读
相关标签
  

闽ICP备14008679号