kongdeqiang
2023-05-11 d4e6ec0389dd8abbcce4ee4f9e5cdad1633491d5
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package com.wgcloud.util;
 
import com.wgcloud.entity.SnmpInfo;
import com.wgcloud.entity.SnmpInterfaceInfo;
import com.wgcloud.util.staticvar.StaticKeys;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snmp4j.*;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.SecurityModel;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;
 
import java.io.IOException;
import java.net.InetAddress;
import java.util.*;
 
/**
 * @version V1.0
 * @ClassName:SnmpUtil.java
 * @author: wgcloud
 * @date: 2022年4月10日
 * @Description: snmp监测工具类
 * @Copyright: 2019-2021 wgcloud. All rights reserved.
 */
public class SnmpUtil {
 
    private static final Logger logger = LoggerFactory.getLogger(SnmpUtil.class);
 
 
    /**
     * 设定CommunityTarget
     *
     * @param ip          设备IP
     * @param community   团体名称
     * @param port        snmp服务的端口号,默认161
     * @param snmpVersion snmp版本,0对应version1,1对应version2c,3对应version3
     * @return
     */
    public static Target createDefault(String ip, String community, String port, int snmpVersion) {
        if (StringUtils.isBlank(ip)) {
            logger.error("ip is null.");
            return null;
        }
 
        if (StringUtils.isBlank(community)) {
            logger.error("community is null.");
            return null;
        }
 
        //定义远程主机的地址
        Address address = GenericAddress.parse("udp:" + ip + "/" + port);
        //设定CommunityTarget
        Target target = null;
 
        //设置使用的snmp版本 begin
        if (snmpVersion == SnmpConstants.version3) {
            target = new UserTarget();
            //snmpV3需要设置安全级别和安全名称,其中安全名称是创建snmp指定user设置的new OctetString("SNMPV3")
            target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
            target.setSecurityName(new OctetString(community));
        } else {
            //snmpV1和snmpV2需要指定团体名名称
            target = new CommunityTarget();
            ((CommunityTarget) target).setCommunity(new OctetString(community));
            if (snmpVersion == SnmpConstants.version2c) {
                target.setSecurityModel(SecurityModel.SECURITY_MODEL_SNMPv2c);
            }
        }
        target.setVersion(snmpVersion);
        //设置使用的snmp版本 end
 
        // 设定机器的地址
        target.setAddress(address);
        //设置超时的时间,毫秒
        target.setTimeout(15000);
        //设置超时重试次数
        target.setRetries(2);
        return target;
    }
 
    /**
     * 处理snmp设备集合,标注设备是否在线,返回MAP集合<IP,状态1在线2下线>
     *
     * @param snmpInfoAllList
     * @return
     */
    public static Map<String, String> getOnLineList(List<SnmpInfo> snmpInfoAllList) {
        Map<String, String> resultMap = new HashMap<>();
        Set<String> set = new HashSet<>();
        for (SnmpInfo snmpInfo : snmpInfoAllList) {
            set.add(snmpInfo.getHostname());
        }
        for (String hostName : set) {
            if (!isEthernetConnection(hostName)) {
                resultMap.put(hostName, StaticKeys.DOWN_STATE);
            } else {
                resultMap.put(hostName, StaticKeys.ON_STATE);
            }
        }
        logger.info("snmp设备在线集合:" + resultMap.toString());
        return resultMap;
    }
 
    /**
     * 测网络通不通 类似 ping ip,通的话返回true,不通返回false
     *
     * @param ip
     * @return
     * @throws IOException
     */
    public static boolean isEthernetConnection(String ip) {
        try {
            InetAddress ad = InetAddress.getByName(ip);
            // 测试是否可以达到该地址 3秒超时
            boolean state = ad.isReachable(3000);
            return state;
        } catch (Exception e) {
            logger.error("isEthernetConnection错误", e);
        }
        return false;
    }
 
