All Downloads are FREE. Search and download functionalities are using the official Maven repository.

oshi.util.platform.linux.ProcUtil Maven / Gradle / Ivy

There is a newer version: 3.4.0
Show newest version
/**
 * Oshi (https://github.com/dblock/oshi)
 *
 * Copyright (c) 2010 - 2016 The Oshi Project Team
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Maintainers:
 * dblock[at]dblock[dot]org
 * widdis[at]gmail[dot]com
 * enrico.bianchi[at]gmail[dot]com
 *
 * Contributors:
 * https://github.com/dblock/oshi/graphs/contributors
 */
package oshi.util.platform.linux;

import java.io.File;
import java.io.FileFilter;
import java.util.regex.Pattern;

import oshi.util.FileUtil;

/**
 * Provides access to some /proc filesystem info on Linux
 * 
 * @author widdis[at]gmail[dot]com
 */
public class ProcUtil {
    /**
     * Parses the first value in /proc/uptime for seconds since boot
     * 
     * @return Seconds since boot
     */
    public static float getSystemUptimeFromProc() {
        String[] split = FileUtil.getSplitFromFile("/proc/uptime");
        if (split.length > 0) {
            try {
                return Float.parseFloat(split[0]);
            } catch (NumberFormatException nfe) {
                return 0f;
            }
        }
        return 0f;
    }

    /**
     * Gets an array of files in the /proc directory with only numeric digit
     * filenames, corresponding to processes
     * 
     * @return An array of File objects for the process files
     */
    public static File[] getPidFiles() {
        File procdir = new File("/proc");
        final Pattern p = Pattern.compile("\\d+");
        File[] pids = procdir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {
                return p.matcher(file.getName()).matches();
            }
        });
        return pids != null ? pids : new File[0];
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy