示例#1
0
 private void setAttributes(
     JrdsElement probeNode, Object o, Map<String, PropertyDescriptor> beans, Object... context)
     throws IllegalArgumentException, InvocationTargetException {
   // Resolve the beans
   for (JrdsElement attrNode : probeNode.getChildElementsByName("attr")) {
     String name = attrNode.getAttribute("name");
     PropertyDescriptor bean = beans.get(name);
     if (bean == null) {
       logger.error("Unknonw bean " + name);
       continue;
     }
     String textValue = Util.parseTemplate(attrNode.getTextContent(), context);
     logger.trace(Util.delayedFormatString("Fond attribute %s with value %s", name, textValue));
     try {
       Constructor<?> c = bean.getPropertyType().getConstructor(String.class);
       Object value = c.newInstance(textValue);
       bean.getWriteMethod().invoke(o, value);
     } catch (IllegalArgumentException e) {
       throw new InvocationTargetException(e, HostBuilder.class.getName());
     } catch (SecurityException e) {
       throw new InvocationTargetException(e, HostBuilder.class.getName());
     } catch (InstantiationException e) {
       throw new InvocationTargetException(e, HostBuilder.class.getName());
     } catch (IllegalAccessException e) {
       throw new InvocationTargetException(e, HostBuilder.class.getName());
     } catch (InvocationTargetException e) {
       throw new InvocationTargetException(e, HostBuilder.class.getName());
     } catch (NoSuchMethodException e) {
       throw new InvocationTargetException(e, HostBuilder.class.getName());
     }
   }
 }
示例#2
0
  private Map<String, String> makeProperties(JrdsElement n) {
    if (n == null) return Collections.emptyMap();
    JrdsElement propElem = n.getElementbyName("properties");
    if (propElem == null) return Collections.emptyMap();

    Map<String, String> props = new HashMap<String, String>();
    for (JrdsElement propNode : propElem.getChildElementsByName("entry")) {
      String key = propNode.getAttribute("key");
      if (key != null) {
        String value = propNode.getTextContent();
        logger.trace(Util.delayedFormatString("Adding propertie %s=%s", key, value));
        props.put(key, value);
      }
    }
    logger.debug(Util.delayedFormatString("Properties map: %s", props));
    return props;
  }
示例#3
0
 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;
 }
示例#4
0
  /**
   * Enumerate the connections found in an XML node
   *
   * @param domNode a node to parse
   * @param host
   * @return
   */
  Set<ConnectionInfo> makeConnexion(JrdsElement domNode, Object parent) {
    Set<ConnectionInfo> connectionSet = new HashSet<ConnectionInfo>();

    // Check for the old SNMP connection node
    ConnectionInfo cnxSnmp = parseSnmp(domNode);
    if (cnxSnmp != null) connectionSet.add(cnxSnmp);

    for (JrdsElement cnxNode : domNode.getChildElementsByName("connection")) {
      String type = cnxNode.getAttribute("type");
      if (type == null) {
        logger.error("No type declared for a connection");
        continue;
      }
      String name = cnxNode.getAttribute("name");

      try {
        // Load the class for the connection
        @SuppressWarnings("unchecked")
        Class<? extends Connection<?>> connectionClass =
            (Class<? extends Connection<?>>) classLoader.loadClass(type);

        // Build the arguments vector for the connection
        List<Object> args = ArgFactory.makeArgs(cnxNode);

        // Resolve the bean for the connection
        Map<String, String> attrs = new HashMap<String, String>();
        for (JrdsElement attrNode : cnxNode.getChildElementsByName("attr")) {
          String attrName = attrNode.getAttribute("name");
          String textValue = Util.parseTemplate(attrNode.getTextContent(), parent);
          attrs.put(attrName, textValue);
        }
        ConnectionInfo cnx = new ConnectionInfo(connectionClass, name, args, attrs);
        connectionSet.add(cnx);
        logger.debug(Util.delayedFormatString("Added connection %s to node %s", cnx, parent));
      } catch (NoClassDefFoundError ex) {
        logger.warn("Connection class not found: " + type + ": " + ex);
      } catch (ClassCastException ex) {
        logger.warn(type + " is not a connection");
      } catch (LinkageError ex) {
        logger.warn(
            "Incompatible code version during connection creation of type " + type + ": " + ex, ex);
      } catch (Exception ex) {
        logger.warn("Error during connection creation of type " + type + ": " + ex, ex);
      }
    }
    return connectionSet;
  }
