1 // NAME
2 // $RCSfile: InterfaceIndexesBean.java,v $
3 // DESCRIPTION
4 // [given below in javadoc format]
5 // DELTA
6 // $Revision: 1.13 $
7 // CREATED
8 // $Date: 2006/01/25 18:08:55 $
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 collects information about the number of interfaces on a
63 * system and their index.
64 * </p>
65 *
66 * <p>
67 * The number is the number of network interfaces (regardless of
68 * their current state) present on this system. The number
69 * will remain the same untill the next time the SNMP server is
70 * restarted. The interfaces may go up and down while running.
71 * </p>
72 *
73 * <p>
74 * The properties in the parent classes should be set, before calling
75 * the action() method. Via a PropertyChangeEvent the application/applet
76 * will be notified.
77 * </p>
78 *
79 * @see SNMPBean#setHost
80 * @see SNMPBean#setPort
81 * @see SNMPBean#setCommunityName
82 * @see SNMPRunBean#setUpdateInterval
83 * @see SNMPBean#addPropertyChangeListener
84 * @see SNMPBean#action
85 * @see InterfaceGetNextPdu
86 *
87 * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a>
88 * @version $Revision: 1.13 $ $Date: 2006/01/25 18:08:55 $
89 *
90 */
91 public class InterfaceIndexesBean extends SNMPRunBean implements Observer {
92 private static final String version_id = "@(#)$Id: InterfaceIndexesBean.java,v 1.13 2006/01/25 18:08:55 birgit Exp $ Copyright Westhawk Ltd";
93
94 private InterfaceGetNextPdu pdu;
95 private Hashtable interfaceHash;
96
97 private boolean isGetNextInFlight;
98 private Date lastUpdateDate = null;
99
100 /**
101 * The default constructor.
102 */
103 public InterfaceIndexesBean() {
104 interfaceHash = new Hashtable();
105 }
106
107 /**
108 * The constructor that will set the host and the port no.
109 *
110 * @param h the hostname
111 * @param p the port no
112 * @see SNMPBean#setHost
113 * @see SNMPBean#setPort
114 */
115 public InterfaceIndexesBean(String h, int p) {
116 this(h, p, null);
117 }
118
119 /**
120 * The constructor that will set the host, the port no and the local
121 * bind address.
122 *
123 * @param h the hostname
124 * @param p the port no
125 * @param b the local bind address
126 * @see SNMPBean#setHost
127 * @see SNMPBean#setPort
128 * @see SNMPBean#setBindAddress
129 *
130 * @since 4_14
131 */
132 public InterfaceIndexesBean(String h, int p, String b) {
133 this();
134 setHost(h);
135 setPort(p);
136 setBindAddress(b);
137 }
138
139 /**
140 * Returns the date of the moment when this bean was last updated.
141 * This might be null when the first time the update was not finished.
142 *
143 * @return the last update date
144 */
145 public Date getLastUpdateDate() {
146 return lastUpdateDate;
147 }
148
149 /**
150 * Returns the indexes (as Strings) of the current interfaces (the list
151 * of ifIndex).
152 */
153 public Enumeration getInterfaceIndexes() {
154 return interfaceHash.keys();
155 }
156
157 /**
158 * Returns the indexes (as Strings) of the current interfaces (the list
159 * of ifIndex).
160 *
161 * @since 4_14
162 */
163 public Set getInterfaceIndexSet() {
164 return interfaceHash.keySet();
165 }
166
167 /**
168 * Returns the number of current interfaces.
169 */
170 public synchronized int getInterfaceCount() {
171 return interfaceHash.size();
172 }
173
174 /**
175 * This method starts the action of the bean. It will initialises
176 * all variables before starting.
177 */
178 public void action() {
179 if (isHostPortReachable()) {
180 interfaceHash.clear();
181 lastUpdateDate = new Date();
182 isGetNextInFlight = false;
183 setRunning(true);
184 }
185 }
186
187 /**
188 * Implements the running of the bean.
189 *
190 * It will send the Pdu, if the previous one is not still in flight.
191 *
192 * @see SNMPRunBean#isRunning()
193 */
194 public void run() {
195 while (context != null && isRunning()) {
196 if (isGetNextInFlight == false) {
197 // start the GetNext loop again
198 isGetNextInFlight = true;
199 pdu = new InterfaceGetNextPdu(context);
200 pdu.addObserver(this);
201 pdu.addOids(null);
202 try {
203 pdu.send();
204 } catch (PduException exc) {
205 System.out.println("PduException " + exc.getMessage());
206 } catch (IOException exc) {
207 System.out.println("IOException " + exc.getMessage());
208 }
209 }
210
211 try {
212 Thread.sleep(interval);
213 } catch (InterruptedException ix) {
214 ;
215 }
216 }
217 }
218
219 /**
220 * This method is called when the Pdu response is received. When all
221 * answers are received it will fire the property change event.
222 *
223 * The answers are stored in a hashtable, this is done because the speed
224 * can only be calculated with the previous answer.
225 *
226 * @see SNMPBean#addPropertyChangeListener
227 */
228 public void update(Observable obs, Object ov) {
229 InterfaceGetNextPdu prev;
230 String hashKey;
231
232 pdu = (InterfaceGetNextPdu) obs;
233 if (pdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) {
234 hashKey = String.valueOf(pdu.getIfIndex());
235 if ((prev = (InterfaceGetNextPdu) interfaceHash.get(hashKey)) != null) {
236 pdu.getSpeed(prev);
237 }
238
239 // update the hashtable with the new answer
240 interfaceHash.put(hashKey, pdu);
241
242 // perform the GetNext on the just received answer
243 prev = pdu;
244 pdu = new InterfaceGetNextPdu(context);
245 pdu.addObserver(this);
246 pdu.addOids(prev);
247 try {
248 pdu.send();
249 } catch (PduException exc) {
250 System.out.println("PduException " + exc.getMessage());
251 } catch (IOException exc) {
252 System.out.println("IOException " + exc.getMessage());
253 }
254 } else {
255 // the GetNext loop has ended
256 lastUpdateDate = new Date();
257 isGetNextInFlight = false;
258 firePropertyChange("InterfaceIndexes", null, null);
259 }
260 }
261
262 }