    /**
     * 获取设备流量累积和平均值
     *
     * @param snmpInfo
     * @return
     */
    public static SnmpInfo getAvgSnmpInfo(SnmpInfo snmpInfo) {
        SnmpInfo snmpInfo2 = new SnmpInfo();
        try {
            /*if (!isEthernetConnection(snmpInfo.getHostname())) {
                return null;
            }*/
            SnmpInfo snmpInfo1 = SnmpUtil.snmpGet(snmpInfo);
            long beginBytesRecv = Long.valueOf(snmpInfo1.getBytesRecv());
            long beginBytesSent = Long.valueOf(snmpInfo1.getBytesSent());
 
            //暂停3秒,再次获取流量,然后计算平均值
            Thread.sleep(3000);
 
            snmpInfo2 = SnmpUtil.snmpGet(snmpInfo);
            long endBytesRecv = Long.valueOf(snmpInfo2.getBytesRecv());
            long endBytesSent = Long.valueOf(snmpInfo2.getBytesSent());
 
            double recvAvg = (endBytesRecv - beginBytesRecv) / (double) 3;
            double sentAvg = (endBytesSent - beginBytesSent) / (double) 3;
            snmpInfo2.setRecvAvg(FormatUtil.formatDouble(recvAvg, 2) + "");
            snmpInfo2.setSentAvg(FormatUtil.formatDouble(sentAvg, 2) + "");
 
            //CPU使用率% = 100 - 空闲CPU使用率%
            if (!StringUtils.isEmpty(snmpInfo.getCpuPerOID())) {
                double cpuPer = 100 - Double.valueOf(snmpInfo2.getCpuPer());
                snmpInfo2.setCpuPer(FormatUtil.formatDouble(cpuPer, 2) + "");
            } else {
                snmpInfo2.setCpuPer("0");
            }
 
            //计算内存使用率% begin
            if (!StringUtils.isEmpty(snmpInfo.getMemSizeOID()) && !StringUtils.isEmpty(snmpInfo.getMemTotalSizeOID())) {
                long memSize = Long.valueOf(snmpInfo2.getMemSize());
                long memTotalSize = Long.valueOf(snmpInfo2.getMemTotalSize());
                snmpInfo2.setMemPer(FormatUtil.formatDouble(((double) memSize / memTotalSize) * 100, 2) + "");
            } else {
                snmpInfo2.setMemPer("0");
            }
            //计算内存使用率% end
 
            return snmpInfo2;
        } catch (Exception e) {
            logger.error("getAvgSnmpInfo错误", e);
            snmpInfo2.setRecvAvg("0");
            snmpInfo2.setSentAvg("0");
            snmpInfo2.setMemSize("0");
            snmpInfo2.setMemTotalSize("1");
            snmpInfo2.setCpuPer("0");
            snmpInfo2.setMemPer("0");
        }
        return snmpInfo2;
    }
 
