parse_nfsd.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 nfs
  14. import (
  15. "bufio"
  16. "fmt"
  17. "io"
  18. "strings"
  19. "github.com/prometheus/procfs/internal/util"
  20. )
  21. // ParseServerRPCStats returns stats read from /proc/net/rpc/nfsd
  22. func ParseServerRPCStats(r io.Reader) (*ServerRPCStats, error) {
  23. stats := &ServerRPCStats{}
  24. scanner := bufio.NewScanner(r)
  25. for scanner.Scan() {
  26. line := scanner.Text()
  27. parts := strings.Fields(scanner.Text())
  28. // require at least <key> <value>
  29. if len(parts) < 2 {
  30. return nil, fmt.Errorf("invalid NFSd metric line %q", line)
  31. }
  32. label := parts[0]
  33. var values []uint64
  34. var err error
  35. if label == "th" {
  36. if len(parts) < 3 {
  37. return nil, fmt.Errorf("invalid NFSd th metric line %q", line)
  38. }
  39. values, err = util.ParseUint64s(parts[1:3])
  40. } else {
  41. values, err = util.ParseUint64s(parts[1:])
  42. }
  43. if err != nil {
  44. return nil, fmt.Errorf("error parsing NFSd metric line: %s", err)
  45. }
  46. switch metricLine := parts[0]; metricLine {
  47. case "rc":
  48. stats.ReplyCache, err = parseReplyCache(values)
  49. case "fh":
  50. stats.FileHandles, err = parseFileHandles(values)
  51. case "io":
  52. stats.InputOutput, err = parseInputOutput(values)
  53. case "th":
  54. stats.Threads, err = parseThreads(values)
  55. case "ra":
  56. stats.ReadAheadCache, err = parseReadAheadCache(values)
  57. case "net":
  58. stats.Network, err = parseNetwork(values)
  59. case "rpc":
  60. stats.ServerRPC, err = parseServerRPC(values)
  61. case "proc2":
  62. stats.V2Stats, err = parseV2Stats(values)
  63. case "proc3":
  64. stats.V3Stats, err = parseV3Stats(values)
  65. case "proc4":
  66. stats.ServerV4Stats, err = parseServerV4Stats(values)
  67. case "proc4ops":
  68. stats.V4Ops, err = parseV4Ops(values)
  69. default:
  70. return nil, fmt.Errorf("unknown NFSd metric line %q", metricLine)
  71. }
  72. if err != nil {
  73. return nil, fmt.Errorf("errors parsing NFSd metric line: %s", err)
  74. }
  75. }
  76. if err := scanner.Err(); err != nil {
  77. return nil, fmt.Errorf("error scanning NFSd file: %s", err)
  78. }
  79. return stats, nil
  80. }