blob: b6c6b2ce1f06d36741307cc6f0853e92fab766be [file] [log] [blame]
khenaidooffe076b2019-01-15 16:08:08 -05001// Copyright 2018 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package procfs
15
16import (
17 "fmt"
18 "os"
19 "path"
20
21 "github.com/prometheus/procfs/nfs"
22 "github.com/prometheus/procfs/xfs"
23)
24
25// FS represents the pseudo-filesystem proc, which provides an interface to
26// kernel data structures.
27type FS string
28
29// DefaultMountPoint is the common mount point of the proc filesystem.
30const DefaultMountPoint = "/proc"
31
32// NewFS returns a new FS mounted under the given mountPoint. It will error
33// if the mount point can't be read.
34func NewFS(mountPoint string) (FS, error) {
35 info, err := os.Stat(mountPoint)
36 if err != nil {
37 return "", fmt.Errorf("could not read %s: %s", mountPoint, err)
38 }
39 if !info.IsDir() {
40 return "", fmt.Errorf("mount point %s is not a directory", mountPoint)
41 }
42
43 return FS(mountPoint), nil
44}
45
46// Path returns the path of the given subsystem relative to the procfs root.
47func (fs FS) Path(p ...string) string {
48 return path.Join(append([]string{string(fs)}, p...)...)
49}
50
51// XFSStats retrieves XFS filesystem runtime statistics.
52func (fs FS) XFSStats() (*xfs.Stats, error) {
53 f, err := os.Open(fs.Path("fs/xfs/stat"))
54 if err != nil {
55 return nil, err
56 }
57 defer f.Close()
58
59 return xfs.ParseStats(f)
60}
61
62// NFSClientRPCStats retrieves NFS client RPC statistics.
63func (fs FS) NFSClientRPCStats() (*nfs.ClientRPCStats, error) {
64 f, err := os.Open(fs.Path("net/rpc/nfs"))
65 if err != nil {
66 return nil, err
67 }
68 defer f.Close()
69
70 return nfs.ParseClientRPCStats(f)
71}
72
73// NFSdServerRPCStats retrieves NFS daemon RPC statistics.
74func (fs FS) NFSdServerRPCStats() (*nfs.ServerRPCStats, error) {
75 f, err := os.Open(fs.Path("net/rpc/nfsd"))
76 if err != nil {
77 return nil, err
78 }
79 defer f.Close()
80
81 return nfs.ParseServerRPCStats(f)
82}