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

com.quigley.zabbixj.agent.ZabbixAgent Maven / Gradle / Ivy

/*
 * Copyright 2015 Michael Quigley
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.quigley.zabbixj.agent;

import com.quigley.zabbixj.agent.active.ActiveThread;
import com.quigley.zabbixj.agent.passive.ListenerThread;
import com.quigley.zabbixj.metrics.MetricsContainer;
import com.quigley.zabbixj.metrics.MetricsProvider;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ZabbixAgent is the central class of the Zabbix/J implementation. It provides
 * all of the necessary components for exposing metrics from within a Java
 * virtual machine.
 *
 * @author Michael Quigley
 */
public class ZabbixAgent {

    /**
     * Construct a new ZabbixAgent instance.
     * @throws Exception when a problem occurs creating the agent.
     */
    public ZabbixAgent() throws Exception {
        metricsContainer = new MetricsContainer();

        enablePassive = true;
        listenPort = 10050;
        listenAddress = null;

        enableActive = false;
        serverAddress = null;
        serverPort = 10051;
        refreshInterval = 120;
        pskIdentity = null;
        psk = null;
    }

    /**
     * Start processing requests. Starts the passive listener (if enabled) and the
     * active agent (if enabled).
     * @throws Exception when a problem occurs starting the agent.
     */
    public void start() throws Exception {
        if(log.isInfoEnabled()) {
            log.info("Starting Zabbix agent.");
        }

        if(enablePassive) {
        	if(log.isInfoEnabled()) {
        		log.info("Starting passive listener.");
        	}

	        if(listenAddress == null) {
	            listenerThread = new ListenerThread(metricsContainer, listenPort);
	        } else {
	            listenerThread = new ListenerThread(metricsContainer, listenAddress, listenPort);
	        }
	        listenerThread.start();

	        if(log.isInfoEnabled()) {
	        	log.info("Passive listener started.");
	        }
        }

        if(enableActive) {
        	if(log.isInfoEnabled()) {
        		log.info("Starting active agent.");
        	}

        	activeThread = new ActiveThread(metricsContainer, hostName, serverAddress, serverPort, refreshInterval, pskIdentity, psk);
        	activeThread.start();

        	if(log.isInfoEnabled()) {
        		log.info("Active agent started.");
        	}
        }

        if(log.isInfoEnabled()) {
            log.info("Zabbix agent started.");
        }
    }

    /**
     * Stop processing requests. Stops any passive or active components started in the start
     * method.
     */
    public void stop() {
        if(log.isInfoEnabled()) {
            log.info("Stopping Zabbix agent.");
        }

        if(enablePassive) {
        	if(log.isInfoEnabled()) {
        		log.info("Stopping passive listener.");
        	}

	        listenerThread.shutdown();
	        try {
	            listenerThread.join();

	        } catch(InterruptedException ie) {
	            //
	        }

	        if(log.isInfoEnabled()) {
	        	log.info("Passive listener stopped.");
	        }
        }

        if(enableActive) {
        	if(log.isInfoEnabled()) {
        		log.info("Stopping active agent.");
        	}

        	activeThread.shutdown();
        	try {
        		activeThread.join();

        	} catch(InterruptedException ie) {
        		//
        	}

        	if(log.isInfoEnabled()) {
        		log.info("Active agent stopped.");
        	}
        }

        if(log.isInfoEnabled()) {
            log.info("Zabbix agent stopped.");
        }
    }

    /**
     * Return the value of property enablePassive.
     * @return the current value of property enablePassive.
     */
    public boolean isEnablePassive() {
		return enablePassive;
	}

    /**
     * Set the value of property enablePassive.
     * @param enablePassive Set to true if the agent should start a listener
     * 		  for passive checks. Set to false if the agent should omit the
     * 		  passive listener.
     */
	public void setEnablePassive(boolean enablePassive) {
		this.enablePassive = enablePassive;
	}

	/**
     * Return the value of property listenAddress.
     * @return the current value of listenAddress. Returns null if the property is unset.
     */
    public String getListenAddress() {
        return listenAddress.getHostAddress();
    }

