public HostStarter getHost(HostInfo info) { String hostName = info.getName(); HostStarter starter = hostList.get(hostName); if(starter == null) { starter = new HostStarter(info); hostList.put(hostName, starter); starter.setTimeout(getTimeout()); starter.setStep(getStep()); starter.setParent(this); } return starter; }
@Override public Object call() throws Exception { log(Level.DEBUG, "Collect all stats for host %s", host.getName()); String collectName = Timer.this.name + "/" + "JrdsCollect-" + host.getName(); host.setRunningname(collectName); host.collectAll(); host.setRunningname(collectName + ":notrunning"); return null; }
/** * Two hosts are equals if they are equals and their parents too * * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (!(obj instanceof HostStarter)) { return false; } HostStarter other = (HostStarter) obj; boolean parentEquals; if (getParent() != null) { parentEquals = getParent().equals(other.getParent()); } else { parentEquals = other.getParent() == null; } return host.equals(other.getHost()) && parentEquals; }
public void collectAll() { log(Level.DEBUG, "Starting collect"); Timer timer = (Timer) getParent(); long start = System.currentTimeMillis(); startCollect(); String oldThreadName = Thread.currentThread().getName(); for(Probe<?, ?> probe: allProbes) { if(!isCollectRunning()) break; long duration = (System.currentTimeMillis() - start) / 1000; if(duration > (probe.getStep() / 2)) { log(Level.ERROR, "Collect too slow: %ds for timer %s", duration, timer); break; } log(Level.TRACE, "Starting collect for %s", probe); log(Level.DEBUG, "Collect all stats for host " + host.getName()); setRunningname(oldThreadName + "/" + probe.getName()); probe.collect(); setRunningname(oldThreadName + ":finished"); } stopCollect(); long end = System.currentTimeMillis(); float elapsed = (end - start) / 1000f; log(Level.DEBUG, "Collect time for %s: %fs", host.getName(), elapsed); }
@Test public void testConnectTwice() throws Exception { MockHttpServer server = new MockHttpServer(false); server.addResourceHandler(staticFiles); server.start(); HttpClientStarter cnx = new HttpClientStarter(); HostStarter localhost = addConnection(cnx); localhost.find(Resolver.class).doStart(); cnx.doStart(); TestHttpProbe p = new TestHttpProbe(); p.setMainStore(new RrdDbStoreFactory(), empty); p.setHost(localhost); p.setPort(server.getURI().toURL().getPort()); p.configure(); p.checkStore(); localhost.addProbe(p); localhost.getParent().startCollect(); // Run twice, to detect failure management in the probe localhost.collectAll(); shouldFail = false; Thread.sleep(1500); localhost.collectAll(); Assert.assertTrue("Didn't try to collect", p.collected); server.stop(); }
@SuppressWarnings("unused") private void doTest(String proto, int port) throws Exception { mbi = new JrdsMBeanInfo(proto, "localhost", port); HostStarter host = new HostStarter(new HostInfo("localhost")) { public boolean isCollectRunning() { return true; } }; host.setTimeout(1); JMXConnection cnx = getCnx(proto, port); host.registerStarter(new SocketFactory()); host.registerStarter(new JmxSocketFactory()); host.registerStarter(cnx); host.configureStarters(new PropertiesManager()); host.startCollect(); Assert.assertTrue("JMX Connection failed to start", cnx.isStarted()); Assert.assertNotNull("Failed to read uptime", cnx.setUptime()); if(false) enumerate((NativeJmxSource)cnx.getConnection()); }
private HostStarter addConnection(Starter cnx) throws IOException { String truststore = getClass().getClassLoader().getResource("localhost.jks").getFile(); PropertiesManager pm = Tools.makePm(testFolder, "timeout=1", "collectorThreads=1", "ssl.protocol=TLS", "ssl.strict=true", "ssl.truststore=" + truststore, "ssl.trustpassword=123456"); HostStarter localhost = new HostStarter(new HostInfo("localhost")); Timer t = Tools.getDefaultTimer(); localhost.setParent(t); localhost.getHost().setHostDir(testFolder.getRoot()); t.registerStarter(new SSLStarter()); t.registerStarter(new SocketFactory()); t.configureStarters(pm); localhost.registerStarter(cnx); cnx.configure(pm); return localhost; }
@Test public void testHighLow() throws Exception { ProbeDesc<String> pd = new ProbeDesc<String>(); pd.setName("empty"); pd.setProbeName("empty"); Map<String, Object> dsMap = new HashMap<String, Object>(); dsMap.put("dsName", "ds0"); dsMap.put("dsType", DsType.COUNTER); dsMap.put("collecthigh", "high"); dsMap.put("collectlow", "low"); pd.add(dsMap); GenerateProbe.ChainedMap<Object> args = GenerateProbe.ChainedMap.start(); args.set(ProbeDesc.class, pd).set(Probe.class, DummyProbe.class); @SuppressWarnings("unchecked") MokeProbe<String, Number> p = (MokeProbe<String, Number>) GenerateProbe.quickProbe(testFolder, args); HostStarter host = new HostStarter(new HostInfo("DummyHost")); host.setParent(Tools.getDefaultTimer()); host.getHost().setHostDir(testFolder.newFolder("testDefault")); p.setHost(host); p.configure(); Assert.assertTrue("Failed to create storage", p.checkStore()); Map<String, Number> val = new HashMap<String, Number>(); long high = 255L; long low = 64L; val.put("high", high); val.put("low", low); p.injectValues(val); p.collect(); Assert.assertEquals("32 + 32 to 64 failed", (high << 32) + low, p.getLastValues().get("ds0").doubleValue(), 0.1); }
public void configure() throws InvocationTargetException { Map<String, String> empty = Collections.emptyMap(); setMainStore(new RrdDbStoreFactory(), empty); ProbeDesc<String> pd = new ProbeDesc<>(); pd.setName("DummyProbe"); pd.setProbeName("dummyprobe"); setPd(pd); if(getHost() == null) { HostInfo host = new HostInfo("DummyHost"); host.setHostDir(new File("tmp")); setHost(new HostStarter(host)); } Map<String, Object> dsMap = new HashMap<String, Object>(); dsMap.put("dsName", "ds0"); dsMap.put("dsType", DsType.COUNTER); dsMap.put("collectKey", "/jrdsstats/stat[@key='a']/@value"); pd.add(dsMap); dsMap = new HashMap<String, Object>(); dsMap.put("dsName", "ds1"); dsMap.put("dsType", DsType.COUNTER); dsMap.put("collectKey", "/jrdsstats/stat[@key='b']/@value"); pd.add(dsMap); dsMap = new HashMap<String, Object>(); dsMap.put("dsName", "ds2"); dsMap.put("dsType", DsType.COUNTER); pd.add(dsMap); }
Set<Class<? extends Starter>> timerStarterClasses = new HashSet<Class<? extends Starter>>(); for(HostStarter host: timer.getAllHosts()) { hostsTags.addAll(host.getTags()); host.configureStarters(pm); for(Probe<?, ?> p: host.getAllProbes()) { p.configureStarters(pm); try {
HostStarter host = new HostStarter(new HostInfo("moke")); p.setHost(host); p.setPd(pd); host.registerStarter(new XmlProvider()); long l; String uptimeXml;
public void setHost(HostStarter monitoredHost) { this.monitoredHost = monitoredHost.getHost(); setParent(monitoredHost); }
@Test public void instantiate() throws InvocationTargetException { Map<String, String> empty = Collections.emptyMap(); ConnectionInfo ci = new ConnectionInfo(JdbcConnection.class, "jrds.probe.jdbc.JdbcConnection", Collections.emptyList(), empty); HostInfo hi = new HostInfo("localhost"); hi.addConnection(ci); HostStarter hs = new HostStarter(hi); ci.register(hs); Assert.assertEquals("connection not found", "jrds.probe.jdbc.JdbcConnection@localhost", hs.find(JdbcConnection.class).toString()); }
public HostStarter(HostInfo host) { super(); this.host = host; this.runningname = host.getName() + ":notrunning"; registerStarter(new Resolver(host.getDnsName())); }
shost.addProbe(p); } else { return null;
@Test public void testConnectSSL() throws Exception { MockHttpServer server = new MockHttpServer(true); server.addResourceHandler(staticFiles); server.start(); HttpClientStarter cnx = new HttpClientStarter(); HostStarter localhost = addConnection(cnx); localhost.find(Resolver.class).doStart(); localhost.find(SSLStarter.class).doStart(); cnx.doStart(); TestHttpProbe p = new TestHttpProbe(); p.setMainStore(new RrdDbStoreFactory(), empty); p.setHost(localhost); p.setPort(server.getURI().toURL().getPort()); p.setScheme("https"); p.configure(); p.checkStore(); localhost.addProbe(p); localhost.getParent().startCollect(); Assert.assertTrue(p.find(SSLStarter.class).isStarted()); shouldFail = false; localhost.collectAll(); Assert.assertTrue("Didn't try to collect", p.collected); server.stop(); }
MokeProbe<String, Number> p = (MokeProbe<String, Number>) GenerateProbe.quickProbe(testFolder, args); HostStarter host = new HostStarter(new HostInfo("DummyHost")); host.setParent(Tools.getDefaultTimer()); host.getHost().setHostDir(testFolder.newFolder("testDefault")); p.setHost(host);
@Test public void manageArgs() throws Exception { URL url = getClass().getResource("/ressources/xmldata.xml"); List<Object> args = new ArrayList<Object>(1); args.add("a"); args.add("/jrdsstats/stat[@key='a']/@value"); HttpXml p = new jrds.probe.HttpXml() { @Override public String getName() { return "Moke"; } }; HostStarter host = new HostStarter(new HostInfo("moke")); p.setHost(host); p.setPd(pd); p.configure(url, args); Map<String, String> keys = p.getCollectMapping(); logger.trace("Collect keys: " + p.getCollectMapping()); logger.trace("Collect strings: " + pd.getCollectMapping()); Assert.assertTrue(keys.containsKey("/jrdsstats/stat[@key='%1$s']/@value")); Assert.assertTrue(keys.containsKey("/jrdsstats/stat[@key='b']/@value")); Assert.assertTrue(keys.containsKey("c")); }
public static Object makeKey(StarterNode node) { HostInfo host; if(node instanceof HostStarter) host = ((HostStarter) node).getHost(); else if(node instanceof Probe<?, ?>) { Probe<?, ?> p = (Probe<?, ?>) node; host = p.getHost(); } else { return null; } return "resolver:" + host.getDnsName(); }