Commit b0c28a5c authored by ncampbell's avatar ncampbell

removing current version of meter...I'll add the new version later

git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/branches@2942 b35dd754-fafc-0310-a699-88a17e54d16e
parent f4400d3f
/**
*
*/
package org.jivesoftware.messenger.plugin.meter;
import java.util.ArrayList;
import java.util.Set;
import javax.management.openmbean.CompositeData;
import org.apache.commons.jxpath.DynamicPropertyHandler;
/**
* @author Noah Campbell
* @version 1.0
*/
public class CompositeDataPropertyHandler implements DynamicPropertyHandler {
/**
* @see org.apache.commons.jxpath.DynamicPropertyHandler#getPropertyNames(java.lang.Object)
*/
public String[] getPropertyNames(Object object) {
ArrayList<String> names = new ArrayList<String>(20);
if(object instanceof CompositeData) {
CompositeData cd = (CompositeData)object;
Set keys = cd.getCompositeType().keySet();
for(Object key : keys) {
names.add((String) key);
}
}
return names.toArray(new String[names.size()]);
}
/**
* @see org.apache.commons.jxpath.DynamicPropertyHandler#getProperty(java.lang.Object, java.lang.String)
*/
public Object getProperty(Object object, String propertyName) {
if(object instanceof CompositeData) {
CompositeData cd = (CompositeData)object;
return cd.get(propertyName);
}
return null;
}
/**
* @see org.apache.commons.jxpath.DynamicPropertyHandler#setProperty(java.lang.Object, java.lang.String, java.lang.Object)
*/
public void setProperty(@SuppressWarnings("unused") Object object,
@SuppressWarnings("unused") String propertyName,
@SuppressWarnings("unused") Object value) {
throw new UnsupportedOperationException("read-only");
}
}
/**
*
*/
package org.jivesoftware.messenger.plugin.meter;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.jrobin.annotations.Rrd;
/**
* @author Noah Campbell
* @version 1.0
*/
public class GraphBuilderBean {
/**
* Return all the ObjectNames
*
* @return objectnames
* @throws Exception
*/
public List<String> getChartableMBeans() throws Exception {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
Set mbeans = server.queryNames(null, null);
List<String> results = new ArrayList<String>(mbeans.size());
for(Object mbean : mbeans) {
ObjectName objectName = ((ObjectName)mbean);
MBeanInfo info = server.getMBeanInfo(objectName);
Rrd rrd = info.getClass().getAnnotation(Rrd.class);
if(rrd != null)
results.add(objectName.getCanonicalName());
}
return results;
}
public List<String> getStores() {
return new ArrayList<String>(RrdManager.listStores().keySet());
}
}
/**
*
*/
package org.jivesoftware.messenger.plugin.meter;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.management.ObjectName;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.jrobin.core.RrdDb;
import org.jrobin.core.RrdDbPool;
import org.jrobin.graph.RrdGraph;
import org.jrobin.graph.RrdGraphDef;
/**
* @author Noah Campbell
* @version 1.0
*/
public class GraphServlet extends HttpServlet {
/** The serialVersionUID. */
private static final long serialVersionUID = 1L;
/**
* @param request The Request.
* @param response The Response.
* @throws ServletException Throw if unable to process the request due to a Servlet related problem.
* @throws IOException Thrown if unable to process the request due to an IO related problem.
* @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@SuppressWarnings("unused")
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("image/png");
ServletOutputStream out = response.getOutputStream();
response.addHeader("Cache-Control", "no-cache");
createPNGGraph(request, out);
}
/** The colors. */
private static final Color[] colors = new Color[]{new Color(102, 204, 204),
new Color(102,153,204),
new Color(102,102,204),
new Color(153,102,204),
new Color(102,204,153),
new Color( 61,184,184),
new Color( 46,138,138),
new Color(204,102,204),
new Color(102,204,102),
new Color(138, 46, 46),
new Color(184, 61, 61),
new Color(204,102,153),
new Color(153,204,102),
new Color(204,204,102),
new Color(204,153,102),
new Color(204,102,102)};
/** The converter. */
private static final Converter converter = new ColorConverter();
/**
* @param request The servlet request.
* @param out The output stream.
* @throws IOException Thrown if an IO exception occurs.
*/
private void createPNGGraph(HttpServletRequest request, OutputStream out) throws IOException {
int width;
int height;
try {
int w = Integer.parseInt(request.getParameter("width"));
width = w <= 800 ? w : 800;
} catch (NumberFormatException e) {
width = 400;
}
try {
int h = Integer.parseInt(request.getParameter("height"));
height = h <= 600 ? h : 600;
} catch (NumberFormatException e) {
height = 300;
}
String error = "An error has occured.";
try {
ConvertUtils.register(converter, Color.class);
String store = request.getParameter("store");
String objectName = request.getParameter("oname");
RrdGraphDef def = new RrdGraphDef();
BeanUtilsBean.getInstance().populate(def, request.getParameterMap());
String[] lines = request.getParameterValues("lines");
for(String line : lines) {
String[] field = line.split("::");
def.line(field[0], parseColor(field[1]), field[2]);
}
RrdDbPool pool = RrdDbPool.getInstance();
RrdManager manager = RrdManager.listStores().get(store);
String filename = manager.getFileName(new ObjectName(objectName));
RrdDb db = pool.requestRrdDb(filename);
String ref = "input";
int count = 0;
for(String ds : db.getDsNames()) {
String id = ref + count++;
def.datasource(id, filename, ds, "AVERAGE");
def.line(id, colors[count], ds);
}
RrdGraph graph = new RrdGraph(def);
byte[] output;
if(request.getParameter("width") == null) {
output = graph.getPNGBytes();
} else {
output = graph.getPNGBytes(width, height);
}
/**
* write out the byte array to the client.
*/
ByteArrayInputStream bis = new ByteArrayInputStream(output);
while(bis.available() > 0) {
out.write(bis.read());
}
} catch (Exception e) {
error += " " + e.getLocalizedMessage();
} finally {
ConvertUtils.deregister(Color.class);
}
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
g2d.setBackground(Color.white);
// g2d.setBackground(new Color(0,0,0,255));
// GradientPaint gpaint = new GradientPaint(0, height / 2, Color.white, width, height / 2, Color.gray);
// Paint originalPaint = g2d.getPaint();
// g2d.setPaint(gpaint);
g2d.fillRect(0,0,width,height);
int errorWidth = g2d.getFontMetrics().stringWidth(error);
// g2d.setPaint(originalPaint);
g2d.setColor(Color.black);
g2d.drawString(error, width / 2 - errorWidth / 2, height / 2);
g2d.dispose();
ImageIO.write(bufferedImage, "png", out);
}
/**
* @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
*/
@Override
public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub
super.init(arg0);
}
/**
* @author Noah Campbell
* @version 1.0
*/
private static class ColorConverter implements Converter {
/**
* @see org.apache.commons.beanutils.Converter#convert(java.lang.Class, java.lang.Object)
*/
public Object convert(@SuppressWarnings("unused") Class cls, Object input) {
if(input instanceof String) {
String s = (String)input;
return parseColor(s);
}
return colors[0];
}
}
/**
* Parse a string of format #RRBBGGAA where RR, BB, GG, AA are hex values
* ranging between 0 (00) and 255 (FF). AA is optional and can be excluded
* from the #RRBBGG string.
*
* @param s
* @return color
*/
private static Color parseColor(String s) {
try {
if( s.startsWith("#") ) {
int r = Integer.valueOf(s.substring(1, 3), 16);
int g = Integer.valueOf(s.substring(3, 5), 16);
int b = Integer.valueOf(s.substring(5, 7), 16);
int a = 255;
if(s.length() > 7) {
a = Integer.valueOf(s.substring(7, 9), 16);
}
return new Color(r, g, b, a);
}
} catch (RuntimeException e) {
}
return colors[0];
}
}
/**
*
*/
package org.jivesoftware.messenger.plugin.meter;
import org.jrobin.annotations.Ds;
import org.jrobin.annotations.Rrd;
import org.jrobin.annotations.SourceType;
/**
* @author Noah Campbell
* @version 1.0
*/
@Rrd
public interface MemoryMXBeanSupportInfo {
/**
* @return max The max heap memory usage.
*/
@Ds(name="max", expr="/HeapMemoryUsage/max", type=SourceType.COUNTER)
long getMaxHeapMemoryUsage();
/**
* @return initial The initial heap memory usage.
*/
@Ds(name="init", expr="/HeapMemoryUsage/init")
long getInitHeapMemoryUsage();
/**
* @return used The heap memory usage.
*/
@Ds(name="used", expr="/HeapMemoryUsage/used")
long getUsedHeapMemoryUsage();
/**
* @return commited The commited heap memory usage.
*/
@Ds(name="commited", expr="/HeapMemoryUsage/committed")
long getCommittedHeapMemoryUsage();
}
/**
*
*/
package org.jivesoftware.messenger.plugin.meter;
import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import org.jivesoftware.messenger.container.Plugin;
import org.jivesoftware.messenger.container.PluginManager;
import org.jivesoftware.messenger.plugin.meter.accumulator.Accumulator;
import org.jrobin.core.RrdDbPool;
import org.jrobin.core.RrdException;
/**
* @author Noah Campbell
* @version 1.0
*/
public class MeterPlugin implements Plugin {
/** The pluginDirectory. */
@SuppressWarnings("unused")
private File pluginDirectory;
/** The manager. */
@SuppressWarnings("unused")
private PluginManager manager;
/** The logger. */
final static private Logger logger = Logger.getLogger("monitorplugin", "resources");
/** The server. */
private MBeanServer server = ManagementFactory.getPlatformMBeanServer();
/** The preferences. */
private Preferences preferences;
/** The rrdManager. */
private RrdManager rrdManager;
/** The executors. */
ScheduledExecutorService executors = Executors.newScheduledThreadPool(10);
/** The pool. */
RrdDbPool pool = RrdDbPool.getInstance();
/**
* @see org.jivesoftware.messenger.container.Plugin#initializePlugin(org.jivesoftware.messenger.container.PluginManager, java.io.File)
*/
public void initializePlugin(PluginManager manager, File pluginDirectory) {
this.manager = manager;
this.pluginDirectory = pluginDirectory;
File store = new File(pluginDirectory, "store");
if(!store.exists()) {
store.mkdirs();
}
try {
rrdManager = new RrdManager(store.getCanonicalFile(), pool);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
preferences = Preferences.systemNodeForPackage(MeterPlugin.class);
String monitoredObjectNames = preferences.get("monitoredObjectNames", "java.lang:type=Threading;java.lang:type=Memory");
for(String name : monitoredObjectNames.split("; ?")) {
logger.log(Level.FINER, "initialize.objectname", name);
try {
ObjectName objectName = new ObjectName(name);
Accumulator accum = rrdManager.create(objectName, server.getMBeanInfo(objectName));
/**
* Schedule the accumulator.
*/
executors.scheduleAtFixedRate(accum, 0, 300, TimeUnit.SECONDS);
} catch (MalformedObjectNameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstanceNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RrdException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IntrospectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ReflectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @see org.jivesoftware.messenger.container.Plugin#destroyPlugin()
*/
public void destroyPlugin() {
}
}
/**
*
*/
package org.jivesoftware.messenger.plugin.meter;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.naming.OperationNotSupportedException;
import org.apache.commons.jxpath.DynamicPropertyHandler;
/**
* @author Noah Campbell
* @version 1.0
*/
public class ObjectNamePropertyHandler implements DynamicPropertyHandler {
/** The server. */
private static final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
/** The logger. */
final static private Logger logger = Logger.getLogger(
"objectnamepropertyhandler", "resources");
/**
* @see org.apache.commons.jxpath.DynamicPropertyHandler#getPropertyNames(java.lang.Object)
*/
public String[] getPropertyNames(Object object) {
ArrayList<String> list = new ArrayList<String>();
try {
ObjectName objectName = (ObjectName)object;
MBeanInfo info = server.getMBeanInfo(objectName);
MBeanAttributeInfo[] attrs = info.getAttributes();
for(MBeanAttributeInfo attr : attrs) {
list.add(attr.getName());
}
} catch (Exception e) {
logger.log(Level.WARNING, "unabletoevaluatepropertyname", e);
}
return list.toArray(new String[list.size()]);
}
/**
* @see org.apache.commons.jxpath.DynamicPropertyHandler#getProperty(java.lang.Object, java.lang.String)
*/
public Object getProperty(Object object, String propertyName) {
try {
return server.getAttribute((ObjectName) object, propertyName);
} catch (AttributeNotFoundException e) {
logger.log(Level.WARNING, "attributenotfound", e);
} catch (InstanceNotFoundException e) {
logger.log(Level.WARNING, "instancenotfound", e);
} catch (MBeanException e) {
logger.log(Level.WARNING, "mbeanexception", e);
} catch (ReflectionException e) {
logger.log(Level.WARNING, "reflectionexception", e);
}
return null;
}
/**
* @see org.apache.commons.jxpath.DynamicPropertyHandler#setProperty(java.lang.Object, java.lang.String, java.lang.Object)
*/
public void setProperty(@SuppressWarnings("unused") Object object,
@SuppressWarnings("unused") String propertyName,
@SuppressWarnings("unused") Object value) {
throw new RuntimeException(new OperationNotSupportedException("unable to perform this opertion."));
}
}
/**
*
*/
package org.jivesoftware.messenger.plugin.meter;
import org.jrobin.annotations.Ds;
import org.jrobin.annotations.Rrd;
/**
* @author Noah Campbell
* @version 1.0
*/
@Rrd
public interface ThreadMXBeanSupportInfo {
/**
* @return time The current thread cpu time.
*/
@Ds(name="cputime", expr="/CurrentThreadCpuTime" )
long getCurrentThreadCpuTime();
/**
* @return count The current thread count.
*/
@Ds(name="threadCount", expr="/ThreadCount")
long getThreadCount();
}
/**
*
*/
package org.jivesoftware.messenger.plugin.meter;
/**
* @author Noah Campbell
* @version 1.0
*/
public class UnableToAllocateAccumulator extends Exception {
/** The serialVersionUID. */
private static final long serialVersionUID = 1L;
/**
* Construct a new <code>UnableToAllocateAccumulator</code>.
*
*/
public UnableToAllocateAccumulator() {
super();
// TODO Auto-generated constructor stub
}
/**
* Construct a new <code>UnableToAllocateAccumulator</code>.
*
* @param message
* @param cause
*/
public UnableToAllocateAccumulator(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
/**
* Construct a new <code>UnableToAllocateAccumulator</code>.
*
* @param message
*/
public UnableToAllocateAccumulator(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* Construct a new <code>UnableToAllocateAccumulator</code>.
*
* @param cause
*/
public UnableToAllocateAccumulator(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment