| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 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 | |
| 14 | package procfs |
| 15 | |
| 16 | import ( |
| 17 | "fmt" |
| 18 | "strings" |
| 19 | |
| 20 | "github.com/prometheus/procfs/internal/util" |
| 21 | ) |
| 22 | |
| 23 | func sysctlToPath(sysctl string) string { |
| 24 | return strings.ReplaceAll(sysctl, ".", "/") |
| 25 | } |
| 26 | |
| 27 | func (fs FS) SysctlStrings(sysctl string) ([]string, error) { |
| 28 | value, err := util.SysReadFile(fs.proc.Path("sys", sysctlToPath(sysctl))) |
| 29 | if err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | return strings.Fields(value), nil |
| 33 | |
| 34 | } |
| 35 | |
| 36 | func (fs FS) SysctlInts(sysctl string) ([]int, error) { |
| 37 | fields, err := fs.SysctlStrings(sysctl) |
| 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | |
| 42 | values := make([]int, len(fields)) |
| 43 | for i, f := range fields { |
| 44 | vp := util.NewValueParser(f) |
| 45 | values[i] = vp.Int() |
| 46 | if err := vp.Err(); err != nil { |
| 47 | return nil, fmt.Errorf("%w: field %d in sysctl %s is not a valid int: %w", ErrFileParse, i, sysctl, err) |
| 48 | } |
| 49 | } |
| 50 | return values, nil |
| 51 | } |