proc_sys.go 1.4 KB

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