本文章由公号【开发小鸽】发布!欢迎关注!!!
老规矩–妹妹镇楼:
1. 监控程序运行状态包sigar
(1)概述
? ? ? ?Sigar是Hyperic-hq产品的基础包,是Hyperic HQ主要的数据收集组件。它用来从许多平台收集系统和处理信息. 这些平台包括:Linux, Windows, Solaris, AIX, HP-UX, FreeBSD and Mac OSX.Sigar有C,C#,Java和Perl API,java版的API为sigar.jar sigar.jar的底层是用C语言编写的,它通过本地方法来调用操作系统API来获取系统相关数据。
(2)sigar依赖
? ? ? ?Windows操作系统下Sigar.jar 依赖sigar-amd64-winnt.dll或sigar-x86-winnt.dll,linux 操作系统下则依赖libsigar-amd64-linux.so或libsigar-x86-linux.so,在开发中直接放在了项目的lib文件夹中。
? ? ? ?Windows下配置:根据自己的操作系统版本选择sigar-amd64-winnt.dll或sigar-x86-winnt.dll拷贝到C:\Windows\System32中
? ? ? ?Linux下配置:将libsigar-amd64-linux.so或libsigar-x86-linux.so拷贝到/usr/lib64或/lib64或/lib或/usr/lib目录下,如果不起作用,还需要sudochmod 744修改libsigar-amd64-linux.so文件权限
(3)java的maven坐标
? ? ? ?在项目的pom.xml文件中直接导入sigar的坐标,在项目中就可以直接使用了。
2. CPU的信息
public static void cpuInfo() throws SigarException {
Sigar sigar = new Sigar();
Cpu cpu = sigar.getCpu();
System.out.println("cpu的空闲时间 = " + cpu.getIdle());
CpuInfo[] cpuInfoList = sigar.getCpuInfoList();
for (CpuInfo cpuInfo : cpuInfoList) {
System.out.println("cpu的总Mhz = " + cpuInfo.getMhz());
System.out.println("cpu的类别 = " + cpuInfo.getModel());
System.out.println("cpu的总核心数 = "+ cpuInfo.getTotalCores());
System.out.println("cpu的总socket = " +cpuInfo.getTotalSockets());
System.out.println("cpu的Vendor供应商 = " + cpuInfo.getVendor());
System.out.println("cpu的缓冲存储器数量" + cpuInfo.getCacheSize());
}
CpuPerc cpuPerc = sigar.getCpuPerc();
System.out.println(cpuPerc.getIrq());
System.out.println(cpuPerc.getSoftIrq());
System.out.println(cpuPerc.getStolen());
System.out.println("CPU用户使用率: " + CpuPerc.format(cpuPerc.getUser()));
System.out.println("CPU系统使用率: " + CpuPerc.format(cpuPerc.getSys()));
System.out.println("CPU当前等待率: " + CpuPerc.format(cpuPerc.getWait()));
System.out.println("CPU当前错误率: " + CpuPerc.format(cpuPerc.getNice()));
System.out.println("CPU当前空闲率: " + CpuPerc.format(cpuPerc.getIdle()));
System.out.println("CPU总的使用率: " + CpuPerc.format(cpuPerc.getCombined()));
ThreadCpu threadCpu = sigar.getThreadCpu();
System.out.println("线程核心CPU使用时间: " + threadCpu.getSys());
System.out.println("线程总体的CPU使用时间: " + threadCpu.getTotal());
System.out.println("线程的用户CPU使用时间: " + threadCpu.getUser());
}
3. 内存的信息
public static void memInfo() throws SigarException {
Sigar sigar = new Sigar();
Mem mem = sigar.getMem();
System.out.println("ram内存: " + mem.getRam() / (1024 * 1024L) + "MB ");
System.out.println("实际空闲内存: " + mem.getActualFree() / (1024 * 1024L) + "MB");
System.out.println("实际使用内存: " + mem.getActualUsed() / (1024 * 1024L) + "MB");
System.out.println("空闲内存占用的百分比:" + mem.getFreePercent());
System.out.println("使用内存占用的百分比:" + mem.getUsedPercent());
System.out.println("内存总量: " + mem.getTotal() / (1024 * 1024L) + "MB av");
System.out.println("当前内存使用量: " + mem.getUsed() / (1024 * 1024L) + "MB used");
System.out.println("当前内存剩余量: " + mem.getFree() / (1024 * 1024L) + "MB free");
Swap swap = sigar.getSwap();
System.out.println("交换区的pageIn:" + swap.getPageIn() / (1024 * 1024L) + "MB");
System.out.println("交换区的pageOut:" + swap.getPageOut() / (1024 * 1024L) + "MB");
System.out.println("交换区总量: " + swap.getTotal() / (1024 * 1024L) + "MB av");
System.out.println("当前交换区使用量: " + swap.getUsed() / (1024 * 1024L) + "MB used");
System.out.println("当前交换区剩余量: " + swap.getFree() / (1024 * 1024L) + "MB free");
}
4. 磁盘的信息
public static void diskInfo() throws SigarException {
Sigar sigar = new Sigar();
FileSystem fslist[] = sigar.getFileSystemList();
for (int i = 0; i < fslist.length; i++) {
System.out.println("分区的盘符名称:" + i);
FileSystem fs = fslist[i];
String diskName = fs.getDevName();
DiskUsage diskUsage = sigar.getDiskUsage(diskName);
System.out.println(diskName + "磁盘的队列: " + diskUsage.getQueue());
System.out.println(diskName + "磁盘的读取字节数: " + diskUsage.getReadBytes());
System.out.println(diskName + "磁盘的读取数: " + diskUsage.getReads());
System.out.println(diskName + "磁盘的服务时间: " + diskUsage.getServiceTime());
System.out.println(diskName + "磁盘的写入字节数: " + diskUsage.getWriteBytes());
System.out.println(diskName + "磁盘的写入数: " + diskUsage.getWrites());
System.out.println("盘符名称: " + fs.getDevName());
System.out.println("盘符路径: " + fs.getDirName());
System.out.println("盘符标志: " + fs.getFlags());
System.out.println("盘符类型: " + fs.getSysTypeName());
System.out.println("盘符类型名: " + fs.getTypeName());
System.out.println("盘符文件系统类型: " + fs.getType());
FileSystemUsage usage = null;
usage = sigar.getFileSystemUsage(fs.getDirName());
switch (fs.getType()) {
case 0:
break;
case 1:
break;
case 2:
System.out.println(usage.getDiskQueue());
System.out.println(usage.getDiskReadBytes());
System.out.println(fs.getDevName() + "总大小: "
+ usage.getTotal()/(1024 * 1024L) + "GB");
System.out.println(fs.getDevName() + "剩余大小: "
+ usage.getFree()/(1024 * 1024L) + "GB");
System.out.println(fs.getDevName() + "可用大小: "
+ usage.getAvail()/(1024 * 1024L) + "GB");
System.out.println(fs.getDevName() + "已经使用量: "
+ usage.getUsed()/(1024 * 1024L) + "GB");
double usePercent = usage.getUsePercent() * 100D;
System.out.println(fs.getDevName() + "资源的利用率: "
+ usePercent + "%");
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
}
System.out.println(fs.getDevName() + "读出: "
+ usage.getDiskReads()/(1024 * 1024L) );
System.out.println(fs.getDevName() + "写入: "
+ usage.getDiskWrites() /(1024 * 1024L) );
}
}
5. 网络设备的信息
public static void netInterfaceInfo() throws SigarException {
Sigar sigar = new Sigar();
String ifNames[] = sigar.getNetInterfaceList();
for (int i = 0; i < ifNames.length; i++) {
String name = ifNames[i];
NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
System.out.println("网络设备名: " + name);
System.out.println("IP地址: " + ifconfig.getAddress());
System.out.println("子网掩码: " + ifconfig.getNetmask());
System.out.println("网关广播地址: " + ifconfig.getBroadcast());
System.out.println("网卡地址: " + ifconfig.getHwaddr());
System.out.println("网卡描述信息: " + ifconfig.getDescription());
System.out.println("目的地:" + ifconfig.getDestination());
System.out.println("标志: " + ifconfig.getFlags());
System.out.println("尺度标准: " + ifconfig.getMetric());
System.out.println("mtu的大小: " + ifconfig.getMtu());
System.out.println("网卡类型: " + ifconfig.getType());
if ((ifconfig.getFlags() & 1L) <= 0L) {
System.out.println("!IFF_UP...skipping getNetInterfaceStat");
continue;
}
NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
System.out.println(name + "接收的总包裹数:" + ifstat.getRxPackets());
System.out.println(name + "发送的总包裹数:" + ifstat.getTxPackets());
System.out.println(name + "接收到的总字节数:" + ifstat.getRxBytes());
System.out.println(name + "发送的总字节数:" + ifstat.getTxBytes());
System.out.println(name + "接收到的错误包数:" + ifstat.getRxErrors());
System.out.println(name + "发送数据包时的错误数:" + ifstat.getTxErrors());
System.out.println(name + "接收时丢弃的包数:" + ifstat.getRxDropped());
System.out.println(name + "发送时丢弃的包数:" + ifstat.getTxDropped());
System.out.println(name + "速度:" + ifstat.getSpeed()/1000000L + "Mbps");
System.out.println(name + "发送时的帧数:" + ifstat.getRxFrame());
System.out.println(name + "发送时的Overruns:" + ifstat.getRxOverruns());
System.out.println(name + "接受到的Carrier:" + ifstat.getTxCarrier());
System.out.println(name + "接受到的冲突:" + ifstat.getTxCollisions());
double receiveBytes1=ifstat.getRxBytes();
double sendBytes1=ifstat.getTxBytes();
double totalBytes1=receiveBytes1+sendBytes1;
long startTime = System.currentTimeMillis();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
NetInterfaceStat ifstat1 = sigar.getNetInterfaceStat(name);
double receiveBytes2=ifstat1.getRxBytes();
double sendBytes2=ifstat1.getTxBytes();
double totalBytes2=receiveBytes2+sendBytes2;
long endTime=System.currentTimeMillis();
double totalBytes=totalBytes2-totalBytes1;
float interval = (float)(endTime - startTime)/1000;
float curRate = (float)totalBytes*8/(1000000*interval);
System.out.println("网络的带宽: " + curRate);
}
}
6. 网络的信息
public static void netInfo() throws SigarException {
Sigar sigar = new Sigar();
NetConnection[] netConnectionList = sigar.getNetConnectionList(0);
for (NetConnection netConnection : netConnectionList) {
System.out.println("连接的local address:" + netConnection.getLocalAddress());
System.out.println("连接的本地port:" + netConnection.getLocalPort());
System.out.println("连接的接受队列:" + netConnection.getReceiveQueue());
System.out.println("连接的远程 address" + netConnection.getRemoteAddress());
}
NetInfo netInfo = sigar.getNetInfo();
String hostAddress = "";
try {
InetAddress address = InetAddress.getLocalHost();
hostAddress = address.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
System.out.println("本地的IP地址: " + hostAddress);
System.out.println("默认网关:" + netInfo.getDefaultGateway());
System.out.println("网络的域名名称:" + netInfo.getDomainName());
System.out.println("网络的hostname:" + netInfo.getHostName());
System.out.println("网络的首选DNS:" + netInfo.getPrimaryDns());
System.out.println("网络的次选DNS:" + netInfo.getSecondaryDns());
NetRoute[] netRouteList = sigar.getNetRouteList();
for (NetRoute netRoute : netRouteList) {
System.out.println("route的目的地: " + netRoute.getDestination());
System.out.println("route的flag: " + netRoute.getFlags());
System.out.println("route的网关地址: " + netRoute.getGateway());
System.out.println("route的Ifname: " + netRoute.getIfname());
System.out.println("route的Irtt: " + netRoute.getIrtt());
System.out.println("route的掩码: " + netRoute.getMask());
System.out.println("route的Metric: " + netRoute.getMetric());
System.out.println("route的Mtu: " + netRoute.getMtu());
System.out.println("route的Refcnt: " + netRoute.getRefcnt());
System.out.println("route的Use: " + netRoute.getUse());
System.out.println("route的Window: " + netRoute.getWindow());
System.out.println("==========");
}
NetStat netStat = sigar.getNetStat();
System.out.println("网络的in的total: " + netStat.getAllInboundTotal());
System.out.println("网络的out的total: " + netStat.getAllOutboundTotal());
System.out.println("网络的TcpBound: " + netStat.getTcpBound());
System.out.println("网络的TCPClose: " + netStat.getTcpClose());
System.out.println("网络的TCPCloseWait: " + netStat.getTcpCloseWait());
System.out.println("网络的TcpClosing: " + netStat.getTcpClosing());
System.out.println("网络的TcpEstablished: " + netStat.getTcpEstablished());
System.out.println("网络的TcpFinWait1: " + netStat.getTcpFinWait1());
System.out.println("网络的TcpFinWait2: " + netStat.getTcpFinWait2());
System.out.println("网络的TcpIdle: " + netStat.getTcpIdle());
System.out.println("网络的TcpInboundTotal: " + netStat.getTcpInboundTotal());
System.out.println("网络的TcpLastAck: " + netStat.getTcpLastAck());
System.out.println("网络的TcpListen: " + netStat.getTcpListen());
System.out.println("网络的TcpOutBoundTotal: " + netStat.getTcpOutboundTotal());
System.out.println("网络的TcpTimeWait: " + netStat.getTcpTimeWait());
}
|