java获取服务器所有信息

1 import java.net.InetAddress;  2 import java.net.UnknownHostException;  3 import java.util.Map;  4 import java.util.Properties;  5 import org.hyperic.sigar.CpuInfo;  6 import org.hyperic.sigar.CpuPerc;  7 import org.hyperic.sigar.FileSystem;  8 import org.hyperic.sigar.FileSystemUsage;  9 import org.hyperic.sigar.Mem; 10 import org.hyperic.sigar.NetFlags; 11 import org.hyperic.sigar.NetInterfaceConfig; 12 import org.hyperic.sigar.NetInterfaceStat; 13 import org.hyperic.sigar.OperatingSystem; 14 import org.hyperic.sigar.Sigar; 15 import org.hyperic.sigar.SigarException; 16 import org.hyperic.sigar.Swap; 17 import org.hyperic.sigar.Who; 18  19 public class SysListener { 20     public static void main(String[] args) { 21         try { 22             // System信息,从jvm获取 23             property(); 24             System.out.println("-"); 25             // cpu信息 26             cpu(); 27             System.out.println("-"); 28             // 内存信息 29             memory(); 30             System.out.println("-"); 31             // 操作系统信息 32             os(); 33             System.out.println("-"); 34             // 用户信息 35             who(); 36             System.out.println("-"); 37             // 文件系统信息 38             file(); 39             System.out.println("-"); 40             // 网络信息 41             net(); 42             System.out.println("-"); 43             // 以太网信息 44             ethernet(); 45             System.out.println("-"); 46         } catch (Exception e1) { 47             e1.printStackTrace(); 48         } 49     } 50  51     private static void property() throws UnknownHostException { 52         Runtime r = Runtime.getRuntime(); 53         Properties props = System.getProperties(); 54         InetAddress addr; 55         addr = InetAddress.getLocalHost(); 56         String ip = addr.getHostAddress(); 57         Map<String, String> map = System.getenv(); 58         String userName = map.get("USERNAME");// 获取用户名 59         String computerName = map.get("COMPUTERNAME");// 获取计算机名 60         String userDomain = map.get("USERDOMAIN");// 获取计算机域名 61         System.out.println("用户名:    " + userName); 62         System.out.println("计算机名:    " + computerName); 63         System.out.println("计算机域名:    " + userDomain); 64         System.out.println("本地ip地址:    " + ip); 65         System.out.println("本地主机名:    " + addr.getHostName()); 66         System.out.println("JVM可以使用的总内存:    " + r.totalMemory()); 67         System.out.println("JVM可以使用的剩余内存:    " + r.freeMemory()); 68         System.out.println("JVM可以使用的处理器个数:    " + r.availableProcessors()); 69         System.out.println("Java的运行环境版本:    " + props.getProperty("java.version")); 70         System.out.println("Java的运行环境供应商:    " + props.getProperty("java.vendor")); 71         System.out.println("Java供应商的URL:    " + props.getProperty("java.vendor.url")); 72         System.out.println("Java的安装路径:    " + props.getProperty("java.home")); 73         System.out.println("Java的虚拟机规范版本:    " + props.getProperty("java.vm.specification.version")); 74         System.out.println("Java的虚拟机规范供应商:    " + props.getProperty("java.vm.specification.vendor")); 75         System.out.println("Java的虚拟机规范名称:    " + props.getProperty("java.vm.specification.name")); 76         System.out.println("Java的虚拟机实现版本:    " + props.getProperty("java.vm.version")); 77         System.out.println("Java的虚拟机实现供应商:    " + props.getProperty("java.vm.vendor")); 78         System.out.println("Java的虚拟机实现名称:    " + props.getProperty("java.vm.name")); 79         System.out.println("Java运行时环境规范版本:    " + props.getProperty("java.specification.version")); 80         System.out.println("Java运行时环境规范供应商:    " + props.getProperty("java.specification.vender")); 81         System.out.println("Java运行时环境规范名称:    " + props.getProperty("java.specification.name")); 82         System.out.println("Java的类格式版本号:    " + props.getProperty("java.class.version")); 83         System.out.println("Java的类路径:    " + props.getProperty("java.class.path")); 84         System.out.println("加载库时搜索的路径列表:    " + props.getProperty("java.library.path")); 85         System.out.println("默认的临时文件路径:    " + props.getProperty("java.io.tmpdir")); 86         System.out.println("一个或多个扩展目录的路径:    " + props.getProperty("java.ext.dirs")); 87         System.out.println("操作系统的名称:    " + props.getProperty("os.name")); 88         System.out.println("操作系统的构架:    " + props.getProperty("os.arch")); 89         System.out.println("操作系统的版本:    " + props.getProperty("os.version")); 90         System.out.println("文件分隔符:    " + props.getProperty("file.separator")); 91         System.out.println("路径分隔符:    " + props.getProperty("path.separator")); 92         System.out.println("行分隔符:    " + props.getProperty("line.separator")); 93         System.out.println("用户的账户名称:    " + props.getProperty("user.name")); 94         System.out.println("用户的主目录:    " + props.getProperty("user.home")); 95         System.out.println("用户的当前工作目录:    " + props.getProperty("user.dir")); 96     } 97  98     private static void memory() throws SigarException { 99         Sigar sigar = new Sigar();100         Mem mem = sigar.getMem();101         // 内存总量102         System.out.println("内存总量:    " + mem.getTotal() / 1024L + "K av");103         // 当前内存使用量104         System.out.println("当前内存使用量:    " + mem.getUsed() / 1024L + "K used");105         // 当前内存剩余量106         System.out.println("当前内存剩余量:    " + mem.getFree() / 1024L + "K free");107         Swap swap = sigar.getSwap();108         // 交换区总量109         System.out.println("交换区总量:    " + swap.getTotal() / 1024L + "K av");110         // 当前交换区使用量111         System.out.println("当前交换区使用量:    " + swap.getUsed() / 1024L + "K used");112         // 当前交换区剩余量113         System.out.println("当前交换区剩余量:    " + swap.getFree() / 1024L + "K free");114     }115 116     private static void cpu() throws SigarException {117         Sigar sigar = new Sigar();118         CpuInfo infos[] = sigar.getCpuInfoList();119         CpuPerc cpuList[] = null;120         cpuList = sigar.getCpuPercList();121         for (int i = 0; i < infos.length; i++) {// 不管是单块CPU还是多CPU都适用122             CpuInfo info = infos[i];123             System.out.println("第" + (i + 1) + "块CPU信息");124             System.out.println("CPU的总量MHz:    " + info.getMhz());// CPU的总量MHz125             System.out.println("CPU生产商:    " + info.getVendor());// 获得CPU的卖主,如:Intel126             System.out.println("CPU类别:    " + info.getModel());// 获得CPU的类别,如:Celeron127             System.out.println("CPU缓存数量:    " + info.getCacheSize());// 缓冲存储器数量128             printCpuPerc(cpuList[i]);129         }130     }131 132     private static void printCpuPerc(CpuPerc cpu) {133         System.out.println("CPU用户使用率:    " + CpuPerc.format(cpu.getUser()));// 用户使用率134         System.out.println("CPU系统使用率:    " + CpuPerc.format(cpu.getSys()));// 系统使用率135         System.out.println("CPU当前等待率:    " + CpuPerc.format(cpu.getWait()));// 当前等待率136         System.out.println("CPU当前错误率:    " + CpuPerc.format(cpu.getNice()));//137         System.out.println("CPU当前空闲率:    " + CpuPerc.format(cpu.getIdle()));// 当前空闲率138         System.out.println("CPU总的使用率:    " + CpuPerc.format(cpu.getCombined()));// 总的使用率139     }140 141     private static void os() {142         OperatingSystem OS = OperatingSystem.getInstance();143         // 操作系统内核类型如: 386、486、586等x86144         System.out.println("操作系统:    " + OS.getArch());145         System.out.println("操作系统CpuEndian():    " + OS.getCpuEndian());//146         System.out.println("操作系统DataModel():    " + OS.getDataModel());//147         // 系统描述148         System.out.println("操作系统的描述:    " + OS.getDescription());149         // 操作系统类型150         // System.out.println("OS.getName():    " + OS.getName());151         // System.out.println("OS.getPatchLevel():    " + OS.getPatchLevel());//152         // 操作系统的卖主153         System.out.println("操作系统的卖主:    " + OS.getVendor());154         // 卖主名称155         System.out.println("操作系统的卖主名:    " + OS.getVendorCodeName());156         // 操作系统名称157         System.out.println("操作系统名称:    " + OS.getVendorName());158         // 操作系统卖主类型159         System.out.println("操作系统卖主类型:    " + OS.getVendorVersion());160         // 操作系统的版本号161         System.out.println("操作系统的版本号:    " + OS.getVersion());162     }163 164     private static void who() throws SigarException {165         Sigar sigar = new Sigar();166         Who who[] = sigar.getWhoList();167         if (who != null && who.length > 0) {168             for (int i = 0; i < who.length; i++) {169                 // System.out.println("当前系统进程表中的用户名" + String.valueOf(i));170                 Who _who = who[i];171                 System.out.println("用户控制台:    " + _who.getDevice());172                 System.out.println("用户host:    " + _who.getHost());173                 // System.out.println("getTime():    " + _who.getTime());174                 // 当前系统进程表中的用户名175                 System.out.println("当前系统进程表中的用户名:    " + _who.getUser());176             }177         }178     }private static void file() throws Exception {179         Sigar sigar = new Sigar();180         FileSystem fslist[] = sigar.getFileSystemList();181         for (int i = 0; i < fslist.length; i++) {182             System.out.println("分区的盘符名称" + i);183             FileSystem fs = fslist[i];184             // 分区的盘符名称185             System.out.println("盘符名称:    " + fs.getDevName());186             // 分区的盘符名称187             System.out.println("盘符路径:    " + fs.getDirName());188             System.out.println("盘符标志:    " + fs.getFlags());//189             // 文件系统类型,比如 FAT32、NTFS190             System.out.println("盘符类型:    " + fs.getSysTypeName());191             // 文件系统类型名,比如本地硬盘、光驱、网络文件系统等192             System.out.println("盘符类型名:    " + fs.getTypeName());193             // 文件系统类型194             System.out.println("盘符文件系统类型:    " + fs.getType());195             FileSystemUsage usage = null;196             switch (fs.getType()) {197             case 0: // TYPE_UNKNOWN :未知198                 break;199             case 1: // TYPE_NONE200                 break;201             case 2: // TYPE_LOCAL_DISK : 本地硬盘202                 // 文件系统总大小203             usage = sigar.getFileSystemUsage(fs.getDirName());204                 System.out.println(fs.getDevName() + "总大小:    " + usage.getTotal() + "KB");205                 // 文件系统剩余大小206                 System.out.println(fs.getDevName() + "剩余大小:    " + usage.getFree() + "KB");207                 // 文件系统可用大小208 System.out.println(fs.getDevName() + "可用大小: " + usage.getAvail() + "KB");209                 // 文件系统已经使用量210                 System.out.println(fs.getDevName() + "已经使用量:    " + usage.getUsed() + "KB");211                 double usePercent = usage.getUsePercent() * 100D;212                 // 文件系统资源的利用率213                 System.out.println(fs.getDevName() + "资源的利用率:    " + usePercent + "%");214                 System.out.println(fs.getDevName() + "读出:    " + usage.getDiskReads());215                 System.out.println(fs.getDevName() + "写入:    " + usage.getDiskWrites());216                 break;217             case 3:// TYPE_NETWORK :网络218                 break;219             case 4:// TYPE_RAM_DISK :闪存220                 break;221             case 5:// TYPE_CDROM :光驱222                 break;223             case 6:// TYPE_SWAP :页面交换224                 break;225             }226             227         }228         return;229     }230 231     private static void net() throws Exception {232         Sigar sigar = new Sigar();233         String ifNames[] = sigar.getNetInterfaceList();234         for (int i = 0; i < ifNames.length; i++) {235             String name = ifNames[i];236             NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);237             System.out.println("网络设备名:    " + name);// 网络设备名238             System.out.println("IP地址:    " + ifconfig.getAddress());// IP地址239             System.out.println("子网掩码:    " + ifconfig.getNetmask());// 子网掩码240             if ((ifconfig.getFlags() & 1L) <= 0L) {241                 System.out.println("!IFF_UP...skipping getNetInterfaceStat");242                 continue;243             }244             NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);245             System.out.println(name + "接收的总包裹数:" + ifstat.getRxPackets());// 接收的总包裹数246             System.out.println(name + "发送的总包裹数:" + ifstat.getTxPackets());// 发送的总包裹数247             System.out.println(name + "接收到的总字节数:" + ifstat.getRxBytes());// 接收到的总字节数248             System.out.println(name + "发送的总字节数:" + ifstat.getTxBytes());// 发送的总字节数249             System.out.println(name + "接收到的错误包数:" + ifstat.getRxErrors());// 接收到的错误包数250             System.out.println(name + "发送数据包时的错误数:" + ifstat.getTxErrors());// 发送数据包时的错误数251             System.out.println(name + "接收时丢弃的包数:" + ifstat.getRxDropped());// 接收时丢弃的包数252             System.out.println(name + "发送时丢弃的包数:" + ifstat.getTxDropped());// 发送时丢弃的包数253         }254     }255 256     private static void ethernet() throws SigarException {257         Sigar sigar = null;258         sigar = new Sigar();259         String[] ifaces = sigar.getNetInterfaceList();260         for (int i = 0; i < ifaces.length; i++) {261             NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);262             if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress()) (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0263                     NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {264                 continue;265             }266             System.out.println(cfg.getName() + "IP地址:" + cfg.getAddress());// IP地址267             System.out.println(cfg.getName() + "网关广播地址:" + cfg.getBroadcast());// 网关广播地址268             System.out.println(cfg.getName() + "网卡MAC地址:" + cfg.getHwaddr());// 网卡MAC地址269             System.out.println(cfg.getName() + "子网掩码:" + cfg.getNetmask());// 子网掩码270             System.out.println(cfg.getName() + "网卡描述信息:" + cfg.getDescription());// 网卡描述信息271             System.out.println(cfg.getName() + "网卡类型" + cfg.getType());//272         }273     }274 }
(0)

相关推荐