导航:首页 > 配服务器 > java怎么获取服务器cpu

java怎么获取服务器cpu

发布时间:2022-08-14 23:00:05

㈠ 如何利用java获取系统的信息(CPU,内存,各

我知道并且用过的 有一种方法 就是snmp服务 加上snmp jar包可以获取到内存 cpu 磁盘 进程 网络等信息

㈡ java 如何查看服务器的CPU使用率

(){
try{
StringprocCmd=System.getenv("windir")+"\system32\wbem\wmic.exeprocessgetCaption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
//取进程信息
long[]c0=readCpu(Runtime.getRuntime().exec(procCmd));
Thread.sleep(CPUTIME);
long[]c1=readCpu(Runtime.getRuntime().exec(procCmd));
if(c0!=null&&c1!=null){
longidletime=c1[0]-c0[0];
longbusytime=c1[1]-c0[1];
return"CPU使用率:"+Double.valueOf(PERCENT*(busytime)*1.0/(busytime+idletime)).intValue()+"%";
}else{
return"CPU使用率:"+0+"%";
}
}catch(Exceptionex){
ex.printStackTrace();
return"CPU使用率:"+0+"%";
}
}

㈢ java如何获取远程计算机的系统信息,cpu使用情况,磁盘使用情况等等

1、在远程server里搭建一个http服务,并支持某种服务端语言(如.net或php之类)
2、用服务端语言获取到服务器的磁盘信息,把结果输出
3、在本地用JS写一段AJAX脚本,远程请求服务端写好的程序,得到结果,并展示给用户
<html>
<head>
<title>提取硬盘序列号</title>
<script>
function disk() {//硬盘序列号 信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_DiskDrive");
var e = new Enumerator (properties);
document.write("<table border=1>");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("<tr>");
document.write("<td>" + p.signature + "</td>");
document.write("</tr>");
}
document.write("</table>");
}
</script>
</head>
<body>
<input type="button" value="硬盘序列号" onclick="disk()">
</body>
</html>

在这上找的代码:
对于写过ASP或.net 的人来说,通过asp或.net 语言来获得客户端的硬件信息是很熟悉的.但如何通过javascript这种客户端脚本来获得客户端的信息呢?请看以下实例:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>javascript获得客户端硬件信息-www.web2bar.cn/</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET
7.1">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script id=clientEventHandlersJS language=javascript>
<!--

function Button2_onclick() {//CPU 信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_Processor");
var e = new Enumerator (properties);
document.write("<table border=1>");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("<tr>");
document.write("<td>" + p.Caption + "</td>");
document.write("<td>" + p.DeviceID + "</td>");
document.write("<td>" + p.Name + "</td>");
document.write("<td>" + p.CpuStatus + "</td>");
document.write("<td>" + p.Availability + "</td>");
document.write("<td>" + p.Level + "</td>");
document.write("<td>" + p.ProcessorID + "</td>");
document.write("<td>" + p.SystemName + "</td>");
document.write("<td>" + p.ProcessorType + "</td>");
document.write("</tr>");
}
document.write("</table>");
}

function Button1_onclick() {//软盘信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_FloppyDrive");
var e = new Enumerator (properties);
document.write("<table border=1>");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("<tr>");
document.write("<td>" + p.Description + "</td>");
document.write("<td>" + p.DeviceID + "</td>");
document.write("<td>" + p.Status + "</td>");
document.write("<td>" + p.Manufacuturer + "</td>");
document.write("</tr>");
}
document.write("</table>");
}

function Button1_onclick() {//CD-ROM 信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_CDROMDrive");
var e = new Enumerator (properties);
document.write("<table border=1>");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("<tr>");
document.write("<td>" + p.Caption + "</td>");
document.write("<td>" + p.Description + "</td>");
document.write("<td>" + p.Drive + "</td>");
document.write("<td>" + p.Status + "</td>");
document.write("<td>" + p.MediaLoaded + "</td>");
document.write("</tr>");
}
document.write("</table>");
}

function Button1_onclick() {//键盘信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_Keyboard");
var e = new Enumerator (properties);
document.write("<table border=1>");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("<tr>");
document.write("<td>" + p.Description + "</td>");
document.write("<td>" + p.Name + "</td>");
document.write("<td>" + p.Status + "</td>");
document.write("</tr>");
}
document.write("</table>");
}

