
net.welen.jmole.protocols.logstash.Logstash Maven / Gradle / Ivy
package net.welen.jmole.protocols.logstash;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
/*
* #%L
* JMole, https://bitbucket.org/awelen/jmole
* %%
* Copyright (C) 2015 - 2017 Anders Welén, [email protected]
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 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 Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.ReflectionException;
import javax.net.ssl.SSLSocketFactory;
import com.google.gson.Gson;
import net.welen.jmole.JMole;
import net.welen.jmole.presentation.PresentationInformation;
public class Logstash implements LogstashMBean, Runnable {
private final static Logger LOG = Logger.getLogger(Logstash.class.getName());
private static final String SEP = "/";
private static String PROPERTY_LOGSTASH_ENABLED = "jmole.protocol.logstash.enabled";
private static String PROPERTY_LOGSTASH_USESSL = "jmole.protocol.logstash.useSSL";
private static String PROPERTY_LOGSTASH_HOST = "jmole.protocol.logstash.host";
private static String PROPERTY_LOGSTASH_PORT = "jmole.protocol.logstash.port";
private static String PROPERTY_LOGSTASH_KEEPALIVE = "jmole.protocol.logstash.keepAlive";
private static String PROPERTY_LOGSTASH_INTERVAL = "jmole.protocol.logstash.interval";
private Boolean useSSL;
private String host;
private Integer port;
private Long interval;
private Boolean keepAlive;
private boolean stopped = false;
private boolean logstashStopped = false;
private Thread collector;
private JMole jmole;
private Socket socket;
@Override
public boolean isEnabled() {
return Boolean.getBoolean(PROPERTY_LOGSTASH_ENABLED);
}
@Override
public void startProtocol(JMole jmole) throws Exception {
this.jmole = jmole;
useSSL = Boolean.getBoolean(PROPERTY_LOGSTASH_USESSL);
if (useSSL == null) {
useSSL = false;
}
host = System.getProperty(PROPERTY_LOGSTASH_HOST);
if (host == null) {
host = "localhost";
}
port = Integer.getInteger(PROPERTY_LOGSTASH_PORT);
if (port == null) {
port = 5000;
}
interval = Long.getLong(PROPERTY_LOGSTASH_INTERVAL);
if (interval == null) {
interval = 60000L;
}
keepAlive = Boolean.getBoolean(PROPERTY_LOGSTASH_KEEPALIVE);
if (keepAlive == null) {
keepAlive = false;
}
collector = new Thread(this);
collector.setName("JMole Logstash collector thread");
collector.start();
LOG.log(Level.INFO, "JMole Logstash protocol started: " + host + ":" + port + " interval=" + interval);
}
@Override
public void stopProtocol() throws Exception {
LOG.log(Level.INFO, "Stopping JMole Logstash protocol");
stopped = true;
collector.interrupt();
while (!logstashStopped) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
LOG.log(Level.FINE, e.getMessage(), e);
}
}
LOG.log(Level.INFO, "JMole Logstash protocol stopped");
}
@Override
public boolean useSSL() {
return useSSL;
}
@Override
public String getHost() {
return host;
}
@Override
public int getPort() {
return port;
}
@Override
public boolean getKeepAlive() {
return keepAlive;
}
@Override
public long getInterval() {
return interval;
}
@Override
public void setInterval(long interval) {
this.interval = interval;
}
@Override
public void run() {
stopped = false;
logstashStopped = false;
try {
while (!stopped) {
try {
Thread.sleep(interval);
} catch (InterruptedException e) {
LOG.log(Level.FINE, e.getMessage(), e);
}
if (stopped) {
return;
}
try {
collectMeasurements();
sendWarnings();
sendCriticals();
} catch (Exception e) {
LOG.log(Level.SEVERE, e.getMessage(), e);
} finally {
if (!keepAlive) {
try {
if (socket != null) {
socket.close();
}
} catch (IOException e) {
LOG.log(Level.WARNING, "Couldn't close socket.", e);
}
socket = null;
}
}
}
} finally {
logstashStopped = true;
}
}
private void collectMeasurements() throws InstanceNotFoundException, AttributeNotFoundException, ReflectionException, MBeanException, IOException {
Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy