<commit_before>//
// This file is part of the OpenNMS(R) Application.
//
// OpenNMS(R) is Copyright (C) 2006 The OpenNMS Group, Inc.  All rights reserved.
// OpenNMS(R) is a derivative work, containing both original code, included code and modified
// code that was published under the GNU General Public License. Copyrights for modified 
// and included code are below.
//
// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.

package org.opennms.netmgt.collectd;

import java.beans.PropertyVetoException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.UndeclaredThrowableException;
import java.net.InetAddress;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Category;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;
import org.opennms.core.utils.ThreadCategory;
import org.opennms.netmgt.config.DataCollectionConfigFactory;
import org.opennms.netmgt.config.DataSourceFactory;
import org.opennms.netmgt.config.NSClientDataCollectionConfigFactory;
import org.opennms.netmgt.config.NSClientPeerFactory;
import org.opennms.netmgt.config.nsclient.Attrib;
import org.opennms.netmgt.config.nsclient.NsclientCollection;
import org.opennms.netmgt.config.nsclient.Wpm;
import org.opennms.netmgt.model.RrdRepository;
import org.opennms.netmgt.poller.nsclient.NSClientAgentConfig;
import org.opennms.netmgt.poller.nsclient.NsclientCheckParams;
import org.opennms.netmgt.poller.nsclient.NsclientException;
import org.opennms.netmgt.poller.nsclient.NsclientManager;
import org.opennms.netmgt.poller.nsclient.NsclientPacket;
import org.opennms.netmgt.rrd.RrdException;
import org.opennms.netmgt.rrd.RrdUtils;
import org.opennms.netmgt.utils.EventProxy;

public class NSClientCollector implements ServiceCollector {

    // Don't make this static because each service will have its own
    // copy and the key won't require the service name as part of the key.
    private final HashMap<Integer, NSClientAgentState> m_scheduledNodes = new HashMap<Integer, NSClientAgentState>();

    private Category log() {
        return ThreadCategory.getInstance(getClass());
    }

    @SuppressWarnings("unchecked")
    public int collect(CollectionAgent agent, EventProxy eproxy, Map<String, String> parameters) {
        String collectionName = parameters.get("nsclient-collection");
        final CollectionAgent theAgent = agent; // For ResourceIdentifier
                                                // anonymous class to access the
                                                // var

        // Find out what attribs to collect for this node, then collect and
        // store them

        // Find attribs to collect - check groups in configuration. For each,
        // check scheduled nodes to see if that group should be collected
        NsclientCollection collection = NSClientDataCollectionConfigFactory.getInstance().getNSClientCollection(collectionName);
        NSClientAgentState agentState = m_scheduledNodes.get(agent.getNodeId());

        for (Wpm wpm : collection.getWpms().getWpm()) {
            // A wpm consists of a list of attributes, identified by name
            if (agentState.shouldCheckAvailability(wpm.getName(), wpm.getRecheckInterval())) {
                log().debug("Checking availability of group " + wpm.getName());
                try {
                    NsclientManager manager = agentState.getManager();
                    manager.init();
                    NsclientCheckParams params = new NsclientCheckParams(wpm.getKeyvalue());
                    NsclientPacket result = manager.processCheckCommand(NsclientManager.CHECK_COUNTER, params);
                    manager.close();
                    boolean isAvailable = (result.getResultCode() == NsclientPacket.RES_STATE_OK);
                    agentState.setGroupIsAvailable(wpm.getName(), isAvailable);
                } catch (NsclientException e) {
                    throw new NSClientCollectorException("Error checking group (" + wpm.getName() + ") availability", e);
                }
            }

            if (agentState.groupIsAvailable(wpm.getName())) {
                // Collect the data
                RrdRepository rrdRepository = NSClientDataCollectionConfigFactory.getInstance().getRrdRepository(collectionName);
                ResourceIdentifier resource = new ResourceIdentifier() {
                    public String getOwnerName() {
                        return theAgent.getHostAddress();
                    }

                    public File getResourceDir(RrdRepository repository) {
                        return new File(repository.getRrdBaseDir(), Integer.toString(theAgent.getNodeId()));
                    }
                };
                try {
                    NsclientManager manager = agentState.getManager();
                    manager.init(); // Open the connection, then do each
                                    // attribute

                    for (Attrib attrib : wpm.getAttrib()) {
                        NsclientPacket result = null;
                        NSClientCollectionAttribute attribute;
                        PersistOperationBuilder builder;
                        
                        try {
                            NsclientCheckParams params = new NsclientCheckParams(attrib.getName());
                            result = manager.processCheckCommand(NsclientManager.CHECK_COUNTER, params);
                        } catch (NsclientException e) {
                            log().info("unable to collect params for attribute '" + attrib.getName() + "'", e);
                        }

                        attribute = new NSClientCollectionAttribute(attrib.getAlias(), attrib.getType(), result.getResponse());
                        builder = new PersistOperationBuilder(rrdRepository, resource, attribute.getName());
                        builder.declareAttribute(attribute);
                        log().debug("doCollection: setting attribute: " + attribute);
                        builder.setAttributeValue(attribute, attribute.getValue());

                        try {
                            builder.commit();
                        } catch (RrdException e) {
                            throw new NSClientCollectorException("Error writing RRD", e);
                        }
                    }
                    manager.close(); // Only close once all the attribs have
                                        // been done (optimizing as much as
                                        // possible with NSClient)
                } catch (NsclientException e) {
                    throw new NSClientCollectorException("Error collecting data", e);
                }
            }
        }

        return ServiceCollector.COLLECTION_SUCCEEDED;
    }

