View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.software.os.unix.aix;
6   
7   import java.io.File;
8   import java.nio.file.PathMatcher;
9   import java.util.ArrayList;
10  import java.util.HashMap;
11  import java.util.List;
12  import java.util.Map;
13  
14  import oshi.annotation.concurrent.ThreadSafe;
15  import oshi.software.common.AbstractFileSystem;
16  import oshi.software.os.OSFileStore;
17  import oshi.util.ExecutingCommand;
18  import oshi.util.FileSystemUtil;
19  import oshi.util.FileUtil;
20  import oshi.util.ParseUtil;
21  
22  /**
23   * The AIX File System contains {@link oshi.software.os.OSFileStore}s which are a storage pool, device, partition,
24   * volume, concrete file system or other implementation specific means of file storage.
25   */
26  @ThreadSafe
27  public class AixFileSystem extends AbstractFileSystem {
28  
29      public static final String OSHI_AIX_FS_PATH_EXCLUDES = "oshi.os.aix.filesystem.path.excludes";
30      public static final String OSHI_AIX_FS_PATH_INCLUDES = "oshi.os.aix.filesystem.path.includes";
31      public static final String OSHI_AIX_FS_VOLUME_EXCLUDES = "oshi.os.aix.filesystem.volume.excludes";
32      public static final String OSHI_AIX_FS_VOLUME_INCLUDES = "oshi.os.aix.filesystem.volume.includes";
33  
34      private static final List<PathMatcher> FS_PATH_EXCLUDES = FileSystemUtil
35              .loadAndParseFileSystemConfig(OSHI_AIX_FS_PATH_EXCLUDES);
36      private static final List<PathMatcher> FS_PATH_INCLUDES = FileSystemUtil
37              .loadAndParseFileSystemConfig(OSHI_AIX_FS_PATH_INCLUDES);
38      private static final List<PathMatcher> FS_VOLUME_EXCLUDES = FileSystemUtil
39              .loadAndParseFileSystemConfig(OSHI_AIX_FS_VOLUME_EXCLUDES);
40      private static final List<PathMatcher> FS_VOLUME_INCLUDES = FileSystemUtil
41              .loadAndParseFileSystemConfig(OSHI_AIX_FS_VOLUME_INCLUDES);
42  
43      @Override
44      public List<OSFileStore> getFileStores(boolean localOnly) {
45          return getFileStoreMatching(null, localOnly);
46      }
47  
48      // Called by AixOSFileStore
49      static List<OSFileStore> getFileStoreMatching(String nameToMatch) {
50          return getFileStoreMatching(nameToMatch, false);
51      }
52  
53      private static List<OSFileStore> getFileStoreMatching(String nameToMatch, boolean localOnly) {
54          List<OSFileStore> fsList = new ArrayList<>();
55  
56          // Get inode usage data
57          Map<String, Long> inodeFreeMap = new HashMap<>();
58          Map<String, Long> inodeTotalMap = new HashMap<>();
59          String command = "df -i" + (localOnly ? " -l" : "");
60          for (String line : ExecutingCommand.runNative(command)) {
61              /*- Sample Output:
62               $ df -i
63              Filesystem            Inodes   IUsed   IFree IUse% Mounted on
64              /dev/hd4               75081   16741   58340   23% /
65              /dev/hd2              269640   43104  226536   16% /usr
66              /dev/hd9var            43598    1370   42228    4% /var
67              /dev/hd3               79936     386   79550    1% /tmp
68              /dev/hd11admin         29138       7   29131    1% /admin
69              /proc                      0       0       0    -  /proc
70              /dev/hd10opt           47477    4232   43245    9% /opt
71              /dev/livedump          58204       4   58200    1% /var/adm/ras/livedump
72              /dev/fslv00          12419240  292668 12126572    3% /home
73              */
74              if (line.startsWith("/")) {
75                  String[] split = ParseUtil.whitespaces.split(line);
76                  if (split.length > 5) {
77                      inodeTotalMap.put(split[0], ParseUtil.parseLongOrDefault(split[1], 0L));
78                      inodeFreeMap.put(split[0], ParseUtil.parseLongOrDefault(split[3], 0L));
79                  }
80              }
81          }
82  
83          // Get mount table
84          for (String fs : ExecutingCommand.runNative("mount")) { // NOSONAR squid:S135
85              /*- Sample Output:
86               *   node       mounted        mounted over    vfs       date        options
87              * -------- ---------------  ---------------  ------ ------------ ---------------
88              *          /dev/hd4         /                jfs2   Jun 16 09:12 rw,log=/dev/hd8
89              *          /dev/hd2         /usr             jfs2   Jun 16 09:12 rw,log=/dev/hd8
90              *          /dev/hd9var      /var             jfs2   Jun 16 09:12 rw,log=/dev/hd8
91              *          /dev/hd3         /tmp             jfs2   Jun 16 09:12 rw,log=/dev/hd8
92              *          /dev/hd11admin   /admin           jfs2   Jun 16 09:13 rw,log=/dev/hd8
93              *          /proc            /proc            procfs Jun 16 09:13 rw
94              *          /dev/hd10opt     /opt             jfs2   Jun 16 09:13 rw,log=/dev/hd8
95              *          /dev/livedump    /var/adm/ras/livedump jfs2   Jun 16 09:13 rw,log=/dev/hd8
96              * foo      /dev/fslv00      /home            jfs2   Jun 16 09:13 rw,log=/dev/loglv00
97               */
98              // Lines begin with optional node, which we don't use. To force sensible split
99              // behavior, append any character at the beginning of the string
100             String[] split = ParseUtil.whitespaces.split("x" + fs);
101             if (split.length > 7) {
102                 // 1st field is volume name [0-index]
103                 // 2nd field is mount point
104                 // 3rd field is fs type
105                 // 4th-6th fields are date, ignored
106                 // 7th field is options
107                 String volume = split[1];
108                 String path = split[2];
109                 String type = split[3];
110                 String options = split[4];
111 
112                 // Skip non-local drives if requested, and exclude pseudo file systems
113                 if ((localOnly && NETWORK_FS_TYPES.contains(type)) || !path.equals("/")
114                         && (PSEUDO_FS_TYPES.contains(type) || FileSystemUtil.isFileStoreExcluded(path, volume,
115                                 FS_PATH_INCLUDES, FS_PATH_EXCLUDES, FS_VOLUME_INCLUDES, FS_VOLUME_EXCLUDES))) {
116                     continue;
117                 }
118 
119                 String name = path.substring(path.lastIndexOf('/') + 1);
120                 // Special case for /, pull last element of volume instead
121                 if (name.isEmpty()) {
122                     name = volume.substring(volume.lastIndexOf('/') + 1);
123                 }
124 
125                 if (nameToMatch != null && !nameToMatch.equals(name)) {
126                     continue;
127                 }
128                 File f = new File(path);
129                 if (!f.exists() || f.getTotalSpace() < 0) {
130                     continue;
131                 }
132                 long totalSpace = f.getTotalSpace();
133                 long usableSpace = f.getUsableSpace();
134                 long freeSpace = f.getFreeSpace();
135 
136                 String description;
137                 if (volume.startsWith("/dev") || path.equals("/")) {
138                     description = "Local Disk";
139                 } else if (volume.equals("tmpfs")) {
140                     description = "Ram Disk";
141                 } else if (NETWORK_FS_TYPES.contains(type)) {
142                     description = "Network Disk";
143                 } else {
144                     description = "Mount Point";
145                 }
146 
147                 fsList.add(new AixOSFileStore(name, volume, name, path, options, "", "", description, type, freeSpace,
148                         usableSpace, totalSpace, inodeFreeMap.getOrDefault(volume, 0L),
149                         inodeTotalMap.getOrDefault(volume, 0L)));
150             }
151         }
152         return fsList;
153     }
154 
155     @Override
156     public long getOpenFileDescriptors() {
157         boolean header = false;
158         long openfiles = 0L;
159         for (String f : ExecutingCommand.runNative("lsof -nl")) {
160             if (!header) {
161                 header = f.startsWith("COMMAND");
162             } else {
163                 openfiles++;
164             }
165         }
166         return openfiles;
167     }
168 
169     @Override
170     public long getMaxFileDescriptors() {
171         return ParseUtil.parseLongOrDefault(ExecutingCommand.getFirstAnswer("ulimit -n"), 0L);
172     }
173 
174     @Override
175     public long getMaxFileDescriptorsPerProcess() {
176         final List<String> lines = FileUtil.readFile("/etc/security/limits");
177         for (final String line : lines) {
178             if (line.trim().startsWith("nofiles")) {
179                 return ParseUtil.parseLastLong(line, Long.MAX_VALUE);
180             }
181         }
182         return Long.MAX_VALUE;
183     }
184 }