Java获取服务器信息
Posted 爱锅巴
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(java时间操作:获取当天剩余时间
java SD卡相关的工具类包括获取SD卡是否可用,剩余空间,总空间,剩余空间是否足够..当然你必须先申请访问SD卡的权限。