Introducing the basic bag of Hyperic-HQ products Sigar.jar

The basic package of Hyperic-HQ products Sigar: Sigar is the main data collection component of Hyperic HQ. It is used to collect system and processing information from many platforms, including: Linux, Windows, Solaris, AIX, HP-UX, FreeBSD and Mac OSX. SIGAR has C, C #, Java, and Perl API, Java version of the API’s bottom layer is written in C language, which calls the operating system API to obtain system-related data by local methods. Windows operating system Sigar.jar relies on Sigar-AMD64-Winnt.dll or SIGAR-X86-Winnt.dll, the Linux operating system relies on libsigar-amd64-linux.so or libsigar-x86-linux.so, (now everyone is Not a little worried about these dependencies, huh, huh, don’t worry, these dependencies are released along with SIGAR, and all open sources). The Sigar API provides a convenient interface to collect system information, such as:

¡ô System memory, page switches, CPU, average load, run time, login information

¡ô Memory, CPU, account information, status, parameters, environment, open files per process

¡ô File system detection and metrics

¡ô Network interface detection, configuration information and metrics

¡ô Network route and connection table

[At present, I can’t monitor local disk information in real time. After I put a U disk, I didn’t find it] [color = red] [/ color]

Hyperic-HQ official website: http://support.hyperic.com/display/sigar/home

Sigar.jar download address: http://sourceforge.net/projects/sigar/files/

Sigar.jar package method:

CPU resource information

a) Number of CPUs (unit: one)

PrivateStaticint getcpucount () throws sigarexception {

Sigar sigar = new sigar ();

Try {

Return Sigar.getcpuinfolist (). Length;

} finally {

Sigar.Close ();

}

}

b) Information of the total amount of CPU (unit: Hz) and CPU

Sigar sigar = getsigar ();

CPUINFO INFOS [] = SIGAR.GETCPUInfolist ();

For (int i = 0; I

CPUINFO INFO = INFOS [I];

Traceln (“MHz =” + Info.getMHz ()); // CPU’s total MHz

Traceln (“vendor =” + info.getvendor ()); // Get the seller of the CPU, such as Intel

Traceln (“Model =” + Info.getModel ()); // Get categories for CPUs, such as Celeron

Traceln (“cache size =” + info.getCachesize ()); // Buffer memory number

}

c) The user usage of the CPU, the system uses the residual amount, the total residual amount, the total use of total usage, etc. (unit: 100%)

Publicvoid testcpuperc () {

Sigar sigar = new sigar ();

// mode 1, mainly for a CPU

CPUPERC CPU;

Try {

CPU = sigar.getcpuperc ();

Printcpuperc (CPU);

} catch (sigarexception e) {

E.PrintStackTrace ();

}

// mode 2, whether it is single CPU or more CPUs apply

CPUPERC CPULIST [] = NULL;

Try {

Cpulist = sigar.getcpuperclist ();

} catch (sigarexception e) {

E.PrintStackTrace ();

Return;

}

For (int i = 0; i

}

}

Privatevoid Printcpuperc (CPUPERC CPU) {

Println (“User:” + CPUPERC.FORMAT (CPU.GETUSER ()); // User usage

Println (“sys:” + cpuperc.format (cpu.getsys ())); // System usage

Println (“Wait:” + CPUPERC.FORMAT (CPU.Getwait ()))); // Current waiting rate

Println (“Nice:” + CPUPERC.FORMAT (CPU.Getnice ())); //

Println (“idle:” + cpuperc.format (cpu.getidle ())); // Current idle rate

Println (“total:” + cpuperc.format (cpu.getcombined ()))); // Total usage rate

}

d) …

2. Memory resource information

a) Physical memory information

Sigar sigar = new sigar ();

MEM MEM = Sigar.getMem ();

// Total memory

System.out.println (“Total =” + Mem.gettotal () / 1024L + “K av”);

// Current memory usage

