2015-05-12 11:06:41 +00:00
|
|
|
// +build !nomeminfo
|
2014-06-04 11:12:34 +00:00
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-05-28 19:21:44 +00:00
|
|
|
"github.com/prometheus/log"
|
2014-06-04 11:12:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-06-26 17:20:36 +00:00
|
|
|
procMemInfo = "/proc/meminfo"
|
|
|
|
memInfoSubsystem = "memory"
|
2014-06-04 11:12:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type meminfoCollector struct {
|
2014-11-25 02:00:17 +00:00
|
|
|
metrics map[string]prometheus.Gauge
|
2014-06-04 11:12:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Factories["meminfo"] = NewMeminfoCollector
|
|
|
|
}
|
|
|
|
|
2015-05-20 18:04:49 +00:00
|
|
|
// Takes a prometheus registry and returns a new Collector exposing
|
2014-06-04 11:12:34 +00:00
|
|
|
// memory stats.
|
2015-05-20 18:04:49 +00:00
|
|
|
func NewMeminfoCollector() (Collector, error) {
|
2014-11-25 02:00:17 +00:00
|
|
|
return &meminfoCollector{
|
|
|
|
metrics: map[string]prometheus.Gauge{},
|
|
|
|
}, nil
|
2014-06-04 11:12:34 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 14:16:43 +00:00
|
|
|
func (c *meminfoCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
2014-06-04 11:12:34 +00:00
|
|
|
memInfo, err := getMemInfo()
|
|
|
|
if err != nil {
|
2014-10-29 14:16:43 +00:00
|
|
|
return fmt.Errorf("Couldn't get meminfo: %s", err)
|
2014-06-04 11:12:34 +00:00
|
|
|
}
|
2015-05-28 19:21:44 +00:00
|
|
|
log.Debugf("Set node_mem: %#v", memInfo)
|
2014-06-04 11:12:34 +00:00
|
|
|
for k, v := range memInfo {
|
2014-11-25 02:00:17 +00:00
|
|
|
if _, ok := c.metrics[k]; !ok {
|
|
|
|
c.metrics[k] = prometheus.NewGauge(prometheus.GaugeOpts{
|
2014-06-26 17:20:36 +00:00
|
|
|
Namespace: Namespace,
|
|
|
|
Subsystem: memInfoSubsystem,
|
|
|
|
Name: k,
|
|
|
|
Help: k + " from /proc/meminfo.",
|
|
|
|
})
|
2014-06-04 11:12:34 +00:00
|
|
|
}
|
2014-11-25 02:00:17 +00:00
|
|
|
c.metrics[k].Set(v)
|
|
|
|
c.metrics[k].Collect(ch)
|
2014-06-04 11:12:34 +00:00
|
|
|
}
|
2014-10-29 14:16:43 +00:00
|
|
|
return err
|
2014-06-04 11:12:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getMemInfo() (map[string]float64, error) {
|
|
|
|
file, err := os.Open(procMemInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-24 23:30:07 +00:00
|
|
|
defer file.Close()
|
|
|
|
|
2014-06-04 11:12:34 +00:00
|
|
|
return parseMemInfo(file)
|
|
|
|
}
|
|
|
|
|
2014-11-24 23:30:07 +00:00
|
|
|
func parseMemInfo(r io.Reader) (map[string]float64, error) {
|
|
|
|
var (
|
|
|
|
memInfo = map[string]float64{}
|
|
|
|
scanner = bufio.NewScanner(r)
|
|
|
|
re = regexp.MustCompile("\\((.*)\\)")
|
|
|
|
)
|
|
|
|
|
2014-06-04 11:12:34 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
parts := strings.Fields(string(line))
|
|
|
|
fv, err := strconv.ParseFloat(parts[1], 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Invalid value in meminfo: %s", err)
|
|
|
|
}
|
|
|
|
switch len(parts) {
|
|
|
|
case 2: // no unit
|
|
|
|
case 3: // has unit, we presume kB
|
|
|
|
fv *= 1024
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Invalid line in %s: %s", procMemInfo, line)
|
|
|
|
}
|
|
|
|
key := parts[0][:len(parts[0])-1] // remove trailing : from key
|
|
|
|
// Active(anon) -> Active_anon
|
|
|
|
key = re.ReplaceAllString(key, "_${1}")
|
|
|
|
memInfo[key] = fv
|
|
|
|
}
|
|
|
|
|
2014-11-24 23:30:07 +00:00
|
|
|
return memInfo, nil
|
2014-06-04 11:12:34 +00:00
|
|
|
}
|