View Javadoc
1   // NAME
2   //      $RCSfile: OneNTServiceBean.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 network service 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 NTServiceNamesBean
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 OneNTServiceBean extends SNMPRunBean implements Observer {
90      private static final String version_id = "@(#)$Id: OneNTServiceBean.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 instState = "";
100     private String operState = "";
101     private boolean canUninst = false;
102     private boolean canPause = false;
103 
104     private final static int NR_OID = 5;
105     private final static String svSvcName = "1.3.6.1.4.1.77.1.2.3.1.1";
106     private final static String svSvcInstalledState = "1.3.6.1.4.1.77.1.2.3.1.2";
107     private final static String svSvcOperatingState = "1.3.6.1.4.1.77.1.2.3.1.3";
108     private final static String svSvcCanBeUninstalled = "1.3.6.1.4.1.77.1.2.3.1.4";
109     private final static String svSvcCanBePaused = "1.3.6.1.4.1.77.1.2.3.1.5";
110 
111     public final static String msg_inst_state[] = {
112             "unknown",
113             "uninstalled", // 1
114             "install pending", // 2
115             "uninstall pending", // 3
116             "installed" // 4
117     };
118 
119     public final static String msg_oper_state[] = {
120             "unknown",
121             "active", // 1
122             "continue pending", // 2
123             "pause pending", // 3
124             "paused" // 4
125     };
126 
127     public final static int cannot_be_uninstalled = 1;
128     public final static int can_be_uninstalled = 2;
129 
130     public final static int cannot_be_paused = 1;
131     public final static int can_be_paused = 2;
132 
133     /**
134      * The default constructor.
135      */
136     public OneNTServiceBean() {
137     }
138 
139     /**
140      * The constructor that will set the host and the port no.
141      *
142      * @param h the hostname
143      * @param p the port no
144      * @see SNMPBean#setHost
145      * @see SNMPBean#setPort
146      */
147     public OneNTServiceBean(String h, int p) {
148         this(h, p, null);
149     }
150 
151     /**
152      * The constructor that will set the host, the port no and the local
153      * bind address.
154      *
155      * @param h the hostname
156      * @param p the port no
157      * @param b the local bind address
158      * @see SNMPBean#setHost
159      * @see SNMPBean#setPort
160      * @see SNMPBean#setBindAddress
161      *
162      * @since 4_14
163      */
164     public OneNTServiceBean(String h, int p, String b) {
165         this();
166         setHost(h);
167         setPort(p);
168         setBindAddress(b);
169     }
170 
171     /**
172      * Sets the index of the NT network service that will be requested.
173      * 
174      * @param ind the index
175      * @see #getIndex()
176      * @see NTServiceNamesBean#getIndex(String)
177      */
178     public void setIndex(String ind) {
179         if (ind != null && ind.length() > 0) {
180             index = ind;
181         }
182     }
183 
184     /**
185      * Returns the index of the NT network service.
186      * 
187      * @return the index
188      * @see #setIndex(String)
189      */
190     public String getIndex() {
191         return index;
192     }
193 
194     /**
195      * Returns the name of the NT network service.
196      * 
197      * @return the name
198      */
199     public String getName() {
200         return name;
201     }
202 
203     /**
204      * Returns the installation status of the NT network service.
205      * 
206      * @return the installation status
207      * @see #msg_inst_state
208      */
209     public String getInstalledState() {
210         return instState;
211     }
212 
213     /**
214      * Returns the operating status of the NT network service.
215      * 
216      * @return the operating status
217      * @see #msg_oper_state
218      */
219     public String getOperatingState() {
220         return operState;
221     }
222 
223     /**
224      * Returns if the NT network service can be uninstalled.
225      * 
226      * @return the uninstalled option
227      * @see #can_be_uninstalled
228      * @see #cannot_be_uninstalled
229      */
230     public boolean getCanBeUninstalled() {
231         return canUninst;
232     }
233 
234     /**
235      * Returns if the NT network service can be paused.
236      * 
237      * @return the paused option
238      * @see #can_be_paused
239      * @see #cannot_be_paused
240      */
241     public boolean getCanBePaused() {
242         return canPause;
243     }
244 
245     /**
246      * Returns the date of the moment when this bean was last updated.
247      * This might be null when the first time the update was not finished.
248      *
249      * @return the last update date
250      */
251     public Date getLastUpdateDate() {
252         return lastUpdateDate;
253     }
254 
255     /**
256      * This method starts sending the SNMP request. All properties should be
257      * set before this method is called.
258      *
259      * The actual sending will take place in the run method.
260      * It makes a new snmp context and initialises all variables before
261      * starting.
262      */
263     public void action() {
264         if (isHostPortReachable()) {
265             lastUpdateDate = new Date();
266             isPduInFlight = false;
267             setRunning(true);
268         }
269     }
270 
271     /**
272      * The run method according to the Runnable interface.
273      * This method will send the Pdu request, if the previous one is not
274      * still in flight.
275      * 
276      * @see SNMPRunBean#isRunning()
277      */
278     public void run() {
279         while (context != null && isRunning()) {
280             String ind = getIndex();
281             if (isPduInFlight == false && ind != null && ind.length() > 0) {
282                 isPduInFlight = true;
283                 pdu = new GetPdu_vec(context, NR_OID);
284                 pdu.addObserver(this);
285                 pdu.addOid(svSvcName + "." + ind);
286                 pdu.addOid(svSvcInstalledState + "." + ind);
287                 pdu.addOid(svSvcOperatingState + "." + ind);
288                 pdu.addOid(svSvcCanBeUninstalled + "." + ind);
289                 pdu.addOid(svSvcCanBePaused + "." + ind);
290                 try {
291                     pdu.send();
292                 } catch (PduException exc) {
293                     System.out.println("PduException " + exc.getMessage());
294                 } catch (IOException exc) {
295                     System.out.println("IOException " + exc.getMessage());
296                 }
297             }
298 
299             try {
300                 Thread.sleep(interval);
301             } catch (InterruptedException ix) {
302                 ;
303             }
304         }
305     }
306 
307     /**
308      * The update method according to the Observer interface, it will be
309      * called when the Pdu response is received.
310      * The property change event is fired.
311      *
312      * @see SNMPBean#addPropertyChangeListener
313      */
314     public void update(Observable obs, Object ov) {
315         int nr;
316         pdu = (GetPdu_vec) obs;
317         varbind[] varbinds = (varbind[]) ov;
318 
319         if (pdu.getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) {
320             name = ((AsnOctets) varbinds[0].getValue()).getValue();
321 
322             nr = ((AsnInteger) varbinds[1].getValue()).getValue();
323             instState = msg_inst_state[nr];
324 
325             nr = ((AsnInteger) varbinds[2].getValue()).getValue();
326             operState = msg_oper_state[nr];
327 
328             nr = ((AsnInteger) varbinds[3].getValue()).getValue();
329             canUninst = (nr == can_be_uninstalled);
330 
331             nr = ((AsnInteger) varbinds[4].getValue()).getValue();
332             canPause = (nr == can_be_paused);
333 
334             lastUpdateDate = new Date();
335             isPduInFlight = false;
336             firePropertyChange("NTService", null, null);
337         }
338     }
339 
340 }