⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propertymoinput.java

📁 用snmp4j实现的agent,代码比较多,但是很值得一看,尤其是对于要用SNMP监控信息的编程者,可以仔细研究一下里面的代码.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*_############################################################################
  _##
  _##  SNMP4J-Agent - PropertyMOInput.java
  _##
  _##  Copyright (C) 2005-2008  Frank Fock (SNMP4J.org)
  _##
  _##  Licensed under the Apache License, Version 2.0 (the "License");
  _##  you may not use this file except in compliance with the License.
  _##  You may obtain a copy of the License at
  _##
  _##      http://www.apache.org/licenses/LICENSE-2.0
  _##
  _##  Unless required by applicable law or agreed to in writing, software
  _##  distributed under the License is distributed on an "AS IS" BASIS,
  _##  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  _##  See the License for the specific language governing permissions and
  _##  limitations under the License.
  _##
  _##########################################################################*/

package org.snmp4j.agent.io.prop;

import java.io.*;
import java.util.*;
import java.util.Map.Entry;

import org.snmp4j.agent.io.*;
import org.snmp4j.smi.*;
import org.snmp4j.agent.mo.util.VariableProvider;
import org.snmp4j.log.LogAdapter;
import org.snmp4j.log.LogFactory;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

/**
 * The <code>PropertyMOInput</code> can be used to load MIB data from a
 * properties file.
 * <p>
 * The format of the properties file is as follows:
 * <pre>
 *   snmp4j.agent.cfg.contexts={s|x}&lt;context1&gt;[,{s|x}&lt;context2&gt;...]
 *   snmp4j.agent.cfg.oid[.ctx.&lt;context&gt;].&lt;oid&gt;=[&lt;numRows&gt;:&lt;numCols&gt;]
 *   snmp4j.agent.cfg.oid[.ctx.&lt;context&gt;].&lt;oid&gt;=[{&lt;format&gt;}&lt;value&gt;]
 *   snmp4j.agent.cfg.index[.ctx.&lt;context&gt;].oid.&lt;rowIndex&gt;={o}&lt;index&gt;
 *   snmp4j.agent.cfg.value[.ctx.&lt;context&gt;].oid.&lt;rowIndex&gt;.&lt;colIndex>=[{&lt;format&gt;}&lt;value&gt;]
 * </pre>
 * where text enclosed in [] is optional and
 * <ul>
 * <li>context1 - is a SNMPv3 context name as UTF-8 string (format {s}) or
 * a hexadecimal string (format {x}).
 * </li>
 * <li>context - is a SNMPv3 context name as UTF-8 string if that string does
 * not contain any ISO control characters, otherwise a hexdecimal representation
 * separated by a colon (:).
 * </li>
 * <li>format - one of
 * <ul>
 * <li>u - an Unsigned32 value.</li>
 * <li>i - an Integer32 value.</li>
 * <li>s - an OctetString value.</li>
 * <li>x - an OctetString value in hexadecimal format (separated by :).</li>
 * <li>d - an OctetString value in decimal format (separated by .).</li>
 * <li>b - an OctetString value in decimal format (separated by ' ' per byte).</li>
 * <li>n - a Null value.</li>
 * <li>o - an OID value as dotted string where string parts may be specified
 * directly enclosed in single quotes (') and the to an OID converted value
 * of a variable/oid instance may be specified in the format
 * <code>[#]{&lt;name/oid&gt;}</code>. The value of the variable will be included into
 * the OID with prependend length if the # is used in the format string
 * otherwise no length will be included.</li>
 * <li>t - a TimeTicks value as an unsigned long value.</li>
 * <li>a - a IpAddress value.</li>
 * <li>$ - gets the value from the variable or object instance specified by the
 * name/oid following the $.</li>
 * </ul>
 * </li>
 * <li>value - a variable value in the format specified by <code>format</code>.
 * </li>
 * <li>numRows - the number of rows in the table.</li>
 * <li>numCols - the number of columns in the table.</li>
 * <li>rowIndex - the row index as a zero based unsigned integer.</li>
 * <li>colIndex - the column index as a zero based unsigned integer.</li>
 * <li>index - the OID value of the row's index.</li>
 * </ul>
 * <p>
 * An example properties file is:
 * <pre>
 * snmp4j.agent.cfg.contexts=
 * snmp4j.agent.cfg.oid.1.3.6.1.2.1.1.2.0={o}1.3.6.1.4.1.4976
 * snmp4j.agent.cfg.oid.1.3.6.1.2.1.1.4.0={s}System Administrator
 * snmp4j.agent.cfg.oid.1.3.6.1.2.1.1.6.0={s}<edit location>
 * snmp4j.agent.cfg.oid.1.3.6.1.2.1.1.7.0={i}10
 * snmp4j.agent.cfg.oid.1.3.6.1.2.1.1.9.1=1:2
 * snmp4j.agent.cfg.index.1.3.6.1.2.1.1.9.1.0={o}1
 * snmp4j.agent.cfg.value.1.3.6.1.2.1.1.9.1.0.0={o}1.3.6.1.4.1.4976.10.1.1.100.4.1
 * snmp4j.agent.cfg.value.1.3.6.1.2.1.1.9.1.0.1=
 * ## VACM MIB
 * # security2Group
 * snmp4j.agent.cfg.oid.1.3.6.1.6.3.16.1.2.1=2:3
 * snmp4j.agent.cfg.index.1.3.6.1.6.3.16.1.2.1.0={o}2.6.'public'
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.2.1.0.0={s}v1v2cgroup
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.2.1.0.1={i}4
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.2.1.0.2={i}1
 * snmp4j.agent.cfg.index.1.3.6.1.6.3.16.1.2.1.1={o}3.6.'SHADES'
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.2.1.1.0={s}v3group
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.2.1.1.1={i}4
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.2.1.1.2={i}1
 * # access
 * snmp4j.agent.cfg.oid.1.3.6.1.6.3.16.1.4.1=2:6
 * snmp4j.agent.cfg.index.1.3.6.1.6.3.16.1.4.1.0={o}10.'v1v2cgroup'.0.2.1
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.4.1.0.0={i}1
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.4.1.0.1={s}unrestrictedReadView
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.4.1.0.2={s}unrestrictedWriteView
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.4.1.0.3={s}unrestrictedNotifyView
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.4.1.0.4={i}4
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.4.1.0.5={i}1
 * snmp4j.agent.cfg.index.1.3.6.1.6.3.16.1.4.1.1={o}7.'v3group'.0.3.3
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.4.1.1.0={i}1
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.4.1.1.1={s}unrestrictedReadView
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.4.1.1.2={s}unrestrictedWriteView
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.4.1.1.3={s}unrestrictedNotifyView
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.4.1.1.4={i}4
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.4.1.1.5={i}1
 * # view trees
 * snmp4j.agent.cfg.oid.1.3.6.1.6.3.16.1.5.2.1=3:4
 * snmp4j.agent.cfg.index.1.3.6.1.6.3.16.1.5.2.1.0={o}20.'unrestrictedReadView'.3.1.3.6
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.5.2.1.0.0={s}
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.5.2.1.0.1={i}1
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.5.2.1.0.2={i}4
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.5.2.1.0.3={i}1
 * snmp4j.agent.cfg.index.1.3.6.1.6.3.16.1.5.2.1.1={o}21.'unrestrictedWriteView'.3.1.3.6
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.5.2.1.1.0={s}
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.5.2.1.1.1={i}1
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.5.2.1.1.2={i}4
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.5.2.1.1.3={i}1
 * snmp4j.agent.cfg.index.1.3.6.1.6.3.16.1.5.2.1.2={o}22.'unrestrictedNotifyView'.3.1.3.6
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.5.2.1.2.0={s}
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.5.2.1.2.1={i}1
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.5.2.1.2.2={i}4
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.16.1.5.2.1.2.3={i}1
 * ## SNMP community MIB
 * snmp4j.agent.cfg.oid.1.3.6.1.6.3.18.1.1.1=1:7
 * snmp4j.agent.cfg.index.1.3.6.1.6.3.18.1.1.1.0={o}'public'
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.18.1.1.1.0.0={s}public
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.18.1.1.1.0.1={s}public
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.18.1.1.1.0.2={$1.3.6.1.6.3.10.2.1.1.0}
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.18.1.1.1.0.3={s}
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.18.1.1.1.0.4={s}
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.18.1.1.1.0.5={i}4
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.18.1.1.1.0.6={i}1
 * ## USM MIB
 * snmp4j.agent.cfg.oid.1.3.6.1.6.3.15.1.2.2.1=1:14
 * snmp4j.agent.cfg.index.1.3.6.1.6.3.15.1.2.2.1.0={o}$#{1.3.6.1.6.3.10.2.1.1.0}.6.'SHADES'
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.15.1.2.2.1.0.0={s}SHADES
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.15.1.2.2.1.0.1={o}
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.15.1.2.2.1.0.2={o}1.3.6.1.6.3.10.1.1.3
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.15.1.2.2.1.0.3={s}
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.15.1.2.2.1.0.4={s}
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.15.1.2.2.1.0.5={o}1.3.6.1.6.3.10.1.2.2
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.15.1.2.2.1.0.6={s}
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.15.1.2.2.1.0.7={s}
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.15.1.2.2.1.0.8={s}
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.15.1.2.2.1.0.9={i}4
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.15.1.2.2.1.0.10={i}1
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.15.1.2.2.1.0.11={s}SHADESAuthPassword
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.15.1.2.2.1.0.12={s}SHADESPrivPassword
 * snmp4j.agent.cfg.value.1.3.6.1.6.3.15.1.2.2.1.0.13=
 *
 * </pre>
 *
 * @author Frank Fock
 * @version 1.2
 */
