Search in sources :

Example 1 with AmiAgentConfig

use of org.opennms.netmgt.config.ami.AmiAgentConfig in project opennms by OpenNMS.

the class AmiPeerFactory method getAgentConfig.

/**
 * <p>getAgentConfig</p>
 *
 * @param agentInetAddress a {@link java.net.InetAddress} object.
 * @return a {@link org.opennms.protocols.ami.AmiAgentConfig} object.
 */
public AmiAgentConfig getAgentConfig(final InetAddress agentInetAddress) {
    getReadLock().lock();
    try {
        if (m_config == null)
            return new AmiAgentConfig(agentInetAddress);
        final AmiAgentConfig agentConfig = new AmiAgentConfig(agentInetAddress);
        // Now set the defaults from the m_config
        setAmiAgentConfig(agentConfig, new Definition());
        // Attempt to locate the node
        DEFLOOP: for (final Definition def : m_config.getDefinitions()) {
            // check the specifics first
            for (String saddr : def.getSpecifics()) {
                saddr = saddr.trim();
                final InetAddress addr = InetAddressUtils.addr(saddr);
                if (addr.equals(agentConfig.getAddress())) {
                    setAmiAgentConfig(agentConfig, def);
                    break DEFLOOP;
                }
            }
            // check the ranges
            for (final Range rng : def.getRanges()) {
                if (InetAddressUtils.isInetAddressInRange(InetAddressUtils.str(agentConfig.getAddress().orElse(null)), rng.getBegin(), rng.getEnd())) {
                    setAmiAgentConfig(agentConfig, def);
                    break DEFLOOP;
                }
            }
            // check the matching IP expressions
            for (final String ipMatch : def.getIpMatches()) {
                if (IPLike.matches(InetAddressUtils.str(agentInetAddress), ipMatch)) {
                    setAmiAgentConfig(agentConfig, def);
                    break DEFLOOP;
                }
            }
        }
        if (agentConfig == null)
            setAmiAgentConfig(agentConfig, new Definition());
        return agentConfig;
    } finally {
        getReadLock().unlock();
    }
}
Also used : AmiAgentConfig(org.opennms.netmgt.config.ami.AmiAgentConfig) Definition(org.opennms.netmgt.config.ami.Definition) Range(org.opennms.netmgt.config.ami.Range) InetAddress(java.net.InetAddress)

Example 2 with AmiAgentConfig

use of org.opennms.netmgt.config.ami.AmiAgentConfig in project opennms by OpenNMS.

the class AsteriskSIPPeerMonitor method poll.

