当前位置:   article > 正文

Java获取服务器信息

java获取服务器信息
通过java代码获取服务器相关信息工具类
import com.sun.management.OperatingSystemMXBean;
import org.springframework.util.ClassUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.lang.management.ManagementFactory;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.Formatter;
import java.util.StringTokenizer;

public class MonitorInfo {

    private static final int BYTE_TO_KB = 1024;

    private static final int BYTE_TO_MB = 1024 * 1024;

    private static final int BYTE_TO_GB = 1024 * 1024 * 1024;

    private static final String CMD = "cat /proc/net/dev";

    private static final String WINCMD = "netstat -e";

    /**
     * 获取操作系统名称
     *
     * @return
     */
    public static String getOsName() {
        String osName = System.getProperty("os.name");
        return osName;
    }

    /**
     * 获取系统cpu负载(百分比不带百分号)
     *
     * @return
     */
    public static double getSystemCpuLoad() {
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        double systemCpuLoad = osmxb.getSystemCpuLoad() * 100;
        return formatNumber(systemCpuLoad);
    }

    /**
     * 获取物理内存负载(百分比不带百分号)
     *
     * @return
     */
    public static double getMemoryLoad() {
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        double totalMemorySize = osmxb.getTotalPhysicalMemorySize() / BYTE_TO_MB;
        double usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / BYTE_TO_MB;
        double memorySizeLoad = usedMemory / totalMemorySize * 100;
        return formatNumber(memorySizeLoad);
    }

    /**
     * 获取总的物理内存(单位:M)
     *
     * @return
     */
    public static long getTotalMemorySize() {
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / BYTE_TO_MB;
        return totalMemorySize;
    }

    /**
     * 获取剩余的物理内存(单位:M)
     *
     * @return
     */
    public static long getFreePhysicalMemorySize() {
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() / BYTE_TO_MB;
        return freePhysicalMemorySize;
    }

    /**
     * 获取已使用的物理内存(单位:M)
     *
     * @return
     */
    public static long getUsedMemory() {
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / BYTE_TO_MB;
        return usedMemory;
    }

    /**
     * JVM内存负载(百分比不带百分号)
     *
     * @return
     */
    public static double getJvmMemoryLoad() {
        Runtime rt = Runtime.getRuntime();
        long jvmTotal = rt.totalMemory();
        long jvmFree = rt.freeMemory();
        long jvmUse = jvmTotal - jvmFree;
        double jvmMemoryLoad = (double) jvmUse / jvmTotal * 100;
        return formatNumber(jvmMemoryLoad);
    }

    /**
     * 获取jvm线程负载(百分比不带百分号)
     *
     * @return
     */
    public static double getProcessCpuLoad() {
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        double ProcessCpuLoad = osmxb.getProcessCpuLoad() * 100;
        return formatNumber(ProcessCpuLoad);
    }

    /**
     * JVM内存的空闲空间(单位:M)
     *
     * @return
     */
    public static long getJvmFreeMemory() {
        Runtime rt = Runtime.getRuntime();
        long jvmFree = rt.freeMemory() / BYTE_TO_MB;
        return jvmFree;
    }

    /**
     * JVM内存已用的空间(单位:M)
     *
     * @return
     */
    public static long getJvmUseMemory() {
        Runtime rt = Runtime.getRuntime();
        long jvmTotal = rt.totalMemory() / BYTE_TO_MB;
        long jvmFree = rt.freeMemory() / BYTE_TO_MB;
        long jvmUse = jvmTotal - jvmFree;
        return jvmUse;
    }

    /**
     * JVM总内存空间(单位:M)
     *
     * @return
     */
    public static long getJvmTotalMemory() {
        Runtime rt = Runtime.getRuntime();
        long jvmTotal = rt.totalMemory() / BYTE_TO_MB;
        return jvmTotal;
    }

    /**
     * JVM最大能够申请的内存(单位:M)
     *
     * @return
     */
    public static long getJvmMaxMemory() {
        Runtime rt = Runtime.getRuntime();
        long jvmMax = rt.maxMemory() / BYTE_TO_MB;
        return jvmMax;
    }

    /**
     * 获取磁盘已使用大小(单位:G)
     *
     * @return
     */
    public static long getTotalDisk() {
        File[] roots = File.listRoots();// 获取磁盘分区列表
        long total = 0;
        for (File file : roots) {
            total += file.getTotalSpace();
        }
        return total / BYTE_TO_GB;
    }

