我是靠谱客的博主 自觉煎蛋,这篇文章主要介绍springboot中使用snmp4j对linux及windows系统进行资源情况监测,现在分享给大家,希望可以做个参考。

前言:上面两篇博客已经分享了分别在windows和linux安装snmp服务的教程,现在开始应用到springboot项目中去,用来监控服务器的内存CPU占用率、磁盘大小、已使用、未使用的磁盘大小、网络上行下行速率等,步骤如下:

1.导入maven包:

复制代码
1
2
3
4
5
6
7
<!--snmp4j--> <dependency> <groupId>org.snmp4j</groupId> <artifactId>snmp4j</artifactId> <version>2.5.11</version> </dependency>

2.Snmp工具类:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import com.alibaba.fastjson.JSON; import com.cecjx.common.config.DictRedisConfig; import com.cecjx.common.domain.DictDO; import com.cecjx.oamonitor.domain.SnmpOid; import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; import org.snmp4j.CommunityTarget; import org.snmp4j.PDU; import org.snmp4j.Snmp; import org.snmp4j.TransportMapping; import org.snmp4j.event.ResponseEvent; import org.snmp4j.event.ResponseListener; import org.snmp4j.mp.SnmpConstants; import org.snmp4j.smi.*; import org.snmp4j.transport.DefaultUdpTransportMapping; import java.io.IOException; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; public class SnmpUtils { private static final Logger logger = Logger.getLogger(SnmpUtils.class); public static CommunityTarget createDefault(String ip, String community, String port) { if (StringUtils.isBlank(ip)) { throw new NullPointerException("ip is null."); } if (StringUtils.isBlank(community)) { throw new NullPointerException("community is null."); } Address address = GenericAddress.parse("udp:" + ip + "/" + port); CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString(community)); target.setAddress(address); target.setVersion(SnmpConstants.version2c); target.setTimeout(3000); target.setRetries(5); return target; } public static List<Map<String, String>> snmpGet(String ip, String community, String port, String oid) { if (StringUtils.isBlank(ip)) { throw new NullPointerException("ip is null."); } if (StringUtils.isBlank(community)) { throw new NullPointerException("community is null."); } List<Map<String, String>> list = null; CommunityTarget target = createDefault(ip, community, port); Snmp snmp = null; try { PDU pdu = new PDU(); pdu.add(new VariableBinding(new OID(oid))); DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping(); snmp = new Snmp(transport); snmp.listen(); pdu.setType(PDU.GET); ResponseEvent respEvent = snmp.send(pdu, target); PDU response = respEvent.getResponse(); if (null != response && response.size() > 0) { list = new ArrayList<Map<String, String>>(); for (VariableBinding vb : response.getVariableBindings()) { Map<String, String> map = new HashMap<String, String>(); map.put(oid, vb.toValueString()); list.add(map); } } } catch (Exception e) { logger.error(e.getMessage()); } finally { closeSnmp(snmp); } return list; } public static void snmpGetList(String ip, String community, String port, List<String> oidList) { if (StringUtils.isBlank(ip)) { throw new NullPointerException("ip is null."); } if (StringUtils.isBlank(community)) { throw new NullPointerException("community is null."); } if (null == oidList || oidList.isEmpty()) { throw new NullPointerException("oidList is null."); } CommunityTarget target = createDefault(ip, community, port); Snmp snmp = null; try { PDU pdu = new PDU(); for (String oid : oidList) { pdu.add(new VariableBinding(new OID(oid))); } DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping(); snmp = new Snmp(transport); snmp.listen(); pdu.setType(PDU.GET); ResponseEvent respEvent = snmp.send(pdu, target); PDU response = respEvent.getResponse(); if (null != response && response.size() > 0) { for (VariableBinding vb : response.getVariableBindings()) { logger.info(vb.getOid() + " = " + vb.getVariable()); } } } catch (Exception e) { logger.error(e.getMessage()); } finally { closeSnmp(snmp); } } public static void snmpAsynGetList(String ip, String community, String port, List<String> oidList) { if (StringUtils.isBlank(ip)) { throw new NullPointerException("ip is null."); } if (StringUtils.isBlank(community)) { throw new NullPointerException("community is null."); } if (null == oidList || oidList.isEmpty()) { throw new NullPointerException("oidList is null."); } CommunityTarget target = createDefault(ip, community, port); Snmp snmp = null; try { PDU pdu = new PDU(); for (String oid : oidList) { pdu.add(new VariableBinding(new OID(oid))); } DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping(); snmp = new Snmp(transport); snmp.listen(); pdu.setType(PDU.GET); /*异步获取*/ final CountDownLatch latch = new CountDownLatch(1); ResponseListener listener = new ResponseListener() { @Override public void onResponse(ResponseEvent event) { ((Snmp) event.getSource()).cancel(event.getRequest(), this); PDU response = event.getResponse(); if (null != response && response.size() > 0) { for (VariableBinding vb : response.getVariableBindings()) { logger.info(vb.getOid() + " = " + vb.getVariable()); } latch.countDown(); } } }; pdu.setType(PDU.GET); snmp.send(pdu, target, null, listener); boolean wait = latch.await(30, TimeUnit.SECONDS); snmp.close(); } catch (Exception e) { logger.error(e.getMessage()); } finally { closeSnmp(snmp); } } public static Map<String, String> snmpWalk(String ip, String community, String port, String targetOid) { CommunityTarget target = createDefault(ip, community, port); TransportMapping transport = null; Snmp snmp = null; Map<String, String> map = new HashMap(); try { transport = new DefaultUdpTransportMapping(); snmp = new Snmp(transport); transport.listen(); PDU pdu = new PDU(); OID targetOID = new OID(targetOid); pdu.add(new VariableBinding(targetOID)); boolean finished = false; while (!finished) { VariableBinding vb = null; ResponseEvent respEvent = snmp.getNext(pdu, target); PDU response = respEvent.getResponse(); if (null == response) { System.out.println("responsePDU == null"); finished = true; break; } else { vb = response.get(0); } finished = checkWalkFinished(targetOID, pdu, vb); if (!finished) { logger.info(vb.getOid() + " = " + vb.getVariable()); pdu.setRequestID(new Integer32(0)); pdu.set(0, vb); map.put(vb.getOid().toString(), vb.getVariable().toString()); } else { snmp.close(); } } } catch (Exception e) { logger.error(e.getMessage()); } finally { closeSnmp(snmp); } return map; } public static void closeSnmp(Snmp snmp) { try { if (null != snmp) { snmp.close(); } } catch (IOException e) { logger.error(e.getMessage()); } } private static boolean checkWalkFinished(OID targetOID, PDU pdu, VariableBinding vb) { boolean finished = false; if (pdu.getErrorStatus() != 0) { logger.info("[true] responsePDU.getErrorStatus() != 0"); logger.info(pdu.getErrorStatusText()); finished = true; } else if (vb.getOid() == null) { logger.info("[true] vb.getOid() == null"); finished = true; } else if (vb.getOid().size() < targetOID.size()) { logger.info("[true] vb.getOid().size() < targetOID.size()"); finished = true; } else if (targetOID.leftMostCompare(targetOID.size(), vb.getOid()) != 0) { logger.info("[true] targetOID.leftMostCompare() != 0"); finished = true; } else if (Null.isExceptionSyntax(vb.getVariable().getSyntax())) { logger.info("[true] Null.isExceptionSyntax(vb.getVariable().getSyntax())"); finished = true; } else if (vb.getOid().compareTo(targetOID) <= 0) { logger.info("[true] Variable received is not " + "lexicographic successor of requested " + "one:"); logger.info(vb.toString() + " <= " + targetOID); finished = true; } return finished; } public static String getPrintSize(long size) { //如果字节数少于1024,则直接以B为单位,否则先除于1024,后3位因太少无意义 if (size < 1024) { return String.valueOf(size) + "B"; } else { size = size / 1024; } //如果原字节数除于1024之后,少于1024,则可以直接以KB作为单位 //因为还没有到达要使用另一个单位的时候 //接下去以此类推 if (size < 1024) { return String.valueOf(size) + "KB"; } else { size = size / 1024; } if (size < 1024) { //因为如果以MB为单位的话,要保留最后1位小数, //因此,把此数乘以100之后再取余 size = size * 100; return String.valueOf((size / 100)) + "." + String.valueOf((size % 100)) + "MB"; } else { //否则如果要以GB为单位的,先除于1024再作同样的处理 size = size * 100 / 1024; return String.valueOf((size / 100)) + "." + String.valueOf((size % 100)) + "GB"; } } //从redis获取字典表里的oid数据 public static SnmpOid getOid(RedisUtil redisUtil, String oidType) { SnmpOid snmpOid = new SnmpOid(); Object obj = redisUtil.getObject(DictRedisConfig.dictResisPrefix + oidType); List<DictDO> oidList = JSON.parseArray(redisUtil.getObject(DictRedisConfig.dictResisPrefix + oidType).toString(), DictDO.class); if (oidList.size() > 0) { for (DictDO dictDO : oidList) { switch (dictDO.getName()) { case "sysDescOid": snmpOid.setSysDescOid(dictDO.getValue()); break; case "ramTotalOid": snmpOid.setRamTotalOid(dictDO.getValue()); break; case "ramAvailableOid": snmpOid.setRamAvailableOid(dictDO.getValue()); break; case "ramSharedOid": snmpOid.setRamSharedOid(dictDO.getValue()); break; case "ramBufferOid": snmpOid.setRamBufferOid(dictDO.getValue()); break; case "ramCachedOid": snmpOid.setRamCachedOid(dictDO.getValue()); break; case "cpuProcessorLoadOid": snmpOid.setCpuProcessorLoadOid(dictDO.getValue()); break; case "diskPathOid": snmpOid.setDiskPathOid(dictDO.getValue()); break; case "diskTypeOid": snmpOid.setDiskTypeOid(dictDO.getValue()); break; case "diskAllocationUnitsOid": snmpOid.setDiskAllocationUnitsOid(dictDO.getValue()); break; case "diskStorageSizeOid": snmpOid.setDiskStorageSizeOid(dictDO.getValue()); break; case "diskUsedOid": snmpOid.setDiskUsedOid(dictDO.getValue()); break; case "mainDiskUsedOid": snmpOid.setMainDiskUsedOid(dictDO.getValue()); break; case "sendOid": snmpOid.setSendOid(dictDO.getValue()); break; case "receiveOid": snmpOid.setReceiveOid(dictDO.getValue()); break; case "cpuIdleOId": snmpOid.setCpuIdleOId(dictDO.getValue()); break; } } return snmpOid; } else { return null; } } //统一判断snmp的返回数据是否有误 public static Boolean ifListTrue(List<Map<String, String>> list, String oid) { return list != null && list.size() > 0 && list.get(0).get(oid) != null && !list.get(0).get(oid).equals("noSuchInstance") && !list.get(0).get(oid).equals("noSuchObject"); } public static String getChinese(String octetString) { //snmp4j遇到中文直接转成16进制字符串 try { String[] temps = octetString.split(":"); byte[] bs = new byte[temps.length]; for (int i = 0; i < temps.length; i++) bs[i] = (byte) Integer.parseInt(temps[i], 16); return new String(bs, "GB2312"); } catch (Exception e) { return octetString; } } public static void main(String[] args) { String memTotal = ".1.3.6.1.4.1.2021.4.5.0"; String memAvailable = ".1.3.6.1.4.1.2021.4.6.0"; String memShared = ".1.3.6.1.4.1.2021.4.13.0"; String memBuffer = ".1.3.6.1.4.1.2021.4.14.0"; String memCached = ".1.3.6.1.4.1.2021.4.15.0"; List<Map<String, String>> memTotalList = SnmpUtils.snmpGet("", "public", "161", memTotal); List<Map<String, String>> memAvailableList = SnmpUtils.snmpGet("", "public", "161", memAvailable); List<Map<String, String>> memSharedList = SnmpUtils.snmpGet("", "public", "161", memShared); List<Map<String, String>> memBufferList = SnmpUtils.snmpGet("", "public", "161", memBuffer); List<Map<String, String>> memCachedList = SnmpUtils.snmpGet("", "public", "161", memCached); Long memTotalTemp = Long.parseLong(memTotalList.get(0).get(memTotal)); Long memAvailableTemp = Long.parseLong(memAvailableList.get(0).get(memAvailable)); Long memSharedTemp = Long.parseLong(memSharedList.get(0).get(memShared)); Long memBufferTemp = Long.parseLong(memBufferList.get(0).get(memBuffer)); Long memCachedTemp = Long.parseLong(memCachedList.get(0).get(memCached)); if (memSharedTemp + memAvailableTemp + memCachedTemp > memTotalTemp) { Long temp = memTotalTemp - memAvailableTemp - memBufferTemp - memCachedTemp + memSharedTemp; String used = new DecimalFormat("0").format(temp * 100 / memTotalTemp); System.out.println(temp); System.out.println(memTotalTemp); System.out.println(used); System.out.println("111"); } else { Long temp = memTotalTemp - memAvailableTemp - memBufferTemp - memCachedTemp; String used = new DecimalFormat("0").format(temp * 100 / memTotalTemp); System.out.println(temp); System.out.println(memTotalTemp); System.out.println(used); System.out.println("222"); } } }

3.windows监测所使用的oid

参考:https://blog.csdn.net/liuhuango123/article/details/52253953

4.linux监测所使用的oid

参考:https://blog.csdn.net/apple_llb/article/details/50494787

5.根据业务需求采用所需的oid通过调用util工具类里面的snmpGet及snmpWalk方法即可

例子:获取windows磁盘总大小

复制代码
1
2
3
4
5
6
7
public static void main(String[] args) { String memTotal = ".1.3.6.1.4.1.2021.4.5.0"; List<Map<String, String>> memTotalList = SnmpUtils.snmpGet("ip", "public", "161", memTotal); Long memTotalTemp = Long.parseLong(memTotalList.get(0).get(memTotal)); System.out.println(memTotalTemp); } }

 

最后

以上就是自觉煎蛋最近收集整理的关于springboot中使用snmp4j对linux及windows系统进行资源情况监测的全部内容,更多相关springboot中使用snmp4j对linux及windows系统进行资源情况监测内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(94)

评论列表共有 0 条评论

立即
投稿
返回
顶部