Record first, to be tested

Source: Internet
Author: User

 

If you read all the MAC addresses, print the MAC address in the following code.
Package sigar-1.6.3.jar to be referenced
During the test, I loaded all the built-in dynamic libraries of sigar.
For details, see the main method.

 

Import java. Io. bufferedreader;
Import java. Io. file;
Import java. Io. filewriter;
Import java. Io. inputstreamreader;

Import org. hyperic. sigar. cpuperc;
Import org. hyperic. sigar. sigar;
Import org. hyperic. sigar. sigarexception;
Import org. hyperic. sigar. sigarloader;
Import org. hyperic. sigar. cmd. shell;
Import org. hyperic. sigar. cmd. sigarcommandbase;

Public class cpuinfo extends sigarcommandbase {

Public Boolean displaytimes = true;

Public cpuinfo (shell ){
Super (Shell );
}

Public cpuinfo (){
Super ();
}

Public String getusageshort (){
Return "Display CPU information ";
}

Private void output (cpuperc CPU ){
Println ("user time..." + cpuperc. Format (CPU. getuser ()));
Println ("sys time..." + cpuperc. Format (CPU. getsys ()));
Println ("idle time..." + cpuperc. Format (CPU. getidle ()));
Println ("wait time ......" + cpuperc. Format (CPU. getwait ()));
Println ("nice time..." + cpuperc. Format (CPU. getnice ()));
Println ("combined..." + cpuperc. Format (CPU. getcombined ()));
Println ("IRQ time ......" + cpuperc. Format (CPU. getirq ()));
If (sigarloader. is_linux ){
Println ("softirq time .." + cpuperc. Format (CPU. getsoftirq ()));
Println ("stolen time..." + cpuperc. Format (CPU. getstolen ()));
}
Println ("");
}

Public void output (string [] ARGs) throws sigarexception {
Org. hyperic. sigar. cpuinfo [] Infos = This. sigar. getcpuinfolist ();

Cpuperc [] CPUs = This. sigar. getcpuperclist ();

Org. hyperic. sigar. cpuinfo info = Infos [0];
Long cachesize = info. getcachesize ();
Println ("vendor ......" + info. getvendor ());
Println ("Model ......" + info. GetModel ());
Println ("MHz..." + info. getmhz ());
Println ("Total CPUs..." + info. gettotalcores ());
If (info. gettotalcores ()! = Info. gettotalsockets () | (info. getcorespersocket ()> info. gettotalcores ())){
Println ("physical CPUs..." + info. gettotalsockets ());
Println ("cores per CPU .." + info. getcorespersocket ());
}
If (cachesize! = Sigar. field_notimpl ){
Println ("cache size..." + cachesize );
}
Println ("");
If (! This. displaytimes ){
Return;
}
For (INT I = 0; I <CPUs. length; I ++ ){
Println ("CPU" + I + ".........");
Output (CPUs [I]);
}
Println ("totals ........");
Output (this. sigar. getcpuperc ());

Stringbuffer sb = new stringbuffer ("CPU number =" + getcpuserial () + "\ n ");

String [] interfaces = sigar. getnetinterfacelist ();
If (interfaces! = NULL | interfaces. length> 0)
SB. append ("first network card number =" + sigar. getnetinterfaceconfig (interfaces [0]). gethwaddr ());

Org. hyperic. sigar. filesystem [] filesystems = sigar. getfilesystemlist ();
If (filesystems! = NULL | filesystems. length> 0)
SB. append ("\ n" + "volume label of the first partition of the hard disk =" + gethdserial (filesystems [0]. getdevname ()));

System. Out. println (sb. tostring ());
}

Public static void main (string [] ARGs) throws exception {
// First load the SIGA dynamic library. on different platforms, as long as a specific dynamic library is loaded, I will load it all.
// In the IDE environment, you can set the natinve lib Patch location without loading the dynamic library.
File nativedir = new file ("E: \ mypm10_new \ mypmdoc \ webroot \ WEB-INF \ native ");
File [] libs = nativedir. listfiles ();
For (INT I = 0; I <libs. length; I ++ ){
If (libs [I]. isfile ())
Try {
System. Load (new file (nativedir, Libs [I]. getname (). getpath ());
} Catch (throwable t ){

}
}
New cpuinfo (). processcommand (ARGs );
}

/**
*
* Returns the idle rate of the CPU.
*
*/
Public static double getcpuidle (){
Sigar = NULL;
Try {
Sigar = new sigar ();
Return sigar. getcpuperc (). getidle ();
} Catch (throwable t ){
T. printstacktrace ();
} Finally {
If (sigar! = NULL)
Sigar. Close ();
}
Return 0;
}

/**
*
* @ Param drive hard drive partition such as C, D
* @ Return the volume label of the partition
*/
Public static string gethdserial (string drive ){
String result = "";
Try {
File file = file. createtempfile ("tmp", ". vbs ");
File. deleteonexit ();
Filewriter fw = new java. Io. filewriter (File );

String vbs = "set objfso = Createobject (\" scripting. FileSystemObject \ ") \ n"
+ "Set coldrives = objfso. Drives \ n" + "set objdrive = coldrives. Item (\" "+ drive +" \ ") \ n"
+ "Wscript. Echo objdrive. serialnumber ";
FW. Write (vbs );
FW. Close ();
PROCESS p = runtime.getruntime(cmd.exe C ("cscript // nologo" + file. getpath ());
Bufferedreader input = new bufferedreader (New inputstreamreader (P. getinputstream ()));
String line;
While (line = input. Readline ())! = NULL ){
Result + = line;
}
Input. Close ();
File. Delete ();
} Catch (exception e ){

}
If (result. Trim (). Length () <1 | result = NULL ){
Result = "no disk ID is read ";

}

Return result. Trim ();
}

/**
* Obtain the CPU number. When multiple CPUs are used, only the first one is used.
* @ Return
*/
Public static string getcpuserial (){
String result = "";
Try {
File file = file. createtempfile ("tmp", ". vbs ");
File. deleteonexit ();
Filewriter fw = new java. Io. filewriter (File );

String vbs = "on error resume next \ r \ n" + "strcomputer = \". \ "\ r \ n"
+ "Set ob1_miservice = GetObject (\" winmgmts: \ "_ \ r \ n"
+ "& \" {Impersonationlevel = impersonate }! \\\\\ "& Strcomputer & \" \\ Root \ cimv2 \ ") \ r \ n"
+ "Set colitems = obw.miservice. execquery (\" select * From win32_processor \ ") \ r \ n"
+ "For each objitem in colitems \ r \ n" + "wscript. Echo objitem. processorid \ r \ n"
+ "Exit for 'Do the first CPU only! \ R \ n "+" Next ";

FW. Write (vbs );
FW. Close ();
PROCESS p = runtime.getruntime(cmd.exe C ("cscript // nologo" + file. getpath ());
Bufferedreader input = new bufferedreader (New inputstreamreader (P. getinputstream ()));
String line;
While (line = input. Readline ())! = NULL ){
Result + = line;
}
Input. Close ();
File. Delete ();
} Catch (exception e ){
E. fillinstacktrace ();
}
If (result. Trim (). Length () <1 | result = NULL ){
Result = "No cpu_id is read ";
}
Return result. Trim ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.