Add CPU and Memory info to your HTTP Provider

Wowza provides a couple of useful HTTP Providers to obtain serverinfo and connection counts. You could extend those to add some relevant information regarding system resource utilization etc.

Here are a couple of functions that will help us obtain this information.

1. Get the total physical memory:

	private double getTotalMemory() {      // returns a percentage value with 1 decimal point precision
		com.sun.management.OperatingSystemMXBean bean =
				  (com.sun.management.OperatingSystemMXBean)
				    java.lang.management.ManagementFactory.getOperatingSystemMXBean();
		long value = bean.getTotalPhysicalMemorySize();
		return value;
	}

2. Get the total free physical memory:

	private double getMemoryFree(){
		com.sun.management.OperatingSystemMXBean bean =
				  (com.sun.management.OperatingSystemMXBean)
				    java.lang.management.ManagementFactory.getOperatingSystemMXBean();
		long value = bean.getFreePhysicalMemorySize();
		return value;
	}

3. Get the cpu percentage:

	private double getCPUPercentage() throws MalformedObjectNameException, ReflectionException, InstanceNotFoundException {

	    MBeanServer mbs    = ManagementFactory.getPlatformMBeanServer();
	    ObjectName name    = ObjectName.getInstance("java.lang:type=OperatingSystem");
	    AttributeList list = mbs.getAttributes(name, new String[]{ "ProcessCpuLoad" });

	    if (list.isEmpty())     return Double.NaN;

	    Attribute att = (Attribute)list.get(0);
	    Double value  = (Double)att.getValue();

	    if (value == -1.0)      return Double.NaN;  // usually takes a couple of seconds before we get real values

	    return ((int)(value * 1000) / 10.0);        // returns a percentage value with 1 decimal point precision
	}

4. Package it up into your http provider or run it on its own as follows:

	public void onHTTPRequest(IVHost vhost, IHTTPRequest req, IHTTPResponse resp) {
		if (!doHTTPAuthentication(vhost, req, resp))
			return;
		
	    StringBuilder sb = new StringBuilder();
		sb.append("\n");
		
		Runtime runtime = Runtime.getRuntime();
	    try {
			sb.append(""+this.getCPUPercentage() + "");
			sb.append("");
			sb.append(""+this.getTotalMemory()+ "");
			sb.append(""+this.getMemoryFree()+ "");
		    sb.append("" + runtime.maxMemory()  + "");
		    sb.append("" + runtime.freeMemory() + "");
			sb.append("");
		} catch (MalformedObjectNameException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InstanceNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ReflectionException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		sb.append(""); 
		try {
			resp.setHeader("Content-Type", "text/xml");
			OutputStream out = resp.getOutputStream();
			byte[] outBytes = sb.toString().getBytes();
			out.write(outBytes);
		} catch (Exception e) {
			System.out.println("Error: " + e.toString());
		}
	}

*Requires Java 1.7+


Comments are closed.