View Javadoc
1   // NAME
2   //      $RCSfile: UpSincePdu.java,v $
3   // DESCRIPTION
4   //      [given below in javadoc format]
5   // DELTA
6   //      $Revision: 3.16 $
7   // CREATED
8   //      $Date: 2006/11/29 16:12:50 $
9   // COPYRIGHT
10  //      Westhawk Ltd
11  // TO DO
12  //
13  
14  /*
15   * Copyright (C) 1996 - 1998 by Westhawk Ltd (www.westhawk.nl)
16   * Copyright (C) 1998 - 2006 by Westhawk Ltd 
17   * <a href="www.westhawk.co.uk">www.westhawk.co.uk</a>
18   *
19   * Permission to use, copy, modify, and distribute this software
20   * for any purpose and without fee is hereby granted, provided
21   * that the above copyright notices appear in all copies and that
22   * both the copyright notice and this permission notice appear in
23   * supporting documentation.
24   * This software is provided "as is" without express or implied
25   * warranty.
26   * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
27   */
28   
29  package uk.co.westhawk.snmp.pdu;
30  
31  /*-
32   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
33   * SNMP Java Client
34   * ჻჻჻჻჻჻
35   * Copyright 2023 MetricsHub, Westhawk
36   * ჻჻჻჻჻჻
37   * This program is free software: you can redistribute it and/or modify
38   * it under the terms of the GNU Lesser General Public License as
39   * published by the Free Software Foundation, either version 3 of the
40   * License, or (at your option) any later version.
41   *
42   * This program is distributed in the hope that it will be useful,
43   * but WITHOUT ANY WARRANTY; without even the implied warranty of
44   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
45   * GNU General Lesser Public License for more details.
46   *
47   * You should have received a copy of the GNU General Lesser Public
48   * License along with this program.  If not, see
49   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
50   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
51   */
52  import uk.co.westhawk.snmp.stack.*;
53  import java.util.*;
54  
55  /**
56   * The UpSincePdu class will send a Get request for the sysUpTime and
57   * will calculate that date the system rebooted the last time.
58   *
59   * @author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
60   * @version $Revision: 3.16 $ $Date: 2006/11/29 16:12:50 $
61   */
62  public class UpSincePdu extends GetPdu {
63      private static final String version_id = "@(#)$Id: UpSincePdu.java,v 3.16 2006/11/29 16:12:50 birgit Exp $ Copyright Westhawk Ltd";
64  
65      Date since;
66      /**
67       * The oid of sysUpTime
68       */
69      public final static String SYSUPTIME = "1.3.6.1.2.1.1.3.0";
70  
71      /**
72       * Constructor that will send the request immediately.
73       *
74       * @param con The context of the request
75       * @param o   the Observer that will be notified when the answer is received
76       */
77      public UpSincePdu(SnmpContextBasisFace con, Observer o)
78              throws PduException, java.io.IOException {
79          super(con);
80          addOid(SYSUPTIME);
81          if (o != null) {
82              addObserver(o);
83          }
84          send();
85      }
86  
87      /**
88       * Returns the date when the system went up, (sysUpTime).
89       * 
90       * @return the date
91       */
92      public Date getDate() {
93          return since;
94      }
95  
96      /**
97       * The value of the request is set. This will be called by
98       * Pdu.fillin().
99       *
100      * @param n   the index of the value
101      * @param res the value
102      * @see Pdu#new_value
103      */
104     protected void new_value(int n, varbind res) {
105         // given the uptime in centi seconds and the time now,
106         // calculate the time it rebooted
107 
108         AsnObject val = res.getValue();
109         if (val instanceof AsnUnsInteger) {
110             AsnUnsInteger va = (AsnUnsInteger) res.getValue();
111             if (n == 0) {
112                 long value = va.getValue();
113                 Date now = new Date();
114                 long then = now.getTime();
115                 then -= 10 * value;
116                 since = new Date(then);
117             }
118         } else {
119             since = null;
120         }
121     }
122 
123     /**
124      * This method notifies all observers.
125      * This will be called by Pdu.fillin().
126      * 
127      * <p>
128      * Unless an exception occurred the Object to the update() method of the
129      * Observer will be a Date.
130      * In the case of an exception, that exception will be passed.
131      * </p>
132      */
133     protected void tell_them() {
134         notifyObservers(since);
135     }
136 
137 }