Search in sources :

Example 11 with Service

use of org.opennms.netmgt.config.poller.Service in project opennms by OpenNMS.

the class Test method execute.

@Override
public Object execute() throws Exception {
    // Parse/validate the IP address
    final InetAddress addr = InetAddressUtils.addr(ipAddress);
    if (addr == null) {
        throw new IllegalStateException("Error getting InetAddress object for " + ipAddress);
    }
    final Map<String, Object> parameters = Poll.parse(serviceParameters);
    final MonitoredService monSvc = transactionTemplate.execute(new TransactionCallback<MonitoredService>() {

        @Override
        public MonitoredService doInTransaction(TransactionStatus status) {
            final List<OnmsIpInterface> ips = ipInterfaceDao.findByIpAddress(ipAddress);
            if (ips == null || ips.size() == 0) {
                System.err.printf("Error: Can't find the IP address %s on the database\n", ipAddress);
                return null;
            }
            if (ips.size() > 1) {
                System.out.printf("Warning: there are several IP interface objects associated with the IP address %s (picking the first one)\n", ipAddress);
            }
            OnmsNode n = ips.get(0).getNode();
            return new SimpleMonitoredService(addr, n.getId(), n.getLabel(), serviceName);
        }
    });
    if (monSvc == null) {
        // in which case we already printed an error message above
        return null;
    }
    // Read a fresh copy of poller-configuration.xml
    final PollerConfig pollerConfig = ReadOnlyPollerConfigManager.create();
    System.out.printf("Checking service %s on IP %s%n", serviceName, ipAddress);
    final org.opennms.netmgt.config.poller.Package pkg = packageName == null ? pollerConfig.getFirstLocalPackageMatch(ipAddress) : pollerConfig.getPackage(packageName);
    if (pkg == null) {
        System.err.printf("Error: Package %s doesn't exist%n", packageName);
        return null;
    }
    System.out.printf("Package: %s%n", pkg.getName());
    final Service svc = pollerConfig.getServiceInPackage(serviceName, pkg);
    if (svc == null) {
        System.err.printf("Error: Service %s not defined on package %s%n", serviceName, packageName);
        return null;
    }
    ServiceMonitor monitor = null;
    if (monitorClass == null) {
        monitor = pollerConfig.getServiceMonitor(serviceName);
        if (monitor == null) {
            System.err.printf("Error: Service %s doesn't have a monitor class defined%n", serviceName);
            return null;
        }
    } else {
        monitor = registry.getMonitorByClassName(monitorClass);
        System.err.printf("Error: No monitor found with class name %s\n", monitorClass);
        if (monitor == null) {
            return null;
        }
    }
    System.out.printf("Monitor: %s%n", monitor.getClass().getName());
    if (pollerConfig.isPolledLocally(ipAddress, serviceName)) {
        for (Parameter p : svc.getParameters()) {
            if (!parameters.containsKey(p.getKey())) {
                String value = p.getValue();
                if (value == null) {
                    try {
                        value = JaxbUtils.marshal(p.getAnyObject());
                    } catch (Exception e) {
                    }
                }
                parameters.put(p.getKey(), value);
            }
        }
        for (Entry<String, Object> e : parameters.entrySet()) {
            System.out.printf("Parameter %s : %s%n", e.getKey(), e.getValue());
        }
        try {
            PollStatus status = monitor.poll(monSvc, parameters);
            System.out.printf("Available ? %s (status %s[%s])%n", status.isAvailable(), status.getStatusName(), status.getStatusCode());
            if (status.isAvailable()) {
                System.out.printf("Response time: %s%n", status.getResponseTime());
            } else {
                if (status.getReason() != null) {
                    System.out.printf("Reason: %s%n", status.getReason());
                }
            }
        } catch (Exception e) {
            System.err.println("Error: Can't execute the monitor. " + e.getMessage());
            return null;
        }
    } else {
        System.err.printf("Error: Polling is not enabled for service %s using IP %s%n", serviceName, ipAddress);
    }
    return null;
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) PollStatus(org.opennms.netmgt.poller.PollStatus) SimpleMonitoredService(org.opennms.netmgt.poller.support.SimpleMonitoredService) MonitoredService(org.opennms.netmgt.poller.MonitoredService) PollerConfig(org.opennms.netmgt.config.PollerConfig) TransactionStatus(org.springframework.transaction.TransactionStatus) SimpleMonitoredService(org.opennms.netmgt.poller.support.SimpleMonitoredService) MonitoredService(org.opennms.netmgt.poller.MonitoredService) Service(org.opennms.netmgt.config.poller.Service) ServiceMonitor(org.opennms.netmgt.poller.ServiceMonitor) SimpleMonitoredService(org.opennms.netmgt.poller.support.SimpleMonitoredService) Parameter(org.opennms.netmgt.config.poller.Parameter) List(java.util.List) InetAddress(java.net.InetAddress)

Example 12 with Service

use of org.opennms.netmgt.config.poller.Service in project opennms by OpenNMS.

the class DefaultPollerBackEnd method saveResponseTimeData.

/**
 * <p>saveResponseTimeData</p>
 *
 * @param locationMonitorId a {@link java.lang.String} object.
 * @param monSvc a {@link org.opennms.netmgt.model.OnmsMonitoredService} object.
 * @param responseTime a double.
 * @param pkg a {@link org.opennms.netmgt.config.poller.Package} object.
 */
@Override
public void saveResponseTimeData(final String locationMonitorId, final OnmsMonitoredService monSvc, final double responseTime, final Package pkg) {
    final String svcName = monSvc.getServiceName();
    final Service svc = m_pollerConfig.getServiceInPackage(svcName, pkg);
    String dsName = getServiceParameter(svc, "ds-name");
    if (dsName == null) {
        dsName = PollStatus.PROPERTY_RESPONSE_TIME;
    }
    String rrdBaseName = getServiceParameter(svc, "rrd-base-name");
    if (rrdBaseName == null) {
        rrdBaseName = dsName;
    }
    final String rrdRepository = getServiceParameter(svc, "rrd-repository");
    if (rrdRepository == null) {
        return;
    }
    RrdRepository repository = new RrdRepository();
    repository.setStep(m_pollerConfig.getStep(pkg));
    repository.setHeartBeat(repository.getStep() * HEARTBEAT_STEP_MULTIPLIER);
    repository.setRraList(m_pollerConfig.getRRAList(pkg));
    repository.setRrdBaseDir(new File(rrdRepository));
    DistributedLatencyCollectionResource distributedLatencyResource = new DistributedLatencyCollectionResource(locationMonitorId, InetAddressUtils.toIpAddrString(monSvc.getIpAddress()));
    DistributedLatencyCollectionAttributeType distributedLatencyType = new DistributedLatencyCollectionAttributeType(rrdBaseName, dsName);
    distributedLatencyResource.addAttribute(new DistributedLatencyCollectionAttribute(distributedLatencyResource, distributedLatencyType, responseTime));
    ServiceParameters params = new ServiceParameters(Collections.emptyMap());
    CollectionSetVisitor persister = m_persisterFactory.createPersister(params, repository, false, true, true);
    SingleResourceCollectionSet collectionSet = new SingleResourceCollectionSet(distributedLatencyResource, new Date());
    collectionSet.setStatus(CollectionStatus.SUCCEEDED);
    collectionSet.visit(persister);
}
Also used : SingleResourceCollectionSet(org.opennms.netmgt.collection.support.SingleResourceCollectionSet) CollectionSetVisitor(org.opennms.netmgt.collection.api.CollectionSetVisitor) PolledService(org.opennms.netmgt.poller.remote.PolledService) OnmsMonitoredService(org.opennms.netmgt.model.OnmsMonitoredService) Service(org.opennms.netmgt.config.poller.Service) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) File(java.io.File) Date(java.util.Date)

Example 13 with Service

use of org.opennms.netmgt.config.poller.Service in project opennms by OpenNMS.

the class PollerBackEndTest method addService.

private Service addService(Package pkg, String serviceName, int serviceInterval, String... parms) {
    // assume that parms are key then value pairs
    assertTrue(parms.length % 2 == 0);
    Service service = new Service();
    service.setName(serviceName);
    service.setInterval(Long.valueOf(serviceInterval));
    for (int i = 0; i < parms.length - 1; i += 2) {
        String key = parms[i];
        String value = parms[i + 1];
        addParameter(service, key, value);
    }
    pkg.addService(service);
    return service;
}
Also used : OnmsMonitoredService(org.opennms.netmgt.model.OnmsMonitoredService) Service(org.opennms.netmgt.config.poller.Service)

Example 14 with Service

use of org.opennms.netmgt.config.poller.Service in project opennms by OpenNMS.

the class DefaultPollerBackEnd method createPollerConfiguration.

private SimplePollerConfiguration createPollerConfiguration(String pollingPackageName) {
    final Package pkg = getPollingPackage(pollingPackageName);
    final ServiceSelector selector = m_pollerConfig.getServiceSelectorForPackage(pkg);
    final Collection<OnmsMonitoredService> services = m_monSvcDao.findMatchingServices(selector);
    final List<PolledService> configs = new ArrayList<PolledService>(services.size());
    LOG.debug("Found {} services in polling package {}", services.size(), pollingPackageName);
    for (final OnmsMonitoredService monSvc : services) {
        final Service serviceConfig = m_pollerConfig.getServiceInPackage(monSvc.getServiceName(), pkg);
        final long interval = serviceConfig.getInterval();
        final Map<String, Object> parameters = getParameterMap(serviceConfig);
        if (LOG.isTraceEnabled()) {
            for (Map.Entry<String, Object> entry : parameters.entrySet()) {
                LOG.trace("Service {} has parameter {} with type {} and value: {}", monSvc.getServiceName(), entry.getKey(), entry.getValue() != null ? entry.getValue().getClass().getCanonicalName() : "null", entry.getValue());
            }
        }
        configs.add(new PolledService(monSvc, parameters, new OnmsPollModel(interval)));
    }
    Collections.sort(configs);
    return new SimplePollerConfiguration(getConfigurationTimestamp(), configs.toArray(new PolledService[configs.size()]));
}
Also used : OnmsPollModel(org.opennms.netmgt.poller.remote.OnmsPollModel) ServiceSelector(org.opennms.netmgt.model.ServiceSelector) ArrayList(java.util.ArrayList) PolledService(org.opennms.netmgt.poller.remote.PolledService) OnmsMonitoredService(org.opennms.netmgt.model.OnmsMonitoredService) Service(org.opennms.netmgt.config.poller.Service) OnmsMonitoredService(org.opennms.netmgt.model.OnmsMonitoredService) PolledService(org.opennms.netmgt.poller.remote.PolledService) Package(org.opennms.netmgt.config.poller.Package) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 15 with Service

use of org.opennms.netmgt.config.poller.Service in project opennms by OpenNMS.

the class MockPollerConfig method addService.

public void addService(String name, long interval, ServiceMonitor monitor) {
    Service service = findService(m_currentPkg, name);
    if (service == null) {
        service = new Service();
        service.setName(name);
        service.setInterval(interval);
        m_currentPkg.addService(service);
        m_currentSvc = service;
    }
    addServiceMonitor(name, monitor);
}
Also used : OnmsMonitoredService(org.opennms.netmgt.model.OnmsMonitoredService) Service(org.opennms.netmgt.config.poller.Service)

Aggregations

Service (org.opennms.netmgt.config.poller.Service)22 Package (org.opennms.netmgt.config.poller.Package)11 OnmsMonitoredService (org.opennms.netmgt.model.OnmsMonitoredService)6 Test (org.junit.Test)5 PollerConfig (org.opennms.netmgt.config.PollerConfig)5 Parameter (org.opennms.netmgt.config.poller.Parameter)5 PollStatus (org.opennms.netmgt.poller.PollStatus)5 File (java.io.File)4 Rrd (org.opennms.netmgt.config.poller.Rrd)4 Downtime (org.opennms.netmgt.config.poller.Downtime)3 Filter (org.opennms.netmgt.config.poller.Filter)3 OnmsNode (org.opennms.netmgt.model.OnmsNode)3 MonitoredService (org.opennms.netmgt.poller.MonitoredService)3 ServiceMonitor (org.opennms.netmgt.poller.ServiceMonitor)3 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 InetAddress (java.net.InetAddress)2 HashMap (java.util.HashMap)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2