/** * SnmpVar转化Mac地址 * * @param var * @return */ public static String snmpvar2Mac(SnmpVar var) { byte[] bytes = var.toBytes(); if (bytes.length == 6) { return ByteFormatter.convertStringInHextoASCII(var.toBytes()); } else if (bytes.length == 17) { String str = var.toString(); return str.replaceAll(":", "-"); } else return var.toString(); }
/** * SnmpVar转换为DateTime类型 * * @param var * @return */ public static String snmpVar2DateAndTime(SnmpVar var) { // 添加对于系统运行时间是int型的处理,一般是非标准设备比如C2000 if (var instanceof SnmpGauge) { SnmpGauge gauge = (SnmpGauge) var; return convertLong2DateTime(gauge.longValue()); } byte[] bytes = var.toBytes(); int i = bytes.length; byte abyte0[] = new byte[i * 2]; int j = 0; for (int k = 0; k < bytes.length; k++) { abyte0[j] = (byte) (bytes[k] >> 4 & 15); abyte0[++j] = (byte) (bytes[k] & 15); j++; } i = abyte0.length; if (i != 16 && i != 22) { return null; } String s = ""; j = abyte0[0] * 4096 + abyte0[1] * 256 + abyte0[2] * 16 + abyte0[3]; s = s + j + "-"; int k = abyte0[4] * 16 + abyte0[5]; s = s + k + "-"; int l = abyte0[6] * 16 + abyte0[7]; s = s + l + ","; int i1 = abyte0[8] * 16 + abyte0[9]; s = s + i1 + ":"; int j1 = abyte0[10] * 16 + abyte0[11]; s = s + j1 + ":"; int k1 = abyte0[12] * 16 + abyte0[13]; s = s + k1 + "."; int l1 = abyte0[14] * 16 + abyte0[15]; s = s + l1; if (i > 16 && i != 32) { s = s + ","; char c = (char) (abyte0[16] * 16 + abyte0[17]); s = s + c; int i2 = abyte0[18] * 16 + abyte0[19]; s = s + i2 + ":"; int j2 = abyte0[20] * 16 + abyte0[21]; s = s + j2; } return s; }
protected void processSetRequest(SnmpVarBind varb, AgentNode node, VarBindRequestEvent pe) throws AgentSnmpException { try { // This means that the node is outside the perview of the // mibs loaded if (node == null) { AgentUtil.throwNoCreation(pe.getVersion()); } int[] oid = (int[]) varb.getObjectID().toValue(); if (oid.length != REDEOidRep.length + 2) { AgentUtil.throwNoCreation(pe.getVersion()); } int[] inst = (int[]) AgentUtil.getInstance(oid, REDEOidRep.length); if (inst[1] != 0) { AgentUtil.throwNoCreation(pe.getVersion()); } // For adding trap varbinds ... Vector varbindVector = new Vector(); SnmpVar var = varb.getVariable(); switch (node.getSubId()) { case REDE_BUSCAIP: // Check for the proper instance if (!(var instanceof SnmpIpAddress)) { AgentUtil.throwWrongType(pe.getVersion()); } String REDE_BuscaIPStr = (String) var.getVarObject(); instrument.setREDE_BuscaIP(REDE_BuscaIPStr); break; case REDE_HOSTNAME: // Check for the proper instance if (!(var instanceof SnmpString)) { AgentUtil.throwWrongType(pe.getVersion()); } String REDE_HOSTNAMEStr = (String) var.getVarObject(); byte[] REDE_HOSTNAMEArr = AgentUtil.getOctetBytes(REDE_HOSTNAMEStr); instrument.setREDE_HOSTNAME(REDE_HOSTNAMEArr); break; default: AgentUtil.throwNoSuchInstance(pe.getVersion()); } } catch (Exception exp) { if (exp instanceof AgentException) { AgentException ae = (AgentException) exp; AgentSnmpException ase = AgentUtil.convertToAgentSnmpException(pe.getVersion(), ae); utils.handleError(ase); throw ase; } else if (exp instanceof AgentSnmpException) { utils.handleError((AgentSnmpException) exp); throw ((AgentSnmpException) exp); } utils.handleError(exp); } }