Search in sources :

Example 31 with SnmpAgentConfig

use of org.opennms.netmgt.snmp.SnmpAgentConfig in project opennms by OpenNMS.

the class SnmpAssetProvisioningAdapter method doAddNode.

/**
 * <p>doAdd</p>
 *
 * @param nodeId a int.
 * @param retry a boolean.
 * @throws org.opennms.netmgt.provision.ProvisioningAdapterException if any.
 */
@Override
public void doAddNode(final int nodeId) throws ProvisioningAdapterException {
    LOG.debug("doAdd: adding nodeid: {}", nodeId);
    final OnmsNode node = m_nodeDao.get(nodeId);
    Assert.notNull(node, "doAdd: failed to return node for given nodeId:" + nodeId);
    InetAddress ipaddress = m_template.execute(new TransactionCallback<InetAddress>() {

        @Override
        public InetAddress doInTransaction(TransactionStatus arg0) {
            return getIpForNode(node);
        }
    });
    SnmpAgentConfig agentConfig = null;
    String locationName = node.getLocation() != null ? node.getLocation().getLocationName() : null;
    agentConfig = m_snmpConfigDao.getAgentConfig(ipaddress, locationName);
    final OnmsAssetRecord asset = node.getAssetRecord();
    m_config.getReadLock().lock();
    try {
        for (final AssetField field : m_config.getAssetFieldsForAddress(ipaddress, node.getSysObjectId())) {
            try {
                final String value = fetchSnmpAssetString(m_locationAwareSnmpClient, agentConfig, locationName, field.getMibObjs(), field.getFormatString());
                LOG.debug("doAdd: Setting asset field \" {} \" to value: {}", field.getName(), value);
                // Use Spring bean-accessor classes to set the field value
                final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(asset);
                try {
                    wrapper.setPropertyValue(field.getName(), value);
                } catch (final BeansException e) {
                    LOG.warn("doAdd: Could not set property \" {} \" on asset object {}", field.getName(), e.getMessage(), e);
                }
            } catch (final Throwable t) {
                // This exception is thrown if the SNMP operation fails or an incorrect number of
                // parameters is returned by the agent or because of a misconfiguration.
                LOG.warn("doAdd: Could not set value for asset field \" {} \": {}", field.getName(), t.getMessage(), t);
            }
        }
    } finally {
        m_config.getReadLock().unlock();
    }
    node.setAssetRecord(asset);
    m_nodeDao.saveOrUpdate(node);
    m_nodeDao.flush();
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) BeanWrapper(org.springframework.beans.BeanWrapper) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsAssetRecord(org.opennms.netmgt.model.OnmsAssetRecord) AssetField(org.opennms.netmgt.config.snmpAsset.adapter.AssetField) TransactionStatus(org.springframework.transaction.TransactionStatus) InetAddress(java.net.InetAddress) BeansException(org.springframework.beans.BeansException)

Example 32 with SnmpAgentConfig

use of org.opennms.netmgt.snmp.SnmpAgentConfig in project opennms by OpenNMS.

the class SnmpConfigRestService method getSnmpInfo.

/**
 * <p>getSnmpInfo</p>
 *
 * @param ipAddr a {@link java.lang.String} object.
 * @return a {@link org.opennms.web.snmpinfo.SnmpInfo} object.
 */
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
@Path("{ipAddr}")
public SnmpInfo getSnmpInfo(@PathParam("ipAddr") String ipAddr, @QueryParam("location") String location) {
    final InetAddress addr = InetAddressUtils.addr(ipAddr);
    if (addr == null) {
        throw getException(Status.BAD_REQUEST, "Malformed IP Address: {}.", ipAddr);
    }
    final SnmpAgentConfig config = m_accessService.getAgentConfig(addr, location);
    return new SnmpInfo(config);
}
Also used : SnmpInfo(org.opennms.web.svclayer.model.SnmpInfo) SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) InetAddress(java.net.InetAddress) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 33 with SnmpAgentConfig

use of org.opennms.netmgt.snmp.SnmpAgentConfig in project opennms by OpenNMS.

the class ManageSnmpIntfServlet method doPost.