    /**
     * 获取接口信息
     *
     * @param snmpInfo
     * @return
     */
    public static List<SnmpInterfaceInfo> getInterfaceSnmpInfo(SnmpInfo snmpInfo) {
 
        //接口名称OID
        String interfaceNameOID = snmpInfo.getInterfaceNameOID();
        //接口状态 OID
        String interfaceStatusOID = snmpInfo.getInterfaceStatusOID();
        //速率 OID
        String actualSpeedOID = snmpInfo.getActualSpeedOID();
        //工作模式OID
        String workMTUOID = snmpInfo.getWorkMTUOID();
        //双工模式OID
        String actualMTUOID = snmpInfo.getActualMTUOID();
        //环回检测 OID
        String loopbackOID = snmpInfo.getLoopbackOID();
        //描述OID
        String descriptionOID = snmpInfo.getDescriptionOID();
 
        List<Map<String,Object>> interfaceStatusList = new ArrayList<>();
        List<Map<String,Object>> interfaceNameList = new ArrayList<>();
        List<Map<String,Object>> actualSpeedList = new ArrayList<>();
        List<Map<String,Object>> workMTUList = new ArrayList<>();
        List<Map<String,Object>> actualMTUList = new ArrayList<>();
        List<Map<String,Object>> loopbackList = new ArrayList<>();
        List<Map<String,Object>> descriptionList = new ArrayList<>();
 
        List<SnmpInterfaceInfo> interfaceInfos = new ArrayList<>();
 
        if(StringUtils.isNotEmpty(interfaceNameOID)){
            interfaceNameList = snmpGetInterface(snmpInfo, interfaceNameOID);
        }
        if(StringUtils.isNotEmpty(interfaceStatusOID)){
            interfaceStatusList = snmpGetInterface(snmpInfo, interfaceStatusOID);
        }
        if(StringUtils.isNotEmpty(actualSpeedOID)){
            actualSpeedList = snmpGetInterface(snmpInfo, actualSpeedOID);
        }
        if(StringUtils.isNotEmpty(workMTUOID)){
            workMTUList = snmpGetInterface(snmpInfo, workMTUOID);
        }
        if(StringUtils.isNotEmpty(actualMTUOID)){
            actualMTUList = snmpGetInterface(snmpInfo, actualMTUOID);
        }
        if(StringUtils.isNotEmpty(loopbackOID)){
            loopbackList = snmpGetInterface(snmpInfo, loopbackOID);
        }
        if(StringUtils.isNotEmpty(descriptionOID)){
            descriptionList = snmpGetInterface(snmpInfo, descriptionOID);
        }
 
        if (interfaceNameList.size() >0 ){
            for (int i = 0; i < interfaceNameList.size(); i++) {
                Map<String, Object> stringObjectMap = interfaceNameList.get(i);
                for (Map.Entry<String, Object> stringObjectEntry : stringObjectMap.entrySet()) {
                    SnmpInterfaceInfo snmpInterfaceInfo = new SnmpInterfaceInfo();
                    snmpInterfaceInfo.setId(UUIDUtil.getUUID());
                    snmpInterfaceInfo.setCreateTime(new Date());
                    snmpInterfaceInfo.setHostname(snmpInfo.getHostname());
                    snmpInterfaceInfo.setInterfaceName(stringObjectEntry.getValue().toString().trim());
                    interfaceInfos.add(snmpInterfaceInfo);
                }
            }
        }
        if(interfaceStatusList.size() > 0){
            for (int i = 0; i < interfaceStatusList.size(); i++) {
                Map<String, Object> stringObjectMap = interfaceStatusList.get(i);
                SnmpInterfaceInfo snmpInterfaceInfo = interfaceInfos.get(i);
                for (Map.Entry<String, Object> stringObjectEntry : stringObjectMap.entrySet()) {
                    try {
                        snmpInterfaceInfo.setIsUp(Integer.parseInt(stringObjectEntry.getValue().toString().trim()));
                    }catch (Exception e){
                        snmpInterfaceInfo.setIsUp(0);
                        e.printStackTrace();
                    }
                }
            }
        }
        if (actualSpeedList.size()>0){
            for (int i = 0; i < actualSpeedList.size(); i++) {
                Map<String, Object> stringObjectMap = actualSpeedList.get(i);
                SnmpInterfaceInfo snmpInterfaceInfo = interfaceInfos.get(i);
                for (Map.Entry<String, Object> stringObjectEntry : stringObjectMap.entrySet()) {
                    try {
                        snmpInterfaceInfo.setActualSpeed(Long.parseLong(stringObjectEntry.getValue().toString().trim()));
                    }catch (Exception e){
                        snmpInterfaceInfo.setActualSpeed(100000L);
                        e.printStackTrace();
                    }
                }
            }
        }
        if (workMTUList.size()>0){
            for (int i = 0; i < workMTUList.size(); i++) {
                Map<String, Object> stringObjectMap = workMTUList.get(i);
                SnmpInterfaceInfo snmpInterfaceInfo = interfaceInfos.get(i);
                for (Map.Entry<String, Object> stringObjectEntry : stringObjectMap.entrySet()) {
                    try {
                        snmpInterfaceInfo.setWorkMTU(Integer.parseInt(stringObjectEntry.getValue().toString().trim()));
                    }catch (Exception e){
                        snmpInterfaceInfo.setWorkMTU(1500);
                        e.printStackTrace();
                    }
                }
            }
        }
        if (actualMTUList.size()>0){
            for (int i = 0; i < actualMTUList.size(); i++) {
                Map<String, Object> stringObjectMap = actualMTUList.get(i);
                SnmpInterfaceInfo snmpInterfaceInfo = interfaceInfos.get(i);
                for (Map.Entry<String, Object> stringObjectEntry : stringObjectMap.entrySet()) {
                    try {
                        snmpInterfaceInfo.setActualMTU(Integer.parseInt(stringObjectEntry.getValue().toString().trim()));
                    }catch (Exception e){
                        snmpInterfaceInfo.setActualMTU(1500);
                        e.printStackTrace();
                    }
                }
            }
        }
        if (loopbackList.size()>0){
            for (int i = 0; i < loopbackList.size(); i++) {
                Map<String, Object> stringObjectMap = loopbackList.get(i);
                SnmpInterfaceInfo snmpInterfaceInfo = interfaceInfos.get(i);
                for (Map.Entry<String, Object> stringObjectEntry : stringObjectMap.entrySet()) {
                    try {
                        snmpInterfaceInfo.setLoopback(Integer.parseInt(stringObjectEntry.getValue().toString().trim()));
                    }catch (Exception e){
                        snmpInterfaceInfo.setLoopback(0);
                        e.printStackTrace();
                    }
                }
            }
        }
        if (descriptionList.size()>0){
            for (int i = 0; i < descriptionList.size(); i++) {
                Map<String, Object> stringObjectMap = descriptionList.get(i);
                SnmpInterfaceInfo snmpInterfaceInfo = interfaceInfos.get(i);
                for (Map.Entry<String, Object> stringObjectEntry : stringObjectMap.entrySet()) {
                    try {
                        snmpInterfaceInfo.setDescription(stringObjectEntry.getValue().toString().trim());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
        return interfaceInfos;
    }
 
    /**
     * snmpGet方式 获取交换机设备的发送、接收流量数据
     */
    public static SnmpInfo snmpGet(SnmpInfo snmpInfo) {
        Snmp snmp = null;
        TransportMapping transport = null;
        SnmpInfo snmpInfoRes = new SnmpInfo();
        Vector<? extends VariableBinding> vector = null;
        try {
            //设备IP
            String ip = snmpInfo.getHostname();
            //团体名称
            String community = snmpInfo.getSnmpCommunity();
            //snmp服务的端口号,默认161
            String port = snmpInfo.getSnmpPort();
            //snmp版本,0对应version1,1对应version2c,3对应version3
            int snmpVersion = Integer.valueOf(snmpInfo.getSnmpVersion());
            //上行出口流量字节数 OID
            String sendOID = snmpInfo.getSentOID();
            //下行入口流量字节数 OID
            String receiveOID = snmpInfo.getRecvOID();
            //空闲%CPU使用率 OID
            String cpuPerOID = snmpInfo.getCpuPerOID();
            //内存已使用大小 OID
            String memSizeOID = snmpInfo.getMemSizeOID();
            //内存总大小 OID
            String memTotalSizeOID = snmpInfo.getMemTotalSizeOID();
            if (StringUtils.isEmpty(sendOID) || StringUtils.isEmpty(receiveOID)) {
                snmpInfoRes.setBytesSent("0");
                snmpInfoRes.setBytesRecv("0");
            }
            if (StringUtils.isEmpty(cpuPerOID)) {
                snmpInfoRes.setCpuPer("100");
            }
            if (StringUtils.isEmpty(memSizeOID)) {
                snmpInfoRes.setMemSize("0");
            }
            if (StringUtils.isEmpty(memTotalSizeOID)) {
                snmpInfoRes.setMemTotalSize("1");
            }
            //设定CommunityTarget
            Target myTarget = createDefault(ip, community, port, snmpVersion);
 
            // 设定采取的协议 // 设定传输协议为UDP
            transport = new DefaultUdpTransportMapping();
            // 调用TransportMapping中的listen()方法,启动监听进程,接收消息,由于该监听进程是守护进程,最后应调用close()方法来释放该进程
            transport.listen();
            // 创建SNMP对象,用于发送请求PDU
            snmp = new Snmp(transport);
            // 创建请求pdu,获取mib
            PDU request = new PDU();
 
            // 调用的add方法绑定要查询的OID
            //上行,端口出口流量(byte),绑定多个OID
            String[] sendOIDs = sendOID.split(StaticKeys.SPLIT_ENTER);
            for (String sendOIDTmp : sendOIDs) {
                request.add(new VariableBinding(new OID(sendOIDTmp.trim())));
            }
 
            //下行,端口入口流量(byte),绑定多个OID
            String[] receiveOIDs = receiveOID.split(StaticKeys.SPLIT_ENTER);
            for (String receiveOIDTmp : receiveOIDs) {
                request.add(new VariableBinding(new OID(receiveOIDTmp.trim())));
            }
 
            //绑定空闲cpu使用率OID
            if (!StringUtils.isEmpty(cpuPerOID)) {
                request.add(new VariableBinding(new OID(cpuPerOID.trim())));
            }
 
            //绑定内存已使用大小OID和内存总大小OID
            if (!StringUtils.isEmpty(memSizeOID) && !StringUtils.isEmpty(memTotalSizeOID)) {
                request.add(new VariableBinding(new OID(memSizeOID.trim())));
                request.add(new VariableBinding(new OID(memTotalSizeOID.trim())));
            }
 
 
            // 调用setType()方法来确定该pdu的类型
            request.setType(PDU.GET);
            // 调用 send(PDU pdu,Target target)发送pdu,返回一个ResponseEvent对象
            ResponseEvent responseEvent = snmp.send(request, myTarget);
            // 通过ResponseEvent对象来获得SNMP请求的应答pdu,方法:public PDU getResponse()
            PDU response = responseEvent.getResponse();
            vector = response.getVariableBindings();
 
            long bytesSentSum = 0;
            long bytesRecvSum = 0;
 
            // 通过应答pdu获得mib信息(之前绑定的OID的值),方法:VaribleBinding get(int index)
            for (int i = 0; i < vector.size(); i++) {
                try {
                    VariableBinding vb1 = vector.get(i);
                    if (sendOID.contains(String.valueOf(vb1.getOid()))) {
                        bytesSentSum += Long.valueOf(String.valueOf(vb1.getVariable()));
                        continue;
                    }
                    if (receiveOID.contains(String.valueOf(vb1.getOid()))) {
                        bytesRecvSum += Long.valueOf(String.valueOf(vb1.getVariable()));
                        continue;
                    }
                    if (cpuPerOID.contains(String.valueOf(vb1.getOid()))) {
                        snmpInfoRes.setCpuPer(String.valueOf(vb1.getVariable()));
                        continue;
                    }
                    if (memSizeOID.contains(String.valueOf(vb1.getOid()))) {
                        snmpInfoRes.setMemSize(String.valueOf(vb1.getVariable()));
                        continue;
                    }
                    if (memTotalSizeOID.contains(String.valueOf(vb1.getOid()))) {
                        snmpInfoRes.setMemTotalSize(String.valueOf(vb1.getVariable()));
                        continue;
                    }
                } catch (Exception e) {
                    logger.error("snmp应答pdu获得mib信息错误", e);
                }
            }
            snmpInfoRes.setBytesSent(String.valueOf(bytesSentSum));
            snmpInfoRes.setBytesRecv(String.valueOf(bytesRecvSum));
            return snmpInfoRes;
        } catch (Exception e) {
            logger.error("snmp检测错误", e);
        } finally {
            // 调用close()方法释放该进程
            closeTransport(transport);
            closeSnmp(snmp);
        }
        return snmpInfoRes;
    }
 
 
    /**
     * WALK方式请求
     *功能简介:这个方法是getnext,就是遍历该oid下的所有子节点
     */
    public static List<Map<String,Object>> snmpGetInterface(SnmpInfo snmpInfo, String oid) {
        Snmp snmp = null;
        try {
            List<Map<String,Object>> list = new ArrayList<>();
 
            //1、初始化snmp,并开启监听
            snmp = new Snmp(new DefaultUdpTransportMapping());
            //开启监听
            snmp.listen();
            //2、创建目标对象
            Target target = createTarget(snmpInfo.getHostname(),snmpInfo.getSnmpPort(),snmpInfo.getSnmpCommunity());
            //3、创建报文
            PDU pdu = createPDU(Integer.parseInt(snmpInfo.getSnmpVersion()), PDU.GETNEXT,oid );
            //4、发送报文,并获取返回结果
            boolean matched = true;
            while (matched) {
                ResponseEvent responseEvent = snmp.send(pdu, target);
                if (responseEvent == null || responseEvent.getResponse() == null) {
                    //LogUtil.info("snmp TimeOut...");
                    break;
                }
                PDU response = responseEvent.getResponse();
                String nextOid = null;
                Vector<? extends VariableBinding> variableBindings = response.getVariableBindings();
                for (int i = 0; i < variableBindings.size(); i++) {
 
                    VariableBinding variableBinding = variableBindings.elementAt(i);
                    Variable variable = variableBinding.getVariable();
                    nextOid = variableBinding.getOid().toDottedString();
                    //如果不是这个节点下的oid则终止遍历,否则会输出很多,直到整个遍历完。
                    if (!nextOid.startsWith(oid)) {
                        matched = false;
                        break;
                    }
                    Map<String,Object> map = new HashMap();
                    //放入oid以及oid对应获得的值
                    map.put(nextOid,variable);
                    list.add(map);
                }
                if (!matched) {
                    break;
                }
                pdu.clear();
                pdu.add(new VariableBinding(new OID(nextOid)));
            }
            System.out.println("list的大小 = " +list.size());
            System.out.println(list);
            return list;
 
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
 
    private static PDU createPDU(int version, int type, String oid) {
        PDU pdu = null;
        if (version == SnmpConstants.version3) {
            pdu = new ScopedPDU();
        } else {
            pdu = new PDUv1();
        }
        pdu.setType(type);
        pdu.add(new VariableBinding(new OID(oid.trim())));
//        //接口状态OID
//        if (!StringUtils.isEmpty(actualSpeedOID)) {
//            pdu.add(new VariableBinding(new OID(interfaceStatusOID.trim())));
//        }
//        //绑定速率 OID
//        if (!StringUtils.isEmpty(actualSpeedOID)) {
//            pdu.add(new VariableBinding(new OID(actualSpeedOID.trim())));
//        }
//        //工作模式OID
//        if (!StringUtils.isEmpty(workMTUOID)) {
//            pdu.add(new VariableBinding(new OID(workMTUOID.trim())));
//        }
//        //双工模式OID
//        if (!StringUtils.isEmpty(actualMTUOID)) {
//            pdu.add(new VariableBinding(new OID(actualMTUOID.trim())));
//        }
//        //环回检测 OID
//        if (!StringUtils.isEmpty(loopbackOID)) {
//            pdu.add(new VariableBinding(new OID(loopbackOID.trim())));
//        }
//        //描述OID
//        if (!StringUtils.isEmpty(descriptionOID)) {
//            pdu.add(new VariableBinding(new OID(descriptionOID.trim())));
//        }
        return pdu;
    }
 
 
    private static Target createTarget(String hostIp, String port, String community) {
        //先得到连接的ip和端口号
        String addressGet = "udp:" + hostIp+"/"+port;
 
        Target target = null;
        int version = 1;
        if (!(version == SnmpConstants.version3 || version == SnmpConstants.version2c || version == SnmpConstants.version1)) {
            return target;
        }
//        if (version == SnmpConstants.version3) {
//            target = new UserTarget();
//            //snmpV3需要设置安全级别和安全名称,其中安全名称是创建snmp指定user设置的new OctetString("SNMPV3")
//            target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
//            target.setSecurityName(new OctetString(this.username));
//        } else
        {
            //snmpV1和snmpV2需要指定团体名名称
            target = new CommunityTarget();
            ((CommunityTarget) target).setCommunity(new OctetString(community));
            if (version == SnmpConstants.version2c) {
                target.setSecurityModel(SecurityModel.SECURITY_MODEL_SNMPv2c);
            }
        }
        target.setVersion(version);
        //必须指定,没有设置就会报错。
        target.setAddress(GenericAddress.parse(addressGet));
        target.setRetries(3);
        target.setTimeout(2000);
        return target;
    }
 
 
 
    /**
     * 调用close()方法释放该进程
     *
     * @param transport
     */
    private static void closeTransport(TransportMapping transport) {
        try {
            if (null != transport) {
                transport.close();
            }
        } catch (Exception e) {
            logger.error("closeTransport错误", e);
        }
    }
 
    /**
     * 关闭snmp
     *
     * @param snmp
     */
    private static void closeSnmp(Snmp snmp) {
        try {
            if (null != snmp) {
                snmp.close();
            }
        } catch (Exception e) {
            logger.error("closeSnmp错误", e);
        }
    }
 
    public static void main(String[] args) {
        SnmpInfo snmpInfo = new SnmpInfo();
        snmpInfo.setHostname("192.168.8.150");
        snmpInfo.setSnmpCommunity("public");
        snmpInfo.setSnmpVersion("1");
        snmpInfo.setSnmpPort("161");
        snmpInfo.setInterfaceNameOID("1.3.6.1.2.1.31.1.1.1.1");
        snmpInfo.setInterfaceStatusOID("1.3.6.1.2.1.2.2.1.8");
        snmpInfo.setActualSpeedOID("1.3.6.1.2.1.2.2.1.5");
        snmpInfo.setActualMTUOID("1.3.6.1.2.1.10.7.2.1.19");
        snmpInfo.setDescriptionOID("1.3.6.1.2.1.2.2.1.2");
        List<Map<String, Object>> maps = snmpGetInterface(snmpInfo, snmpInfo.getDescriptionOID());
        for (Map<String, Object> map : maps) {
            for (Map.Entry<String, Object> stringObjectEntry : map.entrySet()) {
                Object value = stringObjectEntry.getValue();
                System.out.println(value);
            }
        }
    }
 
 
}