1 // NAME
2 // $RCSfile: IsHostReachableBean.java,v $
3 // DESCRIPTION
4 // [given below in javadoc format]
5 // DELTA
6 // $Revision: 1.14 $
7 // CREATED
8 // $Date: 2006/01/26 12:38:59 $
9 // COPYRIGHT
10 // Westhawk Ltd
11 // TO DO
12 //
13
14 /*
15 * Copyright (C) 1998 - 2006 by Westhawk Ltd
16 *
17 * Permission to use, copy, modify, and distribute this software
18 * for any purpose and without fee is hereby granted, provided
19 * that the above copyright notices appear in all copies and that
20 * both the copyright notice and this permission notice appear in
21 * supporting documentation.
22 * This software is provided "as is" without express or implied
23 * warranty.
24 * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
25 */
26
27 package uk.co.westhawk.snmp.beans;
28
29 /*-
30 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
31 * SNMP Java Client
32 * ჻჻჻჻჻჻
33 * Copyright 2023 MetricsHub, Westhawk
34 * ჻჻჻჻჻჻
35 * This program is free software: you can redistribute it and/or modify
36 * it under the terms of the GNU Lesser General Public License as
37 * published by the Free Software Foundation, either version 3 of the
38 * License, or (at your option) any later version.
39 *
40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Lesser Public License for more details.
44 *
45 * You should have received a copy of the GNU General Lesser Public
46 * License along with this program. If not, see
47 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
48 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
49 */
50
51 import uk.co.westhawk.snmp.stack.*;
52 import uk.co.westhawk.snmp.pdu.*;
53 import java.awt.*;
54 import java.util.*;
55 import java.text.*;
56 import java.lang.*;
57 import java.io.*;
58 import java.beans.*;
59
60 /**
61 * <p>
62 * This bean will determine whether the host+port is up, by sending an
63 * UpSincePdu.
64 * </p>
65 *
66 * <p>
67 * The properties in the parent classes should be set, before calling
68 * the action() method. Via a PropertyChangeEvent the application/applet
69 * will be notified.
70 * </p>
71 *
72 * @see SNMPBean#setHost
73 * @see SNMPBean#setPort
74 * @see SNMPBean#setCommunityName
75 * @see SNMPBean#addPropertyChangeListener
76 * @see SNMPBean#action
77 * @see UpSincePdu
78 *
79 * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a>
80 * @version $Revision: 1.14 $ $Date: 2006/01/26 12:38:59 $
81 *
82 */
83 public class IsHostReachableBean extends SNMPBean implements Observer {
84 private static final String version_id = "@(#)$Id: IsHostReachableBean.java,v 1.14 2006/01/26 12:38:59 birgit Exp $ Copyright Westhawk Ltd";
85
86 private UpSincePdu pdu;
87
88 private Date upSince = null;
89 private boolean isReachable = false;
90
91 private SimpleDateFormat dateFormat;
92
93 /**
94 * The default constructor.
95 */
96 public IsHostReachableBean() {
97 dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz");
98 dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
99 }
100
101 /**
102 * The constructor that will set the host and the port no.
103 *
104 * @param h the hostname
105 * @param p the port no
106 * @see SNMPBean#setHost
107 * @see SNMPBean#setPort
108 */
109 public IsHostReachableBean(String h, int p) {
110 this(h, p, null);
111 }
112
113 /**
114 * The constructor that will set the host, the port no and the local
115 * bind address.
116 *
117 * @param h the hostname
118 * @param p the port no
119 * @param b the local bind address
120 * @see SNMPBean#setHost
121 * @see SNMPBean#setPort
122 * @see SNMPBean#setBindAddress
123 *
124 * @since 4_14
125 */
126 public IsHostReachableBean(String h, int p, String b) {
127 this();
128 setHost(h);
129 setPort(p);
130 setBindAddress(b);
131 }
132
133 /**
134 * Returns the date when the host went up. This might be null is no
135 * answer is received yet.
136 *
137 * @return the date
138 *
139 * @see SNMPBean#getMessage()
140 * @see #isReachable()
141 */
142 public Date getUpSinceDate() {
143 return upSince;
144 }
145
146 /**
147 * Answer is set according to the received SNMP response.
148 *
149 * @see #getUpSinceDate()
150 */
151 protected void setUpSinceDate(Date d) {
152 upSince = d;
153 }
154
155 /**
156 * Indicates whether the host + port is reachable.
157 * The host + port is reachable if a valid response is received from a
158 * UpSincePdu request.
159 *
160 * @return is the host + port + community name valid
161 *
162 * @see SNMPBean#getMessage()
163 * @see #getUpSinceDate()
164 */
165 public boolean isReachable() {
166 return isReachable;
167 }
168
169 /**
170 * The reachable property is set according to the succes of making
171 * a connection and the response of the SNMP request.
172 * This method will fire a Property Change Event.
173 *
174 * @see #isReachable()
175 * @see SNMPBean#addPropertyChangeListener
176 */
177 protected void setReachable(boolean b) {
178 boolean old;
179 old = isReachable;
180 isReachable = b;
181
182 firePropertyChange("Reachable", new Boolean(old),
183 new Boolean(isReachable));
184 }
185
186 /**
187 * This method performs the request and wait for it. It is not
188 * recommended to use the waitForSelf option, preferred is the action()
189 * method.
190 * Use it if it is really necessary to wait.
191 *
192 * @see SnmpContext
193 * @see #action()
194 * @see Pdu#waitForSelf()
195 */
196 public void waitForSelfAction()
197 throws PduException, IOException {
198 action(true);
199 }
200
201 /**
202 * This method actually performs the request. All properties should be
203 * set before this method is called.
204 *
205 * This will create a new SnmpContext and send a UpSincePdu SNMP
206 * request.
207 * If the connection fails, the reachable property will be set to false.
208 *
209 * @see SnmpContext
210 */
211 public void action()
212 throws PduException, IOException {
213 action(false);
214 }
215
216 protected void action(boolean wait)
217 throws PduException, IOException {
218 if (host != null
219 &&
220 host.length() > 0
221 &&
222 port > 0) {
223 try {
224 if (context != null) {
225 context.destroy();
226 }
227 context = new SnmpContext(host, port, bindAddr, socketType);
228 context.setCommunity(community);
229 setMessage("Connection to host " + host
230 + " is made succesfully");
231
232 if (wait == true) {
233 pdu = new UpSincePdu(context, null);
234 if (pdu.waitForSelf()) {
235 this.update((Observable) pdu, (Object) null);
236 } else {
237 setMessage("No SNMP Response from " + host);
238 }
239 } else {
240 pdu = new UpSincePdu(context, this);
241 }
242 } catch (IOException exc) {
243 setMessage("IOException: " + exc.getMessage());
244 setReachable(false);
245 } catch (RuntimeException exc) {
246 setMessage("RuntimeException: " + exc.getMessage());
247 setReachable(false);
248 }
249 }
250 }
251
252 /**
253 * The update method according to the Observer interface, it will be
254 * called when the Pdu response is received.
255 * If there is no error it means that the host is reachable. If it is
256 * not reachable the message will contain the type of error, if it is
257 * reachable the message will say since when the host is up.
258 */
259 public void update(Observable obs, Object ov) {
260 pdu = (UpSincePdu) obs;
261
262 if (pdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) {
263 setUpSinceDate(pdu.getDate());
264 setMessage("Host " + host + " is up since "
265 + dateFormat.format(getUpSinceDate()));
266 setReachable(true);
267 } else {
268 setUpSinceDate(null);
269 setMessage("SNMP Response: " + pdu.getErrorStatusString());
270 setReachable(false);
271 }
272 }
273
274 /**
275 * Destroys the context.
276 *
277 * @since 4_14
278 */
279 public void freeResources() {
280 if (context != null) {
281 context.destroy();
282 context = null;
283 }
284 }
285
286 }