/************************************************************************** /* This is a sample JMX-ready application. /* /* Copyright (c) 2003 by Bernhard Bablok (mail@bablokb.de) /* /* This program is free software; you can redistribute it and/or modify /* it under the terms of the GNU General Public License as published /* by the Free Software Foundation; either version 2 of the License or /* (at your option) any later version. /* /* This program is distributed in the hope that it will be useful, but /* WITHOUT ANY WARRANTY; without even the implied warranty of /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /* GNU General Public License for more details. /* /* You should have received a copy of the GNU General Public License /* along with this program; see the file COPYING. If not, write to /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, /* Boston, MA 02111-1307 USA /**************************************************************************/ import java.io.*; import java.util.*; import javax.management.*; import mx4j.adaptor.http.*; import com.sun.jdmk.comm.*; /** This is a sample JMX-ready application. It reads cpu and memory-usage from /proc/stat and /proc/meminfo. @version $Revision: 1.1 $ @author $Author: Bablokb $ */ public class LinuxInfo implements Runnable { private long iMemTotal, iMemUsed, iMemFree; private long iCpuUser1, iCpuNice1, iCpuSystem1, iCpuIdle1, iCpuTime1; private long iCpuUser2, iCpuNice2, iCpuSystem2, iCpuIdle2, iCpuTime2; private Object iCpuLock = new Object(); private int iUpdateInterval = 1000; //////////////////////////////////////////////////////////////////////////// /** Read memory status from /proc/meminfo. */ private void readMem() throws IOException { FileReader fr = new FileReader("/proc/meminfo"); BufferedReader input = new BufferedReader(fr); // skip first line input.readLine(); String meminfo=input.readLine(); input.close(); // extract information. Format of line: // total: used: free: shared: buffers: cached: // Mem: 301375488 186585088 114790400 0 76517376 53612544 StringTokenizer st = new StringTokenizer(meminfo); st.nextToken(); try { iMemTotal = Long.parseLong(st.nextToken()); iMemUsed = Long.parseLong(st.nextToken()); iMemFree = Long.parseLong(st.nextToken()); } catch (NumberFormatException e) { } } //////////////////////////////////////////////////////////////////////////// /** Query used memory */ public long getMemUsed() { return iMemUsed; } //////////////////////////////////////////////////////////////////////////// /** Read cpu status from /proc/stat. */ private void readCpu() { try { FileReader fr = new FileReader("/proc/stat"); BufferedReader input = new BufferedReader(fr); String cpuinfo=input.readLine(); input.close(); // extract information. Format of line: // user nice system ticks // cpu 16389 794 5262 987851 synchronized (iCpuLock) { StringTokenizer st = new StringTokenizer(cpuinfo); st.nextToken(); iCpuUser1 = iCpuUser2; iCpuNice1 = iCpuNice2; iCpuSystem1 = iCpuSystem2; iCpuIdle1 = iCpuIdle2; iCpuUser2 = Long.parseLong(st.nextToken()); iCpuNice2 = Long.parseLong(st.nextToken()); iCpuSystem2 = Long.parseLong(st.nextToken()); iCpuIdle2 = Long.parseLong(st.nextToken()); } } catch (Exception e) { } } //////////////////////////////////////////////////////////////////////////// /** Query cpu-user percentage. */ public int getCpuUserPercent() { synchronized (iCpuLock) { long diff = iCpuUser2 - iCpuUser1; long total = iCpuUser2+iCpuSystem2+iCpuIdle2 - (iCpuUser1+iCpuSystem1+iCpuIdle1); return (int) Math.round((100.0*diff)/total); } } //////////////////////////////////////////////////////////////////////////// /** Set update interval. */ public void setInterval(int interval) { iUpdateInterval = interval; } //////////////////////////////////////////////////////////////////////////// /** Get update interval. */ public int getInterval() { return iUpdateInterval; } //////////////////////////////////////////////////////////////////////////// /** Run indefinitely. */ public void run() { try { readCpu(); while (true) { readCpu(); readMem(); Thread.currentThread().sleep(iUpdateInterval); } } catch (Exception e) { } } //////////////////////////////////////////////////////////////////////////// /** Setup agent-layer for JMX-management. */ private void setupAgent() { try { MBeanServer server = MBeanServerFactory.createMBeanServer(); ObjectName cpuName = new ObjectName("LinuxInfo:name=cpu"); ObjectName memName = new ObjectName("LinuxInfo:name=mem"); ObjectName updateIntervalName = new ObjectName("LinuxInfo:name=updateInterval"); Cpu cpu = new Cpu(); cpu.setServer(this); Mem mem = new Mem(); mem.setServer(this); UpdateInterval updateInterval = new UpdateInterval(); updateInterval.setServer(this); server.registerMBean(cpu,cpuName); server.registerMBean(mem,memName); server.registerMBean(updateInterval,updateIntervalName); setupMX4J(server); setupSunRI(server); } catch (Exception e) { e.printStackTrace(); System.exit(3); } } //////////////////////////////////////////////////////////////////////////// /** MX4J http-adaptor. */ private void setupMX4J(MBeanServer server) throws Exception { HttpAdaptor adaptor = new HttpAdaptor(); ObjectName httpName = new ObjectName("mx4j:name=HttpAdaptor"); server.registerMBean(adaptor, httpName); adaptor.setPort(8888); adaptor.start(); XSLTProcessor xslt = new XSLTProcessor(); ObjectName processorName = new ObjectName("mx4j:name=XSLTProcessor"); server.registerMBean(xslt,processorName); server.setAttribute(httpName,new Attribute("ProcessorName",processorName)); } //////////////////////////////////////////////////////////////////////////// /** HTML adaptor of reference-implementation. */ private void setupSunRI(MBeanServer server) throws Exception { HtmlAdaptorServer adaptor = new HtmlAdaptorServer(); ObjectName htmlName = new ObjectName("sun:name=HtmlAdaptorServer"); server.registerMBean(adaptor,htmlName); adaptor.setPort(9999); adaptor.start(); } //////////////////////////////////////////////////////////////////////////// /** Start the program, setup MBean-server. */ public static void main(String[] args) { LinuxInfo li = new LinuxInfo(); Thread t = new Thread(li); li.readCpu(); t.start(); li.setupAgent(); } }