/**
 * {@inheritDoc}
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    HttpSession userSession = request.getSession(false);
    if (userSession == null)
        throw new ServletException("Session exceeded");
    String nodeIdString = request.getParameter("node");
    if (nodeIdString == null) {
        throw new org.opennms.web.servlet.MissingParameterException("node");
    }
    int nodeId = WebSecurityUtils.safeParseInt(nodeIdString);
    String intfIdString = request.getParameter("intf");
    if (intfIdString == null) {
        throw new org.opennms.web.servlet.MissingParameterException("intf");
    }
    int intfId = WebSecurityUtils.safeParseInt(intfIdString);
    String statusString = request.getParameter("status");
    if (statusString == null) {
        throw new org.opennms.web.servlet.MissingParameterException("status");
    }
    int status = WebSecurityUtils.safeParseInt(statusString);
    LOG.debug("ManageSnmpIntfServlet.doPost: parameters - node {} intf {} status {}", nodeId, intfId, status);
    String snmpIp = null;
    Service[] snmpServices = null;
    try {
        snmpServices = NetworkElementFactory.getInstance(getServletContext()).getServicesOnNode(nodeId, this.snmpServiceId);
        if (snmpServices != null && snmpServices.length > 0) {
            List<InetAddress> ips = new ArrayList<>();
            for (int i = 0; i < snmpServices.length; i++) {
                ips.add(InetAddressUtils.addr(snmpServices[i].getIpAddress()));
            }
            InetAddress lowest = InetAddressUtils.getLowestInetAddress(ips);
            if (lowest != null) {
                snmpIp = InetAddressUtils.str(lowest);
            }
        }
        InetAddress[] inetAddress = InetAddress.getAllByName(snmpIp);
        SnmpAgentConfig agent = SnmpPeerFactory.getInstance().getAgentConfig(inetAddress[0]);
        LOG.debug("ManageSnmpIntfServlet.doPost: agent SNMP version/write community {}/{}", agent.getVersion(), agent.getWriteCommunity());
        SnmpIfAdmin snmpIfAdmin = new SnmpIfAdmin(nodeId, agent);
        if (snmpIfAdmin.setIfAdmin(intfId, status)) {
            LOG.debug("ManageSnmpIntfServlet.doPost: snmpIAdmin return OK ");
        } else {
            LOG.debug("ManageSnmpIntfServlet.doPost: snmpIAdmin return error ");
        }
        redirect(request, response);
    } catch (SQLException e) {
        throw new ServletException(e);
    } catch (UnknownHostException e) {
        throw new ServletException(e);
    } catch (IOException e) {
        throw new ServletException(e);
    }
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) UnknownHostException(java.net.UnknownHostException) SQLException(java.sql.SQLException) HttpSession(javax.servlet.http.HttpSession) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) InetAddress(java.net.InetAddress)

Example 34 with SnmpAgentConfig

use of org.opennms.netmgt.snmp.SnmpAgentConfig in project opennms by OpenNMS.

the class SnmpCollectionSet method getAgentConfig.

/**
 * <p>getAgentConfig</p>
 *
 * @return a {@link org.opennms.netmgt.snmp.SnmpAgentConfig} object.
 */
public SnmpAgentConfig getAgentConfig() {
    SnmpAgentConfig agentConfig = getCollectionAgent().getAgentConfig();
    agentConfig.setPort(m_snmpCollection.getSnmpPort(agentConfig.getPort()));
    agentConfig.setRetries(m_snmpCollection.getSnmpRetries(agentConfig.getRetries()));
    agentConfig.setTimeout(m_snmpCollection.getSnmpTimeout(agentConfig.getTimeout()));
    agentConfig.setReadCommunity(m_snmpCollection.getSnmpReadCommunity(agentConfig.getReadCommunity()));
    agentConfig.setWriteCommunity(m_snmpCollection.getSnmpWriteCommunity(agentConfig.getWriteCommunity()));
    agentConfig.setProxyFor(m_snmpCollection.getSnmpProxyFor(agentConfig.getProxyFor()));
    agentConfig.setVersion(m_snmpCollection.getSnmpVersion(agentConfig.getVersion()));
    agentConfig.setMaxVarsPerPdu(m_snmpCollection.getSnmpMaxVarsPerPdu(agentConfig.getMaxVarsPerPdu()));
    agentConfig.setMaxRepetitions(m_snmpCollection.getSnmpMaxRepetitions(agentConfig.getMaxRepetitions()));
    agentConfig.setMaxRequestSize(m_snmpCollection.getSnmpMaxRequestSize(agentConfig.getMaxRequestSize()));
    agentConfig.setSecurityName(m_snmpCollection.getSnmpSecurityName(agentConfig.getSecurityName()));
    agentConfig.setAuthPassPhrase(m_snmpCollection.getSnmpAuthPassPhrase(agentConfig.getAuthPassPhrase()));
    agentConfig.setAuthProtocol(m_snmpCollection.getSnmpAuthProtocol(agentConfig.getAuthProtocol()));
    agentConfig.setPrivPassPhrase(m_snmpCollection.getSnmpPrivPassPhrase(agentConfig.getPrivPassPhrase()));
    agentConfig.setPrivProtocol(m_snmpCollection.getSnmpPrivProtocol(agentConfig.getPrivProtocol()));
    return agentConfig;
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig)