System.out.println (“Used =” + Mem.getused () / 1024L + “K Used”);

// Current memory remaining

System.out.println (“free =” + mem.getfree () / 1024L + “k free”);

b) System page file exchange area information

Sigar sigar = new sigar ();

Swap swap = sigar.getswap ();

// Exchange area total

System.out.println (“Total =” + swap.gettotal () / 1024L + “K av”);

// Current exchange area usage

System.out.println (“Used =” + swap.getused () / 1024l + “k used”);

// Current exchange area remaining

System.out.println (“free =” + swap.getfree () / 1024L + “k free”);

c) …

3. Operating system information

a) Take the name of the current operating system:

Private string getPlatformName () {

String hostname = “”;

Try {

Hostname = inetaddress.getlocalhost (). gethostname ();

} catch (Exception EXC) {

Sigar sigar = new sigar ();

Try {

Hostname = sigar.getNetInfo (). gethostname ();

} catch (sigarexception e) {

Hostname = “Localhost.unknown”;

} finally {

Sigar.Close ();

}

}

Return Hostname;

}

b) Take information about the current operating system

Publicvoid testgetosinfo () {

Operatingsystem OS = OperatingSystem.getInstance ();

// Operating system kernel type such as: 386, 486, 586, etc. x86

System.out.println (“Os.Getarch () =” + OS.GeTARCH ());

System.out.println (“Os.Getcpuendian () =” + OS.Getcpuendian ()); //

System.out.println (“Os.GetDataModel () =” + OS.GetDataModel ()); /// System Description

System.out.println (“Os.GetDescription () =” + OS.GetDescription ());

System.out.println (“Os.getMachine () =” + OS.GETMACHINE ()); //

// Operating system type

System.out.println (“Os.GetName () =” + OS.GetName ());

System.out.println (“Os.GetPatchLevel () =” + OS.GetPatchLevel ()); //

// Seller of the operating system

System.out.println (“Os.Getvendor () =” + OS.Getvendor ());

// Sell the master name

System.out.println (“Os.Getvendorcodename () =” + OS.Getvendorcodename ());

// Operating system name

System.out.println (“Os.Getvendorname () =” + OS.Getvendorname ());

// Operating system seller type

System.out.println (“os.getvendorversion () =” + os.getvendorversion ());

// Operating system version number

System.out.println (“os.getversion () =” + os.getversion ());

}

c) Take user information in the current system process table

Publicvoid testWho () {

Try {

Sigar sigar = new sigar ();

Who who [] = sigar.getwholist ();

IF (WHO! = Null && Who.length> 0) {

For (int i = 0; i

SYSOUT.OUT.PRINTLN (“

~~~~~~~~~ “+ String.Valueof (i) +” ~~~~~~~~~ “);

WHO _WHO = WHO [I];

SYSOUT.OUT.Println (“getDevice () =” + _who.getDevice ());

SYSOUT.OUT.Println (“gethost () =” + _WHO.GETHOST ());

SYSOUT.OUT.Println (“getTime () =” + _WHO.GETTIME ());

// Username in the current system process table

Sysout.out.println (“getuser () =” + _who.getuser ());

}

}

} catch (sigarexception e) {

E.PrintStackTrace ();

}

}

d) …

4. Resource information (mainly hard drives)

a) Take a hard disk existing partition and its details (by sigar.getfilesystemlist () to get a FileSystem list object, then make a copy of it):

