㈠ 如何利用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不是特別了解,或者想一次獲取比較全面,可以通過編寫腳本或者相關的監控工具。