package com.eactive.eai.rms.client.util;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

// use for windows demo
public class WindowsSystemMon {
	private static double getBeanMonitoringValue(OperatingSystemMXBean oBean, String methodName) {
		double doubleValue = 0;
		for (Method method : oBean.getClass().getDeclaredMethods()) {
			method.setAccessible(true);
			if (method.getName().startsWith(methodName) && Modifier.isPublic(method.getModifiers())) {
				Object value;
				try {
					value = method.invoke(oBean);
					if (value != null) {
						if (value instanceof Double)
							doubleValue = (Double) value;
						if (value instanceof Long)
							doubleValue = ((Long) value).doubleValue();
					}
					break;
				} catch (Exception e) {
					value = e;
				} // try
//				System.out.println(method.getName() + " = " + value);
			} // if
		} // for
		return doubleValue;
	}
	
	private static void printSystemUsage() {
		OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
		// System.out.println("CPU Usage : " + String.format("%.2f", getBeanMonitoringValue(osBean, "getSystemCpuLoad") * 100));
		// System.out.println("CPU Usage : " + String.format("%.2f", getBeanMonitoringValue(osBean, "getCpuLoad") * 100));
		// System.out.println("CPU JVM Usage : " + String.format("%.2f", getBeanMonitoringValue(osBean, "getProcessCpuLoad") * 100));
		// System.out.println("Memory Free Space G : " + String.format("%.2f",getBeanMonitoringValue(osBean, "getFreePhysicalMemorySize") / 1024 / 1024 / 1024));
		// System.out.println("Memory Total Space G : " + String.format("%.2f",getBeanMonitoringValue(osBean, "getTotalPhysicalMemorySize") / 1024 / 1024 / 1024));
	}
	
	public static int getCpuPercentage() {
		try {
			OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
			int cpu = new Double(getBeanMonitoringValue(osBean, "getSystemCpuLoad") * 100).intValue();
			return cpu;
		}
		catch(Exception ex) {
			return 0;
		}
	}

	public static int getDiskPercentage(){
		String rootDrive = System.getProperty("user.dir");
		File root = new File(rootDrive);

		double free = (double)root.getFreeSpace();
		double total = (double)root.getTotalSpace();
		double tmp = (free / total) * 100;
		return (int)Math.round(tmp);
	}
	
	public static int getMemoryPercentage() {
		try {
			OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
			double total = getBeanMonitoringValue(osBean, "getTotalPhysicalMemorySize");
			double free = getBeanMonitoringValue(osBean, "getFreePhysicalMemorySize");
			int memUsed = new Double((total-free)/total * 100).intValue();
			return memUsed;
		}
		catch(Exception ex) {
			return 0;
		}
	}
	
	public static void main(String[] args) {
//		printSystemUsage();
		// System.out.println("getCpuPercentage = " + getCpuPercentage());
		// System.out.println("getMemoryPercentage = " + getMemoryPercentage());
		// System.out.println("getDiskPercentage = " + getDiskPercentage());
	}

}
