/**
  * Parses a textual representation of an object ID as dotted string (e.g. "1.3.6.1.2.1.1") and
  * returns its raw value.
  *
  * @param text a textual representation of an OID.
  * @return the raw OID value.
  * @throws ParseException if the OID cannot be parsed successfully.
  */
 public int[] parse(String text) throws ParseException {
   StringTokenizer st = new StringTokenizer(text, ".", true);
   int size = st.countTokens();
   int[] value = new int[size];
   size = 0;
   StringBuffer buf = null;
   while (st.hasMoreTokens()) {
     String t = st.nextToken();
     if ((buf == null) && t.startsWith("'")) {
       buf = new StringBuffer();
       t = t.substring(1);
     }
     if ((buf != null) && (t.endsWith("'"))) {
       buf.append(t.substring(0, t.length() - 1));
       OID o = new OctetString(buf.toString()).toSubIndex(true);
       int[] h = value;
       value = new int[st.countTokens() + h.length + o.size()];
       System.arraycopy(h, 0, value, 0, size);
       System.arraycopy(o.getValue(), 0, value, size, o.size());
       size += o.size();
       buf = null;
     } else if (buf != null) {
       buf.append(t);
     } else if (!".".equals(t)) {
       value[size++] = (int) Long.parseLong(t.trim());
     }
   }
   if (size < value.length) {
     int[] h = value;
     value = new int[size];
     System.arraycopy(h, 0, value, 0, size);
   }
   return value;
 }
Ejemplo n.º 2
0
  private int enumerateIndexed(JrdsElement hostEleme, ProbeDescSummary summary, boolean withOid)
      throws IOException {
    OID indexOid = new OID(summary.specifics.get("indexOid"));
    int count = 0;
    log(Level.TRACE, "Will enumerate %s", indexOid);
    Set<OID> oidsSet = Collections.singleton(indexOid);
    Map<OID, Object> indexes = SnmpRequester.TREE.doSnmpGet(active, oidsSet);
    log(Level.TRACE, "Elements : %s", indexes);
    for (Map.Entry<OID, Object> e : indexes.entrySet()) {
      count++;
      Map<String, String> beans = new HashMap<String, String>(2);
      OID rowOid = e.getKey();
      String indexName = e.getValue().toString();
      beans.put("index", indexName);

      int[] index = Arrays.copyOfRange(rowOid.getValue(), indexOid.size(), rowOid.size());

      // If we wanted to generate a static oid
      if (withOid) {
        OID suffixOid = new OID(index);
        beans.put("oid", suffixOid.toString());
      }

      // We try to auto-generate the label
      String label = summary.specifics.get("labelOid");
      String labelValue = null;
      if (label != null && !label.isEmpty()) {
        OID suffixOid = new OID(index);
        for (String lookin : label.split(",")) {
          OID labelOID = new OID(lookin.trim() + "." + suffixOid.toString());
          labelValue = getLabel(active, labelOID);
          if (labelValue != null) break;
        }
      }

      addProbe(hostEleme, summary.name, labelValue, null, null, beans);
    }
    return count;
  }