Skip to content

Commit

Permalink
First implementation of passive probes
Browse files Browse the repository at this point in the history
  • Loading branch information
fbacchella committed Aug 22, 2013
1 parent 1c09873 commit 21db61c
Show file tree
Hide file tree
Showing 15 changed files with 406 additions and 27 deletions.
1 change: 1 addition & 0 deletions dtd/host.dtd
Expand Up @@ -31,6 +31,7 @@
label CDATA #IMPLIED
connection CDATA #IMPLIED
timer CDATA #IMPLIED
listener CDATA #IMPLIED
>
<!ELEMENT arg (#PCDATA)>
<!ATTLIST arg
Expand Down
10 changes: 10 additions & 0 deletions dtd/listener.dtd
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT listener (attr*)>
<!ATTLIST listener
class CDATA #REQUIRED
name CDATA #IMPLIED
>
<!ELEMENT attr (#PCDATA)>
<!ATTLIST attr
name CDATA #REQUIRED
>
5 changes: 5 additions & 0 deletions junit/jrds/configuration/TestDtd.java
Expand Up @@ -63,4 +63,9 @@ public void checkFilter() throws Exception {
Tools.parseRessource("view1.xml");
}

@Test
public void checkListener() throws Exception {
Tools.parseRessource("configfull/listener.xml");
}

}
67 changes: 67 additions & 0 deletions junit/jrds/starter/ListenerTest.java
@@ -0,0 +1,67 @@
package jrds.starter;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.BeforeClass;
import org.junit.Test;

import jrds.Tools;
import jrds.factories.ProbeBean;
import jrds.probe.PassiveProbe;

public class ListenerTest {
@ProbeBean({"dummy"})
public static final class PassiveListener extends Listener {

@Override
public void register(PassiveProbe p) {
}

public void setDummy(String dummy) {

}

public String getDummy() {
return "dummy";
}

@Override
public void listen() throws Exception {
}

@Override
protected String identifyHost(Object message) {
// TODO Auto-generated method stub
return null;
}

@Override
protected String identifyProbe(Object message) {
// TODO Auto-generated method stub
return null;
}

@Override
protected String getHost(PassiveProbe pp) {
// TODO Auto-generated method stub
return null;
}

};

static Logger logger = Logger.getLogger(ListenerTest.class);

@BeforeClass
static public void configure() throws Exception {
Tools.configure();

logger.setLevel(Level.TRACE);
Tools.setLevel(new String[] {StarterNode.class.toString(), Starter.class.toString()}, logger.getLevel());
}

@Test
public void test1() {
PassiveProbe pp = new PassiveProbe();
pp.setListener(new PassiveListener());
}
}
6 changes: 6 additions & 0 deletions junit/ressources/configfull/listener.xml
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE listener PUBLIC "-//jrds//DTD Listener//EN"
"urn:jrds:listner">
<listener class="jrds.starter.ListnerTest.PassiveListener">
<attr name="dummy">dummy</attr>
</listener>
5 changes: 5 additions & 0 deletions src/jrds/HostsList.java
Expand Up @@ -22,6 +22,7 @@
import jrds.factories.ProbeMeta;
import jrds.graphe.Sum;
import jrds.starter.HostStarter;
import jrds.starter.Listener;
import jrds.starter.Starter;
import jrds.starter.StarterNode;
import jrds.webapp.ACL;
Expand Down Expand Up @@ -127,6 +128,10 @@ public void configure(PropertiesManager pm) {
conf.setGraphDescMap();
conf.setProbeDescMap();
conf.setMacroMap();
for(Listener<?,?> l: conf.setListenerMap().values()) {
registerStarter(l);
topStarters.add(l);
}

Set<String> hostsTags = new HashSet<String>();
conf.setHostMap(timers);
Expand Down
53 changes: 29 additions & 24 deletions src/jrds/Probe.java
Expand Up @@ -418,33 +418,38 @@ public Map<KeyType, String> getCollectMapping() {
private boolean updateSample(Sample oneSample) {
if(isCollectRunning()) {
Map<KeyType, ValueType> sampleVals = getNewSampleValues();
if (sampleVals != null) {
log(Level.TRACE, "Collected values: %s", sampleVals);
if(getUptime() * pd.getUptimefactor() >= pd.getHeartBeatDefault()) {
//Set the default values that might be defined in the probe description
for(Map.Entry<String, Double> e: getPd().getDefaultValues().entrySet()) {
oneSample.setValue(e.getKey(), e.getValue());
return injectSample(oneSample, sampleVals);
}
return false;
}

public boolean injectSample(Sample oneSample, Map<KeyType, ValueType> sampleVals) {
if (sampleVals != null) {
log(Level.TRACE, "Collected values: %s", sampleVals);
if(getUptime() * pd.getUptimefactor() >= pd.getHeartBeatDefault()) {
//Set the default values that might be defined in the probe description
for(Map.Entry<String, Double> e: getPd().getDefaultValues().entrySet()) {
oneSample.setValue(e.getKey(), e.getValue());
}
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());
double value = e.getValue().doubleValue();
if (dsName != null) {
oneSample.setValue(dsName, value);
}
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());
double value = e.getValue().doubleValue();
if (dsName != null) {
oneSample.setValue(dsName, value);
}
else {
log(Level.TRACE, "Dropped entry: %s", e.getKey());
}
else {
log(Level.TRACE, "Dropped entry: %s", e.getKey());
}
modifySample(oneSample, sampleVals);
return true;
}
else {
log(Level.INFO, "uptime too low: %f", getUptime() * pd.getUptimefactor());
}
modifySample(oneSample, sampleVals);
return true;
}
else {
log(Level.INFO, "uptime too low: %f", getUptime() * pd.getUptimefactor());
}
}
return false;
Expand Down
16 changes: 14 additions & 2 deletions src/jrds/configuration/ConfigObjectFactory.java
Expand Up @@ -17,6 +17,7 @@
import jrds.factories.ProbeFactory;
import jrds.factories.xml.JrdsDocument;
import jrds.graphe.Sum;
import jrds.starter.Listener;
import jrds.starter.Timer;

import org.apache.log4j.Logger;
Expand All @@ -27,6 +28,7 @@ public class ConfigObjectFactory {
private ProbeFactory pf;
private ClassLoader cl = this.getClass().getClassLoader();
private Map<String, GraphDesc> graphDescMap = Collections.emptyMap();
private Map<String, Listener<?, ?>> listenerMap = Collections.emptyMap();
Map<String, Macro> macrosmap = Collections.emptyMap();
private final jrds.PropertiesManager pm;
private Loader load = null;
Expand Down Expand Up @@ -58,7 +60,7 @@ private void init() {

if(pm.configdir !=null)
load.importDir(pm.configdir);

load.done();
}

Expand Down Expand Up @@ -136,6 +138,7 @@ public Map<String, HostInfo> setHostMap(Map<String, Timer> timers) {
ob.setProbeFactory(pf);
ob.setPm(pm);
ob.setTimers(timers);
ob.setListeners(listenerMap);
Map<String, HostInfo> hostsMap = getObjectMap(ob, nodemap);
logger.debug(jrds.Util.delayedFormatString("Hosts configured: %s", hostsMap.keySet()));
return hostsMap;
Expand Down Expand Up @@ -167,8 +170,17 @@ public Map<String, Tab> setTabMap() {
return tabsMap;
}

public Map<String, Listener<?,?>> setListenerMap() {
Map<String, JrdsDocument> nodemap = load.getRepository(ConfigType.LISTENER);
ListenerBuilder ob = new ListenerBuilder();
ob.setClassLoader(cl);
listenerMap = getObjectMap(ob, nodemap);
logger.debug(jrds.Util.delayedFormatString("listener configured: %s", listenerMap.keySet()));
return listenerMap;
}

/**
* @return the load
* @return the loader
*/
Loader getLoader() {
return load;
Expand Down
13 changes: 13 additions & 0 deletions src/jrds/configuration/ConfigType.java
Expand Up @@ -75,6 +75,19 @@ public String getName(JrdsDocument d) {
public String getRootNode() {
return "probedesc";
}
},
LISTENER {
public String getName(JrdsDocument d) {
String name = getNameByAttribute(d);
if (name == null || name.isEmpty()) {
name = d.getRootElement().getAttribute("class");
}
return name != null ? name.trim() : null;
}
@Override
public String getRootNode() {
return "listener";
}
};

public abstract String getName(JrdsDocument d);
Expand Down
22 changes: 22 additions & 0 deletions src/jrds/configuration/HostBuilder.java
Expand Up @@ -26,9 +26,11 @@
import jrds.factories.xml.JrdsDocument;
import jrds.factories.xml.JrdsElement;
import jrds.factories.xml.JrdsNode;
import jrds.probe.PassiveProbe;
import jrds.starter.Connection;
import jrds.starter.ConnectionInfo;
import jrds.starter.HostStarter;
import jrds.starter.Listener;
import jrds.starter.Timer;

import org.apache.log4j.Logger;
Expand All @@ -40,6 +42,7 @@ public class HostBuilder extends ConfigObjectBuilder<HostInfo> {
private ProbeFactory pf;
private Map<String, Macro> macrosMap;
private Map<String, Timer> timers = Collections.emptyMap();
private Map<String, Listener<?, ?>> listeners = Collections.emptyMap();

public HostBuilder() {
super(ConfigType.HOSTS);
Expand Down Expand Up @@ -314,6 +317,21 @@ public Probe<?,?> makeProbe(JrdsElement probeNode, HostInfo host, Map<String, St
}
}

//A passive probe, perhaps a specific listener is defined
if(p instanceof PassiveProbe) {
PassiveProbe<?> pp = (PassiveProbe<?>) p;
String listenerName = probeNode.getAttribute("listener");
if(listenerName != null && ! listenerName.trim().isEmpty()) {
Listener<?, ?> l = listeners.get(listenerName);
if(l != null) {
pp.setListener(l);
}
else {
logger.error(Util.delayedFormatString("Listener name not found for %s: %s", pp, listenerName));
}
}
}

if(p.checkStore()) {
shost.addProbe(p);
}
Expand Down Expand Up @@ -489,4 +507,8 @@ public void setTimers(Map<String, Timer> timers) {
this.timers = timers;
}

public void setListeners(Map<String, Listener<?, ?>> listenerMap) {
listeners = listenerMap;
}

}
43 changes: 43 additions & 0 deletions src/jrds/configuration/ListenerBuilder.java
@@ -0,0 +1,43 @@
package jrds.configuration;

import java.lang.reflect.InvocationTargetException;

import jrds.factories.xml.JrdsDocument;
import jrds.factories.xml.JrdsElement;
import jrds.starter.Listener;

@SuppressWarnings("rawtypes")
public class ListenerBuilder extends ConfigObjectBuilder<Listener<?,?>> {

private ClassLoader classLoader = ListenerBuilder.class.getClassLoader();

protected ListenerBuilder() {
super(ConfigType.LISTENER);
}

@Override
Listener build(JrdsDocument n) throws InvocationTargetException {
JrdsElement root = n.getRootElement();
String className = root.getAttribute("class");
if(className != null)
className = className.trim();
if(className.isEmpty())
return null;
try {
@SuppressWarnings("unchecked")
Class<? extends Listener> starterClass = (Class<? extends Listener>) classLoader.loadClass(className);
Listener s = starterClass.newInstance();
return s;
} catch (Exception e) {
throw new InvocationTargetException(e, ListenerBuilder.class.getName());
}
}

/**
* @param classLoader the classLoader to set
*/
void setClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}

}
3 changes: 3 additions & 0 deletions src/jrds/factories/xml/EntityResolver.java
Expand Up @@ -33,6 +33,9 @@ else if("-//jrds//DTD Sum//EN".equals(publicId)) {
else if("-//jrds//DTD Tab//EN".equals(publicId)) {
realSystemId = getClass().getResource("/tab.dtd");
}
else if("-//jrds//DTD Listener//EN".equals(publicId)) {
realSystemId = getClass().getResource("/listener.dtd");
}
else if("-//W3C//DTD XHTML 1.0 Strict//EN".equals(publicId)) {
realSystemId = getClass().getResource("/ressources/xhtml1-strict.dtd");
}
Expand Down

0 comments on commit 21db61c

Please sign in to comment.