Beispiel #1
0
  public Mib loadMib(File file) throws MibLoaderException, IOException {

    MibLoader loader = new MibLoader();

    loader.addDir(file.getParentFile());
    return loader.load(file);
  }
Beispiel #2
0
  private void loadDirectory(String[] files, String path) throws Exception {
    StringBuilder currentPath = new StringBuilder();
    for (int i = 0; i < files.length; i++) {
      if (!path.endsWith("/")) {
        currentPath.append(path).append("/");
      }
      currentPath.append(files[i]);

      if (SingletonFSInterface.instance().isFile(new URI(currentPath.toString()))) // if it's a file
      {
        try {
          if (!files[i].startsWith(".")) {
            mibLoader.load(files[i]);
          }
        } catch (Exception e) {
          GlobalLogger.instance()
              .getApplicationLogger()
              .error(TextEvent.Topic.PROTOCOL, e, "ERROR : loading the MIBS files");
          if (e instanceof MibLoaderException) {
            ((MibLoaderException) e).getLog().printTo(new PrintStream("../logs/snmpStack.log"));
          }
        }
      } else // if it's a directory, load it and all it's content
      {
        if (!files[i].startsWith(".")) {
          loadDirectory(
              SingletonFSInterface.instance().list(new URI(currentPath.toString())),
              currentPath.toString());
        }
      }
      // reset currentPath
      currentPath.delete(0, currentPath.length());
    }
  }
Beispiel #3
0
  private void loadAllMibs() throws Exception {
    mibLoader = new MibLoader();
    mibLoader.addAllDirs(new File("../conf/snmp/mibs")); // add all subDirectories to search for mib

    // travel through mibs repository to add all mibs
    loadDirectory(
        SingletonFSInterface.instance().list(new URI("../conf/snmp/mibs")), "../conf/snmp/mibs");
  }
Beispiel #4
0
  public void convert() throws IOException, MibLoaderException {
    m_loader = new MibLoader();

    URL url;
    try {
      url = new URL(m_mibLocation);
    } catch (MalformedURLException e) {
      url = null;
    }

    if (url == null) {
      File file = new File(m_mibLocation);
      m_loader.addDir(file.getParentFile());
      m_mib = m_loader.load(file);
    } else {
      m_mib = m_loader.load(url);
    }
  }
Beispiel #5
0
  public void printEvents(PrintStream out)
      throws MarshalException, ValidationException, ParserConfigurationException, SAXException,
          IOException {
    if (m_loader == null) {
      throw new IllegalStateException("convert() must be called first");
    }

    for (Mib mib : m_loader.getAllMibs()) {
      if (!mib.isLoaded()) {
        continue;
      }

      Events events = convertMibToEvents(mib, getEffectiveUeiBase());

      if (events.getEventCount() < 1) {
        System.err.println(
            "No trap or notification definitions found in this MIB ("
                + mib.getName()
                + "), exiting");
        System.exit(0);
      }

      if (!m_compat) {
        StringWriter writer = new StringWriter();

        events.marshal(writer);

        stripXmlNameSpace(writer.toString(), out);
      } else {
        for (Event event : events.getEventCollection()) {
          StringWriter writer = new StringWriter();

          event.marshal(writer);

          ByteArrayOutputStream formattedXml = new ByteArrayOutputStream();

          stripXmlNameSpace(writer.toString(), formattedXml);

          String noXmlProcessingInstruction =
              formattedXml
                  .toString()
                  .replaceAll("(?m)<\\?xml version=\"1.0\" encoding=\"UTF-8\"\\?>\n", "");
          out.print(
              noXmlProcessingInstruction.replaceAll("dest=\"logndisplay\"", "dest='logndisplay'"));
        }
      }
    }
  }