Publicvoid testfilesysteminfo () throws exception {

Sigar sigar = getsigar ();

FileSystem fslist [] = sigar.getfilesystemlist ();

String Dir = system.getProperty (“user.home”); // Current User Folder Path

For (int I = 0; i

System.out.println (“

~~~~~~~~~~ “+ i +” ~~~~~~~~~~ “);

FileSystem fs = fslist [i];

// Partitioned drive letter name system.out.println (“fs.getdevname () =” + fs.getDevname ());

// Partition disk letter name

System.out.println (“fs.getdiRName () =” + fs.getdiRName ());

System.out.println (“fs.getflags () =” + fs.getflags ()); //

// File system type, such as FAT32, NTFS

System.out.println (“fs.getsystypename () =” + fs.getsystypename ());

// File system type name, such as local hard drive, optical drive, network file system, etc.

System.out.println (“fs.gettypename () =” + fs.gettypename ());

// File system type

System.out.println (“fs.gettype () =” + fs.gettype ());

FileSystemusage Usage = NULL;

Try {

Usage = sigar.getfilesystemusage (fs.getdiRName ());

} catch (sigarexception e) {

IF (fs.gettype () == 2)

Throw e;

CONTINUE;

}

Switch (fs.gettype ()) {

Case 0: // type_unknown: unknown

Break;

Case 1: // Type_none

Break;

Case 2: // type_local_disk: Local hard drive

// File system total size

System.out.println (“total =” + usage.gettotal () + “kb”);

// File system remaining size

System.out.println (“free =” + usage.getfree () + “kb”);

// File system can be used

System.out.println (“avail =” + usage.getavail () + “kb”);

// File system has been used

System.out.println (“Used =” + USAGE.GETUSED () + “KB”);

Double usepercent = usage.getusepercent () * 100D;

// Usage of file system resources

System.out.println (“USAGE =” + USEPERCENT + “%”);

Break;

Case 3: // type_network: network

Break;

Case 4: // type_ram_disk: Flash

Break;

Case 5: // type_cdrom: optical drive

Break;

Case 6: // type_swap: Page Switch

Break;

}

System.out.println (“DiskReads =” + usage.getdiskreads ());

System.out.println (“Diskwrites =” + usage.getdiskwrites ());

}

Return;

}

b) ……

5. Network information

a) the official domain name of the current machine

Public String getfqdn () {

Try {

Return inetaddress.getlocalhost (). getcanonicalhostname ();

} catch (unknownhostexception e) {

Try {

Sigar sigar = new sigar ();

Return Sigar.getfqdn ();

} catch (sigarexception ex) {

ReturnNun;

} finally {

Sigar.Close ();

}

}

}

b) Take the IP address of the current machine

Public string getDefaultIpaddress () {

String address = null;

Try {

Address = inetaddress.getlocalhost (). gethostaddress ();

/ / Do not have an abnormality and the IP that is properly taken, if you take it, you will return to the NIC recovery.

/ / Otherwise, get it again through the method in the SiGAR toolkit.

IF (! Netflags.LoopBack_address.Equals (address)) {

Return Address;

}

} catch (unknownhostexception e) {

// Hostname Not in DNS or / etc / hosts

}

Sigar sigar = new sigar ();

Try {

Address = Sigar.getNetInterfaceConfig (). getAddress ();

} catch (sigarexception e) {

Address = Netflags.loopBack_Address;

} finally {

Sigar.Close ();

}

Return Address;

}

c) take the MAC address of the current machine

Public string getMac () {

Sigar sigar = null;

Try {

sigar = new sigar ();

String [] ifaces = sigar.getNetInterfaceList ();

String hwaddr = NULL;

For (int i = 0; i

NetInterfaceConfig cfg = sigar.getNetInterfaceConfig (ifaces [i]);

IF (netflags.loopback_address.equals (cfg.getaddress ())

|| (cfg.getflags () & NetFlags.iff_loopback! = 0

|| Netflags.null_hwaddr.equals (cfg.gethwaddr ())) {

CONTINUE;

}

/ * If there is a network card including a virtual machine, only the first NIC’s MAC address is taken by default. If you want to return all NICs (including physical and virtual), you can modify the return type of the method as an array or Collection. By multiple MAC addresses taken in the FOR cycle. * /

Hwaddr = cfg.gethwaddr ();

Break;

}

Return hwaddr! = null? hwaddr: null;

} catch (exception e) {

ReturnNun;

} finally {

IF (Sigar! = NULL)

Sigar.Close ();

}

}

d) get a GUID number according to the MAC address

