summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/com/nis/job/ConfagentJob.java2
-rw-r--r--src/main/java/com/nis/util/SnmpUtil.java75
-rw-r--r--src/main/java/com/nis/util/YamlUtil.java8
3 files changed, 42 insertions, 43 deletions
diff --git a/src/main/java/com/nis/job/ConfagentJob.java b/src/main/java/com/nis/job/ConfagentJob.java
index ef3d92b..f7389b3 100644
--- a/src/main/java/com/nis/job/ConfagentJob.java
+++ b/src/main/java/com/nis/job/ConfagentJob.java
@@ -162,7 +162,7 @@ public class ConfagentJob extends QuartzJobBean {
String scrapeInterval = sysConfigService.queryValueByParamkey(Constant.SCRAPE_INTERVAL);
Map param = new HashMap();
param.put(Constant.SCRAPE_INTERVAL, scrapeInterval);
- boolean prometheusSettingHandle = YamlUtil.prometheusSettingHandle(param, ymlPath, promserver.getHost());
+ boolean prometheusSettingHandle = YamlUtil.prometheusSettingHandle(param, ymlPath);
logger.info("prometheusSettingHandle result {}", JSON.toJSON(prometheusSettingHandle));
if (prometheusSettingHandle) {
ResponseEntity<String> responseEntity = null;
diff --git a/src/main/java/com/nis/util/SnmpUtil.java b/src/main/java/com/nis/util/SnmpUtil.java
index 5ba1f59..6f018e7 100644
--- a/src/main/java/com/nis/util/SnmpUtil.java
+++ b/src/main/java/com/nis/util/SnmpUtil.java
@@ -21,7 +21,7 @@ public class SnmpUtil {
private static Logger logger = LoggerFactory.getLogger(SnmpUtil.class);
- private static Snmp snmp = null;
+ // private static Snmp snmp = null;
// 默认版本 v2
private static final int DEFAULT_VERSION = SnmpConstants.version2c;
@@ -35,7 +35,7 @@ public class SnmpUtil {
// 默认端口
private static final Integer DEFAULT_PORT = 161;
- public static Target getTatget(Integer version, String community, Map<String, String> auth, String ip, Integer port) {
+ public static Target getTatget(Integer version, String community, Map<String, String> auth, String ip, Integer port, Snmp snmp) {
Target target = null;
if (version == SnmpConstants.version3) {
// 添加用户
@@ -139,6 +139,7 @@ public class SnmpUtil {
*/
public static List<Map> snmpWalk(String ip, Integer port, Integer version, String community, String oid, Map<String, String> auth) throws IOException {
List<Map> resultData = new ArrayList<>();
+ Snmp snmp = null;
TransportMapping transport = null;
try {
transport = new DefaultUdpTransportMapping();
@@ -151,7 +152,7 @@ public class SnmpUtil {
SecurityModels.getInstance().addSecurityModel(usm);
}
- Target target =getTatget(version, community, auth, ip, port);
+ Target target =getTatget(version, community, auth, ip, port, snmp);
snmp.listen();
PDU pdu = getPdu(version);
@@ -163,41 +164,45 @@ public class SnmpUtil {
boolean finished = false;
Map map;
- while (!finished) {
- VariableBinding vb = null;
- ResponseEvent respEvent = snmp.getNext(pdu, target);
-
- PDU response = respEvent.getResponse();
-
- // 为空则直接结束
- if (response == null) {
- finished = true;
- break;
- } else {
- vb = response.get(0);
- }
+ try {
+ while (!finished) {
+ VariableBinding vb = null;
+ ResponseEvent respEvent = snmp.getNext(pdu, target);
+
+ PDU response = respEvent.getResponse();
+
+ // 为空则直接结束
+ if (response == null) {
+ finished = true;
+ break;
+ } else {
+ vb = response.get(0);
+ }
- // 判断是否结束
- finished = checkWalkFinished(targetOID, pdu, vb);
- if (!finished) {
- pdu.setRequestID(new Integer32(0));
- pdu.set(0, vb);
-
- map = new HashMap();
- 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());
+ // 判断是否结束
+ finished = checkWalkFinished(targetOID, pdu, vb);
+ if (!finished) {
+ pdu.setRequestID(new Integer32(0));
+ pdu.set(0, vb);
+
+ map = new HashMap();
+ 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 {
+ // 获取结束
+ snmp.close();
}
- resultData.add(map);
- } else {
- // 获取结束
- snmp.close();
}
+ } catch (IOException e) {
+ logger.error("snmp采集失败,错误信息是:" + e.getMessage() + " pdu是:" + pdu.toString() + " target是:" + target.toString(), e);
}
} finally {
if (snmp != null) {
diff --git a/src/main/java/com/nis/util/YamlUtil.java b/src/main/java/com/nis/util/YamlUtil.java
index bf28375..e598d1b 100644
--- a/src/main/java/com/nis/util/YamlUtil.java
+++ b/src/main/java/com/nis/util/YamlUtil.java
@@ -318,7 +318,7 @@ public class YamlUtil {
* @return
*/
@SuppressWarnings("unchecked")
- public static boolean prometheusSettingHandle(Map<String, Object> param, String ymlPath, String host) {
+ public static boolean prometheusSettingHandle(Map<String, Object> param, String ymlPath) {
boolean flag=false;
if(param!=null && !param.isEmpty() &&ymlPath != null && ymlPath.length() != 0) { //
FileInputStream fileInputStream =null;
@@ -335,12 +335,6 @@ public class YamlUtil {
Map object = (Map) map.get("global");
object.put(Constant.SCRAPE_INTERVAL, param.get(Constant.SCRAPE_INTERVAL)+"s");
- // 外部标签
- Map externalMap = new HashMap();
- // 副本标签
- externalMap.put(Constant.REPLICA_LABEL, host);
- object.put("external_labels", externalMap);
-
out = new FileOutputStream(ymlfile);
osw = new OutputStreamWriter(out, "UTF-8");
y.dump(map,osw);