/**
 * {@inheritDoc}
 *
 * <P>
 * Run the service monitor and return the poll status
 * </P>
 */
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
    // read configuration parameters
    String sipPeer = ParameterMap.getKeyedString(parameters, "sip-peer", DEFAULT_SIPPEER);
    if (sipPeer.equals(DEFAULT_SIPPEER)) {
        LOG.error("AsteriskMonitor: No sip-peer parameter in poller configuration");
        throw new RuntimeException("AsteriskMonitor: required parameter 'sip-peer' is not present in supplied properties.");
    }
    TimeoutTracker timeoutTracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
    AmiPeerFactory amiPeerFactory = AmiPeerFactory.getInstance();
    AmiAgentConfig amiConfig = amiPeerFactory.getAgentConfig(svc.getAddress());
    // setting up AMI connection
    LOG.debug("{}: Creating new AMI-Connection: {}:{}, {}/{}", svc.getSvcName(), svc.getIpAddr(), amiConfig.getPort(), amiConfig.getUsername(), amiConfig.getPassword());
    ManagerConnectionFactory factory = new ManagerConnectionFactory(svc.getIpAddr(), amiConfig.getPort().orElse(null), amiConfig.getUsername().orElse(null), amiConfig.getPassword().orElse(null));
    ManagerConnection managerConnection;
    if (amiConfig.getUseTls().orElse(false)) {
        managerConnection = factory.createSecureManagerConnection();
    } else {
        managerConnection = factory.createManagerConnection();
    }
    managerConnection.setSocketTimeout(new Long(timeoutTracker.getTimeoutInMillis()).intValue());
    // start with polling
    while (timeoutTracker.shouldRetry()) {
        timeoutTracker.nextAttempt();
        LOG.debug("{}: Attempt {}", svc.getSvcName(), timeoutTracker.getAttempt());
        try {
            LOG.debug("{}: AMI login", svc.getSvcName());
            managerConnection.login();
            LOG.debug("{}: AMI sendAction SipShowPeer", svc.getSvcName());
            ManagerResponse response = managerConnection.sendAction(new SipShowPeerAction(sipPeer));
            if (response.getAttribute("Status") == null) {
                LOG.debug("{}: service status down", svc.getSvcName());
                return PollStatus.decode("Down", "State of SIP Peer is unknown, because it was not found on the Asterisk server");
            }
            LOG.debug("{}: Response: {}", svc.getSvcName(), response.getAttribute("Status"));
            LOG.debug("{}: AMI logoff", svc.getSvcName());
            managerConnection.logoff();
            if (response.getAttribute("Status").startsWith("OK")) {
                LOG.debug("{}: service status up", svc.getSvcName());
                return PollStatus.decode("Up", "OK");
            } else {
                LOG.debug("{}: service status down", svc.getSvcName());
                return PollStatus.decode("Down", "State of SIP Peer is " + response.getAttribute("Status") + " and not OK");
            }
        } catch (AuthenticationFailedException e) {
            LOG.debug("{}: AMI AuthenticationError.", svc.getSvcName(), e);
            return PollStatus.decode("Down", "Could not get the state of SIP Peer: AMI AuthenticationError");
        } catch (TimeoutException e) {
            LOG.debug("{}: TimeOut reached.", svc.getSvcName(), e);
        } catch (SocketTimeoutException e) {
            LOG.debug("{}: TimeOut reached.", svc.getSvcName(), e);
        } catch (Exception e) {
            LOG.error("{}: An Unknown Exception Occurred.", svc.getSvcName(), e);
            return PollStatus.decode("Down", "Could not get the state of SIP Peer: " + e.toString());
        }
    }
    // If none of the retries worked
    return PollStatus.decode("Down", "Could not get the state of SIP Peer: Timeout exceeded");
}
Also used : ManagerConnection(org.asteriskjava.manager.ManagerConnection) ManagerResponse(org.asteriskjava.manager.response.ManagerResponse) AuthenticationFailedException(org.asteriskjava.manager.AuthenticationFailedException) ManagerConnectionFactory(org.asteriskjava.manager.ManagerConnectionFactory) AmiAgentConfig(org.opennms.netmgt.config.ami.AmiAgentConfig) SipShowPeerAction(org.asteriskjava.manager.action.SipShowPeerAction) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) TimeoutException(org.asteriskjava.manager.TimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) AuthenticationFailedException(org.asteriskjava.manager.AuthenticationFailedException) SocketTimeoutException(java.net.SocketTimeoutException) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) AmiPeerFactory(org.opennms.netmgt.config.AmiPeerFactory) TimeoutException(org.asteriskjava.manager.TimeoutException) SocketTimeoutException(java.net.SocketTimeoutException)

Aggregations

AmiAgentConfig (org.opennms.netmgt.config.ami.AmiAgentConfig)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 InetAddress (java.net.InetAddress)1 SocketTimeoutException (java.net.SocketTimeoutException)1 AuthenticationFailedException (org.asteriskjava.manager.AuthenticationFailedException)1 ManagerConnection (org.asteriskjava.manager.ManagerConnection)1 ManagerConnectionFactory (org.asteriskjava.manager.ManagerConnectionFactory)1 TimeoutException (org.asteriskjava.manager.TimeoutException)1 SipShowPeerAction (org.asteriskjava.manager.action.SipShowPeerAction)1 ManagerResponse (org.asteriskjava.manager.response.ManagerResponse)1 TimeoutTracker (org.opennms.core.utils.TimeoutTracker)1 AmiPeerFactory (org.opennms.netmgt.config.AmiPeerFactory)1 Definition (org.opennms.netmgt.config.ami.Definition)1 Range (org.opennms.netmgt.config.ami.Range)1