Moving buddyinfo logic to procfs
parent
2f16f6b84e
commit
55417d7688
|
@ -1,4 +1,4 @@
|
||||||
// Copyright 2015 The Prometheus Authors
|
// Copyright 2017 The Prometheus Authors
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
// You may obtain a copy of the License at
|
// You may obtain a copy of the License at
|
||||||
|
@ -17,19 +17,14 @@
|
||||||
package collector
|
package collector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/common/log"
|
"github.com/prometheus/common/log"
|
||||||
|
"github.com/prometheus/procfs"
|
||||||
)
|
)
|
||||||
|
|
||||||
type buddyInfo map[string]map[string][]float64
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
buddyInfoSubsystem = "buddyinfo"
|
buddyInfoSubsystem = "buddyinfo"
|
||||||
)
|
)
|
||||||
|
@ -55,10 +50,16 @@ func NewBuddyinfoCollector() (Collector, error) {
|
||||||
// Update calls (*buddyinfoCollector).getBuddyInfo to get the platform specific
|
// Update calls (*buddyinfoCollector).getBuddyInfo to get the platform specific
|
||||||
// buddyinfo metrics.
|
// buddyinfo metrics.
|
||||||
func (c *buddyinfoCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
func (c *buddyinfoCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
||||||
buddyInfo, err := c.getBuddyInfo()
|
fs, err := procfs.NewFS(*procPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to open procfs: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
buddyInfo, err := fs.NewBuddyInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("couldn't get buddyinfo: %s", err)
|
return fmt.Errorf("couldn't get buddyinfo: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("Set node_buddy: %#v", buddyInfo)
|
log.Debugf("Set node_buddy: %#v", buddyInfo)
|
||||||
for node, zones := range buddyInfo {
|
for node, zones := range buddyInfo {
|
||||||
for zone, values := range zones {
|
for zone, values := range zones {
|
||||||
|
@ -73,42 +74,3 @@ func (c *buddyinfoCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (c *buddyinfoCollector) getBuddyInfo() (buddyInfo, error) {
|
|
||||||
file, err := os.Open(procFilePath("buddyinfo"))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
return parseBuddyInfo(file)
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseBuddyInfo(r io.Reader) (buddyInfo, error) {
|
|
||||||
var (
|
|
||||||
buddyInfo = buddyInfo{}
|
|
||||||
scanner = bufio.NewScanner(r)
|
|
||||||
)
|
|
||||||
|
|
||||||
for scanner.Scan() {
|
|
||||||
var err error
|
|
||||||
line := scanner.Text()
|
|
||||||
parts := strings.Fields(string(line))
|
|
||||||
node := strings.TrimRight(parts[1], ",")
|
|
||||||
zone := strings.TrimRight(parts[3], ",")
|
|
||||||
arraySize := len(parts[4:])
|
|
||||||
sizes := make([]float64, arraySize)
|
|
||||||
for i := 0; i < arraySize; i++ {
|
|
||||||
sizes[i], err = strconv.ParseFloat(parts[i+4], 64)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("invalid value in buddyinfo: %s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := buddyInfo[node]; !ok {
|
|
||||||
buddyInfo[node] = make(map[string][]float64)
|
|
||||||
}
|
|
||||||
buddyInfo[node][zone] = sizes
|
|
||||||
}
|
|
||||||
|
|
||||||
return buddyInfo, nil
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue