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;
}
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);
}
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;
}
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()]));
}
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);
}
Aggregations