cubefs
51 строка · 1.4 Кб
1// Copyright 2022 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 procfs
15
16import (
17"fmt"
18"strings"
19
20"github.com/prometheus/procfs/internal/util"
21)
22
23func sysctlToPath(sysctl string) string {
24return strings.Replace(sysctl, ".", "/", -1)
25}
26
27func (fs FS) SysctlStrings(sysctl string) ([]string, error) {
28value, err := util.SysReadFile(fs.proc.Path("sys", sysctlToPath(sysctl)))
29if err != nil {
30return nil, err
31}
32return strings.Fields(value), nil
33
34}
35
36func (fs FS) SysctlInts(sysctl string) ([]int, error) {
37fields, err := fs.SysctlStrings(sysctl)
38if err != nil {
39return nil, err
40}
41
42values := make([]int, len(fields))
43for i, f := range fields {
44vp := util.NewValueParser(f)
45values[i] = vp.Int()
46if err := vp.Err(); err != nil {
47return nil, fmt.Errorf("field %d in sysctl %s is not a valid int: %w", i, sysctl, err)
48}
49}
50return values, nil
51}
52