Skip to content

Commit 0e92698

Browse files
authored
Add network interface dropped packets and collisions (oshi#1101)
* Add network interface dropped packets and collisions * Changelog, tests, resolve todos
1 parent b24ae10 commit 0e92698

File tree

8 files changed

+240
-113
lines changed

8 files changed

+240
-113
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
================
33
* [#1098](https://github.com/oshi/oshi/pull/1098): Option to limit FileStore list to local file systems. - [@Space2Man](https://github.com/Space2Man).
44
* [#1100](https://github.com/oshi/oshi/pull/1100): Get FileStore options. - [@dbwiddis](https://github.com/dbwiddis).
5+
* [#1101](https://github.com/oshi/oshi/pull/1101): Add network interface dropped packets and collisions. - [@dbwiddis](https://github.com/dbwiddis).
56
* Your contribution here.
67

78
4.3.0 (1/2/2020), 4.3.1 (2/5/2020)

oshi-core/src/main/java/oshi/hardware/NetworkIF.java

+135-66
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public class NetworkIF {
6363
private long packetsSent;
6464
private long inErrors;
6565
private long outErrors;
66+
private long inDrops;
67+
private long collisions;
6668
private long speed;
6769
private long timeStamp;
6870

@@ -156,10 +158,11 @@ public String getDisplayName() {
156158
* The interface Maximum Transmission Unit (MTU).
157159
* </p>
158160
*
159-
* @return The MTU of the network interface. This value is set when the
160-
* {@link oshi.hardware.NetworkIF} is instantiated and may not be up to
161-
* date. To update this value, execute the
162-
* {@link #setNetworkInterface(NetworkInterface)} method
161+
* @return The MTU of the network interface.
162+
* <p>
163+
* This value is set when the {@link oshi.hardware.NetworkIF} is
164+
* instantiated and may not be up to date. To update this value, execute
165+
* the {@link #setNetworkInterface(NetworkInterface)} method
163166
*/
164167
public int getMTU() {
165168
return this.mtu;
@@ -170,10 +173,11 @@ public int getMTU() {
170173
* The Media Access Control (MAC) address.
171174
* </p>
172175
*
173-
* @return The MAC Address. This value is set when the
174-
* {@link oshi.hardware.NetworkIF} is instantiated and may not be up to
175-
* date. To update this value, execute the
176-
* {@link #setNetworkInterface(NetworkInterface)} method
176+
* @return The MAC Address.
177+
* <p>
178+
* This value is set when the {@link oshi.hardware.NetworkIF} is
179+
* instantiated and may not be up to date. To update this value, execute
180+
* the {@link #setNetworkInterface(NetworkInterface)} method
177181
*/
178182
public String getMacaddr() {
179183
return this.mac;
@@ -184,10 +188,11 @@ public String getMacaddr() {
184188
* The Internet Protocol (IP) v4 address.
185189
* </p>
186190
*
187-
* @return The IPv4 Addresses. This value is set when the
188-
* {@link oshi.hardware.NetworkIF} is instantiated and may not be up to
189-
* date. To update this value, execute the
190-
* {@link #setNetworkInterface(NetworkInterface)} method
191+
* @return The IPv4 Addresses.
192+
* <p>
193+
* This value is set when the {@link oshi.hardware.NetworkIF} is
194+
* instantiated and may not be up to date. To update this value, execute
195+
* the {@link #setNetworkInterface(NetworkInterface)} method
191196
*/
192197
public String[] getIPv4addr() {
193198
return Arrays.copyOf(this.ipv4, this.ipv4.length);
@@ -198,10 +203,11 @@ public String[] getIPv4addr() {
198203
* The Internet Protocol (IP) v4 subnet masks.
199204
* </p>
200205
*
201-
* @return The IPv4 subnet mask length. Ranges between 0-32 This value is set
202-
* when the {@link oshi.hardware.NetworkIF} is instantiated and may not
203-
* be up to date. To update this value, execute the
204-
* {@link #setNetworkInterface(NetworkInterface)} method.
206+
* @return The IPv4 subnet mask length. Ranges between 0-32.
207+
* <p>
208+
* This value is set when the {@link oshi.hardware.NetworkIF} is
209+
* instantiated and may not be up to date. To update this value, execute
210+
* the {@link #setNetworkInterface(NetworkInterface)} method.
205211
*
206212
*/
207213
public Short[] getSubnetMasks() {
@@ -213,10 +219,11 @@ public Short[] getSubnetMasks() {
213219
* The Internet Protocol (IP) v6 address.
214220
* </p>
215221
*
216-
* @return The IPv6 Addresses. This value is set when the
217-
* {@link oshi.hardware.NetworkIF} is instantiated and may not be up to
218-
* date. To update this value, execute the
219-
* {@link #setNetworkInterface(NetworkInterface)} method
222+
* @return The IPv6 Addresses.
223+
* <p>
224+
* This value is set when the {@link oshi.hardware.NetworkIF} is
225+
* instantiated and may not be up to date. To update this value, execute
226+
* the {@link #setNetworkInterface(NetworkInterface)} method
220227
*/
221228
public String[] getIPv6addr() {
222229
return Arrays.copyOf(this.ipv6, this.ipv6.length);
@@ -227,10 +234,11 @@ public String[] getIPv6addr() {
227234
* The Internet Protocol (IP) v6 address.
228235
* </p>
229236
*
230-
* @return The IPv6 address prefix lengths. Ranges between 0-128. This value is
231-
* set when the {@link oshi.hardware.NetworkIF} is instantiated and may
232-
* not be up to date. To update this value, execute the
233-
* {@link #setNetworkInterface(NetworkInterface)} method
237+
* @return The IPv6 address prefix lengths. Ranges between 0-128.
238+
* <p>
239+
* This value is set when the {@link oshi.hardware.NetworkIF} is
240+
* instantiated and may not be up to date. To update this value, execute
241+
* the {@link #setNetworkInterface(NetworkInterface)} method
234242
*/
235243
public Short[] getPrefixLengths() {
236244
return Arrays.copyOf(this.prefixLengths, this.prefixLengths.length);
@@ -241,10 +249,11 @@ public Short[] getPrefixLengths() {
241249
* Getter for the field <code>bytesRecv</code>.
242250
* </p>
243251
*
244-
* @return The Bytes Received. This value is set when the
245-
* {@link oshi.hardware.NetworkIF} is instantiated and may not be up to
246-
* date. To update this value, execute the {@link #updateAttributes()}
247-
* method
252+
* @return The Bytes Received.
253+
* <p>
254+
* This value is set when the {@link oshi.hardware.NetworkIF} is
255+
* instantiated and may not be up to date. To update this value, execute
256+
* the {@link #updateAttributes()} method
248257
*/
249258
public long getBytesRecv() {
250259
return this.bytesRecv;
@@ -267,10 +276,11 @@ public void setBytesRecv(long bytesRecv) {
267276
* Getter for the field <code>bytesSent</code>.
268277
* </p>
269278
*
270-
* @return The Bytes Sent. This value is set when the
271-
* {@link oshi.hardware.NetworkIF} is instantiated and may not be up to
272-
* date. To update this value, execute the {@link #updateAttributes()}
273-
* method
279+
* @return The Bytes Sent.
280+
* <p>
281+
* This value is set when the {@link oshi.hardware.NetworkIF} is
282+
* instantiated and may not be up to date. To update this value, execute
283+
* the {@link #updateAttributes()} method
274284
*/
275285
public long getBytesSent() {
276286
return this.bytesSent;
@@ -293,10 +303,11 @@ public void setBytesSent(long bytesSent) {
293303
* Getter for the field <code>packetsRecv</code>.
294304
* </p>
295305
*
296-
* @return The Packets Received. This value is set when the
297-
* {@link oshi.hardware.NetworkIF} is instantiated and may not be up to
298-
* date. To update this value, execute the {@link #updateAttributes()}
299-
* method
306+
* @return The Packets Received.
307+
* <p>
308+
* This value is set when the {@link oshi.hardware.NetworkIF} is
309+
* instantiated and may not be up to date. To update this value, execute
310+
* the {@link #updateAttributes()} method
300311
*/
301312
public long getPacketsRecv() {
302313
return this.packetsRecv;
@@ -319,10 +330,11 @@ public void setPacketsRecv(long packetsRecv) {
319330
* Getter for the field <code>packetsSent</code>.
320331
* </p>
321332
*
322-
* @return The Packets Sent. This value is set when the
323-
* {@link oshi.hardware.NetworkIF} is instantiated and may not be up to
324-
* date. To update this value, execute the {@link #updateAttributes()}
325-
* method
333+
* @return The Packets Sent.
334+
* <p>
335+
* This value is set when the {@link oshi.hardware.NetworkIF} is
336+
* instantiated and may not be up to date. To update this value, execute
337+
* the {@link #updateAttributes()} method
326338
*/
327339
public long getPacketsSent() {
328340
return this.packetsSent;
@@ -345,10 +357,11 @@ public void setPacketsSent(long packetsSent) {
345357
* Getter for the field <code>inErrors</code>.
346358
* </p>
347359
*
348-
* @return Input Errors. This value is set when the
349-
* {@link oshi.hardware.NetworkIF} is instantiated and may not be up to
350-
* date. To update this value, execute the {@link #updateAttributes()}
351-
* method
360+
* @return Input Errors.
361+
* <p>
362+
* This value is set when the {@link oshi.hardware.NetworkIF} is
363+
* instantiated and may not be up to date. To update this value, execute
364+
* the {@link #updateAttributes()} method
352365
*/
353366
public long getInErrors() {
354367
return this.inErrors;
@@ -371,10 +384,11 @@ public void setInErrors(long inErrors) {
371384
* Getter for the field <code>outErrors</code>.
372385
* </p>
373386
*
374-
* @return The Output Errors. This value is set when the
375-
* {@link oshi.hardware.NetworkIF} is instantiated and may not be up to
376-
* date. To update this value, execute the {@link #updateAttributes()}
377-
* method
387+
* @return The Output Errors.
388+
* <p>
389+
* This value is set when the {@link oshi.hardware.NetworkIF} is
390+
* instantiated and may not be up to date. To update this value, execute
391+
* the {@link #updateAttributes()} method
378392
*/
379393
public long getOutErrors() {
380394
return this.outErrors;
@@ -392,15 +406,71 @@ public void setOutErrors(long outErrors) {
392406
this.outErrors = ParseUtil.unsignedLongToSignedLong(outErrors);
393407
}
394408

409+
/**
410+
* <p>
411+
* Getter for the field <code>inDrops</code>.
412+
* </p>
413+
*
414+
* @return Incoming/Received dropped packets. On Windows, returns discarded
415+
* incoming packets.
416+
* <p>
417+
* This value is set when the {@link oshi.hardware.NetworkIF} is
418+
* instantiated and may not be up to date. To update this value, execute
419+
* the {@link #updateAttributes()} method
420+
*/
421+
public long getInDrops() {
422+
return inDrops;
423+
}
424+
425+
/**
426+
* <p>
427+
* Setter for the field <code>inDrops</code>.
428+
* </p>
429+
*
430+
* @param inDrops
431+
* The incoming (receive) dropped packets to set.
432+
*/
433+
public void setInDrops(long inDrops) {
434+
this.inDrops = inDrops;
435+
}
436+
437+
/**
438+
* <p>
439+
* Getter for the field <code>collisions</code>.
440+
* </p>
441+
*
442+
* @return Packet collisions. On Windows, returns discarded outgoing packets.
443+
* <p>
444+
* This value is set when the {@link oshi.hardware.NetworkIF} is
445+
* instantiated and may not be up to date. To update this value, execute
446+
* the {@link #updateAttributes()} method
447+
*/
448+
public long getCollisions() {
449+
return collisions;
450+
}
451+
452+
/**
453+
* <p>
454+
* Setter for the field <code>collisions</code>.
455+
* </p>
456+
*
457+
* @param collisions
458+
* The collisions to set.
459+
*/
460+
public void setCollisions(long collisions) {
461+
this.collisions = collisions;
462+
}
463+
395464
/**
396465
* <p>
397466
* Getter for the field <code>speed</code>.
398467
* </p>
399468
*
400-
* @return The speed of the network interface in bits per second. This value is
401-
* set when the {@link oshi.hardware.NetworkIF} is instantiated and may
402-
* not be up to date. To update this value, execute the
403-
* {@link #updateAttributes()} method
469+
* @return The speed of the network interface in bits per second.
470+
* <p>
471+
* This value is set when the {@link oshi.hardware.NetworkIF} is
472+
* instantiated and may not be up to date. To update this value, execute
473+
* the {@link #updateAttributes()} method
404474
*/
405475
public long getSpeed() {
406476
return this.speed;
@@ -444,27 +514,24 @@ public void setTimeStamp(long timeStamp) {
444514
/**
445515
* Updates interface network statistics on this interface. Statistics include
446516
* packets and bytes sent and received, and interface speed.
517+
*
518+
* @return {@code true} if the update was successful, {@code false} otherwise.
447519
*/
448-
public void updateAttributes() {
520+
public boolean updateAttributes() {
449521
switch (SystemInfo.getCurrentPlatformEnum()) {
450522
case WINDOWS:
451-
WindowsNetworks.updateNetworkStats(this);
452-
break;
523+
return WindowsNetworks.updateNetworkStats(this);
453524
case LINUX:
454-
LinuxNetworks.updateNetworkStats(this);
455-
break;
525+
return LinuxNetworks.updateNetworkStats(this);
456526
case MACOSX:
457-
MacNetworks.updateNetworkStats(this);
458-
break;
527+
return MacNetworks.updateNetworkStats(this);
459528
case SOLARIS:
460-
SolarisNetworks.updateNetworkStats(this);
461-
break;
529+
return SolarisNetworks.updateNetworkStats(this);
462530
case FREEBSD:
463-
FreeBsdNetworks.updateNetworkStats(this);
464-
break;
531+
return FreeBsdNetworks.updateNetworkStats(this);
465532
default:
466533
LOG.error("Unsupported platform. No update performed.");
467-
break;
534+
return false;
468535
}
469536
}
470537

@@ -479,9 +546,11 @@ public String toString() {
479546
sb.append(" IPv6: ").append(Arrays.toString(getIPv6addr())).append("\n");
480547
sb.append(" Prefix Lengths: ").append(Arrays.toString(getPrefixLengths())).append("\n");
481548
sb.append(" Traffic: received ").append(getPacketsRecv()).append(" packets/")
482-
.append(FormatUtil.formatBytes(getBytesRecv())).append(" (" + getInErrors() + " err);");
549+
.append(FormatUtil.formatBytes(getBytesRecv())).append(" (" + getInErrors() + " err, ")
550+
.append(getInDrops() + " drop);");
483551
sb.append(" transmitted ").append(getPacketsSent()).append(" packets/")
484-
.append(FormatUtil.formatBytes(getBytesSent())).append(" (" + getOutErrors() + " err)");
552+
.append(FormatUtil.formatBytes(getBytesSent())).append(" (" + getOutErrors() + " err, ")
553+
.append(getCollisions() + " coll);");
485554
return sb.toString();
486555
}
487556
}

oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxNetworks.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
*/
2424
package oshi.hardware.platform.linux;
2525

26+
import java.io.File;
27+
2628
import oshi.hardware.NetworkIF;
2729
import oshi.hardware.common.AbstractNetworks;
2830
import oshi.util.FileUtil;
@@ -40,13 +42,23 @@ public class LinuxNetworks extends AbstractNetworks {
4042
* @param netIF
4143
* The interface on which to update statistics
4244
*/
43-
public static void updateNetworkStats(NetworkIF netIF) {
45+
public static boolean updateNetworkStats(NetworkIF netIF) {
46+
try {
47+
File ifDir = new File(String.format("/sys/class/net/%s/statistics", netIF.getName()));
48+
if (!ifDir.isDirectory()) {
49+
return false;
50+
}
51+
} catch (SecurityException e) {
52+
return false;
53+
}
4454
String txBytesPath = String.format("/sys/class/net/%s/statistics/tx_bytes", netIF.getName());
4555
String rxBytesPath = String.format("/sys/class/net/%s/statistics/rx_bytes", netIF.getName());
4656
String txPacketsPath = String.format("/sys/class/net/%s/statistics/tx_packets", netIF.getName());
4757
String rxPacketsPath = String.format("/sys/class/net/%s/statistics/rx_packets", netIF.getName());
4858
String txErrorsPath = String.format("/sys/class/net/%s/statistics/tx_errors", netIF.getName());
4959
String rxErrorsPath = String.format("/sys/class/net/%s/statistics/rx_errors", netIF.getName());
60+
String collisionsPath = String.format("/sys/class/net/%s/statistics/collisions", netIF.getName());
61+
String rxDropsPath = String.format("/sys/class/net/%s/statistics/rx_dropped", netIF.getName());
5062
String speed = String.format("/sys/class/net/%s/speed", netIF.getName());
5163

5264
netIF.setTimeStamp(System.currentTimeMillis());
@@ -56,7 +68,10 @@ public static void updateNetworkStats(NetworkIF netIF) {
5668
netIF.setPacketsRecv(FileUtil.getUnsignedLongFromFile(rxPacketsPath));
5769
netIF.setOutErrors(FileUtil.getUnsignedLongFromFile(txErrorsPath));
5870
netIF.setInErrors(FileUtil.getUnsignedLongFromFile(rxErrorsPath));
71+
netIF.setCollisions(FileUtil.getUnsignedLongFromFile(collisionsPath));
72+
netIF.setInDrops(FileUtil.getUnsignedLongFromFile(rxDropsPath));
5973
long netSpeed = FileUtil.getUnsignedLongFromFile(speed) * 1024 * 1024;
6074
netIF.setSpeed(netSpeed < 0 ? 0 : netSpeed);
75+
return true;
6176
}
6277
}

0 commit comments

Comments
 (0)