    class NSClientCollectionAttribute implements AttributeDefinition {
        String m_alias;
        String m_type;
        String m_value;<commit_msg>fix for npe when result is null<commit_after>//
// This file is part of the OpenNMS(R) Application.
//
// OpenNMS(R) is Copyright (C) 2006 The OpenNMS Group, Inc.  All rights reserved.
// OpenNMS(R) is a derivative work, containing both original code, included code and modified
// code that was published under the GNU General Public License. Copyrights for modified 
// and included code are below.
//
// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.

package org.opennms.netmgt.collectd;

import java.beans.PropertyVetoException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.UndeclaredThrowableException;
import java.net.InetAddress;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Category;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;
import org.opennms.core.utils.ThreadCategory;
import org.opennms.netmgt.config.DataCollectionConfigFactory;
import org.opennms.netmgt.config.DataSourceFactory;
import org.opennms.netmgt.config.NSClientDataCollectionConfigFactory;
import org.opennms.netmgt.config.NSClientPeerFactory;
import org.opennms.netmgt.config.nsclient.Attrib;
import org.opennms.netmgt.config.nsclient.NsclientCollection;
import org.opennms.netmgt.config.nsclient.Wpm;
import org.opennms.netmgt.model.RrdRepository;
import org.opennms.netmgt.poller.nsclient.NSClientAgentConfig;
import org.opennms.netmgt.poller.nsclient.NsclientCheckParams;
import org.opennms.netmgt.poller.nsclient.NsclientException;
import org.opennms.netmgt.poller.nsclient.NsclientManager;
import org.opennms.netmgt.poller.nsclient.NsclientPacket;
import org.opennms.netmgt.rrd.RrdException;
import org.opennms.netmgt.rrd.RrdUtils;
import org.opennms.netmgt.utils.EventProxy;

public class NSClientCollector implements ServiceCollector {

    // Don't make this static because each service will have its own
    // copy and the key won't require the service name as part of the key.
    private final HashMap<Integer, NSClientAgentState> m_scheduledNodes = new HashMap<Integer, NSClientAgentState>();

    private Category log() {
        return ThreadCategory.getInstance(getClass());
    }

