fs.go 1.4 KB

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