View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware.platform.unix.aix;
6   
7   import static oshi.util.Memoizer.defaultExpiration;
8   import static oshi.util.Memoizer.memoize;
9   
10  import java.net.NetworkInterface;
11  import java.util.ArrayList;
12  import java.util.List;
13  import java.util.function.Supplier;
14  
15  import org.slf4j.Logger;
16  import org.slf4j.LoggerFactory;
17  
18  import com.sun.jna.Native;
19  import com.sun.jna.platform.unix.aix.Perfstat.perfstat_netinterface_t;
20  
21  import oshi.annotation.concurrent.ThreadSafe;
22  import oshi.driver.unix.aix.perfstat.PerfstatNetInterface;
23  import oshi.hardware.NetworkIF;
24  import oshi.hardware.common.AbstractNetworkIF;
25  
26  /**
27   * AIXNetworks class.
28   */
29  @ThreadSafe
30  public final class AixNetworkIF extends AbstractNetworkIF {
31  
32      private static final Logger LOG = LoggerFactory.getLogger(AixNetworkIF.class);
33  
34      private long bytesRecv;
35      private long bytesSent;
36      private long packetsRecv;
37      private long packetsSent;
38      private long inErrors;
39      private long outErrors;
40      private long inDrops;
41      private long collisions;
42      private long speed;
43      private long timeStamp;
44  
45      private Supplier<perfstat_netinterface_t[]> netstats;
46  
47      public AixNetworkIF(NetworkInterface netint, Supplier<perfstat_netinterface_t[]> netstats)
48              throws InstantiationException {
49          super(netint);
50          this.netstats = netstats;
51          updateAttributes();
52      }
53  
54      /**
55       * Gets all network interfaces on this machine
56       *
57       * @param includeLocalInterfaces include local interfaces in the result
58       * @return A list of {@link NetworkIF} objects representing the interfaces
59       */
60      public static List<NetworkIF> getNetworks(boolean includeLocalInterfaces) {
61          Supplier<perfstat_netinterface_t[]> netstats = memoize(PerfstatNetInterface::queryNetInterfaces,
62                  defaultExpiration());
63          List<NetworkIF> ifList = new ArrayList<>();
64          for (NetworkInterface ni : getNetworkInterfaces(includeLocalInterfaces)) {
65              try {
66                  ifList.add(new AixNetworkIF(ni, netstats));
67              } catch (InstantiationException e) {
68                  LOG.debug("Network Interface Instantiation failed: {}", e.getMessage());
69              }
70          }
71          return ifList;
72      }
73  
74      @Override
75      public long getBytesRecv() {
76          return this.bytesRecv;
77      }
78  
79      @Override
80      public long getBytesSent() {
81          return this.bytesSent;
82      }
83  
84      @Override
85      public long getPacketsRecv() {
86          return this.packetsRecv;
87      }
88  
89      @Override
90      public long getPacketsSent() {
91          return this.packetsSent;
92      }
93  
94      @Override
95      public long getInErrors() {
96          return this.inErrors;
97      }
98  
99      @Override
100     public long getOutErrors() {
101         return this.outErrors;
102     }
103 
104     @Override
105     public long getInDrops() {
106         return this.inDrops;
107     }
108 
109     @Override
110     public long getCollisions() {
111         return this.collisions;
112     }
113 
114     @Override
115     public long getSpeed() {
116         return this.speed;
117     }
118 
119     @Override
120     public long getTimeStamp() {
121         return this.timeStamp;
122     }
123 
124     @Override
125     public boolean updateAttributes() {
126         perfstat_netinterface_t[] stats = netstats.get();
127         long now = System.currentTimeMillis();
128         for (perfstat_netinterface_t stat : stats) {
129             String name = Native.toString(stat.name);
130             if (name.equals(this.getName())) {
131                 this.bytesSent = stat.obytes;
132                 this.bytesRecv = stat.ibytes;
133                 this.packetsSent = stat.opackets;
134                 this.packetsRecv = stat.ipackets;
135                 this.outErrors = stat.oerrors;
136                 this.inErrors = stat.ierrors;
137                 this.collisions = stat.collisions;
138                 this.inDrops = stat.if_iqdrops;
139                 this.speed = stat.bitrate;
140                 this.timeStamp = now;
141                 return true;
142             }
143         }
144         return false;
145     }
146 }