Ejemplo n.º 1
0
  @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());
  }
Ejemplo n.º 2
0
  private void doGraph(GraphNode gn, Renderer r, ParamsBean params, JrdsJSONWriter w)
      throws IOException, JSONException {
    jrds.Graph graph = gn.getGraph();
    params.configureGraph(graph);

    Map<String, Object> imgProps = new HashMap<String, Object>();
    r.render(graph);
    Probe<?, ?> p = gn.getProbe();
    imgProps.put("probename", p.getName());
    imgProps.put("qualifiedname", graph.getQualifiedName());

    imgProps.put("qualifiedname", graph.getQualifiedName());
    GraphDesc gd = gn.getGraphDesc();
    if (gd != null && gd.getDimension() != null) {
      imgProps.put("height", gd.getDimension().height);
      imgProps.put("width", gd.getDimension().width);
    }
    imgProps.put("graph", params.doArgsMap(graph, true));
    imgProps.put("history", params.doArgsMap(graph, false));
    imgProps.put("probe", params.doArgsMap(p, true));
    imgProps.put("graphnode", params.doArgsMap(gn, true));
    doTree(w, graph.getQualifiedName(), gn.hashCode(), "graph", null, imgProps);
  }
Ejemplo n.º 3
0
  public Probe<?, ?> makeProbe(JrdsElement probeNode, HostInfo host, Map<String, String> properties)
      throws InvocationTargetException {
    Probe<?, ?> p = null;
    String type = probeNode.attrMap().get("type");

    List<Map<String, Object>> dsList = doDsList(type, probeNode.getElementbyName("dslist"));
    if (dsList.size() > 0) {
      logger.trace(
          Util.delayedFormatString("Data source replaced for %s/%s: %s", host, type, dsList));
      ProbeDesc oldpd = pf.getProbeDesc(type);
      try {
        ProbeDesc pd = (ProbeDesc) oldpd.clone();
        pd.replaceDs(dsList);
        p = pf.makeProbe(pd);
      } catch (CloneNotSupportedException e) {
      }
    } else {
      p = pf.makeProbe(type);
    }
    if (p == null) return null;

    p.readProperties(pm);

    String timerName = probeNode.getAttribute("timer");
    if (timerName == null) timerName = Timer.DEFAULTNAME;
    Timer timer = timers.get(timerName);
    if (timer == null) {
      logger.error("Invalid timer '" + timerName + "' for probe " + host.getName() + "/" + type);
      return null;
    } else {
      logger.trace(Util.delayedFormatString("probe %s/%s will use timer %s", host, type, timer));
    }
    p.setStep(timer.getStep());
    p.setTimeout(timer.getTimeout());

    // The label is set
    String label = probeNode.getAttribute("label");
    if (label != null && !"".equals(label)) {
      logger.trace(Util.delayedFormatString("Adding label %s to %s", label, p));
      p.setLabel(jrds.Util.parseTemplate(label, host, properties));
      ;
    }

    // The host is set
    HostStarter shost = timer.getHost(host);
    p.setHost(shost);

    ProbeDesc pd = p.getPd();
    List<Object> args = ArgFactory.makeArgs(probeNode, host, properties);
    // Prepare the probe with the default beans values
    Map<String, String> defaultBeans = pd.getDefaultArgs();
    if (defaultBeans != null) {
      for (Map.Entry<String, String> e : defaultBeans.entrySet()) {
        try {
          String beanName = e.getKey();
          String beanValue = e.getValue();
          PropertyDescriptor bean = pd.getBeanMap().get(beanName);
          Object value;
          // If the last argument is a list, give it to the template parser
          Object lastArgs = args.isEmpty() ? null : args.get(args.size() - 1);
          if (lastArgs instanceof List) {
            value =
                ArgFactory.ConstructFromString(
                    bean.getPropertyType(), Util.parseTemplate(beanValue, host, lastArgs));
          } else {
            value =
                ArgFactory.ConstructFromString(
                    bean.getPropertyType(), jrds.Util.parseTemplate(beanValue, host));
          }
          logger.trace(
              jrds.Util.delayedFormatString(
                  "Adding bean %s=%s (%s) to default args", beanName, value, value.getClass()));
          bean.getWriteMethod().invoke(p, value);
        } catch (Exception ex) {
          throw new RuntimeException("Invalid default bean " + e.getKey(), ex);
        }
      }
    }

    // Resolve the beans
    try {
      setAttributes(probeNode, p, pd.getBeanMap(), host);
    } catch (IllegalArgumentException e) {
      logger.error(String.format("Can't configure %s for %s: %s", pd.getName(), host, e));
      return null;
    }

    if (!pf.configure(p, args)) {
      logger.error(p + " configuration failed");
      return null;
    }

    // A connected probe, register the needed connection
    // It can be defined within the node, referenced by it's name, or it's implied name
    if (p instanceof ConnectedProbe) {
      String connectionName = null;
      ConnectedProbe cp = (ConnectedProbe) p;
      // Register the connections defined within the probe
      for (ConnectionInfo ci : makeConnexion(probeNode, p)) {
        ci.register(p);
      }
      String connexionName = probeNode.getAttribute("connection");
      if (connexionName != null && !"".equals(connexionName)) {
        logger.trace(Util.delayedFormatString("Adding connection %s to %s", connexionName, p));
        connectionName = jrds.Util.parseTemplate(connexionName, host);
        cp.setConnectionName(connectionName);
      } else {
        connectionName = cp.getConnectionName();
      }
      // If the connection is not already registred, try looking for it
      // And register it with the host
      if (p.find(connectionName) == null) {
        if (logger.isTraceEnabled())
          logger.trace(
              Util.delayedFormatString(
                  "Looking for connection %s in %s", connectionName, host.getConnections()));
        ConnectionInfo ci = host.getConnection(connectionName);
        if (ci != null) ci.register(shost);
        else {
          logger.error(
              Util.delayedFormatString(
                  "Failed to find a connection %s for a probe %s", connectionName, cp));
          return null;
        }
      }
    }

    // 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));
        }
      }
    }

    shost.addProbe(p);
    return p;
  }