monitoring management sigar

package com.djjs;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.OperatingSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Swap;
import org.hyperic.sigar.Who;

/**
 * Use sigar to monitor, simple and convenient!
 * Instructions for use: The following code needs to be used with the sigar dll file, and the dll file needs to be placed in the bin folder under the JDK for the sigar program to call, and the sigarjar package is also required.
 * @author nxd
 */
public class RuntimeTest {
	public static void main(String[] args) {
		try {
			Timer timer = new Timer();// timer
			TimerTask task = new TimerTask() {
				public void run() {
					try {
						memory();
						property();
						cpu();
						you();
						who();
						System.out.println("====================================================");
					} catch (Exception e) {
						e.printStackTrace ();
					}
				}
			};
			try {
				timer.schedule(task, 0, 3000);// Get it every 3 seconds
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			// System information, obtained from jvm
			// property();//According to your own needs, you can put it into the timer to get it regularly
			// cpu information
			// cpu();
			// memory information
			// memory();
			// OS info
			// you();
			// User Info
			// who();

		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	private static void property() throws UnknownHostException {
		Runtime r = Runtime.getRuntime();
		Properties props = System.getProperties();
		InetAddress addr;
		addr = InetAddress.getLocalHost();
		String ip = addr.getHostAddress();
		Map<String, String> map = System.getenv();
		String userName = map.get("USERNAME");// Get the username
		String computerName = map.get("COMPUTERNAME");// Get the computer name
		String userDomain = map.get("USERDOMAIN");// Get the computer domain name
		System.out.println("Username: " + userName);
		System.out.println("Computer name: " + computerName);
		System.out.println("Computer domain name: " + userDomain);
		System.out.println("local ip address: " + ip);
		System.out.println("Local hostname: " + addr.getHostName());
		System.out.println("Total memory available to JVM: " + r.totalMemory());
		System.out.println("Remaining memory available to JVM: " + r.freeMemory());
		System.out.println("Number of processors available to JVM: " + r.availableProcessors());
		System.out.println("Java runtime environment version: " + props.getProperty("java.version"));
		System.out.println("Java runtime environment vendor: " + props.getProperty("java.vendor"));
		System.out.println("Java vendor URL: " + props.getProperty("java.vendor.url"));
		System.out.println("Java installation path: " + props.getProperty("java.home"));
		System.out.println("Java's virtual machine specification version: " + props.getProperty("java.vm.specification.version"));
		System.out.println("Java virtual machine specification vendor: " + props.getProperty("java.vm.specification.vendor"));
		System.out.println("Java's virtual machine specification name: " + props.getProperty("java.vm.specification.name"));
		System.out.println("Java's virtual machine implementation version: " + props.getProperty("java.vm.version"));
		System.out.println("Java virtual machine implementation vendor: " + props.getProperty("java.vm.vendor"));
		System.out.println("Java's virtual machine implementation name: " + props.getProperty("java.vm.name"));
		System.out.println("Java runtime environment specification version: " + props.getProperty("java.specification.version"));
		System.out.println("Java runtime environment specification vendor: " + props.getProperty("java.specification.vender"));
		System.out.println("Java runtime environment specification name: " + props.getProperty("java.specification.name"));
		System.out.println("Java class format version number: " + props.getProperty("java.class.version"));
		System.out.println("Java's classpath: " + props.getProperty("java.class.path"));
		System.out.println("List of paths searched when loading libraries: " + props.getProperty("java.library.path"));
		System.out.println("Default temporary file path: " + props.getProperty("java.io.tmpdir"));
		System.out.println("Path to one or more extension directories: " + props.getProperty("java.ext.dirs"));
		System.out.println("The name of the operating system: " + props.getProperty("os.name"));
		System.out.println("The architecture of the operating system: " + props.getProperty("os.arch"));
		System.out.println("OS version: " + props.getProperty("os.version"));
		System.out.println("File separator: " + props.getProperty("file.separator"));
		System.out.println("Path separator: " + props.getProperty("path.separator"));
		System.out.println("Line separator: " + props.getProperty("line.separator"));
		System.out.println("User's account name: " + props.getProperty("user.name"));
		System.out.println("User's home directory: " + props.getProperty("user.home"));
		System.out.println("User's current working directory: " + props.getProperty("user.dir"));
	}

	private static void memory() throws SigarException {
		Sigar cigar = new Sigar();
		Mem mem = sigar.getMem();
		float total = mem.getTotal();
		float used = mem.getUsed();
		// total memory
		System.out.println("Total memory: " + mem.getTotal() / 1024L / 1024L + "M av");
		// current memory usage
		System.out.println("Current memory usage: " + mem.getUsed() / 1024L / 1024L + "M used");
		// current amount of memory remaining
		System.out.println("Current memory remaining: " + mem.getFree() / 1024L / 1024L + "M free");
		// current memory usage
		System.out.println("Memory usage: " + used / total * 100 + "%");
		Swap swap = sigar.getSwap();
		// total amount of swap area
		System.out.println("Total swap area: " + swap.getTotal() / 1024L / 1024L + "M av");
		// current swap usage
		System.out.println("Current swap usage: " + swap.getUsed() / 1024L / 1024L + "M used");
		// current swap area remaining
		System.out.println("Current swap area remaining: " + swap.getFree() / 1024L / 1024L + "M free");
	}

	private static void cpu() throws SigarException {
		Sigar cigar = new Sigar();
		CpuInfo infos [] = sigar.getCpuInfoList ();
		CpuPerc cpuList [] = null;
		cpuList = sigar.getCpuPercList();
		for (int i = 0; i < infos.length; i++) {// Whether it is a single CPU or multiple CPUs
			CpuInfo info = infos[i];
			System.out.println("Section" + (i + 1) + "Block CPU Information");
			System.out.println("CPU total MHz: " + info.getMhz());// CPU total MHz
			System.out.println("CPU manufacturer: " + info.getVendor());// Get the vendor of the CPU, such as: Intel
			System.out.println("CPU class: " + info.getModel());// Get the CPU class, such as: Celeron
			System.out.println("Number of CPU caches: " + info.getCacheSize());// Number of buffer memory
			printCpuPerc(cpuList[i]);
		}
	}

	private static void printCpuPerc(CpuPerc cpu) {
		System.out.println("CPU user usage: " + CpuPerc.format(cpu.getUser()));// User usage
		System.out.println("CPU system usage: " + CpuPerc.format(cpu.getSys()));// System usage
		System.out.println("CPU current waiting rate: " + CpuPerc.format(cpu.getWait()));// current waiting rate
		System.out.println("CPU current error rate: " + CpuPerc.format(cpu.getNice()));//
		System.out.println("CPU current idle rate: " + CpuPerc.format(cpu.getIdle()));// Current idle rate
		System.out.println("CPU total usage: " + CpuPerc.format(cpu.getCombined()));// total usage
	}

	private static void os() {
		OperatingSystem OS = OperatingSystem.getInstance();
		// Operating system kernel type such as: 386, 486, 586, etc. x86
		System.out.println("OS: " + OS.getArch());
		System.out.println("OSCpuEndian(): " + OS.getCpuEndian());//
		System.out.println("Operating System DataModel(): " + OS.getDataModel());//
		// System specification
		System.out.println("Description of the operating system: " + OS.getDescription());
		// OS type
		// System.out.println("OS.getName(): " + OS.getName());
		// System.out.println("OS.getPatchLevel(): " + OS.getPatchLevel());//
		// the vendor of the operating system
		System.out.println("OS vendor: " + OS.getVendor());
		// vendor name
		System.out.println("OS vendor name: " + OS.getVendorCodeName());
		// OS name
		System.out.println("OS name: " + OS.getVendorName());
		// OS vendor type
		System.out.println("OS vendor type: " + OS.getVendorVersion());
		// version number of the operating system
		System.out.println("The version number of the operating system: " + OS.getVersion());
	}

	private static void who() throws SigarException {
		Sigar cigar = new Sigar();
		Who who[] = sigar.getWhoList();
		if (who != null && who.length > 0) {
			for (int i = 0; i < who.length; i++) {
				// System.out.println("User name in the current system process table" + String.valueOf(i));
				Who _who = who[i];
				System.out.println("User console: " + _who.getDevice());
				System.out.println("用户host: " + _who.getHost());
				// System.out.println("getTime(): " + _who.getTime());
				// The username in the current system process table
				System.out.println("User name in the current system process table: " + _who.getUser());
			}
		}
	}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326117787&siteId=291194637