Public String getGuid (String Mac) {

IF (Mac == Null)

ReturnNun;

EthernetAddress EADDR = New EthernetAddress (Mac);

Return uuidgenerator.getInstance (). GeneratetimeBaseduUId (EADDR)

.tostring ();

}

e) Get information such as network traffic

Publicvoid test portiFlist () throws exception {

Sigar sigar = new sigar ();

String ifnames [] = sigar.getnetInterfaceList ();

For (int i = 0; i

String name = ifnames [i];

Netinterfaceconfig ifconfig = sigar.getNetInterfaceConfig (Name); Print (“

Name = “+ name); // network device name

Print (“address =” + ifconfig.getaddress ()); // ip address

Print (“Netmask =” + ifconfig.getNetmask ()); // Subnet mask

IF (ifconfig.getflags () & 1L)

Print (“! iff_up … skipping getnetInterfacestat);

CONTINUE;

}

Try {

NetInterfaceStat ifstat = Sigar.getNetInterfaceStat (Name);

Print (“RXPACKETS =” + ifstat.getRxpackets ()); // Received total wrappers

Print (“TXPACKETS =” + ifstat.gettxpackets ()); // The total number of wraps sent

Print (“rxbytes =” + ifstat.getrxBytes ()); // The number of bytes received

Print (“TXBytes =” + ifstat.gettxbytes ()); // The total number of bytes sent

Print (“rxerrors =” + ifstat.getRxErrorS ()); // The number of error packets received

Print (“txerrors =” + ifstat.gettxerrors ()); // number of errors when sending packets

Print (“rxdropped =” + ifstat.getrxdropped ()); // number of packets discarded during reception

Print (“txdropped =” + ifstat.getxdropped ()); // number of packets discarded when sending

} catch (sigarnotimplementexception e) {

} catch (sigarexception e) {

PRINT (E.GetMessage ());

}

}

}

Void Print (String MSG) {

System.out.println (MSG);

}

f) Some other information

Privatevoid getethernetInfo () {

Sigar sigar = null;

Try {

sigar = new sigar ();

String [] ifaces = sigar.getNetInterfaceList ();

For (int i = 0; i

NetInterfaceConfig cfg = sigar.getNetInterfaceConfig (ifaces [i]);

IF (netflags.loopback_address.equals (cfg.getaddress ())

|| (cfg.getflags () & NetFlags.iff_loopback! = 0

|| Netflags.null_hwaddr.equals (cfg.gethwaddr ())) {

CONTINUE;

}

System.out.println (“cfg.getaddress () =” + cfg.getaddress ()); // ip address

System.out.println (“cfg.getBroadcast () =” + cfg.getBroadcast ()); // gateway broadcast address

System.out.println (“cfg.gethwaddr () =” + cfg.gethwaddr ()); // NIC MAC address

System.out.println (“cfg.getnetmask () =” + cfg.getNetmask ()); // Subnet mask

System.out.println (“cfg.getdescription () =” + cfg.getDescription ()); // NIC Description Information System.out.Println (“cfg.gettype () =” + cfg.gettype ()); //

System.out.println (“cfg.getdestination () =” + cfg.getdestination ());

System.out.println (“cfg.getflags () =” + cfg.getflags ()); //

System.out.println (“cfg.getMetric () =” + cfg.getMetric ());

System.out.println (“cfg.getmtu () =” + cfg.getmtu ());

System.out.println (“cfg.getname () =” + cfg.getname ());

System.out.println ();

}

} catch (exception e) {

System.out.println (“Error While Creating GUID” + E);

} finally {

IF (Sigar! = NULL)

Sigar.Close ();

}

}

Hyperic-sigar-1.6.4.zip (3.4 MB) Downloads: 113

Related Posts