Beispiel #1
0
  public void configure(Properties configuration) {
    logger.debug("Configuration: " + configuration);

    host = configuration.getProperty("jetty.host");
    port = jrds.Util.parseStringNumber(configuration.getProperty("jetty.port"), port).intValue();
    propFileName = configuration.getProperty("propertiesFile", propFileName);
    webRoot = configuration.getProperty("webRoot", webRoot);
  }
 private Target makeSnmpTarget(HttpServletRequest request) throws UnknownHostException {
   String hostname = request.getParameter("host");
   String community = request.getParameter("discoverSnmpCommunity");
   if (community == null) {
     community = "public";
   }
   int port = jrds.Util.parseStringNumber(request.getParameter("discoverSnmpPort"), 161);
   IpAddress addr = new UdpAddress(InetAddress.getByName(hostname), port);
   Target hosttarget = new CommunityTarget(addr, new OctetString(community));
   hosttarget.setVersion(SnmpConstants.version2c);
   return hosttarget;
 }
 /* (non-Javadoc)
  * @see com.aol.jrds.HttpProbe#parseLines(java.util.List)
  */
 protected Map<String, Number> parseLines(List<String> lines) {
   Map<String, Number> retValue = new HashMap<String, Number>(lines.size());
   for (String l : lines) {
     String[] kvp = l.split(":");
     if (kvp.length != 2) continue;
     Double value = Util.parseStringNumber(kvp[1].trim(), Double.NaN);
     retValue.put(kvp[0].trim(), value);
   }
   Number uptimeNumber = retValue.remove("Uptime");
   if (uptimeNumber != null) setUptime(uptimeNumber.longValue());
   return retValue;
 }
Beispiel #4
0
 private MBeanServerConnection connect(String hostname, HttpServletRequest request) {
   this.hostname = hostname;
   String protocolName = request.getParameter("discoverJmxProtocol");
   if (protocolName != null && !protocolName.trim().isEmpty()) {
     cnx.setProtocol(JmxProtocol.valueOf(protocolName.trim()));
   }
   Integer port = jrds.Util.parseStringNumber(request.getParameter("discoverJmxPort"), 0);
   if (port != 0) {
     cnx.setPort(port);
   }
   cnx.setUser(request.getParameter("discoverJmxUser"));
   cnx.setPassword(request.getParameter("discoverJmxPassword"));
   if (cnx.startConnection()) {
     return cnx.getConnection().connection;
   } else {
     return null;
   }
 }
Beispiel #5
0
  /**
   * Add a plot, but only uses String as parameters, for the GraphFactory
   *
   * @param name Name of the plot
   * @param dsName the datastore to use
   * @param rpn The RPN, used instead of the datastore
   * @param graphType
   * @param color
   * @param legend
   * @param consFunc
   * @param reversed
   * @param host
   * @param probe
   * @param subDsName
   */
  public void add(
      String name,
      String rpn,
      String graphType,
      String color,
      String legend,
      String consFunc,
      String reversed,
      String percentile,
      // The path to an external datastore
      String host,
      String probe,
      String dsName) {
    if (logger.isTraceEnabled())
      logger.trace(
          "Adding " + name + ", " + rpn + ", " + graphType + ", " + color + ", " + legend + ", "
              + consFunc + ", " + reversed + ", " + host + ", " + probe);
    GraphType gt = null;
    if (graphType == null || "".equals(graphType)) {
      if (legend != null) gt = GraphType.COMMENT;
      else gt = GraphType.NONE;
    } else gt = GraphType.valueOf(graphType.toUpperCase());

    ConsolFun cf = null;
    if (gt != GraphType.COMMENT) {
      cf = DEFAULTCF;
      if (consFunc != null && !"".equals(consFunc)) cf = ConsolFun.valueOf(consFunc.toUpperCase());
    }

    Color c = null;
    if (gt.toPlot()) {
      c = Color.WHITE;
      if (color != null && color.toUpperCase().matches("^#[0-9A-F]{6}")) {
        int r = Integer.parseInt(color.substring(1, 3), 16);
        int g = Integer.parseInt(color.substring(3, 5), 16);
        int b = Integer.parseInt(color.substring(5, 7), 16);
        c = new Color(r, g, b);
      } else if (color != null && !"".equals(color)) {
        c = Colors.valueOf(color.toUpperCase()).getColor();
        if (c == null) c = Color.getColor(color);
        if (c == null) {
          logger.error("Cannot read color " + color);
          c = Color.white;
        }
      } else {
        c = Colors.resolveIndex(lastColor);
        if (gt.toPlot()) lastColor++;
      }
    }
    if (name != null) {
      // If not a rpn, it must be a datastore
      if (gt.datasource() && rpn == null && dsName == null) {
        dsName = name;
      }
    }
    // If the name is missing, generate one ?
    else {
      name = Integer.toHexString((int) (Math.random() * Integer.MAX_VALUE));
    }
    // Auto generated legend
    if (legend == null && name != null && gt.legend()) legend = name;

    Integer valPercentile = null;
    if (percentile != null && !"".equals(percentile)) {
      valPercentile = jrds.Util.parseStringNumber(percentile, Integer.valueOf(0));
    }
    add(name, dsName, rpn, gt, c, legend, cf, reversed != null, valPercentile, host, probe);
  }
  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);
      }
    }
  }