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
14package nfs
15
16import (
17	"bufio"
18	"fmt"
19	"io"
20	"strings"
21
22	"github.com/prometheus/procfs/internal/util"
23)
24
25// ParseClientRPCStats returns stats read from /proc/net/rpc/nfs
26func ParseClientRPCStats(r io.Reader) (*ClientRPCStats, error) {
27	stats := &ClientRPCStats{}
28
29	scanner := bufio.NewScanner(r)
30	for scanner.Scan() {
31		line := scanner.Text()
32		parts := strings.Fields(scanner.Text())
33		// require at least <key> <value>
34		if len(parts) < 2 {
35			return nil, fmt.Errorf("invalid NFS metric line %q", line)
36		}
37
38		values, err := util.ParseUint64s(parts[1:])
39		if err != nil {
40			return nil, fmt.Errorf("error parsing NFS metric line: %w", err)
41		}
42
43		switch metricLine := parts[0]; metricLine {
44		case "net":
45			stats.Network, err = parseNetwork(values)
46		case "rpc":
47			stats.ClientRPC, err = parseClientRPC(values)
48		case "proc2":
49			stats.V2Stats, err = parseV2Stats(values)
50		case "proc3":
51			stats.V3Stats, err = parseV3Stats(values)
52		case "proc4":
53			stats.ClientV4Stats, err = parseClientV4Stats(values)
54		default:
55			return nil, fmt.Errorf("unknown NFS metric line %q", metricLine)
56		}
57		if err != nil {
58			return nil, fmt.Errorf("errors parsing NFS metric line: %w", err)
59		}
60	}
61
62	if err := scanner.Err(); err != nil {
63		return nil, fmt.Errorf("error scanning NFS file: %w", err)
64	}
65
66	return stats, nil
67}
68