1
2
3
4
5 package oshi.hardware.platform.unix;
6
7 import java.net.NetworkInterface;
8 import java.util.ArrayList;
9 import java.util.List;
10
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 import oshi.annotation.concurrent.ThreadSafe;
15 import oshi.hardware.NetworkIF;
16 import oshi.hardware.common.AbstractNetworkIF;
17 import oshi.util.ExecutingCommand;
18 import oshi.util.ParseUtil;
19
20
21
22
23 @ThreadSafe
24 public final class BsdNetworkIF extends AbstractNetworkIF {
25
26 private static final Logger LOG = LoggerFactory.getLogger(BsdNetworkIF.class);
27
28 private long bytesRecv;
29 private long bytesSent;
30 private long packetsRecv;
31 private long packetsSent;
32 private long inErrors;
33 private long outErrors;
34 private long inDrops;
35 private long collisions;
36 private long timeStamp;
37
38 public BsdNetworkIF(NetworkInterface netint) throws InstantiationException {
39 super(netint);
40 updateAttributes();
41 }
42
43
44
45
46
47
48
49 public static List<NetworkIF> getNetworks(boolean includeLocalInterfaces) {
50 List<NetworkIF> ifList = new ArrayList<>();
51 for (NetworkInterface ni : getNetworkInterfaces(includeLocalInterfaces)) {
52 try {
53 ifList.add(new BsdNetworkIF(ni));
54 } catch (InstantiationException e) {
55 LOG.debug("Network Interface Instantiation failed: {}", e.getMessage());
56 }
57 }
58 return ifList;
59 }
60
61 @Override
62 public long getBytesRecv() {
63 return this.bytesRecv;
64 }
65
66 @Override
67 public long getBytesSent() {
68 return this.bytesSent;
69 }
70
71 @Override
72 public long getPacketsRecv() {
73 return this.packetsRecv;
74 }
75
76 @Override
77 public long getPacketsSent() {
78 return this.packetsSent;
79 }
80
81 @Override
82 public long getInErrors() {
83 return this.inErrors;
84 }
85
86 @Override
87 public long getOutErrors() {
88 return this.outErrors;
89 }
90
91 @Override
92 public long getInDrops() {
93 return this.inDrops;
94 }
95
96 @Override
97 public long getCollisions() {
98 return this.collisions;
99 }
100
101 @Override
102 public long getSpeed() {
103 return 0;
104 }
105
106 @Override
107 public long getTimeStamp() {
108 return this.timeStamp;
109 }
110
111 @Override
112 public boolean updateAttributes() {
113 String stats = ExecutingCommand.getAnswerAt("netstat -bI " + getName(), 1);
114 this.timeStamp = System.currentTimeMillis();
115 String[] split = ParseUtil.whitespaces.split(stats);
116 if (split.length < 12) {
117
118 return false;
119 }
120 this.bytesSent = ParseUtil.parseUnsignedLongOrDefault(split[10], 0L);
121 this.bytesRecv = ParseUtil.parseUnsignedLongOrDefault(split[7], 0L);
122 this.packetsSent = ParseUtil.parseUnsignedLongOrDefault(split[8], 0L);
123 this.packetsRecv = ParseUtil.parseUnsignedLongOrDefault(split[4], 0L);
124 this.outErrors = ParseUtil.parseUnsignedLongOrDefault(split[9], 0L);
125 this.inErrors = ParseUtil.parseUnsignedLongOrDefault(split[5], 0L);
126 this.collisions = ParseUtil.parseUnsignedLongOrDefault(split[11], 0L);
127 this.inDrops = ParseUtil.parseUnsignedLongOrDefault(split[6], 0L);
128 return true;
129 }
130 }