public class PropertyMOInput implements MOInput {

  private static final LogAdapter logger =
      LogFactory.getLogger(PropertyMOInput.class);

  public static final String CONFIG_PREFIX = "snmp4j.agent.cfg.";
  public static final String CONTEXTS_ID = "contexts";
  public static final String OID_ID = "oid.";
  public static final String INDEX_ID = "index.";
  public static final String VERSION_ID = "version.";
  public static final String CTX_ID = "ctx.";
  public static final String VALUE_ID = "value.";

  private static final int STATE_ALL_CTX_DATA_SEQ = 0;
  private static final int STATE_ALL_CTX_DATA = 1;
  private static final int STATE_CTX_SEQ = 2;
  private static final int STATE_CTX_DATA_SEQ = 3;
  private static final int STATE_CTX_DATA = 4;

  private int importMode = ImportModes.REPLACE_CREATE;

  private int state = STATE_ALL_CTX_DATA_SEQ;
  private SortedMap properties;
  private ContextInfo contexts;
  private OIDInfo oids;
  private DataInfo data;
  private VariableProvider variables;

  public PropertyMOInput(Properties props) {
    properties = scanProperties(props);
    contexts = scanContexts(properties);
  }

  public PropertyMOInput(Properties props, VariableProvider variables) {
    this(props);
    this.variables = variables;
  }

