View Javadoc
1   // NAME
2   //      $RCSfile: OneNTSharedResBean.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:56 $
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 one NT shared resource installed
63   * on a NT server. The NT mib is described in the 
64   *
65   * <a href="http://premium.microsoft.com/msdn/library/winresource/dnwinnt/f1d/d25/s86a2.htm">LAN Manager MIB II for Windows NT Objects</a> .
66   *
67   * You will have to register to the MSDN before accessing this page.
68   * </p>
69   *
70   * <p>
71   * The properties in the parent classes should be set, before calling
72   * the action() method. Via a PropertyChangeEvent the application/applet
73   * will be notified.
74   * </p>
75   *
76   * @see SNMPBean#setHost
77   * @see SNMPBean#setPort
78   * @see SNMPBean#setCommunityName
79   * @see SNMPRunBean#setUpdateInterval
80   * @see #setIndex(String)
81   * @see SNMPBean#addPropertyChangeListener
82   * @see SNMPBean#action
83   * @see GetPdu_vec
84   * @see NTSharedResBean
85   * 
86   * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a>
87   * @version $Revision: 1.13 $ $Date: 2006/01/25 18:08:56 $
88   */
89  public class OneNTSharedResBean extends SNMPRunBean implements Observer {
90      private static final String version_id = "@(#)$Id: OneNTSharedResBean.java,v 1.13 2006/01/25 18:08:56 birgit Exp $ Copyright Westhawk Ltd";
91  
92      private GetPdu_vec pdu;
93  
94      private boolean isPduInFlight;
95      private Date lastUpdateDate = null;
96  
97      private String index = "";
98      private String name = "";
99      private String path = "";
100     private String comment = "";
101 
102     private final static int NR_OID = 3;
103     private final static String svShareName = "1.3.6.1.4.1.77.1.2.27.1.1";
104     private final static String svSharePath = "1.3.6.1.4.1.77.1.2.27.1.2";
105     private final static String svShareComment = "1.3.6.1.4.1.77.1.2.27.1.3";
106 
107     /**
108      * The default constructor.
109      */
110     public OneNTSharedResBean() {
111     }
112 
113     /**
114      * The constructor that will set the host and the port no.
115      *
116      * @param h the hostname
117      * @param p the port no
118      * @see SNMPBean#setHost
119      * @see SNMPBean#setPort
120      */
121     public OneNTSharedResBean(String h, int p) {
122         this(h, p, null);
123     }
124 
125     /**
126      * The constructor that will set the host, the port no and the local
127      * bind address.
128      *
129      * @param h the hostname
130      * @param p the port no
131      * @param b the local bind address
132      * @see SNMPBean#setHost
133      * @see SNMPBean#setPort
134      * @see SNMPBean#setBindAddress
135      *
136      * @since 4_14
137      */
138     public OneNTSharedResBean(String h, int p, String b) {
139         this();
140         setHost(h);
141         setPort(p);
142         setBindAddress(b);
143     }
144 
145     /**
146      * Sets the index of the NT shared resource that will be requested.
147      * 
148      * @param ind the index
149      * @see #getIndex()
150      * @see NTSharedResBean#getIndex(String)
151      */
152     public void setIndex(String ind) {
153         if (ind != null && ind.length() > 0) {
154             index = ind;
155         }
156     }
157 
158     /**
159      * Returns the index of the NT shared resource.
160      * 
161      * @return the index
162      * @see #setIndex(String)
163      */
164     public String getIndex() {
165         return index;
166     }
167 
168     /**
169      * Returns the name of the NT shared resource.
170      * 
171      * @return the name
172      */
173     public String getName() {
174         return name;
175     }
176 
177     /**
178      * Returns the local name (path) of the NT shared resource.
179      * 
180      * @return the path
181      */
182     public String getPath() {
183         return path;
184     }
185 
186     /**
187      * Returns the comment associated with the NT shared resource.
188      * 
189      * @return the comment
190      */
191     public String getComment() {
192         return comment;
193     }
194 
195     /**
196      * Returns the date of the moment when this bean was last updated.
197      * This might be null when the first time the update was not finished.
198      *
199      * @return the last update date
200      */
201     public Date getLastUpdateDate() {
202         return lastUpdateDate;
203     }
204 
205     /**
206      * This method starts sending the SNMP request. All properties should be
207      * set before this method is called.
208      *
209      * The actual sending will take place in the run method.
210      * It makes a new snmp context and initialises all variables before
211      * starting.
212      */
213     public void action() {
214         if (isHostPortReachable()) {
215             lastUpdateDate = new Date();
216             isPduInFlight = false;
217             setRunning(true);
218         }
219     }
220 
221     /**
222      * The run method according to the Runnable interface.
223      * This method will send the Pdu request, if the previous one is not
224      * still in flight.
225      * 
226      * @see SNMPRunBean#isRunning()
227      */
228     public void run() {
229         while (context != null && isRunning()) {
230             String ind = getIndex();
231             if (isPduInFlight == false && ind != null && ind.length() > 0) {
232                 isPduInFlight = true;
233                 pdu = new GetPdu_vec(context, NR_OID);
234                 pdu.addObserver(this);
235                 pdu.addOid(svShareName + "." + ind);
236                 pdu.addOid(svSharePath + "." + ind);
237                 pdu.addOid(svShareComment + "." + ind);
238                 try {
239                     pdu.send();
240                 } catch (PduException exc) {
241                     System.out.println("PduException " + exc.getMessage());
242                 } catch (IOException exc) {
243                     System.out.println("IOException " + exc.getMessage());
244                 }
245             }
246 
247             try {
248                 Thread.sleep(interval);
249             } catch (InterruptedException ix) {
250                 ;
251             }
252         }
253     }
254 
255     /**
256      * The update method according to the Observer interface, it will be
257      * called when the Pdu response is received.
258      * The property change event is fired.
259      *
260      * @see SNMPBean#addPropertyChangeListener
261      */
262     public void update(Observable obs, Object ov) {
263         pdu = (GetPdu_vec) obs;
264         varbind[] varbinds = (varbind[]) ov;
265 
266         if (pdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) {
267             AsnObject asnobj;
268             name = ((AsnOctets) varbinds[0].getValue()).getValue();
269 
270             asnobj = varbinds[1].getValue();
271             path = " ";
272             if (asnobj instanceof AsnOctets) {
273                 path = ((AsnOctets) asnobj).getValue();
274             }
275 
276             asnobj = varbinds[2].getValue();
277             comment = " ";
278             if (asnobj instanceof AsnOctets) {
279                 comment = ((AsnOctets) asnobj).getValue();
280             }
281 
282             lastUpdateDate = new Date();
283             isPduInFlight = false;
284             firePropertyChange("NTService", null, null);
285         }
286     }
287 
288 }