    @SuppressWarnings("unchecked")
    public int collect(CollectionAgent agent, EventProxy eproxy, Map<String, String> parameters) {
        String collectionName = parameters.get("nsclient-collection");
        final CollectionAgent theAgent = agent; // For ResourceIdentifier
                                                // anonymous class to access the
                                                // var

        // Find out what attribs to collect for this node, then collect and
        // store them

        // Find attribs to collect - check groups in configuration. For each,
        // check scheduled nodes to see if that group should be collected
        NsclientCollection collection = NSClientDataCollectionConfigFactory.getInstance().getNSClientCollection(collectionName);
        NSClientAgentState agentState = m_scheduledNodes.get(agent.getNodeId());

        for (Wpm wpm : collection.getWpms().getWpm()) {
            // A wpm consists of a list of attributes, identified by name
            if (agentState.shouldCheckAvailability(wpm.getName(), wpm.getRecheckInterval())) {
                log().debug("Checking availability of group " + wpm.getName());
                try {
                    NsclientManager manager = agentState.getManager();
                    manager.init();
                    NsclientCheckParams params = new NsclientCheckParams(wpm.getKeyvalue());
                    NsclientPacket result = manager.processCheckCommand(NsclientManager.CHECK_COUNTER, params);
                    manager.close();
                    boolean isAvailable = (result.getResultCode() == NsclientPacket.RES_STATE_OK);
                    agentState.setGroupIsAvailable(wpm.getName(), isAvailable);
                } catch (NsclientException e) {
                    throw new NSClientCollectorException("Error checking group (" + wpm.getName() + ") availability", e);
                }
            }

            if (agentState.groupIsAvailable(wpm.getName())) {
                // Collect the data
                RrdRepository rrdRepository = NSClientDataCollectionConfigFactory.getInstance().getRrdRepository(collectionName);
                ResourceIdentifier resource = new ResourceIdentifier() {
                    public String getOwnerName() {
                        return theAgent.getHostAddress();
                    }

                    public File getResourceDir(RrdRepository repository) {
                        return new File(repository.getRrdBaseDir(), Integer.toString(theAgent.getNodeId()));
                    }
                };
                try {
                    NsclientManager manager = agentState.getManager();
                    manager.init(); // Open the connection, then do each
                                    // attribute

                    for (Attrib attrib : wpm.getAttrib()) {
                        NsclientPacket result = null;
                        NSClientCollectionAttribute attribute;
                        PersistOperationBuilder builder;
                        
                        try {
                            NsclientCheckParams params = new NsclientCheckParams(attrib.getName());
                            result = manager.processCheckCommand(NsclientManager.CHECK_COUNTER, params);
                        } catch (NsclientException e) {
                            log().info("unable to collect params for attribute '" + attrib.getName() + "'", e);
                        }

                        if (result != null) {
                            attribute = new NSClientCollectionAttribute(attrib.getAlias(), attrib.getType(), result.getResponse());
                            builder = new PersistOperationBuilder(rrdRepository, resource, attribute.getName());
                            builder.declareAttribute(attribute);
                            log().debug("doCollection: setting attribute: " + attribute);
                            builder.setAttributeValue(attribute, attribute.getValue());

                            try {
                                builder.commit();
                            } catch (RrdException e) {
                                throw new NSClientCollectorException("Error writing RRD", e);
                            }
                        }
                    }
                    manager.close(); // Only close once all the attribs have
                                        // been done (optimizing as much as
                                        // possible with NSClient)
                } catch (NsclientException e) {
                    throw new NSClientCollectorException("Error collecting data", e);
                }
            }
        }

        return ServiceCollector.COLLECTION_SUCCEEDED;
    }

    class NSClientCollectionAttribute implements AttributeDefinition {
        String m_alias;
        String m_type;
        String m_value;
{'fraction_non_alphanumeric': 0.08169524606161996, 'fraction_numerical': 0.0006970584134950509, 'mean_word_length': 2.096697604144183, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4943716', 'n_tokens_mistral': 3520, 'n_tokens_neox': 3394, 'n_words': 848}