All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.collectd.mx.MBeanSender Maven / Gradle / Ivy

Go to download

The jcollectd package implements the collectd protocol in Java, making it possible for Java applications to push data into collectd over the wire.

The newest version!
/*
 * jcollectd
 * Copyright (C) 2009 Hyperic, Inc.
 * 
 * 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; only version 2 of the License is applicable.
 * 
 * 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; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

package org.collectd.mx;

import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

import org.collectd.api.Notification;
import org.collectd.api.ValueList;
import org.collectd.protocol.Dispatcher;
import org.collectd.protocol.Network;
import org.collectd.protocol.PropertyNames;
import org.collectd.protocol.Sender;
import org.collectd.protocol.UdpSender;

/**
 * Process -javaagent configuration and schedule MBeanCollector objects.
 */
public class MBeanSender implements Dispatcher {
    private static final Logger _log =
        Logger.getLogger(MBeanSender.class.getName());
    private static final String UDP = "udp";
    private static final String PSEP = "://";
    private static final String RUNTIME_NAME =
        "java.lang:type=Runtime";

    private MBeanServerConnection _bs =
            ManagementFactory.getPlatformMBeanServer();

    private ScheduledExecutorService _scheduler =
        Executors.newScheduledThreadPool(1, new ThreadFactory() {
            public Thread newThread(Runnable task) {
                Thread thread = new Thread(task);
                thread.setName("jcollectd");
                thread.setDaemon(true);
                return thread;
            }
        });

    private Map _senders =
        new HashMap();

    private MBeanConfig _config = new MBeanConfig();

    private String _instanceName;
    
    public void setMBeanServerConnection(MBeanServerConnection server) {
        _bs = server;
    }

    public MBeanServerConnection getMBeanServerConnection() {
        return _bs;
    }

    private String getRuntimeName() {
        try {
            ObjectName name = new ObjectName(RUNTIME_NAME);
            return (String)getMBeanServerConnection().getAttribute(name, "Name");
        } catch (Exception e) {
            return ManagementFactory.getRuntimeMXBean().getName();
        }    
    }

    public String getInstanceName() {
        if (_instanceName == null) {
            _instanceName = Network.getProperty(PropertyNames.INSTANCE, getRuntimeName());
        }
        return _instanceName;
    }

    public void setInstanceName(String instanceName) {
        _instanceName = instanceName;
    }

    public void schedule(MBeanCollector collector) {
        collector.setSender(this);
        _scheduler.scheduleAtFixedRate(collector, 0,
                                       collector.getInterval(),
                                       TimeUnit.SECONDS);
    }

    public void addSender(String protocol, Sender sender) {
        _senders.put(protocol, sender);
    }

    public void addDestination(String url) {
        int ix = url.indexOf(PSEP);
        if (ix == -1) {
            throw new IllegalArgumentException("Malformed url: " + url);
        }
        String protocol = url.substring(0, ix);
        String server = url.substring(ix + PSEP.length());
        Sender sender = _senders.get(protocol);
        if (sender == null) {
            if (protocol.equals(UDP)) {
                sender = new UdpSender();
                addSender(UDP, sender);
            }
            else {
                throw new IllegalArgumentException("Unsupported protocol: " + protocol);
            }
        }
        sender.addServer(server);
    }

    public MBeanCollector scheduleTemplate(String name, Long sendInterval) {
        MBeanCollector collector = null;
        try {
            //check for file via path and classpath,
            //e.g. "javalang", "javalang-jcollectd.xml"
            collector = _config.add(name);
        } catch (Exception e) {
            _log.log(Level.WARNING, "add template " + name +
                     ": " + e.getMessage(), e);
        }     
        if (collector != null) {
            if(sendInterval!=null) {
                collector.setInterval(sendInterval);
            }
            schedule(collector);
        }
        return collector;
    }

    public MBeanCollector scheduleMBean(String name, Long sendInterval) {
        try {
            new ObjectName(name);
        } catch (MalformedObjectNameException e) {
            _log.log(Level.WARNING, "add MBean " + name +
                     ": " + e.getMessage(), e);
            return null;
        }
        MBeanCollector collector = new MBeanCollector();
        if(sendInterval!=null) {
            collector.setInterval(sendInterval);
        }
        collector.addMBean(name);
        schedule(collector);
        return collector;
    }

    public MBeanCollector schedule(String name) {
        MBeanCollector collector = scheduleTemplate(name,null);
        if (collector == null) {
            //assume ObjectName, e.g. "sigar:*"
            collector = scheduleMBean(name,null);
        }
        return collector;
    }
    
    public void dispatch(Notification notification) {
        for (Sender sender : _senders.values()) {
            sender.dispatch(notification);
        }
    }

    public void dispatch(ValueList values) {
        for (Sender sender : _senders.values()) {
            sender.dispatch(values);
        }
    }

    public void flush() throws IOException {
        for (Sender sender : _senders.values()) {
            sender.flush();
        }
    }

    public void shutdown() {
        _scheduler.shutdownNow();
    }

    private void addShutdownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                shutdown();
            }
        });
    }

    public void configure() {
        //java -Djcd.dest=udp://localhost -Djcd.tmpl=javalang -Djcd.beans=sigar:* -Djcd.sendinterval=60
        String dest = Network.getProperty(PropertyNames.DESTINATION);        
        if (dest != null) {
            addDestination(dest);
        }
        
        String intervalProp = Network.getProperty(PropertyNames.SEND_INTERVAL,"");
        Long sendInterval = null;
        if (!intervalProp.isEmpty()) {
            sendInterval = Long.parseLong(intervalProp);   
        }        
        String tmpl = Network.getProperty(PropertyNames.TEMPLATE);
        
        if (tmpl != null) {
            for (String t : tmpl.split(",")) {
                scheduleTemplate(t,sendInterval);
            }
        }
        String beans = Network.getProperty(PropertyNames.BEANS);
        if (beans != null) {
            for (String b : beans.split("#")) {
                scheduleMBean(b,sendInterval);
            }
        }       
    }

    protected void init(String args) {
        if (args == null) {
            return;
        }
        //java -javaagent:collectd.jar="udp://localhost#javalang" -jar sigar.jar
        String[] argv = args.split("#");
        for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy