JAVA如何利用Swiger获取Linux系统电脑配置相关信息
最近开发java应用程序,涉及到获取Linux服务器相关配置的问题,特地网上搜寻了下,采用Swiger包可以直接获取,再次小结一下,以便于以后能方便使用,也便于其他童鞋们学习。
推荐大家参考链接:https://www.cnblogs.com/kabi/p/5209315.html
值得注意的问题是:
1.如果是Linux的环境下,要把libsigar-amd64-linux.so文件存放到 lib64下面才能起到作用。
2.如果是Windos环境,将对应的文件存放到jdk的安装目录的bin目录下面即可。
项目中自己封装的工具类代码,大家可参考使用:
package com.xuanyin.common; import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer; import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException; import net.sf.json.JSONObject;
/**
* 此类用户获取Linux服务器配置信息
* @author Administrator
*
*/
public class LinuxSystem {
public static Sigar sigar = new Sigar();
/**
* 功能:获取Linux系统cpu使用率
* */
public static int cpuUsage() {
try {
Map<?, ?> map1 = cpuinfo();
Thread.sleep(500);
Map<?, ?> map2 = cpuinfo();
if(map1.size() > 0 && map2.size() > 0){
long user1 = Long.parseLong(map1.get("user").toString());
long nice1 = Long.parseLong(map1.get("nice").toString());
long system1 = Long.parseLong(map1.get("system").toString());
long idle1 = Long.parseLong(map1.get("idle").toString()); long user2 = Long.parseLong(map2.get("user").toString());
long nice2 = Long.parseLong(map2.get("nice").toString());
long system2 = Long.parseLong(map2.get("system").toString());
long idle2 = Long.parseLong(map2.get("idle").toString()); long total1 = user1 + system1 + nice1;
long total2 = user2 + system2 + nice2;
float total = total2 - total1; long totalIdle1 = user1 + nice1 + system1 + idle1;
long totalIdle2 = user2 + nice2 + system2 + idle2;
float totalidle = totalIdle2 - totalIdle1; float cpusage = (total / totalidle) * 100;
return (int) cpusage;
} } catch (InterruptedException e) {
e.printStackTrace();
}
return 0;
} /**
* 功能:CPU使用信息
* */
public static Map<?, ?> cpuinfo() {
InputStreamReader inputs = null;
BufferedReader buffer = null;
Map<String, Object> map = new HashMap<String, Object>();
try {
inputs = new InputStreamReader(new FileInputStream("/proc/stat"));
buffer = new BufferedReader(inputs);
String line = "";
while (true) {
line = buffer.readLine();
if (line == null) {
break;
}
if (line.startsWith("cpu")) {
StringTokenizer tokenizer = new StringTokenizer(line);
List<String> temp = new ArrayList<String>();
while (tokenizer.hasMoreElements()) {
String value = tokenizer.nextToken();
temp.add(value);
}
map.put("user", temp.get(1));
map.put("nice", temp.get(2));
map.put("system", temp.get(3));
map.put("idle", temp.get(4));
map.put("iowait", temp.get(5));
map.put("irq", temp.get(6));
map.put("softirq", temp.get(7));
map.put("stealstolen", temp.get(8));
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(buffer != null && inputs != null){
buffer.close();
inputs.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return map;
} /**
* huoqu CPU信息
* @throws SigarException
*/
public static JSONObject getCpuInformation() throws SigarException {
CpuInfo infos[] = sigar.getCpuInfoList();
CpuPerc cpuList[] = sigar.getCpuPercList();
JSONObject jsonObject = new JSONObject();
jsonObject.put("type",infos[0].getVendor() + " " + infos[0].getModel());
jsonObject.put("sys",CpuPerc.format(cpuList[0].getSys()));
jsonObject.put("use",CpuPerc.format(cpuList[0].getUser()));
jsonObject.put("total",CpuPerc.format(cpuList[0].getCombined()));
// jsonObject.put("total",CpuPerc.format(cpuUsage()));
return jsonObject;
} /**
*
* huoqu 系统内存
* @throws SigarException
*/
public static JSONObject getMemory() throws SigarException {
Mem mem = sigar.getMem();
JSONObject jsonObject = new JSONObject();
int memTotal = (int)Math.ceil((double)mem.getTotal() / (1024L * 1024L ));
String memFree = String.format("%.1f", (double)mem.getFree() / (1024L * 1024L));
jsonObject.put("total",memTotal); //总内存 单位M
jsonObject.put("use",memTotal-Double.parseDouble(memFree)); //剩余内存 单位M
jsonObject.put("residue",memFree); //剩余内存 单位M
return jsonObject;
} /**
* huoqu 硬盘信息
*/
public static JSONObject getDiskInfromation() {
JSONObject jsonObject = new JSONObject();
//获取硬盘
Long ypTotal = 0L;
Long ypfree = 0L;
try {
FileSystem fslist[] = sigar.getFileSystemList();
for (int i = 0; i < fslist.length; i++) {
FileSystem fs = fslist[i];
FileSystemUsage usage = null;
usage = sigar.getFileSystemUsage(fs.getDirName());
switch (fs.getType()) {
case 0: // TYPE_UNKNOWN :未知
break;
case 1: // TYPE_NONE
break;
case 2: // TYPE_LOCAL_DISK : 本地硬盘
// 文件系统总大小
ypTotal += usage.getTotal();
ypfree += usage.getFree();
break;
case 3:// TYPE_NETWORK :网络
break;
case 4:// TYPE_RAM_DISK :闪存
break;
case 5:// TYPE_CDROM :光驱
break;
case 6:// TYPE_SWAP :页面交换
break;
}
}
int hdTotal = (int)((double)ypTotal / (1024L));
String hdfree = String.format("%.1f", (double)ypfree / (1024L));
jsonObject.put("total",hdTotal); //总内存 单位M
jsonObject.put("use",hdTotal-Double.parseDouble(hdfree)); //剩余内存 单位M
jsonObject.put("residue",hdfree); //剩余内存 单位M
} catch (SigarException e1) {
e1.printStackTrace();
}
return jsonObject;
}
/**
* huoqu 网卡信息
*/
public static JSONObject getInterCardInformation(int i) {
JSONObject jsonObject = new JSONObject();
try {
String ifNames[] = sigar.getNetInterfaceList();
jsonObject.put("name", ifNames[i]);//网络名称
NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(ifNames[i]);
jsonObject.put("ip", ifconfig.getAddress()); //ip地址
jsonObject.put("sonip", ifconfig.getNetmask()); //子网掩码
jsonObject.put("cardinformation", ifconfig.getDescription()); //网卡信息
jsonObject.put("type", ifconfig.getType()); //网卡类型
NetInterfaceStat ifstat = sigar.getNetInterfaceStat(ifNames[i]);
jsonObject.put("recevie", ifstat.getRxBytes()); //接收到的总字节数
jsonObject.put("send", ifstat.getTxBytes()); //发送的总字节数
} catch (SigarException e) {
e.printStackTrace();
}
return jsonObject; }
/**
* 设置系统的CPI属性
*/
public static JSONObject setSysCpi() {
JSONObject jsonObject = new JSONObject();
String exeCmd = exeCmd("lspci | grep -i vga");
String exeCmd2 = exeCmd("lspci | grep -i audio");
jsonObject.put("showcard", exeCmd.substring(exeCmd.indexOf("controller: ")+12));
jsonObject.put("voidcard", exeCmd2.substring(exeCmd2.indexOf("controller: ")+12));
return jsonObject;
}
/**
* 执行linux命令并返回结果
* @param commandStr
* @return
*/
public static String exeCmd(String commandStr) { String result = null;
try {
String[] cmd = new String[]{"/bin/sh", "-c",commandStr};
Process ps = Runtime.getRuntime().exec(cmd); BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
//执行结果加上回车
sb.append(line).append("\n");
}
result = sb.toString(); } catch (Exception e) {
e.printStackTrace();
}
return result;
} }
JAVA如何利用Swiger获取Linux系统电脑配置相关信息的更多相关文章
- Java获取Linux系统cpu使用率
原文:http://www.open-open.com/code/view/1426152165201 import java.io.BufferedReader; import java.io.Fi ...
- C/C++获取Linux系统CPU和内存及硬盘使用情况
需求分析: 不使用Top df free 等命令,利用C/C++获取Linux系统CPU和内存及硬盘使用情况 实现: //通过获取/proc/stat (CPU)和/proc/meminfo(内存 ...
- Linux系统下查看硬件信息命令大全
导读 有许多命令可以用来查看 Linux 系统上的硬件信息.有些命令只能够打印出像 CPU 和内存这一特定的硬件组件信息,另外一些命令可以查看多种硬件组件的信息. 这个教程可以带大家快速了解一下查看各 ...
- Linux 系统下查看硬件信息命令大全
有许多命令可以用来查看 Linux 系统上的硬件信息.有些命令只能够打印出像 CPU 和内存这一特定的硬件组件信息,另外一些命令可以查看多种硬件组件的信息. 这个教程可以带大家快速了解一下查看各种硬件 ...
- 虚拟机Linux系统下配置网络
虚拟机上安装Redhat9.0后是没有网络的,而本来的Windows系统是可以上网的,此时想在Redhat上网就需要在Linux系统上配置网络,以下是笔者自己配置的一点心得. 1.电脑本机系统打开网络 ...
- Linux系统运维相关的面试题 (问答题)
这里给大家整理了一些Linux系统运维相关的面试题,有些问题没有标准答案,希望要去参加Linux运维面试的朋友,可以先思考下这些问题. 一.Linux操作系统知识 1.常见的Linux发行版本都有 ...
- 利用python获取自己的qq群成员信息
利用python获取自己的qq群成员信息! 首先说明一下需要使用的工具以及技术:python3 + selenium selenium安装方法:pip install selenium 前提:获取自己 ...
- Linux系统之网络相关的命令
Linux系统之网络相关的命令 网络概述 网络:通过通信介质和通信设备 将分布不同地点的两台或多台计算机,经过相应的程序实现通信switch 交换机router 路由器网络的功能:数据通信:利用网络传 ...
- Linux 系统 网络配置
Linux 系统 网络配置 配置Linux系统网络的方法有几种,这里介绍本人常用的两种. 第一种:使用命令ifconfig配置,具体用法:Ipconfig ethx x.x.x.x net ...
随机推荐
- 解决Linux启动redis时出现提示权限不够问题
如果权限不够请使用一条命令 chmod 777 操作 参考:https://blog.csdn.net/zczzsq/article/details/8162339
- Linux命令(精简版)
1:init 0 关机(root用户可用) 2:exit退出终端 3:who查看登录用户 4:whoami 查看当前用户 5:data 查看当前时间 data “月日时分年” 修改当前 ...
- Linux最小系统移植之早期打印CONFIG_DEBUG_LL
一.几个关键宏定义 CONFIG_DEBUG_LL. CONFIG_DEBUG_LL_INCLUDE 容我慢慢道来, 首先要使能早期打印, menuconfig必须选中CONFIG_DEBUG_LL, ...
- sleep、yield、join方法简介与用法 sleep与wait区别 多线程中篇(十五)
Object中的wait.notify.notifyAll,可以用于线程间的通信,核心原理为借助于监视器的入口集与等待集逻辑 通过这三个方法完成线程在指定锁(监视器)上的等待与唤醒,这三个方法是以锁( ...
- DSAPI 图形图像篇(上)
彩色文字对象 基于一些特殊需求,本人开发了彩色文字对象,该功能通过类似html代码的方式指示文本,并输出图像. 我们还是先来看一张图像. 这不是文本,是通过指定文本代码输出的图像.我们来看一下实现代码 ...
- Office组件无法正常使用的解决方法
问题与现象 开发时调用Office组件,代码编译是通过的,但在运行时当ApplicationClass对象初始化后程序出现异常. 异常信息如下: 无法将类型为“Microsof ...
- 自定义编译gdal库
作者:朱金灿 来源:http://blog.csdn.net/clever101 使用下载下来的gdal库的makefile来编译gdal库,生成的gdal库的名字debug版本和release版本都 ...
- 服务器三:多线程epoll
#include <fcntl.h> #include <sys/socket.h> #include <netinet/in.h> #include <ar ...
- DevExpress TreeList 禁止节点拖动到其他节点上
背景 在做一个类似文件树的控件,支持节点从树上向其它的控件拖动程序,但是要保证树上的节点不能拖动上其他的节点上. 代码 /// <summary> /// 拖动节点完成 /// </ ...
- MyBatis学习---整合SpringMVC
[目录]