oshi.software.os.unix.solaris.SolarisFileSystem Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2016-2022 The OSHI Project Contributors
* SPDX-License-Identifier: MIT
*/
package oshi.software.os.unix.solaris;
import static oshi.software.os.unix.solaris.SolarisOperatingSystem.HAS_KSTAT2;
import static oshi.util.Memoizer.defaultExpiration;
import java.io.File;
import java.nio.file.PathMatcher;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import com.sun.jna.platform.unix.solaris.LibKstat.Kstat;
import oshi.annotation.concurrent.ThreadSafe;
import oshi.software.common.AbstractFileSystem;
import oshi.software.os.OSFileStore;
import oshi.util.ExecutingCommand;
import oshi.util.FileSystemUtil;
import oshi.util.FileUtil;
import oshi.util.Memoizer;
import oshi.util.ParseUtil;
import oshi.util.platform.unix.solaris.KstatUtil;
import oshi.util.platform.unix.solaris.KstatUtil.KstatChain;
import oshi.util.tuples.Pair;
/**
* The Solaris File System contains {@link oshi.software.os.OSFileStore}s which are a storage pool, device, partition,
* volume, concrete file system or other implementation specific means of file storage. In Solaris, these are found in
* the /proc/mount filesystem, excluding temporary and kernel mounts.
*/
@ThreadSafe
public class SolarisFileSystem extends AbstractFileSystem {
private static final Supplier> FILE_DESC = Memoizer
.memoize(SolarisFileSystem::queryFileDescriptors, defaultExpiration());
public static final String OSHI_SOLARIS_FS_PATH_EXCLUDES = "oshi.os.solaris.filesystem.path.excludes";
public static final String OSHI_SOLARIS_FS_PATH_INCLUDES = "oshi.os.solaris.filesystem.path.includes";
public static final String OSHI_SOLARIS_FS_VOLUME_EXCLUDES = "oshi.os.solaris.filesystem.volume.excludes";
public static final String OSHI_SOLARIS_FS_VOLUME_INCLUDES = "oshi.os.solaris.filesystem.volume.includes";
private static final List FS_PATH_EXCLUDES = FileSystemUtil
.loadAndParseFileSystemConfig(OSHI_SOLARIS_FS_PATH_EXCLUDES);
private static final List FS_PATH_INCLUDES = FileSystemUtil
.loadAndParseFileSystemConfig(OSHI_SOLARIS_FS_PATH_INCLUDES);
private static final List FS_VOLUME_EXCLUDES = FileSystemUtil
.loadAndParseFileSystemConfig(OSHI_SOLARIS_FS_VOLUME_EXCLUDES);
private static final List FS_VOLUME_INCLUDES = FileSystemUtil
.loadAndParseFileSystemConfig(OSHI_SOLARIS_FS_VOLUME_INCLUDES);
@Override
public List getFileStores(boolean localOnly) {
return getFileStoreMatching(null, localOnly);
}
// Called by SolarisOSFileStore
static List getFileStoreMatching(String nameToMatch) {
return getFileStoreMatching(nameToMatch, false);
}
private static List getFileStoreMatching(String nameToMatch, boolean localOnly) {
List fsList = new ArrayList<>();
// Get inode usage data
Map inodeFreeMap = new HashMap<>();
Map inodeTotalMap = new HashMap<>();
String key = null;
String total = null;
String free = null;
String command = "df -g" + (localOnly ? " -l" : "");
for (String line : ExecutingCommand.runNative(command)) {
/*- Sample Output:
/ (/dev/md/dsk/d0 ): 8192 block size 1024 frag size
41310292 total blocks 18193814 free blocks 17780712 available 2486848 total files
2293351 free files 22282240 filesys id
ufs fstype 0x00000004 flag 255 filename length
*/
if (line.startsWith("/")) {
key = ParseUtil.whitespaces.split(line)[0];
total = null;
} else if (line.contains("available") && line.contains("total files")) {
total = ParseUtil.getTextBetweenStrings(line, "available", "total files").trim();
} else if (line.contains("free files")) {
free = ParseUtil.getTextBetweenStrings(line, "", "free files").trim();
if (key != null && total != null) {
inodeFreeMap.put(key, ParseUtil.parseLongOrDefault(free, 0L));
inodeTotalMap.put(key, ParseUtil.parseLongOrDefault(total, 0L));
key = null;
}
}
}
// Get mount table
for (String fs : ExecutingCommand.runNative("cat /etc/mnttab")) { // NOSONAR squid:S135
String[] split = ParseUtil.whitespaces.split(fs);
if (split.length < 5) {
continue;
}
// 1st field is volume name
// 2nd field is mount point
// 3rd field is fs type
// 4th field is options
// other fields ignored
String volume = split[0];
String path = split[1];
String type = split[2];
String options = split[3];
// Skip non-local drives if requested, and exclude pseudo file systems
if ((localOnly && NETWORK_FS_TYPES.contains(type))
|| !path.equals("/") && (PSEUDO_FS_TYPES.contains(type) || FileSystemUtil.isFileStoreExcluded(path,
volume, FS_PATH_INCLUDES, FS_PATH_EXCLUDES, FS_VOLUME_INCLUDES, FS_VOLUME_EXCLUDES))) {
continue;
}
String name = path.substring(path.lastIndexOf('/') + 1);
// Special case for /, pull last element of volume instead
if (name.isEmpty()) {
name = volume.substring(volume.lastIndexOf('/') + 1);
}
if (nameToMatch != null && !nameToMatch.equals(name)) {
continue;
}
File f = new File(path);
long totalSpace = f.getTotalSpace();
long usableSpace = f.getUsableSpace();
long freeSpace = f.getFreeSpace();
String description;
if (volume.startsWith("/dev") || path.equals("/")) {
description = "Local Disk";
} else if (volume.equals("tmpfs")) {
description = "Ram Disk";
} else if (NETWORK_FS_TYPES.contains(type)) {
description = "Network Disk";
} else {
description = "Mount Point";
}
fsList.add(new SolarisOSFileStore(name, volume, name, path, options, "", "", description, type, freeSpace,
usableSpace, totalSpace, inodeFreeMap.containsKey(path) ? inodeFreeMap.get(path) : 0L,
inodeTotalMap.containsKey(path) ? inodeTotalMap.get(path) : 0L));
}
return fsList;
}
@Override
public long getOpenFileDescriptors() {
if (HAS_KSTAT2) {
// Use Kstat2 implementation
return FILE_DESC.get().getA();
}
try (KstatChain kc = KstatUtil.openChain()) {
Kstat ksp = kc.lookup(null, -1, "file_cache");
// Set values
if (ksp != null && kc.read(ksp)) {
return KstatUtil.dataLookupLong(ksp, "buf_inuse");
}
}
return 0L;
}
@Override
public long getMaxFileDescriptors() {
if (HAS_KSTAT2) {
// Use Kstat2 implementation
return FILE_DESC.get().getB();
}
try (KstatChain kc = KstatUtil.openChain()) {
Kstat ksp = kc.lookup(null, -1, "file_cache");
// Set values
if (ksp != null && kc.read(ksp)) {
return KstatUtil.dataLookupLong(ksp, "buf_max");
}
}
return 0L;
}
@Override
public long getMaxFileDescriptorsPerProcess() {
final List lines = FileUtil.readFile("/etc/system");
for (final String line : lines) {
if (line.startsWith("set rlim_fd_max")) {
return ParseUtil.parseLastLong(line, 65536L);
}
}
return 65536L; // 65536 is the default value for the process open file limit in Solaris
}
private static Pair queryFileDescriptors() {
Object[] results = KstatUtil.queryKstat2("kstat:/kmem_cache/kmem_default/file_cache", "buf_inuse", "buf_max");
long inuse = results[0] == null ? 0L : (long) results[0];
long max = results[1] == null ? 0L : (long) results[1];
return new Pair<>(inuse, max);
}
}