天一电厂UPS动环数据对接,SNMP协议
whycxzp
2024-02-29 ac476c867af05823e33c7746eb2ec37dd26abe7a
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
package com.whyc.config;
 
import lombok.extern.slf4j.Slf4j;
import org.snmp4j.*;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.PrivDES;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
 
import java.io.IOException;
 
/**
 * SNMP4j配置类
 */
@Configuration
@Slf4j
public class SNMPConfig {
 
    @Value("${snmp.version}")
    private int version;
 
    @Value("${snmp.address}")
    private String address;
 
    @Value("${snmp.community}")
    private String community;
 
    @Value("${snmp.retries}")
    private int retries;
 
    @Value("${snmp.timeout}")
    private int timeout;
 
    private UdpAddress udpAddress;
 
    @Bean
    @DependsOn("address")
    public TransportMapping<UdpAddress> transportMapping() throws IOException {
        //DefaultUdpTransportMapping transportMapping = new DefaultUdpTransportMapping(udpAddress);
        DefaultUdpTransportMapping transportMapping = new DefaultUdpTransportMapping();
        // 开启监听
        // 一定要开启,不然没有response
        transportMapping.listen();
        return transportMapping;
    }
 
    // 必须要的
    @Bean
    public Snmp snmp() throws IOException {
        return new Snmp(transportMapping());
    }
 
    // target地址
    @Bean
    public Address address(){
        this.udpAddress = new UdpAddress(this.address);
        return udpAddress;
    }
 
    @Bean
    @DependsOn("address")
    public Target target() throws IOException {
        log.debug("------------------------【加载target】--------------------------");
        log.debug("target version: {}", this.version);
        log.debug("target address: {}", this.address);
        log.debug("target community: {}", this.community);
        log.debug("target retries: {}", this.retries);
        log.debug("target timeout: {}", this.timeout);
        Target target = null;
        /*if (version == SnmpConstants.version3) {
            // 添加用户
            snmp().getUSM().addUser(new OctetString("MD5DES"),new UsmUser(new OctetString("MD5DES"), AuthMD5.ID,new OctetString("MD5DESUserAuthPassword"), PrivDES.ID, new OctetString("MD5DESUserPrivPassword")));
            target = new UserTarget();
            // 设置安全级别
            ((UserTarget) target).setSecurityLevel(SecurityLevel.AUTH_PRIV);
            ((UserTarget) target).setSecurityName(new OctetString("MD5DES"));
            target.setVersion(SnmpConstants.version3);
        } else {
            target = new CommunityTarget();
            if (version == SnmpConstants.version1) {
                target.setVersion(SnmpConstants.version1);
                ((CommunityTarget) target).setCommunity(new OctetString(this.community));
            } else {
                target.setVersion(SnmpConstants.version2c);
                ((CommunityTarget) target).setCommunity(new OctetString(this.community));
            }
 
        }*/
        target = new CommunityTarget();
        target.setVersion(SnmpConstants.version2c);
        ((CommunityTarget) target).setCommunity(new OctetString(this.community));
        // 目标对象相关设置
        target.setAddress(this.udpAddress);
        target.setRetries(this.retries);
        target.setTimeout(this.timeout);
        log.debug("【target对象】: {}", target.toString());
        return target;
    }
}