示例#5
0
  @Override
  public boolean generate(JrdsJSONWriter w, HostsList root, ParamsBean params)
      throws IOException, JSONException {

    if (params.getPeriod() == null) {
      return false;
    }

    List<GraphNode> graphs = params.getGraphs(this);
    if (params.isSorted() && graphs.size() > 1) {
      Collections.sort(
          graphs,
          new Comparator<GraphNode>() {
            public int compare(GraphNode g1, GraphNode g2) {
              int order = String.CASE_INSENSITIVE_ORDER.compare(g1.getName(), g2.getName());
              if (order == 0)
                order =
                    String.CASE_INSENSITIVE_ORDER.compare(
                        g1.getProbe().getHost().getName(), g2.getProbe().getHost().getName());
              return order;
            }
          });
    }
    logger.debug(jrds.Util.delayedFormatString("Graphs returned: %s", graphs));
    if (!graphs.isEmpty()) {
      Renderer r = root.getRenderer();
      for (GraphNode gn : graphs) {
        if (!gn.getACL().check(params)) continue;
        if (params.isHistory()) {
          for (int p : periodHistory) {
            params.setScale(p);
            doGraph(gn, r, params, w);
          }
        } else {
          doGraph(gn, r, params, w);
        }
      }
    }
    return true;
  }
示例#6
0
  /** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    int len = request.getContentLength();
    if (len > 4096) {
      logger.error("post data too big: " + len);
      response.sendError(HttpServletResponse.SC_BAD_REQUEST, "post data too big: " + len);
      return;
    }
    byte[] bufferin = new byte[len];
    ServletInputStream postDataStream = request.getInputStream();

    // Build the POST data string
    ByteArrayOutputStream postDataBuffer = new ByteArrayOutputStream(len);
    int read;
    while ((read = postDataStream.read(bufferin)) > 0) {
      postDataBuffer.write(bufferin, 0, read);
    }
    ;
    String postData = postDataBuffer.toString();
    logger.debug(Util.delayedFormatString("Post data: %s", postData));

    JrdsJSONObject paramsClean;

    try {
      JrdsJSONObject params = new JrdsJSONObject(postData);
      paramsClean = new JrdsJSONObject();
      for (String key : params) {
        if (JSONKEYS.contains(key)) {
          Object value = params.get(key);
          if (value instanceof String && "".equals(((String) value).trim())) {
            value = null;
          }
          if (value != null) paramsClean.put(JSONDICT.get(key).toString(), value);
        }
      }
    } catch (JSONException e) {
      logger.error("Invalid JSON object:" + postData);
      response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid POST data");
      return;
    }

    ByteArrayOutputStream packedDataBuffer = new ByteArrayOutputStream(len);
    GZIPOutputStream gzipBuffer = new GZIPOutputStream(new OutputStream(packedDataBuffer), len);
    gzipBuffer.write(paramsClean.toString().getBytes());
    gzipBuffer.close();

    char separator = '?';
    String referer = request.getHeader("Referer");
    try {
      URL refererUrl = new URL(referer);
      if (refererUrl.getQuery() != null) separator = '&';
    } catch (Exception e) {
      String host = request.getHeader("Host");
      String contextPath = request.getContextPath();
      referer = "http://" + host + contextPath + "/";
    }

    String packedurl =
        referer
            + separator
            + "p="
            + new String(packedDataBuffer.toByteArray())
                .substring(GZIPHEADER.length())
                .replace('=', '!')
                .replace('/', '$')
                .replace('+', '*');

    response.getOutputStream().print(packedurl);
    response.flushBuffer();
  }
示例#7
0
 public void addTree(String tab, List<?> tree) {
   trees.put(tab, tree);
   logger.trace(jrds.Util.delayedFormatString("Adding tree %s to tab %s", tree, tab));
 }
示例#8
0
  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);
      }
    }
  }
示例#9
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;
  }