    /**
     * 获取磁盘已使用大小(单位:G)
     *
     * @return
     */
    public static long getUsedDisk() {
        File[] roots = File.listRoots();// 获取磁盘分区列表
        long free = 0;
        long total = 0;
        for (File file : roots) {
            free += file.getFreeSpace();
            total += file.getTotalSpace();
        }
        long used = (total - free) / BYTE_TO_GB;
        return used;
    }

    /**
     * 获取磁盘未使用大小(单位:G)
     *
     * @return
     */
    public static long getFreeSpace() {
        File[] roots = File.listRoots();// 获取磁盘分区列表
        long free = 0;
        for (File file : roots) {
            free += file.getFreeSpace();
        }
        return free / BYTE_TO_GB;
    }

    /**
     * 获取磁盘负载(单位:G)
     *
     * @return
     */
    public static double getDiskLoad() {
        File[] roots = File.listRoots();// 获取磁盘分区列表
        long freeTotal = 0;
        long total = 0;
        for (File file : roots) {
            freeTotal += file.getFreeSpace();
            total += file.getTotalSpace();
        }
        long useTotal = total - freeTotal;
        double spaceLoad = (double) useTotal / total * 100;
        return formatNumber(spaceLoad);
    }

    /**
     * 获取下载速度
     *
     * @return
     */
    public static String getDownloadSpeed(long sleepTime) {
        if (getOsName().toLowerCase().startsWith("windows")) { // 判断操作系统类型是否为:windows
            return getDownloadSpeedForWindows(sleepTime);
        } else {
            return getLinuxDownloadSpeed(sleepTime);
        }
    }

    /**
     * 获取上传速度
     *
     * @return
     */
    public static String getUploadSpeed(long sleepTime) {
        if (getOsName().toLowerCase().startsWith("windows")) { // 判断操作系统类型是否为:windows
            return getUploadSpeedForWindows(sleepTime);
        } else {
            return getLinuxUploadSpeed(sleepTime);
        }
    }