function Button1_onclick() {//主板信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_BaseBoard");
var e = new Enumerator (properties);
document.write("<table border=1>");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("<tr>");
document.write("<td>" + p.HostingBoard + "</td>");
document.write("<td>" + p.Manufacturer + "</td>");
document.write("<td>" + p.PoweredOn + "</td>");
document.write("<td>" + p.Proct + "</td>");
document.write("<td>" + p.SerialNumber + "</td>");
document.write("<td>" + p.Version + "</td>");
document.write("</tr>");
}
document.write("</table>");
}

//-->
</script>
</head>
<body>
<INPUT id="Button1" type="button" value="Button"
name="Button1" language=javascript onclick="return Button1_onclick()">
</body>
</html>
使用javascript方式获得客户端的信息主要的优点是,不需求服务器资源.不需求刷新网页.直接在客户端执行脚本获得显示.

在这上找到了硬盘序列号的参数:
\'获得硬盘序列号
Dim cmicWmi As New System.Management.ManagementObjectSearcher(\"SELECT * FROM Win32_DiskDrive\")
Dim Uint32 As UInt32
For Each cmicWmiObj As ManagementObject In cmicWmi.Get
Uint32 = cmicWmiObj(\"signature\")
Next
TextBox1.Text = Uint32.ToString
和在一块总算给提出来了,呵呵。

㈣ 使用java如何获取远程主机(引擎)的cpu性能,占用内存,硬盘等信息(需要返回数据到前台页面)

我也这在做这方面的信息,我要写一个java客户端,发送命令调用远程snmp,获得一些相关信息

㈤ Java如何读取CPU的数据信息

java获取所有系统信息(CPU、内存、进程等)的代码:
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;

import mytools.com.sun.management.OperatingSystemMXBean;
import mytools.java.io.File;
import mytools.java.lang.management.ManagementFactory;
/**
* 获取windows系统信息(CPU,内存,文件系统)
* @author libing
*
*/

public class WindowsInfoUtil {
private static final int CPUTIME = 500;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;

public static void main(String[] args) {
System.out.println(getCpuRatioForWindows());
System.out.println(getMemery());
System.out.println(getDisk());
}

//获取内存使用率
public static String getMemery(){
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
// 总的物理内存+虚拟内存
long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();
// 剩余的物理内存
long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
Double compare=(Double)(1-freePhysicalMemorySize*1.0/totalvirtualMemory)*100;
String str="内存已使用:"+compare.intValue()+"%";
return str;
}

//获取文件系统使用率
public static List<String> getDisk() {
// 操作系统
List<String> list=new ArrayList<String>();
for (char c = 'A'; c <= 'Z'; c++) {
String dirName = c + ":/";
File win = new File(dirName);
if(win.exists()){
long total=(long)win.getTotalSpace();
long free=(long)win.getFreeSpace();
Double compare=(Double)(1-free*1.0/total)*100;
String str=c+":盘 已使用 "+compare.intValue()+"%";
list.add(str);
}
}
return list;
}

//获得cpu使用率
public static String getCpuRatioForWindows() {
try {
String procCmd = System.getenv("windir") + "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
// 取进程信息
long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
Thread.sleep(CPUTIME);
long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
if (c0 != null && c1 != null) {
long idletime = c1[0] - c0[0];
long busytime = c1[1] - c0[1];
return "CPU使用率:"+Double.valueOf(PERCENT * (busytime)*1.0 / (busytime + idletime)).intValue()+"%";
} else {
return "CPU使用率:"+0+"%";
}
} catch (Exception ex) {
ex.printStackTrace();
return "CPU使用率:"+0+"%";
}
}

//读取cpu相关信息
private static long[] readCpu(final Process proc) {
long[] retn = new long[2];
try {
proc.getOutputStream().close();
InputStreamReader ir = new InputStreamReader(proc.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line = input.readLine();
if (line == null || line.length() < FAULTLENGTH) {
return null;
}
int capidx = line.indexOf("Caption");
int cmdidx = line.indexOf("CommandLine");
int rocidx = line.indexOf("ReadOperationCount");
int umtidx = line.indexOf("UserModeTime");
int kmtidx = line.indexOf("KernelModeTime");
int wocidx = line.indexOf("WriteOperationCount");
long idletime = 0;
long kneltime = 0;
long usertime = 0;
while ((line = input.readLine()) != null) {
if (line.length() < wocidx) {
continue;
}
// 字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount,
// ThreadCount,UserModeTime,WriteOperation
String caption =substring(line, capidx, cmdidx - 1).trim();
String cmd = substring(line, cmdidx, kmtidx - 1).trim();
if (cmd.indexOf("wmic.exe") >= 0) {
continue;
}
String s1 = substring(line, kmtidx, rocidx - 1).trim();
String s2 = substring(line, umtidx, wocidx - 1).trim();
if (caption.equals("System Idle Process") || caption.equals("System")) {
if (s1.length() > 0)
idletime += Long.valueOf(s1).longValue();
if (s2.length() > 0)
idletime += Long.valueOf(s2).longValue();
continue;
}
if (s1.length() > 0)
kneltime += Long.valueOf(s1).longValue();
if (s2.length() > 0)
usertime += Long.valueOf(s2).longValue();
}
retn[0] = idletime;
retn[1] = kneltime + usertime;
return retn;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
proc.getInputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}

/**
* 由于String.subString对汉字处理存在问题(把一个汉字视为一个字节),因此在 包含汉字的字符串时存在隐患,现调整如下:
* @param src 要截取的字符串
* @param start_idx 开始坐标(包括该坐标)
* @param end_idx 截止坐标(包括该坐标)
* @return
*/
private static String substring(String src, int start_idx, int end_idx) {
byte[] b = src.getBytes();
String tgt = "";
for (int i = start_idx; i <= end_idx; i++) {
tgt += (char) b[i];
}
return tgt;
}
}

㈥ 怎样用java 获取 硬盘 cpu 序列号,可调用dll实现

我是通过一个外部的JAR包来间接来获得DLL文件的句柄 的,它就是jacob了,这是java com brige的简写,呵呵, 这个名称起得非常形象吧,我用的版本是jacob 1.9的,你可以到它的官方网站去下载,下载回来的压缩包中会有两个文件我们需要用到的,一个是jacob.dll,一个是jacob.jar,jacob.dll可以将它复制到系统的system32目录下,而jacob.jar文件,直接将它加入到项目的库中就可以了。这两项准备工作完成后,就可以开始尝试调用了。
新建一个类,引入jacob.jar中的两个类,
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
然后通过dll的ControllerId来读取DLL文件
public class Print {
private ActiveXComponent printController = null;
private Dispatch printObj = null;/*** 默认controllerId的方法*/public Print(){try{printController = new ActiveXComponent(POSControler.Controler);
printObj = (Dispatch)printController.getObject();
}catch(Exception e){
printObj = new Dispatch();
如果方法dll中的方法是空参数时,直接call一下就可以了,如
Dispatch.call(printObj,setDefaultFont);而调用有参数的方法时,则需要将参数在后面依次传入,注意按顺序噢:

㈦ 如何在Java程序中获取、监控服务器负载值、CPU、内存、硬盘、网络情况

SUM服务器监控软件吧
可以监控所有品牌的服务器的CPU\内存、磁盘、流量、网络、数据库、中间件,还能监控网络设备
有啥情况可以短信和邮件通知,比较方便
你看看吧,我们用的就是这个,只是温度不能监控,其他都可以

㈧ java怎样获取CPU占用率和硬盘占用率

通过jmx可以监控vm内存使用,系统内存使用等,以下是网上某博客代码,特点是通过window和linux命令获得CPU使用率。

利用java程序实现获取计算机cpu利用率和内存使用信息。

packagecom.amgkaka.performance;

/***//**
*监视信息的JavaBean类.
*@authoramg
*@version1.0
*Creationdate:2008-4-25-上午10:37:00
*/
publicclassMonitorInfoBean{
/***//**可使用内存.*/
privatelongtotalMemory;

/***//**剩余内存.*/
privatelongfreeMemory;

/***//**最大可使用内存.*/
privatelongmaxMemory;

/***//**操作系统.*/
privateStringosName;

/***//**总的物理内存.*/
privatelongtotalMemorySize;

/***//**剩余的物理内存.*/
;

/***//**已使用的物理内存.*/
privatelongusedMemory;

/***//**线程总数.*/
privateinttotalThread;

/***//**cpu使用率.*/
privatedoublecpuRatio;

publiclonggetFreeMemory(){
returnfreeMemory;
}

publicvoidsetFreeMemory(longfreeMemory){
this.freeMemory=freeMemory;
}

(){
returnfreePhysicalMemorySize;
}

(longfreePhysicalMemorySize){
this.freePhysicalMemorySize=freePhysicalMemorySize;
}

publiclonggetMaxMemory(){
returnmaxMemory;
}

publicvoidsetMaxMemory(longmaxMemory){
this.maxMemory=maxMemory;
}

publicStringgetOsName(){
returnosName;
}

publicvoidsetOsName(StringosName){
this.osName=osName;
}

publiclonggetTotalMemory(){
returntotalMemory;
}

publicvoidsetTotalMemory(longtotalMemory){
this.totalMemory=totalMemory;
}

publiclonggetTotalMemorySize(){
returntotalMemorySize;
}

publicvoidsetTotalMemorySize(longtotalMemorySize){
this.totalMemorySize=totalMemorySize;
}

publicintgetTotalThread(){
returntotalThread;
}

publicvoidsetTotalThread(inttotalThread){
this.totalThread=totalThread;
}

publiclonggetUsedMemory(){
returnusedMemory;
}

publicvoidsetUsedMemory(longusedMemory){
this.usedMemory=usedMemory;
}

publicdoublegetCpuRatio(){
returncpuRatio;
}

publicvoidsetCpuRatio(doublecpuRatio){
this.cpuRatio=cpuRatio;
}
}

㈨ Java怎么远程读取Linux的cpu使用率

linux获取cpu使用率
Windows查看CPU使用率很简单,我们通过任务管理器就能看到。那么对于linux来说,怎么查看获取CPU使用率呢?咗嚛本经验以Centos系统为例
工具/原料
Centos
获取CPU使用率
实时CPU使用率
类似任务管理器实时系统信息可以通过top命令查看。显示的信息四个参数分别是:用户的模式(user)、低优先级的用户模式(nice)、系统内核模式(system)以及系统空闲的处理器时间(idle)
查看CPU处理器使用率
对于CPU使用率一般都是通过CPU使用情况,查看/proc/stat cpu状态文件
平均CPU使用率
对于一般某时间段CPU的使用率来说,可以通过查看/pRoc/loadavg 文件信息
第三方监控软件查看
网上有很多网管,监控软件安装配置好之后。可以通过网页管理查看CPU等硬件情况和CPU使用率,负载等参数
其它相关信息
内存使用率 查看 /proc/meminfo查看内存详细信息,也可以通过free 命令查看
网络利用率 通过查看文件/proc/net/dev 可以了解,centos系统的网络使用情况跟windows的网络情况类似
注意事项
如果是查看系统负载的话是需要通过,CPU使用率,内存使用率,网络负载,硬盘容量等等来综合计算出来的。如果对于linux不是特别了解,或者想一次获取比较全面,可以通过编写脚本或者相关的监控工具。

阅读全文

与java怎么获取服务器cpu相关的资料

热点内容
程序员下载器 浏览:46
退出云服务器代码 浏览:900
军状如命令 浏览:263
如何安卓系统更新 浏览:74
linux命令在哪里输入 浏览:497
编程语言集合类怎么选 浏览:93
如何将pdf转化为word 浏览:11
迈克菲隔离区解压密码 浏览:785
怎么用伟福编译 浏览:867
计算机算法专家 浏览:501
什么app清理垃圾 浏览:643
android流媒体服务器 浏览:183
各种算法的时间复杂度是指 浏览:116
帮助高考生缓解压力 浏览:850
自媒体聚合APP需要什么资质 浏览:487
求标准体重的算法 浏览:740
服务器后面插光纤的卡是什么卡 浏览:526
低级格式化命令dos 浏览:87
编译软件的图标 浏览:887
预算法定原则包括 浏览:981