  private ContextInfo scanContexts(Map properties) {
    String ctx = (String) properties.get(CONFIG_PREFIX+CONTEXTS_ID);
    StringTokenizer st = new StringTokenizer(ctx, ",");
    ArrayList l = new ArrayList(st.countTokens());
    while (st.hasMoreTokens()) {
      String token = st.nextToken();
      OctetString s = (OctetString)
          createVariableFromString(token, OctetString.class);
      l.add(new Context(s));
    }
    return new ContextInfo(l.size(), l.iterator());
  }

  private OIDInfo scanOIDs(SortedMap properties, OctetString ctx) {
    String prefix = CONFIG_PREFIX+OID_ID;
    String ctxSuffix = "";
    if (ctx != null) {
      ctxSuffix = CTX_ID+ctx.toString()+".";
    }
    prefix += ctxSuffix;
    SortedMap oids = properties.tailMap(prefix);
    ArrayList l = new ArrayList();
    for (Iterator it = oids.entrySet().iterator(); it.hasNext();) {
      Entry e = (Entry) it.next();
      String k = (String) e.getKey();
      if (k.startsWith(prefix)) {
        String oid;
        oid = k.substring(prefix.length());
        String version =
            (String) properties.get(CONFIG_PREFIX+VERSION_ID+ctxSuffix);
        l.add(new MOInfo(new OID(oid), version));
      }
      else {
        break;
      }
    }
    return new OIDInfo(ctx, l.size(), l.iterator());
  }

  private DataInfo scanData(SortedMap properties, OctetString context,
                            OID oid, boolean indexed) {
    String prefix = CONFIG_PREFIX;
    String ctxSuffix = "";
    if (context != null) {
      ctxSuffix = CTX_ID+context;
    }
    prefix += ctxSuffix;
    if (!indexed) {
      prefix += OID_ID+oid;
      String value = (String) properties.get(prefix);
      Variable v = createVariableFromString(value, Variable.class);
      return new DataInfo(context, 1,
                          Arrays.asList(new Variable[] { v }).iterator());
    }
    else {
      prefix += OID_ID+oid;
      String dimension = (String) properties.get(prefix);
      StringTokenizer st = new StringTokenizer(dimension, ": ");
      int rows = Integer.parseInt(st.nextToken());
      int cols = Integer.parseInt(st.nextToken());
      String indexPrefix = CONFIG_PREFIX+ctxSuffix+INDEX_ID+oid;
      String valuePrefix = CONFIG_PREFIX+ctxSuffix+VALUE_ID+oid;
      ArrayList l = new ArrayList();
      for (int i=0; i<rows; i++) {
        String indexString = (String) properties.get(indexPrefix+"."+i);
        OID index = (OID) createVariableFromString(indexString, OID.class);
        Variable[] v = new Variable[cols];
        for (int j=0; j<cols; j++) {
          String val = (String) properties.get(valuePrefix+"."+i+"."+j);
          if (val != null) {
            v[j] = createVariableFromString(val, Variable.class);
          }
        }
        l.add(new IndexedVariables(index, v));
      }
      return new DataInfo(context, l.size(), l.iterator());
    }
  }

  /**
   * Scans the supplied properties for config relevant properties and stores
   * them into the returned LinkedHashMap.
   *
   * @param props
   *    a set of properties.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -