Bonjour,
J'ai récupéré ce code qui permet de recevoir les traps snmp :
Ce code fonctionne bien, il m'affiche les traps dans la console, mais je ne trouve pas cette commande qui affiche ces traps dans le code et je ne peux donc pas traiter les traps reçues
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package DBNET_Serveur; /*_############################################################################ 2 _## 3 _## SNMP4J - MultiThreadedTrapReceiver.java 4 _## 5 _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org) 6 _## 7 _## Licensed under the Apache License, Version 2.0 (the "License"); 8 _## you may not use this file except in compliance with the License. 9 _## You may obtain a copy of the License at 10 _## 11 _## http://www.apache.org/licenses/LICENSE-2.0 12 _## 13 _## Unless required by applicable law or agreed to in writing, software 14 _## distributed under the License is distributed on an "AS IS" BASIS, 15 _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 _## See the License for the specific language governing permissions and 17 _## limitations under the License. 18 _## 19 _##########################################################################*/ import org.apache.log4j.BasicConfigurator; import org.snmp4j.Snmp; import org.snmp4j.smi.Address; import org.snmp4j.security.AuthMD5; import org.snmp4j.security.PrivDES; import org.snmp4j.security.SecurityProtocols; import org.snmp4j.security.UsmUser; import org.snmp4j.smi.UdpAddress; import org.snmp4j.security.SecurityModels; import org.snmp4j.TransportMapping; import org.snmp4j.smi.OctetString; import org.snmp4j.transport.DefaultTcpTransportMapping; import org.snmp4j.asn1.BER; import org.snmp4j.event.ResponseEvent; import org.snmp4j.log.Log4jLogFactory; import org.snmp4j.log.LogFactory; import org.snmp4j.mp.MPv3; import org.snmp4j.transport.DefaultUdpTransportMapping; import org.snmp4j.security.USM; import org.snmp4j.smi.GenericAddress; import java.net.UnknownHostException ; import java.io.IOException ; import org.snmp4j.util.MultiThreadedMessageDispatcher; import org.snmp4j.util.ThreadPool; import org.snmp4j.MessageDispatcherImpl; import org.snmp4j.smi.TcpAddress; import org.snmp4j.CommandResponder; import org.snmp4j.CommandResponderEvent; import org.snmp4j.mp.MPv1; import org.snmp4j.mp.MPv2c; public class Traps_Server implements CommandResponder{ // initialize Log4J logging static { LogFactory.setLogFactory(new Log4jLogFactory()); BER.setCheckSequenceLength(false); } private MultiThreadedMessageDispatcher dispatcher; private Snmp snmp = null; private Address listenAddress; private ThreadPool threadPool; private int n = 0; private long start = -1; public Traps_Server() { BasicConfigurator.configure(); } private void init() throws UnknownHostException , IOException { threadPool = ThreadPool.create("Trap", 2); dispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl()); listenAddress = GenericAddress.parse(System.getProperty("snmp4j.listenAddress", "udp:0.0.0.0/162")); TransportMapping transport; if (listenAddress instanceof UdpAddress) { transport = new DefaultUdpTransportMapping((UdpAddress)listenAddress); } else { transport = new DefaultTcpTransportMapping((TcpAddress)listenAddress); } snmp = new Snmp(dispatcher, transport); snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1()); snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c()); snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3()); USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0); SecurityModels.getInstance().addSecurityModel(usm); snmp.listen(); } public void run() { try { init(); snmp.addCommandResponder(this); } catch (Exception ex) { ex.printStackTrace(); } } public void processPdu(CommandResponderEvent event) { if (start < 0) { start = System.currentTimeMillis()-1; System.out.println("blabla1"); } n++; if ((n % 100 == 1)) { } } public String return_log(){ return String.valueOf(LogFactory.getLogFactory()); } }
Partager