diff options
| author | shizhendong <[email protected]> | 2020-04-30 18:57:44 +0800 |
|---|---|---|
| committer | shizhendong <[email protected]> | 2020-04-30 18:57:44 +0800 |
| commit | f837ea4eafff8e549299aeb65aa8c5c3932b504a (patch) | |
| tree | d6c5eb92892954af25293b75a1cbbd008282b4f2 | |
| parent | 48346ca79b1ef24b8ccbd826fefd5ffc6d80428d (diff) | |
fix: 修改traffic接口返回标签内容格式
| -rw-r--r-- | src/main/java/com/nis/service/impl/TrafficServiceImpl.java | 75 | ||||
| -rw-r--r-- | src/main/java/com/nis/util/SnmpUtil.java | 6 |
2 files changed, 68 insertions, 13 deletions
diff --git a/src/main/java/com/nis/service/impl/TrafficServiceImpl.java b/src/main/java/com/nis/service/impl/TrafficServiceImpl.java index 8d4afd8..a9b497b 100644 --- a/src/main/java/com/nis/service/impl/TrafficServiceImpl.java +++ b/src/main/java/com/nis/service/impl/TrafficServiceImpl.java @@ -1,6 +1,5 @@ package com.nis.service.impl; -import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.nis.dao.SnmpMibDao; @@ -61,11 +60,12 @@ public class TrafficServiceImpl implements TrafficService { // IfMib 文件中 oid 与 name对应关系 map Map<String, String> oidWithNameMap = this.getIfMibTreeNodeOidAndNameMap(); - String resultOid; - String name, index, direction, descHelp, descType, inOutPrefix, trafficTags, tags, trafficDirection; + String resultOid, name, index, directionStr, inOutPrefix, trafficTags, tagStr, trafficDirection, ifPhysAddresStr, ifDescrStr; List<Map> resultData = new ArrayList<>(); Traffic t; Map auth = new HashMap(); + Map<String, String> tagMap; + for (Map.Entry<String, List<Traffic>> entry : listMap.entrySet()) { try { t = entry.getValue().get(0); @@ -91,7 +91,7 @@ public class TrafficServiceImpl implements TrafficService { for (Map map : resultData) { tempMap = new HashMap(); boolean directionFlag = true; - tags = direction = ""; + tagStr = directionStr = ifPhysAddresStr = ifDescrStr = ""; resultOid = map.get("oid").toString(); index = resultOid; @@ -105,30 +105,51 @@ public class TrafficServiceImpl implements TrafficService { } if (index.equals(traffic.getIfIndex().toString())) { - if (name.contains("ifIndex") || (!name.contains("ifIn") && !name.contains("ifOut"))) { + // ifSpecific已过时 + if (name.contains("ifSpecific")) { continue; } if (trafficDirection.contains(Constant.TRAFFIC_RX)) { - direction += Constant.TRAFFIC_RX + "=" + "\"" + 1 + "\""; + directionStr += Constant.TRAFFIC_RX + "=" + "\"" + 1 + "\""; directionFlag = false; } if (trafficDirection.contains(Constant.TRAFFIC_TX)) { if (!directionFlag) { - direction += ","; + directionStr += ","; } - direction += Constant.TRAFFIC_TX + "=" + "\"" + 1 + "\""; + directionStr += Constant.TRAFFIC_TX + "=" + "\"" + 1 + "\""; } if (StringUtils.isNotEmpty(trafficTags)) { - tags = "tags=" + JSON.toJSONString(trafficTags) + ","; + tagMap = JSONObject.parseObject(trafficTags, Map.class); + for (Map.Entry<String, String> tagEntry : tagMap.entrySet()) { + if (tagEntry.getKey().toLowerCase().equals("ifIndex".toLowerCase())) { + continue; + } + tagStr += tagEntry.getKey() + "=" + "\"" + tagEntry.getValue() + "\"" + ","; + } + } + if (name.contains("ifPhysAddress")) { + if (StringUtils.isEmpty(map.get("value").toString())) { + map.put("value", "00:00:00:00:00:00"); + } + ifPhysAddresStr = "ifPhysAddress=" + "\"" + map.get("value").toString().toUpperCase() + "\"" + ","; + map.put("value", 1); } - inOutPrefix = "{ifIndex=" + "\"" + traffic.getIfIndex() + "\"" + "," - + "ifDescr=" + "\"" + traffic.getIfdescr() + "\"" + "," + + if (name.contains("ifDescr")) { + ifDescrStr = "ifDescr=" + "\"" + map.get("value").toString().toUpperCase() + "\"" + ","; + map.put("value", 1); + } + + inOutPrefix = "{ifIndex=" + "\"" + index + "\"" + "," + + ifPhysAddresStr + + ifDescrStr + "datacenter=" + "\"" + traffic.getDatacenter() + "\"" + "," + "asset=" + "\"" + traffic.getHost() + "\"" + "," - + tags - + direction + "}" + + tagStr + + directionStr + "}" + " " + map.get("value"); tempMap.put(name, inOutPrefix); @@ -142,6 +163,7 @@ public class TrafficServiceImpl implements TrafficService { Map<Object, List<Map<String, String>>> groupListMap = list.stream().collect(Collectors.groupingBy(map -> map.keySet().toArray()[0])); String r = ""; + String descHelp, descType; for (Map.Entry<Object, List<Map<String, String>>> entry : groupListMap.entrySet()) { name = (String) entry.getKey(); descHelp = desc.get(name)[0] + "\n"; @@ -200,6 +222,33 @@ public class TrafficServiceImpl implements TrafficService { map.put("ifOutQLen", new String[]{"# HELP ifOutQLen The length of the output packet queue (in packets). - 1.3.6.1.2.1.2.2.1.21", "# TYPE ifOutQLen gauge"}); + + map.put("ifAdminStatus", new String[]{"# HELP ifAdminStatus The desired state of the interface - 1.3.6.1.2.1.2.2.1.7", + "# TYPE ifAdminStatus gauge"}); + + map.put("ifDescr", new String[]{"# HELP ifDescr A textual string containing information about the interface - 1.3.6.1.2.1.2.2.1.2", + "# TYPE ifDescr gauge"}); + + map.put("ifIndex", new String[]{"# HELP ifIndex A unique value, greater than zero, for each interface - 1.3.6.1.2.1.2.2.1.1", + "# TYPE ifIndex gauge"}); + + map.put("ifLastChange", new String[]{"# HELP ifLastChange The value of sysUpTime at the time the interface entered its current operational state - 1.3.6.1.2.1.2.2.1.9", + "# TYPE ifLastChange gauge"}); + + map.put("ifMtu", new String[]{"# HELP ifMtu The size of the largest packet which can be sent/received on the interface, specified in octets - 1.3.6.1.2.1.2.2.1.4", + "# TYPE ifMtu gauge"}); + + map.put("ifOperStatus", new String[]{"# HELP ifOperStatus The current operational state of the interface - 1.3.6.1.2.1.2.2.1.8", + "# TYPE ifOperStatus gauge"}); + + map.put("ifPhysAddress", new String[]{"# HELP ifPhysAddress The interface's address at its protocol sub-layer - 1.3.6.1.2.1.2.2.1.6", + "# TYPE ifPhysAddress gauge"}); + + map.put("ifSpeed", new String[]{"# HELP ifSpeed An estimate of the interface's current bandwidth in bits per second - 1.3.6.1.2.1.2.2.1.5", + "# TYPE ifSpeed gauge"}); + + map.put("ifType", new String[]{"# HELP ifType The type of interface - 1.3.6.1.2.1.2.2.1.3", + "# TYPE ifType gauge"}); return map; } diff --git a/src/main/java/com/nis/util/SnmpUtil.java b/src/main/java/com/nis/util/SnmpUtil.java index ed08422..36a7ead 100644 --- a/src/main/java/com/nis/util/SnmpUtil.java +++ b/src/main/java/com/nis/util/SnmpUtil.java @@ -186,6 +186,12 @@ public class SnmpUtil { map.put("oid", vb.getOid().toString()); map.put("value", vb.getVariable().toString()); map.put("type", vb.getVariable().getSyntaxString()); + + // 时间类型值转为long类型 + if("TimeTicks".equals(vb.getVariable().getSyntaxString())){ + TimeTicks variable = (TimeTicks) vb.getVariable(); + map.put("value", variable.getValue()); + } resultData.add(map); } else { // 获取结束 |
