use of org.opennms.netmgt.config.agents.AgentResponse in project opennms by OpenNMS.
the class AgentConfigurationResourceTest method testSimpleFilter.
@Test
public void testSimpleFilter() throws Exception {
final InetAddress oneNinetyTwo = addr("192.168.0.1");
final List<InetAddress> addresses = Arrays.asList(oneNinetyTwo);
m_filterDao.setActiveIPAddressList(addresses);
final OnmsNode node = new OnmsNode();
node.setId(1);
node.setForeignSource("foo");
node.setForeignId("bar");
node.setLabel("foo");
node.setSysObjectId(".1.2.3.4.5");
final OnmsIpInterface iface = new OnmsIpInterface(oneNinetyTwo, node);
final OnmsServiceType serviceType = new OnmsServiceType("SNMP");
final OnmsMonitoredService service = new OnmsMonitoredService(iface, serviceType);
m_monitoredServiceDao.setMatching(Arrays.asList(service));
final Response response = m_configResource.getAgentsJson("example1", "SNMP");
assertEquals(200, response.getStatus());
final Object entity = response.getEntity();
assertNotNull(entity);
assertTrue(entity instanceof List<?>);
@SuppressWarnings("unchecked") final List<AgentResponse> agentResponses = (List<AgentResponse>) entity;
System.err.println(agentResponses);
assertEquals(1, agentResponses.size());
assertTrue(agentResponses.get(0).getAddress().isPresent());
assertEquals(oneNinetyTwo, agentResponses.get(0).getAddress().get());
assertTrue(agentResponses.get(0).getPort().isPresent());
assertEquals(1161, agentResponses.get(0).getPort().get().intValue());
assertEquals(".1.2.3.4.5", agentResponses.get(0).getParameters().get("sysObjectId"));
assertEquals("1", agentResponses.get(0).getParameters().get("nodeId"));
assertEquals("foo", agentResponses.get(0).getParameters().get("foreignSource"));
assertEquals("bar", agentResponses.get(0).getParameters().get("foreignId"));
}
use of org.opennms.netmgt.config.agents.AgentResponse in project opennms by OpenNMS.
the class AgentConfigurationResource method getResponses.
protected List<AgentResponse> getResponses(final String filterName, final String serviceName) throws ConfigurationResourceException {
LOG.debug("getAgentsForService(): filterName={}, serviceName={}", filterName, serviceName);
if (filterName == null || serviceName == null) {
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).type(MediaType.TEXT_PLAIN).entity("You must specify a filter name and service name!").build());
}
final Filter filter = m_collectdConfigurationResource.get().getFilter(filterName);
if (filter == null) {
LOG.warn("No filter matching {} could be found.", filterName);
throw new WebApplicationException(Status.NOT_FOUND);
}
final List<InetAddress> addresses = m_filterDao.getActiveIPAddressList(filter.getContent());
LOG.debug("Matched {} IP addresses for filter {}", addresses == null ? 0 : addresses.size(), filterName);
if (addresses == null || addresses.size() == 0) {
return Collections.emptyList();
}
final CriteriaBuilder builder = new CriteriaBuilder(OnmsMonitoredService.class);
builder.createAlias("ipInterface", "iface");
builder.createAlias("serviceType", "type");
builder.createAlias("iface.node", "node");
builder.in("iface.ipAddress", addresses);
builder.eq("type.name", serviceName);
final List<OnmsMonitoredService> services = m_monitoredServiceDao.findMatching(builder.toCriteria());
int defaultPort = -1;
// TODO: We shouldn't have to hardcode like this; what's the right way to know the port to return?
final CollectdConfiguration collectdConfiguration = m_collectdConfigurationResource.get();
org.opennms.netmgt.config.collectd.Package pack = collectdConfiguration.getPackage(filterName);
if (pack == null) {
for (final org.opennms.netmgt.config.collectd.Package p : collectdConfiguration.getPackages()) {
if (filterName.equals(p.getFilter().getName())) {
pack = p;
break;
}
}
}
if (pack != null) {
final Service svc = pack.getService(serviceName);
final String port = svc.getParameter("port");
if (port != null) {
try {
defaultPort = Integer.valueOf(port);
} catch (final NumberFormatException e) {
LOG.debug("Unable to turn port {} from service {} into a number.", port, serviceName);
}
}
}
final List<AgentResponse> responses = new ArrayList<>();
for (final OnmsMonitoredService service : services) {
final InetAddress ipAddress = service.getIpAddress();
final OnmsIpInterface iface = service.getIpInterface();
OnmsNode node = null;
if (iface != null) {
node = iface.getNode();
}
final Map<String, String> parameters = new TreeMap<String, String>();
// all service parameters from collectd configuration to parameters map
for (Parameter eachParameter : pack.getService(serviceName).getParameters()) {
parameters.put(eachParameter.getKey(), eachParameter.getValue());
}
int port = defaultPort;
if ("SNMP".equals(serviceName)) {
final String sysObjectId = node == null ? null : node.getSysObjectId();
if (sysObjectId != null) {
parameters.put("sysObjectId", sysObjectId);
}
OnmsMonitoringLocation location = (node == null) ? null : node.getLocation();
String locationName = (location == null) ? null : location.getLocationName();
final SnmpAgentConfig config = m_agentConfigFactory.getAgentConfig(ipAddress, locationName);
if (config != null) {
port = config.getPort();
}
}
if (node != null) {
if (node.getNodeId() != null && !node.getNodeId().trim().isEmpty()) {
parameters.put("nodeId", node.getNodeId());
}
if (node.getForeignSource() != null && !node.getForeignSource().trim().isEmpty()) {
parameters.put("foreignSource", node.getForeignSource());
}
if (node.getForeignId() != null && !node.getForeignId().trim().isEmpty()) {
parameters.put("foreignId", node.getForeignId());
}
}
responses.add(new AgentResponse(ipAddress, port, service.getServiceName(), parameters));
}
return responses;
}
Aggregations