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 package uk.co.westhawk.snmp.stack;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 import uk.co.westhawk.snmp.beans.UsmDiscoveryBean;
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 public class DefaultUsmAgent implements UsmAgent {
78 static final String version_id = "@(#)$Id: DefaultUsmAgent.java,v 3.10 2009/03/05 15:51:42 birgita Exp $ Copyright Westhawk Ltd";
79
80
81
82
83 public final static String LOCAL_HOST = "localhost";
84
85
86
87
88 public final static int LOCAL_PORT = 161;
89
90 private SnmpContextv3Basis context;
91 private String hostname;
92 private String hostaddress;
93 private int port;
94
95 public DefaultUsmAgent() {
96 try {
97 setAgentName(LOCAL_HOST);
98 } catch (java.net.UnknownHostException exc) {
99 hostname = LOCAL_HOST;
100 hostaddress = "127.0.0.1";
101 }
102 setAgentPort(LOCAL_PORT);
103 }
104
105
106
107
108
109
110
111 public String getSnmpEngineId() {
112 TimeWindow tWindow = TimeWindow.getCurrent();
113 String engineId = tWindow.getSnmpEngineId(hostaddress, port);
114 return engineId;
115 }
116
117
118
119
120
121
122
123 public int getSnmpEngineBoots() {
124 int boots = 1;
125 TimeWindowNode node = null;
126 TimeWindow tWindow = TimeWindow.getCurrent();
127 String engineId = tWindow.getSnmpEngineId(hostaddress, port);
128 if (engineId != null) {
129 node = tWindow.getTimeLine(engineId);
130 }
131 if (node != null) {
132 boots = node.getSnmpEngineBoots();
133 }
134 return boots;
135 }
136
137
138
139
140
141
142
143 public int getSnmpEngineTime() {
144 int time = 1;
145 TimeWindowNode node = null;
146 TimeWindow tWindow = TimeWindow.getCurrent();
147 String engineId = tWindow.getSnmpEngineId(hostaddress, port);
148 if (engineId != null) {
149 node = tWindow.getTimeLine(engineId);
150 }
151 if (node != null) {
152 time = node.getSnmpEngineTime();
153 }
154 return time;
155 }
156
157
158
159
160 public void setSnmpContext(SnmpContextv3Basis c) {
161 context = c;
162 try {
163 discoverIfNeeded();
164 } catch (PduException exc) {
165 if (AsnObject.debug > 4) {
166 System.out.println(getClass().getName() + ".setSnmpContext(): "
167 + exc.getMessage());
168 }
169 } catch (java.io.IOException exc) {
170 if (AsnObject.debug > 4) {
171 System.out.println(getClass().getName() + ".setSnmpContext(): "
172 + exc.getMessage());
173 }
174 }
175 }
176
177
178
179
180
181
182
183
184
185
186
187 public void setAgentName(String host)
188 throws java.net.UnknownHostException {
189 hostname = host;
190 java.net.InetAddress ipAddr = java.net.InetAddress.getByName(hostname);
191 hostaddress = ipAddr.getHostAddress();
192 }
193
194
195
196
197
198
199
200 public void setAgentPort(int p) {
201 port = p;
202 }
203
204
205
206
207
208
209
210
211
212 void discoverIfNeeded()
213 throws java.io.IOException, PduException {
214 UsmDiscoveryBean discBean = null;
215 boolean isNeeded = false;
216
217 TimeWindow tWindow = TimeWindow.getCurrent();
218 String engineId = tWindow.getSnmpEngineId(hostaddress, port);
219 if (engineId == null) {
220 isNeeded = true;
221 discBean = new UsmDiscoveryBean(hostname,
222 port, context.getBindAddress(), context.getTypeSocket());
223 }
224
225 if (context.isUseAuthentication()) {
226 if (isNeeded) {
227 discBean.setAuthenticationDetails(context.getUserName(),
228 context.getUserAuthenticationPassword(),
229 context.getAuthenticationProtocol());
230 } else if (tWindow.isTimeLineKnown(engineId) == false) {
231 isNeeded = true;
232 discBean = new UsmDiscoveryBean(
233 hostname, port, context.getBindAddress(),
234 context.getTypeSocket());
235 discBean.setAuthenticationDetails(context.getUserName(),
236 context.getUserAuthenticationPassword(),
237 context.getAuthenticationProtocol());
238 }
239
240 if (isNeeded && context.isUsePrivacy()) {
241 discBean.setPrivacyDetails(context.getUserPrivacyPassword(),
242 context.getPrivacyProtocol());
243 }
244 }
245
246 if (isNeeded) {
247 discBean.startDiscovery();
248 discBean.freeResources();
249 }
250 }
251
252
253
254
255 public long getUsmStatsUnknownEngineIDs() {
256 return 0;
257 }
258
259
260
261
262 public long getUsmStatsNotInTimeWindows() {
263 return 0;
264 }
265
266 }