fs.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // 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. package procfs
  14. import (
  15. "fmt"
  16. "os"
  17. "path"
  18. "github.com/prometheus/procfs/nfs"
  19. "github.com/prometheus/procfs/xfs"
  20. )
  21. // FS represents the pseudo-filesystem proc, which provides an interface to
  22. // kernel data structures.
  23. type FS string
  24. // DefaultMountPoint is the common mount point of the proc filesystem.
  25. const DefaultMountPoint = "/proc"
  26. // NewFS returns a new FS mounted under the given mountPoint. It will error
  27. // if the mount point can't be read.
  28. func NewFS(mountPoint string) (FS, error) {
  29. info, err := os.Stat(mountPoint)
  30. if err != nil {
  31. return "", fmt.Errorf("could not read %s: %s", mountPoint, err)
  32. }
  33. if !info.IsDir() {
  34. return "", fmt.Errorf("mount point %s is not a directory", mountPoint)
  35. }
  36. return FS(mountPoint), nil
  37. }
  38. // Path returns the path of the given subsystem relative to the procfs root.
  39. func (fs FS) Path(p ...string) string {
  40. return path.Join(append([]string{string(fs)}, p...)...)
  41. }
  42. // XFSStats retrieves XFS filesystem runtime statistics.
  43. func (fs FS) XFSStats() (*xfs.Stats, error) {
  44. f, err := os.Open(fs.Path("fs/xfs/stat"))
  45. if err != nil {
  46. return nil, err
  47. }
  48. defer f.Close()
  49. return xfs.ParseStats(f)
  50. }
  51. // NFSClientRPCStats retrieves NFS client RPC statistics.
  52. func (fs FS) NFSClientRPCStats() (*nfs.ClientRPCStats, error) {
  53. f, err := os.Open(fs.Path("net/rpc/nfs"))
  54. if err != nil {
  55. return nil, err
  56. }
  57. defer f.Close()
  58. return nfs.ParseClientRPCStats(f)
  59. }
  60. // NFSdServerRPCStats retrieves NFS daemon RPC statistics.
  61. func (fs FS) NFSdServerRPCStats() (*nfs.ServerRPCStats, error) {
  62. f, err := os.Open(fs.Path("net/rpc/nfsd"))
  63. if err != nil {
  64. return nil, err
  65. }
  66. defer f.Close()
  67. return nfs.ParseServerRPCStats(f)
  68. }