25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

63 satır
1.6 KiB

  1. // Copyright 2019 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. "strconv"
  17. "strings"
  18. "github.com/prometheus/procfs/internal/util"
  19. )
  20. // LoadAvg represents an entry in /proc/loadavg.
  21. type LoadAvg struct {
  22. Load1 float64
  23. Load5 float64
  24. Load15 float64
  25. }
  26. // LoadAvg returns loadavg from /proc.
  27. func (fs FS) LoadAvg() (*LoadAvg, error) {
  28. path := fs.proc.Path("loadavg")
  29. data, err := util.ReadFileNoStat(path)
  30. if err != nil {
  31. return nil, err
  32. }
  33. return parseLoad(data)
  34. }
  35. // Parse /proc loadavg and return 1m, 5m and 15m.
  36. func parseLoad(loadavgBytes []byte) (*LoadAvg, error) {
  37. loads := make([]float64, 3)
  38. parts := strings.Fields(string(loadavgBytes))
  39. if len(parts) < 3 {
  40. return nil, fmt.Errorf("malformed loadavg line: too few fields in loadavg string: %q", string(loadavgBytes))
  41. }
  42. var err error
  43. for i, load := range parts[0:3] {
  44. loads[i], err = strconv.ParseFloat(load, 64)
  45. if err != nil {
  46. return nil, fmt.Errorf("could not parse load %q: %w", load, err)
  47. }
  48. }
  49. return &LoadAvg{
  50. Load1: loads[0],
  51. Load5: loads[1],
  52. Load15: loads[2],
  53. }, nil
  54. }