    /**
     * Set the value of property listenAddress.
     * @param listenAddress a string representation of the interface address the agent should
     *        bind on. This address must match the address of an active interface on the host
     *        system.
     * @throws UnknownHostException when the provided address does not match one of the active
     *         interfaces on the host system.
     */
    public void setListenAddress(String listenAddress) throws UnknownHostException {
        this.listenAddress = InetAddress.getByName(listenAddress);
    }

    /**
     * Return the value of property listenPort.
     * @return the current value of listenPort. Defaults to 10050.
     */
    public int getListenPort() {
        return listenPort;
    }

    /**
     * Set the value of property listenPort.
     * @param listenPort the new value for property listenPort. Must be a valid TCP port number
     *        or the agent will fail when the start() method is invoked.
     */
    public void setListenPort(int listenPort) {
        this.listenPort = listenPort;
    }

    /**
     * Return the value of property enableActive.
     * @return the current value of enableActive. Defaults to false.
     */
    public boolean isEnableActive() {
		return enableActive;
	}

    /**
     * Set the value of property enableActive.
     * @param enableActive Set to true when an active check configuration is
     * 		  desired, false otherwise.
     */
	public void setEnableActive(boolean enableActive) {
		this.enableActive = enableActive;
	}

	/**
	 * Return the value of property hostName.
	 * @return the current value of hostName.
	 */
	public String getHostName() {
		return hostName;
	}

	/**
	 * Set the value of property hostName.
	 * @param hostName the name of this host, as configured in Zabbix.
	 */
	public void setHostName(String hostName) {
		this.hostName = hostName;
	}

	/**
	 * Return the value of property serverAddress.
	 * @return the current value of serverAddress.
	 */
	public InetAddress getServerAddress() {
		return serverAddress;
	}

	/**
	 * Set the value of property serverAddress.
	 * @param serverAddress the IP address for the Zabbix server listening for active checks.
	 */
	public void setServerAddress(InetAddress serverAddress) {
		this.serverAddress = serverAddress;
	}

	/**
	 * Return the value of property serverPort.
	 * @return the current value of the serverPort property.
	 */
	public int getServerPort() {
		return serverPort;
	}

	/**
	 * Set the value of property serverPort.
	 * @param serverPort the TCP port for the Zabbix server listening for active checks. Defaults to
	 * 		  10051.
	 */
	public void setServerPort(int serverPort) {
		this.serverPort = serverPort;
	}

	/**
	 * Return the value of property refreshInterval.
	 * @return the current value of the refreshInterval property. Defaults to 120.
	 */
	public int getRefreshInterval() {
		return refreshInterval;
	}
	public void setRefreshInterval(int refreshInterval) {
		this.refreshInterval = refreshInterval;
	}

	/**
	 * Return the value of property pskIdentity.
	 * @return the current value of the pskIdentity property. Defaults to null.
	 */
	public String getPskIdentity() {
		return pskIdentity;
	}
	public void setPskIdentity(String psk) {
		this.psk = pskIdentity;
	}

	/**
	 * Return the value of property psk.
	 * @return the current value of the psk property. Defaults to null.
	 */
	public String getPsk() {
		return psk;
	}
	public void setPsk(String psk) {
		this.psk = psk;
	}

	/**
     * Add a MetricsProvider to the agent.
     * @param name bind the provider to this name.
     * @param provider the provider instance.
     */
    public void addProvider(String name, MetricsProvider provider) {
        metricsContainer.addProvider(name, provider);
    }

    public void setProviders(Map providers) {
    	metricsContainer.addProviders(providers);
    }

    private boolean enablePassive;
    private InetAddress listenAddress;
    private int listenPort;

    private boolean enableActive;
    private String hostName;
    private InetAddress serverAddress;
    private int serverPort;
    private int refreshInterval;
    private String pskIdentity;
    private String psk;

    private MetricsContainer metricsContainer;
    private ListenerThread listenerThread;
    private ActiveThread activeThread;

    private static Logger log = LoggerFactory.getLogger(ZabbixAgent.class);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy