protected DsDef[] getDsDefs() { return p.getPd().getDsDefs(); }
@SuppressWarnings("unchecked") public Map<KeyType, String> getCollectMapping() { Map<KeyType, String> rawMap = (Map<KeyType, String>) getPd().getCollectMapping(); Map<KeyType, String> retValues = new HashMap<KeyType, String>(rawMap.size()); for(Map.Entry<KeyType, String> e: rawMap.entrySet()) { String value = jrds.Util.parseTemplate(e.getValue(), this); KeyType key = e.getKey(); if(key instanceof String) key = (KeyType) jrds.Util.parseTemplate((String) key, this); retValues.put(key, value); } return retValues; }
/** * Return true if is collect key was marked optionnal in the probe * description * * @param collect * @return */ public boolean isOptional(KeyType collect) { return getPd().isOptional(getCollectMapping().get(collect)); }
@Override public CacheStore create(Probe<?, ?> p) { String hostname = p.getHost().getName(); String probeName = p.getName(); if(!cache.containsKey(hostname)) cache.put(hostname, new HashMap<String, Map<String, Number>>()); Map<String, Map<String, Number>> probes = cache.get(hostname); if(!probes.containsKey(probeName)) probes.put(probeName, new HashMap<String, Number>(p.getPd().getDsDefs().length)); return new CacheStore(p, probes.get(probeName)); }
/** * The sample itself can be modified<br> * * @param sample * @param values */ public void modifySample(JrdsSample sample, Map<KeyType, ValueType> values) { for(Map.Entry<String, ProbeDesc.Joined> e: getPd().getHighlowcollectmap().entrySet()) { Long joined = joinCounter32(values.remove(e.getValue().keyhigh), values.remove(e.getValue().keylow)); if(joined != null) { Map<KeyType, String> mapping = getPd().getCollectMapping(); sample.remove(mapping.get(e.getValue().keyhigh)); sample.remove(mapping.get(e.getValue().keylow)); sample.put(e.getKey(), joined.doubleValue()); } } }
private String oneLine(Probe<?, ?> p) { ProbeDesc<?> pd = p.getPd(); String description = pd.getSpecific("description"); if(description == null) description = ""; return "| " + getSourceTypeLink(p, true) + " | " + description + " | " + classToLink(p.getClass()) + " | "; }
private void setAttributes(Map<String, ProbeDesc.DefaultBean> defaultBeans, JrdsElement probeNode, Probe<?, ?> p, Object... context) throws InvocationTargetException { // Resolve the beans for(JrdsElement attrNode: probeNode.getChildElementsByName("attr")) { String name = attrNode.getAttribute("name"); GenericBean bean = p.getPd().getBean(name); if(bean == null) { // Context[0] should be the host logger.error(context[0] + "/" + p.getPd().getName() + ": unknown bean '" + name + "'"); continue; } String textValue = Util.parseTemplate(attrNode.getTextContent(), context); logger.trace(Util.delayedFormatString("Found attribute %s with value %s", name, textValue)); bean.set(p, textValue); if(defaultBeans.containsKey(name)) { defaultBeans.remove(name); } } }
private boolean resolveDefaultBean(Probe<?, ?> p, List<Object> args, Map<String, String> properties, String beanName, String beanValue) { HostInfo host = p.getHost(); ProbeDesc<?> pd = p.getPd(); GenericBean bean = pd.getBean(beanName); String value;
/** * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse) */ public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/plain"); res.addHeader("Cache-Control", "no-cache"); ServletOutputStream out = res.getOutputStream(); ParamsBean params = getParamsBean(req); Probe<?, ?> probe = params.getProbe(); if(probe != null) { Period p = params.getPeriod(); ExtractInfo ei = ExtractInfo.get().make(p.getBegin(), p.getEnd()); DataProcessor dp = probe.extract(ei); for(String dsName: probe.getPd().getDs()) { try { out.print(dsName + " "); out.println(dp.getVariable(dsName, new Variable.AVERAGE()).value + " "); out.println(dp.getVariable(dsName, new Variable.MIN()).value + " "); out.println(dp.getVariable(dsName, new Variable.MAX()).value); } catch (IOException e) { logger.error("Probe " + probe + "unusable: " + e); } } } else { logger.error("Probe id provided " + params.getId() + " invalid"); } }
/** * Store the collected values in the sample * * @param oneSample */ public boolean injectSample(JrdsSample oneSample, Map<KeyType, ValueType> sampleVals) { log(Level.TRACE, "Collected values: %s", sampleVals); if(getUptime() * pd.getUptimefactor() < pd.getHeartBeatDefault()) { log(Level.INFO, "uptime too low: %.0f", getUptime() * pd.getUptimefactor()); return false; } // Set the default values that might be defined in the probe description for(Map.Entry<String, Double> e: getPd().getDefaultValues().entrySet()) { oneSample.put(e); } Map<?, String> nameMap = getCollectMapping(); log(Level.TRACE, "Collect keys: %s", nameMap); Map<KeyType, Number> filteredSamples = filterValues(sampleVals); log(Level.TRACE, "Filtered values: %s", filteredSamples); for(Map.Entry<KeyType, Number> e: filteredSamples.entrySet()) { String dsName = nameMap.get(e.getKey()); if(dsName != null) { oneSample.put(dsName, e.getValue()); } else { log(Level.TRACE, "Dropped entry: %s", e.getKey()); } } modifySample(oneSample, sampleVals); return true; }
/** * Check the final status of the probe. It must be called once before an * probe can be used * * Open the rrd backend of the probe. it's created if it's needed * */ public boolean checkStore() { ProbeDesc<KeyType> pd = getPd(); if(pd == null) { log(Level.ERROR, "Missing Probe description"); return false; } if(getHost() == null) { log(Level.ERROR, "Missing host"); return false; } // Name can be set by other means if(name == null) name = parseTemplate(pd.getProbeName()); finished = mainStore.checkStoreFile(archives); return finished; }
@Test public void testParseTemplate5() throws Exception { Probe<String, Number> testProbe = GenerateProbe.fillProbe(new GenerateProbe.EmptyProbe(), testFolder, GenerateProbe.ChainedMap.start()); GenericBean b1 = new GenericBean.CustomBean("one"); b1.set(testProbe, "one"); testProbe.getPd().addBean(b1); GenericBean b2 = new GenericBean.CustomBean("two"); testProbe.getPd().addBean(b2); String parsed = Util.parseTemplate("${attr.one} ${attr.two} ${attr.two.signature} ${attr.three} ${probename}", testProbe); Assert.assertEquals("one ${attr.two} ${attr.two.signature} EmptyProbe", parsed); }
for(String graphName: p.getPd().getGraphClasses()) { try { GraphDesc gd = graphDescMap.get(graphName);
@Test public void readDatasourcesNames() throws Exception { GenerateProbe.ChainedMap<Object> factoryArgs = GenerateProbe.ChainedMap.start(1).set("rrdfile", rrdfile.getCanonicalPath()); GenerateProbe.ChainedMap<Object> args = GenerateProbe.ChainedMap.start(2) .set(StoreFactory.class, new RRDToolStoreFactory()) .set(GenerateProbe.FACTORYCONFIG, factoryArgs); Probe<?,?> p = GenerateProbe.fillProbe(new GenerateProbe.EmptyProbe(), testFolder, args); p.getPd().add("speed", DsType.GAUGE); p.getPd().add("weight", DsType.GAUGE); Assert.assertTrue(p.checkStore()); Period period = new Period("1999-03-07T13:00:00", "1999-03-07T13:15:00"); DataProcessor dp = p.extract(ExtractInfo.get().make(period.getBegin(), period.getEnd())); String[] dsNames = dp.getSourceNames(); Assert.assertEquals("data source weight not found", "weight", dsNames[0]); Assert.assertEquals("data source speed not found", "speed", dsNames[1]); Assert.assertEquals("Missing last values", 2, p.getMainStore().getLastValues().size()); }
@SuppressWarnings("unchecked") @Before public void prepare() throws Exception { pm = Tools.makePm(testFolder); hl = new HostsList(pm); GenerateProbe.ChainedMap<Object> args = GenerateProbe.ChainedMap.start().set(PropertiesManager.class, pm); p = GenerateProbe.quickProbe(testFolder, args); p.getPd().add("data", DsType.GAUGE); Assert.assertTrue("Failed to create probe " + p.getMainStore().getPath(), p.checkStore()); }
@Test public void testCreate() throws Exception { @SuppressWarnings("unchecked") Probe<?, ?> p = GenerateProbe.quickProbe(testFolder, GenerateProbe.ChainedMap.start(0)); p.getPd().add("test", DsType.COUNTER); Assert.assertTrue("Probe file creation failed", p.checkStore()); Extractor e = p.getMainStore().getExtractor(); e.addSource("test", "test"); String[] dsNames = e.getNames(); Assert.assertEquals("data source test not found", "test", dsNames[0]); e.release(); }
@Test public void testFill() throws Exception { @SuppressWarnings("unchecked") Probe<?, ?> p = GenerateProbe.quickProbe(testFolder); p.setStep(30); p.getPd().add("test", DsType.GAUGE); Assert.assertTrue("Probe file creation failed", p.checkStore()); Extractor e = p.getMainStore().getExtractor(); e.addSource("test", "test"); String[] dsNames = e.getNames(); Assert.assertEquals("data source test not found", "test", dsNames[0]); long start = p.getLastUpdate().getTime(); for(int i = 1; i <= 30; i++) { JrdsSample s = p.newSample(); long sampletime = i * p.getStep() * 1000 + start; sampletime = (sampletime) - (sampletime % (p.getStep() * 1000)); s.setTime(new Date(sampletime)); s.put("test", i); p.getMainStore().commit(s); } ExtractInfo ei = ExtractInfo.get().make(new Date(start), new Date(start + 30 * p.getStep() * 1000)); DataProcessor dp = p.extract(ei); double[][] values = dp.getValues(); for(int i = 1; i <= 30; i++) { // Check raw values Assert.assertEquals("Wrong values stored", i, values[0][i], 1e-10); long sampletime = i * p.getStep() * 1000 + start; sampletime = (sampletime) - (sampletime % (p.getStep() * 1000)); } }
Assert.assertEquals("localhost", p.getPd().getBean("hostInfo").get(p)); logger.trace(p.getName());
@Test public void testDsreplace() throws Exception { JrdsDocument d = Tools.parseRessource("dsoverride.xml"); PropertiesManager pm = Tools.makePm(testFolder); HostBuilder hb = new HostBuilder(); ProbeFactory pf = new MokeProbeFactory(); hb.setProbeFactory(pf); hb.setPm(pm); hb.setTimers(Tools.getSimpleTimerMap()); HostInfo host = new HostInfo("testDsreplace"); host.setHostDir(pm.rrddir); Probe<?, ?> p = hb.makeProbe(d.getRootElement().getElementbyName("probe"), host, null); ProbeDesc<?> pd = p.getPd(); Assert.assertNotNull(pd); Assert.assertEquals(1, pd.getSize()); Assert.assertNotSame(pf.getProbeDesc(pd.getName()), pd.getSize()); }