一、前言
今天来了个需求,需要实时监控服务器cpu跟内存参数的占用百分比。百度了一波性能监控方面的文章,看到很多文章都在使用 sigar,虽然我之前没接触过sigar,但是这丝毫不影响我跟随大流,反正大家都在用,也不差我一个
二、引入依赖
首先,引入sigar包,gradle方式如下,maven方式自行百度
友情提示:本文章所需要的文件都放在了文章末尾,有需要的自行下载即可
1
| implementation 'org.fusesource:sigar:1.6.4'
|
windows环境下,将lib包下的sigar-amd64-winnt.dll,sigar-x86-winnt.dll,sigar-x86-winnt.lib 三个文件放入jdk的bin目录下面
或者Windows下配置:根据自己的操作系统版本选择sigar-amd64-winnt.dll或sigar-x86-winnt.dll拷贝到C:\Windows\System32中
Linux下配置:将lib包下的sigar-amd64-linux.so或libsigar-x86-linux.so拷贝到/usr/lib64或/lib64或/lib或/usr/lib目录下,如果不起作用,还需要sudochmod 744修改libsigar-amd64-linux.so文件权限
三、创建相关工具类
创建controller如下,当然,我这里没有进行封装,全部裸奔在外的,需要我们自己封装一下再用
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
| import com.boran.demonstration.config.BaseController; import com.boran.demonstration.config.Result; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.hyperic.sigar.*; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
@Api(tags = "查看计算机相关信息") @CrossOrigin @RestController @RequestMapping("/computer") public class ComputerInformationController {
@ApiOperation("查看计算机信息") @GetMapping("/information") public Result ComputerInformation() { Map<String, Object> all = new HashMap<>(); try { Map<String, String> memory = getMemory(); all.put("memory", memory); System.out.println("----------------------------------"); List<Object> cpu = getCpu(); all.put("cpu", cpu); System.out.println("----------------------------------"); file(); System.out.println("----------------------------------"); net(); System.out.println("----------------------------------");
} catch (Exception e1) { e1.printStackTrace(); } return Result.ok(all); }
private Map<String, String> getMemory() throws SigarException { Sigar sigar = new Sigar(); Mem mem = sigar.getMem(); Swap swap = sigar.getSwap(); Map<String, String> map = new HashMap<>(16); map.put("memTotal", mem.getTotal() / 1024L + "K av"); map.put("memUsed", mem.getUsed() / 1024L + "K used"); map.put("memFree", mem.getFree() / 1024L + "K free"); map.put("swapTotal", swap.getTotal() / 1024L + "K av"); map.put("swapUsed", swap.getUsed() / 1024L + "K used"); map.put("swapFree", swap.getFree() / 1024L + "K free"); System.out.println("内存总量: " + mem.getTotal() / 1024L + "K av"); System.out.println("当前内存使用量: " + mem.getUsed() / 1024L + "K used"); System.out.println("当前内存剩余量: " + mem.getFree() / 1024L + "K free"); System.out.println("交换区总量: " + swap.getTotal() / 1024L + "K av"); System.out.println("当前交换区使用量: " + swap.getUsed() / 1024L + "K used"); System.out.println("当前交换区剩余量: " + swap.getFree() / 1024L + "K free"); return map; }
private List getCpu() throws SigarException { Sigar sigar = new Sigar(); CpuInfo infos[] = sigar.getCpuInfoList(); CpuPerc cpuList[] = sigar.getCpuPercList(); List<Object> list = new ArrayList<>(); for (int i = 0; i < infos.length; i++) { CpuInfo info = infos[i]; Map<String, Object> map = new HashMap<>(16); map.put("number", i + 1); map.put("getMhz", info.getMhz()); map.put("getVendor", info.getVendor()); map.put("getModel", info.getModel()); map.put("getCacheSize", info.getCacheSize()); map.put("getUser", CpuPerc.format(cpuList[i].getUser())); map.put("getSys", CpuPerc.format(cpuList[i].getSys())); map.put("getWait", CpuPerc.format(cpuList[i].getWait())); map.put("getNice", CpuPerc.format(cpuList[i].getNice())); map.put("getIdle", CpuPerc.format(cpuList[i].getIdle())); map.put("getCombined", CpuPerc.format(cpuList[i].getCombined())); list.add(map); System.out.println("第" + (i + 1) + "块CPU信息"); System.out.println("CPU的总量MHz: " + info.getMhz()); System.out.println("CPU生产商: " + info.getVendor()); System.out.println("CPU类别: " + info.getModel()); System.out.println("CPU缓存数量: " + info.getCacheSize()); System.out.println("CPU用户使用率: " + CpuPerc.format(cpuList[i].getUser())); System.out.println("CPU系统使用率: " + CpuPerc.format(cpuList[i].getSys())); System.out.println("CPU当前等待率: " + CpuPerc.format(cpuList[i].getWait())); System.out.println("CPU当前错误率: " + CpuPerc.format(cpuList[i].getNice())); System.out.println("CPU当前空闲率: " + CpuPerc.format(cpuList[i].getIdle())); System.out.println("CPU总的使用率: " + CpuPerc.format(cpuList[i].getCombined())); } return list; }
private static void file() throws Exception { Sigar sigar = new Sigar(); FileSystem fslist[] = sigar.getFileSystemList(); for (int i = 0; i < fslist.length; i++) { System.out.println("分区的盘符名称" + i); FileSystem fs = fslist[i]; 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(fs.getDevName() + "总大小: " + usage.getTotal() + "KB"); System.out.println(fs.getDevName() + "剩余大小: " + usage.getFree() + "KB"); System.out.println(fs.getDevName() + "可用大小: " + usage.getAvail() + "KB"); System.out.println(fs.getDevName() + "已经使用量: " + usage.getUsed() + "KB"); 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()); System.out.println(fs.getDevName() + "写入: " + usage.getDiskWrites()); } return; }
private static void net() throws Exception { 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);
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()); } }
}
|
四、结语
相关功能的测试结果就不贴了,有兴趣可以自己测试一下,不出意外的话,是没有问题的,当然了,出了问题概不负责,哈哈哈哈哈哈……
注意:如果将该程序打成jar包后通过cmd命令运行,则无法获取相应的信息,提示找不到对应得dll文件,我们可将上述的文件放在cmd同目录即 C:\Windows\System32 目录下,则可成功运行获取相应信息……
最后,基本上你想要的服务器信息都有了……上面代码没写完,这个问题不大,改改还是能用的……附带提供文中相关文件下载
sigar需要使用的相关文件(windows以及linux下)