parse_nfs.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // ParseClientRPCStats returns stats read from /proc/net/rpc/nfs
  22. func ParseClientRPCStats(r io.Reader) (*ClientRPCStats, error) {
  23. stats := &ClientRPCStats{}
  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 NFS metric line %q", line)
  31. }
  32. values, err := util.ParseUint64s(parts[1:])
  33. if err != nil {
  34. return nil, fmt.Errorf("error parsing NFS metric line: %s", err)
  35. }
  36. switch metricLine := parts[0]; metricLine {
  37. case "net":
  38. stats.Network, err = parseNetwork(values)
  39. case "rpc":
  40. stats.ClientRPC, err = parseClientRPC(values)
  41. case "proc2":
  42. stats.V2Stats, err = parseV2Stats(values)
  43. case "proc3":
  44. stats.V3Stats, err = parseV3Stats(values)
  45. case "proc4":
  46. stats.ClientV4Stats, err = parseClientV4Stats(values)
  47. default:
  48. return nil, fmt.Errorf("unknown NFS metric line %q", metricLine)
  49. }
  50. if err != nil {
  51. return nil, fmt.Errorf("errors parsing NFS metric line: %s", err)
  52. }
  53. }
  54. if err := scanner.Err(); err != nil {
  55. return nil, fmt.Errorf("error scanning NFS file: %s", err)
  56. }
  57. return stats, nil
  58. }