Beispiel #6
0
  private void parseVariableBinding(Element variable, PDU pdu) throws Exception {
    String name = variable.attributeValue("name");
    String value = variable.attributeValue("value");
    String type = variable.attributeValue("type");
    String mibName = null;

    if (name == null) {
      throw new Exception("ERROR : The variablebinding name is required to send the variable.");
    }

    VariableBinding varBind = null;
    boolean found = false;
    if (name.startsWith(
        "1.")) // if name begin with digit 1 follow by a dot, it is an oid, else we consider it's a
    // variable mib
    {
      varBind = new VariableBinding(new OID(name));
      found = true;
    } else {
      if (name.contains(".")) {
        mibName = name.substring(0, name.indexOf('.'));
        name = name.substring(name.indexOf('.') + 1);
      }

      Mib[] mibs = mibLoader.getAllMibs();
      for (int i = 0; (i < mibs.length) && !found; i++) {
        Collection symbols = mibs[i].getAllSymbols();
        for (Object symb : symbols) {
          if (symb instanceof MibValueSymbol) {
            if (((MibValueSymbol) symb).getName().equalsIgnoreCase(name)) {
              if ((mibName == null)
                  || ((MibValueSymbol) symb).getMib().getName().equalsIgnoreCase(mibName)) {
                varBind =
                    new VariableBinding(new OID(((MibValueSymbol) symb).getValue().toString()));

                if (type == null) {
                  type = findType((MibValueSymbol) symb);
                }
                found = true;
                break;
              }
            }
          } else if (symb instanceof MibTypeSymbol) {
            //                        System.out.println("mibTypeSymbol " +
            // ((MibTypeSymbol)symb).getName() + " for mib name " + mibs[i].getName());
          } else if (symb instanceof MibMacroSymbol) {
            //                        System.out.println("mibMacroSymbol " +
            // ((MibMacroSymbol)symb).getName() + " for mib name " + mibs[i].getName());
          } else {
            throw new Exception(
                "ERROR : Unknown class for variablebinding \"" + symb.getClass() + "\"");
          }
        }
      }
    }
    if (!found) {
      throw new Exception(
          "ERROR : Unknown varbinding name \"" + name + "\" not found in the MIB files.");
    }

    if (type == null) // /if type not set, search in mib
    {
      Mib[] mibs = mibLoader.getAllMibs();
      for (int i = 0; (i < mibs.length); i++) {
        MibValueSymbol symbol = mibs[i].getSymbolByValue(varBind.getOid().toString());

        if (symbol != null) {
          // set type in function of information in variable
          type = findType(symbol);
          break;
        }
      }
    }

    if ((value != null) && (value.length() > 0)) {
      if (type == null) {
        throw new Exception(
            "ERROR : The variablebinding type is required : it is not defined nor in XML script nor in MIB files.");
      }
      if (type.equalsIgnoreCase("counter64")) {
        varBind.setVariable(new Counter64(Long.parseLong(value)));
      } else if (type.equalsIgnoreCase("integer32")) {
        varBind.setVariable(new Integer32(Integer.parseInt(value)));
      } else if (type.equalsIgnoreCase("octetString") || type.equalsIgnoreCase("octet string")) {
        varBind.setVariable(new OctetString(value));
      } else if (type.equalsIgnoreCase("opaque")) {
        varBind.setVariable(new Opaque(value.getBytes()));
      } else if (type.equalsIgnoreCase("oid")) {
        varBind.setVariable(new OID(value));
      } else if (type.equalsIgnoreCase("genericAddress")) {
        GenericAddress add = new GenericAddress();
        add.setValue(value);
        varBind.setVariable(add);
      } else if (type.equalsIgnoreCase("ipAddress")) {
        varBind.setVariable(new IpAddress(value));
      } else if (type.equalsIgnoreCase("unsignedInteger32")) {
        varBind.setVariable(new UnsignedInteger32(Integer.parseInt(value)));
      } else if (type.equalsIgnoreCase("counter32")) {
        varBind.setVariable(new Counter32(Long.parseLong(value)));
      } else if (type.equalsIgnoreCase("gauge32")) {
        varBind.setVariable(new Gauge32(Long.parseLong(value)));
      } else if (type.equalsIgnoreCase("timeTicks")) {
        varBind.setVariable(new TimeTicks(Long.parseLong(value)));
      } else {
        throw new Exception(
            "Error : Unknown variablebinding type \""
                + type
                + "\" : not understood by the application");
      }
    }

    pdu.add(varBind);
  }