1
2
3
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
24
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
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
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
62
63
64
65
66
67
68
69
70
71
72
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
84 for (String fs : ExecutingCommand.runNative("mount")) {
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 String[] split = ParseUtil.whitespaces.split("x" + fs);
101 if (split.length > 7) {
102
103
104
105
106
107 String volume = split[1];
108 String path = split[2];
109 String type = split[3];
110 String options = split[4];
111
112
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
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 }