@Test public void testFullConfigpath() throws Exception { PropertiesManager localpm = Tools.makePm(); JrdsDocument host = new JrdsDocument(Tools.dbuilder.newDocument()); host.doRootElement("host", "name=name"); ConfigObjectFactory conf = new ConfigObjectFactory(localpm, localpm.extensionClassLoader); conf.getNodeMap(ConfigType.HOSTS).put("name", host); Assert.assertNotNull( "Probedesc not build", conf.setHostMap(Tools.getSimpleTimerMap()).get("name")); }
public HostInfo makeHost(JrdsDocument n) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, ClassNotFoundException { JrdsElement hostNode = n.getRootElement(); String hostName = hostNode.getAttribute("name"); String dnsHostname = hostNode.getAttribute("dnsName"); if (hostName == null) { return null; } HostInfo host = null; if (dnsHostname != null) { host = new HostInfo(hostName, dnsHostname); } else { host = new HostInfo(hostName); } host.setHostDir(new File(pm.rrddir, host.getName())); String hidden = hostNode.getAttribute("hidden"); host.setHidden(hidden != null && Boolean.parseBoolean(hidden)); Map<String, Set<String>> collections = new HashMap<String, Set<String>>(); parseFragment(hostNode, host, collections, null); return host; }
@Test public void testNewProbe() throws InvocationTargetException, IllegalArgumentException, IllegalAccessException, IOException { PropertiesManager localpm = Tools.makePm(); HostBuilder hb = new HostBuilder(); // Generate a probe with a bean hostInfo with a default value of ${host} hb.setProbeFactory( new MokeProbeFactory() { @Override public Probe<?, ?> makeProbe(String type) { logger.trace(type); ProbeDesc pd = generateProbeDesc(type); try { pd.setProbeClass(MokeProbeBean.class); } catch (InvocationTargetException e1) { throw new RuntimeException(e1); } Probe<?, ?> p = new MokeProbeBean(pd); try { pd.addDefaultArg("hostInfo", "${host}"); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } return p; } }); hb.setPm(localpm); hb.setTimers(Tools.getSimpleTimerMap()); HostInfo host = new HostInfo("localhost"); host.setHostDir(testFolder.getRoot()); JrdsDocument probeNode = new JrdsDocument(Tools.dbuilder.newDocument()); probeNode.doRootElement("probe", "type=probetype"); Probe<?, ?> p = hb.makeProbe(probeNode.getRootElement(), host, null); Assert.assertEquals( "localhost", p.getPd().getBeanMap().get("hostInfo").getReadMethod().invoke(p)); logger.trace(p.getName()); }
@Test public void testConnectionInfo() throws Exception { PropertiesManager pm = Tools.makePm(); HostBuilder hb = new HostBuilder(); hb.setPm(pm); hb.setClassLoader(this.getClass().getClassLoader()); JrdsDocument cnxdoc = new JrdsDocument(Tools.dbuilder.newDocument()); cnxdoc .doRootElement("host") .addElement("connection", "type=jrds.probe.JMXConnection") .addElement("attr", "name=port") .setTextContent("8999"); for (ConnectionInfo ci : hb.makeConnexion(cnxdoc.getRootElement(), new HostInfo("localhost"))) { logger.trace(ci.getName()); StarterNode sn = new StarterNode() {}; ci.register(sn); JMXConnection cnx = sn.find(JMXConnection.class); Assert.assertEquals("Attributed not setted", new Integer(8999), cnx.getPort()); } }
public Filter makeFilter(JrdsDocument n) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { JrdsElement root = n.getRootElement(); JrdsElement name = root.getElementbyName("name"); if (name == null) return null; FilterXml f = new FilterXml(name.getTextContent()); setMethod(root.getChildElementsByName("path"), f, "addPath", String.class); setMethod(root.getChildElementsByName("tag"), f, "addTag", String.class); setMethod(root.getChildElementsByName("qualifiedname"), f, "addGraph", String.class); doACL(f, n, root); logger.trace(Util.delayedFormatString("Filter loaded: %s", f.getName())); return f; }
private void parseFragment( JrdsElement fragment, HostInfo host, Map<String, Set<String>> collections, Map<String, String> properties) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { // Find the connection for this host // Will the registered latter, in the starter node, one for each timer for (ConnectionInfo cnx : makeConnexion(fragment, host)) { host.addConnection(cnx); } for (JrdsElement tagElem : fragment.getChildElementsByName("tag")) { try { logger.trace(Util.delayedFormatString("adding tag %s to %s", tagElem, host)); setMethod(tagElem, host, "addTag"); } catch (InstantiationException e) { } } for (JrdsElement collectionNode : fragment.getChildElementsByName("collection")) { String name = collectionNode.getAttribute("name"); Set<String> set = new HashSet<String>(); for (JrdsElement e : collectionNode.getChildElementsByName("element")) { set.add(e.getTextContent()); } collections.put(name, set); } for (JrdsElement macroNode : fragment.getChildElementsByName("macro")) { String name = macroNode.getAttribute("name"); Macro m = macrosMap.get(name); logger.trace(Util.delayedFormatString("Adding macro %s: %s", name, m)); if (m != null) { Map<String, String> macroProps = makeProperties(macroNode); Map<String, String> newProps = new HashMap<String, String>( (properties != null ? properties.size() : 0) + macroProps.size()); if (properties != null) newProps.putAll(properties); newProps.putAll(macroProps); JrdsDocument hostdoc = (JrdsDocument) fragment.getOwnerDocument(); // Make a copy of the document fragment JrdsNode newDf = JrdsNode.build(hostdoc.importNode(m.getDf(), true)); JrdsElement macrodef = JrdsNode.build(newDf.getFirstChild()); parseFragment(macrodef, host, collections, newProps); } else { logger.error("Unknown macro:" + name); } } for (JrdsElement forNode : fragment.getChildElementsByName("for")) { Map<String, String> forattr = forNode.attrMap(); String iterprop = forattr.get("var"); Collection<String> set = null; String name = forNode.attrMap().get("collection"); if (name != null) set = collections.get(name); else if (forattr.containsKey("min") && forattr.containsKey("max") && forattr.containsKey("step")) { int min = Util.parseStringNumber(forattr.get("min"), Integer.MAX_VALUE); int max = Util.parseStringNumber(forattr.get("max"), Integer.MIN_VALUE); int step = Util.parseStringNumber(forattr.get("step"), Integer.MIN_VALUE); if (min > max || step <= 0) { logger.error("invalid range from " + min + " to " + max + " with step " + step); break; } set = new ArrayList<String>((max - min) / step + 1); for (int i = min; i <= max; i += step) { set.add(Integer.toString(i)); } } if (set != null) { logger.trace(Util.delayedFormatString("for using %s", set)); for (String i : set) { Map<String, String> temp; if (properties != null) { temp = new HashMap<String, String>(properties.size() + 1); temp.putAll(properties); temp.put(iterprop, i); } else { temp = Collections.singletonMap(iterprop, i); } parseFragment(forNode, host, collections, temp); } } else { logger.error("Invalid host configuration, collection " + name + " not found"); } } for (JrdsElement probeNode : fragment.getChildElements()) { if (!"probe".equals(probeNode.getNodeName()) && !"rrd".equals(probeNode.getNodeName())) continue; try { makeProbe(probeNode, host, properties); } catch (Exception e) { logger.error("Probe creation failed for host " + host.getName() + ": "); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(buffer)); logger.error(buffer); } } }