java获得远程服务器cpu,Java肿么远程读取Linux的cpu使用率

using System;

using System.Diagnostics;

using System.Threading;

public class CpuLoadInfo

{

// auxiliary print methods

private static void Say ( string txt )

{

Console.WriteLine(txt);

}

// auxiliary print methods

private static void Say()

{

Say();

}

// The main method. Command line arguments are ignored.

[STAThread]

public static void Main()

{

Say($Id: CpuLoadInfo.cs,v 1.2 2002/08/17 17:45:48 rz65 Exp $);

Say();

Say(Attempt to create a PerformanceCounter instance:);

Say(Category name = + CategoryName);

Say(Counter name = + CounterName);

Say(Instance name = + InstanceName);

PerformanceCounter pc

= new PerformanceCounter(CategoryName,CounterName,InstanceName);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java的Runtime类和Process类来获取Linux服务器CPU和内存使用率。具体实现方法如下: 1. 获取CPU使用率: ```java public static double getCpuUsage() { try { Process process = Runtime.getRuntime().exec("top -b -n1"); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = reader.readLine(); while (line != null) { if (line.startsWith("Cpu(s)")) { String[] cpuInfo = line.split("\\s+"); double idleCpuUsage = Double.parseDouble(cpuInfo[4]); double totalCpuUsage = 100 - idleCpuUsage; return totalCpuUsage; } line = reader.readLine(); } } catch (IOException e) { e.printStackTrace(); } return 0; } ``` 2. 获取内存使用率: ```java public static double getMemoryUsage() { try { Process process = Runtime.getRuntime().exec("free -m"); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = reader.readLine(); if (line != null) { String[] memoryInfo = line.split("\\s+"); double totalMemory = Double.parseDouble(memoryInfo[1]); double usedMemory = Double.parseDouble(memoryInfo[2]); double memoryUsage = usedMemory / totalMemory * 100; return memoryUsage; } } catch (IOException e) { e.printStackTrace(); } return 0; } ``` 以上代码通过执行Linux命令获取CPU和内存使用率,并返回一个double类型的数值,表示使用率的百分比。需要注意的是,这些命令的输出格式可能会因Linux版本而异,需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值