Example 35 with SnmpAgentConfig

use of org.opennms.netmgt.snmp.SnmpAgentConfig in project opennms by OpenNMS.

the class NodeDiscoveryIpNetToMedia method runCollection.

protected void runCollection() {
    final Date now = new Date();
    IpNetToMediaTableTracker ipNetToMediaTableTracker = new IpNetToMediaTableTracker() {

        public void processIpNetToMediaRow(final IpNetToMediaRow row) {
            IpNetToMedia macep = row.getIpNetToMedia();
            if (macep.getPhysAddress() == null && macep.getNetAddress() == null) {
                LOG.debug("processIpNetToMediaRow: node [{}], null:null:{}. ip and mac addresses null. skipping", getNodeId(), macep.getIpNetToMediaType());
            } else if (macep.getPhysAddress() == null) {
                LOG.debug("processIpNetToMediaRow: node [{}], null:{}:{}. mac address null. skipping", getNodeId(), str(macep.getNetAddress()), macep.getIpNetToMediaType());
            } else if (macep.getNetAddress() == null) {
                LOG.warn("processIpNetToMediaRow: node [{}], {}:null:{}. ip address null. skipping", getNodeId(), macep.getPhysAddress(), macep.getIpNetToMediaType());
            } else if (macep.getIpNetToMediaType() == IpNetToMediaType.IPNETTOMEDIA_TYPE_DYNAMIC || macep.getIpNetToMediaType() == IpNetToMediaType.IPNETTOMEDIA_TYPE_STATIC) {
                LOG.debug("processIpNetToMediaRow: node [{}], mac address {} and ip {} mediatype {}. saving", getNodeId(), macep.getPhysAddress(), str(macep.getNetAddress()), macep.getIpNetToMediaType());
                m_linkd.getQueryManager().store(getNodeId(), macep);
            } else {
                LOG.warn("processIpNetToMediaRow: node [{}],  {}:{}:{}. mediatype not valid. skipping", getNodeId(), macep.getPhysAddress(), str(macep.getNetAddress()), macep.getIpNetToMediaType());
            }
        }
    };
    SnmpAgentConfig peer = m_linkd.getSnmpAgentConfig(getPrimaryIpAddress(), getLocation());
    try {
        m_linkd.getLocationAwareSnmpClient().walk(peer, ipNetToMediaTableTracker).withDescription("ipNetToMedia").withLocation(getLocation()).execute().get();
    } catch (ExecutionException e) {
        LOG.info("run: node [{}]: ExecutionException: ipNetToMedia table: {}", getNodeId(), e.getMessage());
        return;
    } catch (final InterruptedException e) {
        LOG.info("run: node [{}]: InterruptedException: ipNetToMedia table: {}", getNodeId(), e.getMessage());
        return;
    }
    m_linkd.getQueryManager().reconcileIpNetToMedia(getNodeId(), now);
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) IpNetToMedia(org.opennms.netmgt.model.IpNetToMedia) ExecutionException(java.util.concurrent.ExecutionException) IpNetToMediaTableTracker(org.opennms.netmgt.enlinkd.snmp.IpNetToMediaTableTracker) Date(java.util.Date)

Aggregations

SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)120 InetAddress (java.net.InetAddress)31 Test (org.junit.Test)31 JUnitSnmpAgents (org.opennms.core.test.snmp.annotations.JUnitSnmpAgents)23 ArrayList (java.util.ArrayList)22 SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)21 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)21 SnmpInstId (org.opennms.netmgt.snmp.SnmpInstId)16 PollStatus (org.opennms.netmgt.poller.PollStatus)14 Map (java.util.Map)11 ExecutionException (java.util.concurrent.ExecutionException)9 ParameterMap (org.opennms.core.utils.ParameterMap)9 SnmpWalker (org.opennms.netmgt.snmp.SnmpWalker)8 Date (java.util.Date)7 LldpLink (org.opennms.netmgt.model.LldpLink)7 OnmsNode (org.opennms.netmgt.model.OnmsNode)7 HashMap (java.util.HashMap)6 List (java.util.List)5 LldpLocPortGetter (org.opennms.netmgt.enlinkd.snmp.LldpLocPortGetter)4 LldpRemTableTracker (org.opennms.netmgt.enlinkd.snmp.LldpRemTableTracker)4