1 // NAME 2 // $RCSfile: InterfacePdu.java,v $ 3 // DESCRIPTION 4 // [given below inOctet javadoc format] 5 // DELTA 6 // $Revision: 3.19 $ 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 /** 57 * The InterfacePdu class asks one interface for information, useful for admin 58 * purposes. 59 * See <a href="http://www.ietf.org/rfc/rfc2863.txt">IF-MIB</a>. 60 * 61 * @author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a> 62 * @version $Revision: 3.19 $ $Date: 2006/11/29 16:12:50 $ 63 * @see InterfacesPdu 64 * 65 */ 66 public class InterfacePdu extends GetPdu { 67 private static final String version_id = "@(#)$Id: InterfacePdu.java,v 3.19 2006/11/29 16:12:50 birgit Exp $ Copyright Westhawk Ltd"; 68 69 // see rfc 2863 70 71 /** 72 * ifNumber - 73 * The number of network interfaces (regardless of their current state) 74 * present on this system. 75 */ 76 final static String IFNUMBER = "1.3.6.1.2.1.2.1.0"; 77 78 /** 79 * sysUpTime - 80 * The time (in hundredths of a second) since the network management 81 * portion of the system was last re-initialized. 82 */ 83 final static String SYS_UPTIME = "1.3.6.1.2.1.1.3"; 84 85 /** 86 * ifDescr - 87 * A textual string containing information about the 88 * interface. This string should include the name of 89 * the manufacturer, the product name and the version 90 * of the hardware interface. 91 */ 92 final static String DESCR = "1.3.6.1.2.1.2.2.1.2"; 93 94 /** 95 * ifOperStatus - 96 * The current operational state of the interface. 97 * The testing(3) state indicates that no operational 98 * packets can be passed. 99 */ 100 final static String OPR_STATUS = "1.3.6.1.2.1.2.2.1.8"; 101 102 /** 103 * ifInOctets - 104 * The total number of octets received on the 105 * interface, including framing characters. 106 */ 107 final static String IN_OCTETS = "1.3.6.1.2.1.2.2.1.10"; 108 109 /** 110 * ifOutOctets - 111 * The total number of octets transmitted outOctets of the 112 * interface, including framing characters. 113 */ 114 final static String OUT_OCTETS = "1.3.6.1.2.1.2.2.1.16"; 115 116 /** 117 * The current operational state is up 118 */ 119 final public static String UP = "up"; 120 /** 121 * The current operational state is down 122 */ 123 final public static String DOWN = "down"; 124 /** 125 * The current operational state is testing 126 */ 127 final public static String TESTING = "testing"; 128 /** 129 * The current operational state is unknown 130 */ 131 final public static String UNKNOWN = "unknown"; 132 133 long sysUpTime; 134 int operStatus; 135 long inOctet; 136 long outOctet; 137 long speed; 138 int index; 139 String descr; 140 boolean valid = false; 141 142 /** 143 * Constructor. 144 * It permits this package to create this PDU without data. 145 * 146 * @param con The context of the request 147 */ 148 InterfacePdu(SnmpContextBasisFace con) { 149 super(con); 150 } 151 152 /** 153 * Constructor that will send the request immediately. 154 * 155 * @param con the SnmpContextBasisFace 156 * @param o the Observer that will be notified when the answer is received 157 * @param interf the index of the requested interface 158 */ 159 public InterfacePdu(SnmpContextBasisFace con, Observer o, int interf) 160 throws PduException, java.io.IOException { 161 super(con); 162 163 addOids(interf); 164 if (o != null) { 165 addObserver(o); 166 } 167 index = interf; 168 send(); 169 } 170 171 /** 172 * Returns the index of the interface. 173 * 174 * @return the index 175 */ 176 public int getIndex() { 177 return index; 178 } 179 180 /** 181 * Returns the time (in hundredths of a second) since the network management 182 * portion of the system was last re-initialized. 183 */ 184 public long getSysUpTime() { 185 return sysUpTime; 186 } 187 188 /** 189 * Returns the description of the interface. 190 * 191 * @return the description 192 */ 193 public String getDescription() { 194 return descr; 195 } 196 197 /** 198 * Returns the operational state of the interface. 199 * 200 * @return the operational state 201 */ 202 public int getOperStatus() { 203 return operStatus; 204 } 205 206 /** 207 * Returns the string representation of the operational state of the 208 * interface. 209 * 210 * @return the operational state as string 211 * @see #getOperStatus() 212 * @see #getOperStatusString(int) 213 */ 214 public String getOperStatusString() { 215 return getOperStatusString(operStatus); 216 } 217 218 /** 219 * Returns the string representation of a operational state. 220 * 221 * @see #getOperStatusString() 222 */ 223 public String getOperStatusString(int status) { 224 String str = null; 225 switch (status) { 226 case 1: 227 str = UP; 228 break; 229 case 2: 230 str = DOWN; 231 break; 232 case 3: 233 str = TESTING; 234 break; 235 default: 236 str = UNKNOWN; 237 } 238 return str; 239 } 240 241 /** 242 * Returns the total number of octets received on the 243 * interface, including framing characters. 244 */ 245 public long getInOctet() { 246 return inOctet; 247 } 248 249 /** 250 * Returns the total number of octets transmitted outOctets of the 251 * interface, including framing characters. 252 */ 253 public long getOutOctet() { 254 return outOctet; 255 } 256 257 /** 258 * Calculates the speed of the interface. This is done by providing the 259 * method with <i>the previous value of this interface</i>. An interface 260 * is marked by its index. 261 * 262 * @param old The previous value of this interface 263 */ 264 public long getSpeed(InterfacePdu old) { 265 long speed = -1; 266 if ((this.operStatus < 1) || (old.operStatus < 1) 267 || 268 !this.valid || !old.valid) { 269 return -1; 270 } 271 long tdif = (this.sysUpTime - old.sysUpTime); 272 if (tdif != 0) { 273 speed = 100 * ((this.inOctet - old.inOctet) 274 + (this.outOctet - old.outOctet)) / tdif; 275 } 276 return speed; 277 } 278 279 void addOids(int interf) { 280 addOid(SYS_UPTIME + ".0"); 281 addOid(DESCR + "." + interf); 282 addOid(OPR_STATUS + "." + interf); 283 addOid(IN_OCTETS + "." + interf); 284 addOid(OUT_OCTETS + "." + interf); 285 } 286 287 /** 288 * The value of the request is set. This will be called by 289 * Pdu.fillin(). 290 * 291 * @param n the index of the value 292 * @param res the value 293 * @see Pdu#new_value 294 */ 295 protected void new_value(int n, varbind res) { 296 AsnObject obj = res.getValue(); 297 if (getErrorStatus() == AsnObject.SNMP_ERR_NOERROR) { 298 try { 299 switch (n) { 300 case 0: 301 sysUpTime = ((AsnUnsInteger) obj).getValue(); 302 break; 303 case 1: 304 descr = ((AsnOctets) obj).getValue(); 305 break; 306 case 2: 307 operStatus = ((AsnInteger) obj).getValue(); 308 break; 309 case 3: 310 inOctet = ((AsnUnsInteger) obj).getValue(); 311 break; 312 case 4: 313 outOctet = ((AsnUnsInteger) obj).getValue(); 314 valid = true; 315 break; 316 default: 317 valid = false; 318 } 319 } catch (ClassCastException exc) { 320 sysUpTime = 0; 321 descr = null; 322 operStatus = 0; 323 inOctet = 0; 324 outOctet = 0; 325 valid = false; 326 } 327 } else { 328 valid = false; 329 } 330 } 331 332 /** 333 * This method notifies all observers. 334 * This will be called by Pdu.fillin(). 335 * 336 * <p> 337 * Unless an exception occurred the Object to the update() method of the 338 * Observer will be a varbind, so any AsnObject type can be returned. 339 * In the case of an exception, that exception will be passed. 340 * </p> 341 */ 342 protected void tell_them() { 343 notifyObservers(this); 344 } 345 346 /** 347 * Returns how many interfaces are present. 348 * 349 * @return the number of interfaces 350 */ 351 public static int getNumIfs(SnmpContextBasisFace con) 352 throws PduException, java.io.IOException { 353 int ifCount = 0; 354 355 if (con != null) { 356 OneIntPdu numIfs = new OneIntPdu(con, IFNUMBER); 357 boolean answered = numIfs.waitForSelf(); 358 boolean timedOut = numIfs.isTimedOut(); 359 if (answered == true && timedOut == false) { 360 ifCount = numIfs.getValue().intValue(); 361 } 362 } 363 return ifCount; 364 } 365 366 }