    /**
     * 获取Windows环境下网口的下行速率
     *
     * @return
     */
    public static String getDownloadSpeedForWindows(long sleepTime) {
        Process pro1;
        Process pro2;
        Runtime r = Runtime.getRuntime();
        BufferedReader input;
        String downloadSpeed = "";
        try {
            pro1 = r.exec(WINCMD);
            input = new BufferedReader(new InputStreamReader(pro1.getInputStream()));
            String result1[] = readInLine(input);
            Thread.sleep(sleepTime * 1000);
            pro2 = r.exec(WINCMD);
            input = new BufferedReader(new InputStreamReader(pro2.getInputStream()));
            String result2[] = readInLine(input);
            downloadSpeed = Double.toString(formatNumber((double) (Long.parseLong(result2[0]) - Long.parseLong(result1[0])) / BYTE_TO_KB / (sleepTime))); // 平均到每秒的上行速率(KB/s)
            pro1.destroy();
            pro2.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return downloadSpeed;
    }

    /**
     * 获取Windows环境下网口的上行速率
     *
     * @return
     */
    public static String getUploadSpeedForWindows(long sleepTime) {
        Process pro1;
        Process pro2;
        Runtime r = Runtime.getRuntime();
        BufferedReader input;
        String uploadSpeed = "";
        try {
            pro1 = r.exec(WINCMD);
            input = new BufferedReader(new InputStreamReader(pro1.getInputStream()));
            String result1[] = readInLine(input);
            Thread.sleep(sleepTime * 1000);
            pro2 = r.exec(WINCMD);
            input = new BufferedReader(new InputStreamReader(pro2.getInputStream()));
            String result2[] = readInLine(input);
            uploadSpeed = Double.toString(formatNumber((double) (Long.parseLong(result2[1]) - Long.parseLong(result1[1])) / BYTE_TO_KB / (sleepTime))); // 平均到每秒的下行速率(KB/s)
            pro1.destroy();
            pro2.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return uploadSpeed;
    }

    /**
     * 获取Linux环境下网口的下行速率
     *
     * @return
     */
    public static String getLinuxDownloadSpeed(long time) {
        String info1 = null;
        String info2 = null;
        try {
            info1 = MonitorInfo.runCommand(CMD);
            Thread.sleep(time * 1000);
            info2 = MonitorInfo.runCommand(CMD);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String[] data1 = info1.split("\n");
        String[] data2 = info2.split("\n");
        long receiveBytes1 = 0;
        long receiveBytes2 = 0;

        long receiveBytes = 0;
        for (int i = 2; i < data1.length; i++) {
            if (data1[i].trim().startsWith("eth0")) {
                String[] numdata1 = data1[i].trim().split(" +");
                String[] numdata2 = data2[i].trim().split(" +");
                receiveBytes1 = Long.parseLong(numdata1[1]);
                receiveBytes2 = Long.parseLong(numdata2[1]);
                receiveBytes = receiveBytes2 - receiveBytes1;
            }
        }
        return Long.toString(receiveBytes / time / BYTE_TO_KB);
    }

    /**
     * 获取Linux环境下网口的上行速率
     *
     * @return
     */
    public static String getLinuxUploadSpeed(long time) {
        String info1 = null;
        String info2 = null;
        try {
            info1 = MonitorInfo.runCommand(CMD);
            Thread.sleep(time * 1000);
            info2 = MonitorInfo.runCommand(CMD);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String[] data1 = info1.split("\n");
        String[] data2 = info2.split("\n");
        long transmitBytes1 = 0;
        long transmitBytes2 = 0;

        long transmitBytes = 0;
        for (int i = 2; i < data1.length; i++) {
            if (data1[i].trim().startsWith("eth0")) {
                String[] numdata1 = data1[i].trim().split(" +");
                String[] numdata2 = data2[i].trim().split(" +");
                transmitBytes1 = Long.parseLong(numdata1[8]);
                transmitBytes2 = Long.parseLong(numdata1[8]);
                transmitBytes = transmitBytes2 - transmitBytes1;
            }
        }
        return Long.toString(transmitBytes / time / BYTE_TO_KB);
    }

    /**
     * springboot项目用java -jar运行的jar包运行目录
     * @return
     */
    public static String getJarPath() {
        String jarPath = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        String[] sarry = jarPath.split("/");
        String path = "";
        for (int i = 1; i < sarry.length - 5; i++) {
            path += "/" + sarry[i];
        }
        return path;
    }

    /**
     * 获取本地IP
     * @return
     */
    public static String getLocalIp() {
        /*InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return addr.getHostAddress();*/
        return getRouterIp();
    }

    /**
     * 获取路由分配的IP
     * @return
     */
    @SuppressWarnings("unchecked")
    public static String getRouterIp() {
        String SERVER_IP = null;
        try {
            Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                Enumeration addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    ip = (InetAddress) addresses.nextElement();
                    if (ip != null && ip instanceof Inet4Address) {
                        if(!"127.0.0.1".equals(ip.getHostAddress())){
                            SERVER_IP = ip.getHostAddress();
                        }
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return SERVER_IP;
    }

    /**
     * 获取公网IP
     * @return
     * @throws IOException
     */
    public static String getServerIp() throws IOException {
        InputStream ins = null;
        try {
            URL url = new URL("https://ip.cn/");
            URLConnection con = url.openConnection();
            con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

            ins = con.getInputStream();
            InputStreamReader isReader = new InputStreamReader(ins, "utf-8");
            BufferedReader bReader = new BufferedReader(isReader);
            StringBuffer webContent = new StringBuffer();
            String str;
            while ((str = bReader.readLine()) != null) {
                webContent.append(str);
            }
            int start = webContent.indexOf("<code>") + 6;
            int end = webContent.indexOf("</code");
            return webContent.substring(start, end);
        } finally {
            if (ins != null) {
                ins.close();
            }
        }
    }
    /**
     * 获取网口上下行速率
     *
     * @param input
     * @return
     */
    public static String[] readInLine(BufferedReader input) {
        String rxResult = "";
        String txResult = "";
        StringTokenizer tokenStat;
        try {
            input.readLine();
            input.readLine();
            input.readLine();
            input.readLine();
            tokenStat = new StringTokenizer(input.readLine());
            tokenStat.nextToken();
            rxResult = tokenStat.nextToken();
            txResult = tokenStat.nextToken();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String arr[] = {rxResult, txResult};
        return arr;
    }

    /**
     * Java运行Linux命令
     * @param CMD
     * @return
     */
    public static String runCommand(String CMD) {
        String info = "";
        try {
            Process pos = Runtime.getRuntime().exec(CMD);
            pos.waitFor();
            InputStreamReader isr = new InputStreamReader(pos.getInputStream());
            LineNumberReader lnr = new LineNumberReader(isr);
            String line = "";
            while ((line = lnr.readLine()) != null) {
                info = info + line + "\n";
            }
            pos.destroy();
        } catch (IOException e) {
            info = e.toString();
        } catch (Exception e) {
            info = e.toString();
        }
        return info;
    }
    /**
     * 格式化浮点数保留两位小数
     * @param d
     * @return
     */
    private static double formatNumber(double d) {
        return Double.parseDouble(new Formatter().format("%.2f", d).toString());
    }

}
  • 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
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/58848
推荐阅读
相关标签
  

闽ICP备14008679号