1package procfs
2
3import (
4	"fmt"
5	"os"
6	"path"
7
8	"github.com/prometheus/procfs/xfs"
9)
10
11// FS represents the pseudo-filesystem proc, which provides an interface to
12// kernel data structures.
13type FS string
14
15// DefaultMountPoint is the common mount point of the proc filesystem.
16const DefaultMountPoint = "/proc"
17
18// NewFS returns a new FS mounted under the given mountPoint. It will error
19// if the mount point can't be read.
20func NewFS(mountPoint string) (FS, error) {
21	info, err := os.Stat(mountPoint)
22	if err != nil {
23		return "", fmt.Errorf("could not read %s: %s", mountPoint, err)
24	}
25	if !info.IsDir() {
26		return "", fmt.Errorf("mount point %s is not a directory", mountPoint)
27	}
28
29	return FS(mountPoint), nil
30}
31
32// Path returns the path of the given subsystem relative to the procfs root.
33func (fs FS) Path(p ...string) string {
34	return path.Join(append([]string{string(fs)}, p...)...)
35}
36
37// XFSStats retrieves XFS filesystem runtime statistics.
38func (fs FS) XFSStats() (*xfs.Stats, error) {
39	f, err := os.Open(fs.Path("fs/xfs/stat"))
40	if err != nil {
41		return nil, err
42	}
43	defer f.Close()
44
45	return xfs.ParseStats(f)
46}
47