2015-09-26 17:36:40 +02:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-05-12 13:06:41 +02:00
|
|
|
// +build !nomeminfo
|
2014-06-04 13:12:34 +02:00
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-05-28 21:21:44 +02:00
|
|
|
"github.com/prometheus/log"
|
2014-06-04 13:12:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-06-26 19:20:36 +02:00
|
|
|
memInfoSubsystem = "memory"
|
2014-06-04 13:12:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type meminfoCollector struct {
|
2014-11-25 03:00:17 +01:00
|
|
|
metrics map[string]prometheus.Gauge
|
2014-06-04 13:12:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Factories["meminfo"] = NewMeminfoCollector
|
|
|
|
}
|
|
|
|
|
2015-05-20 20:04:49 +02:00
|
|
|
// Takes a prometheus registry and returns a new Collector exposing
|
2014-06-04 13:12:34 +02:00
|
|
|
// memory stats.
|
2015-05-20 20:04:49 +02:00
|
|
|
func NewMeminfoCollector() (Collector, error) {
|
2014-11-25 03:00:17 +01:00
|
|
|
return &meminfoCollector{
|
|
|
|
metrics: map[string]prometheus.Gauge{},
|
|
|
|
}, nil
|
2014-06-04 13:12:34 +02:00
|
|
|
}
|
|
|
|
|
2014-10-29 15:16:43 +01:00
|
|
|
func (c *meminfoCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
2014-06-04 13:12:34 +02:00
|
|
|
memInfo, err := getMemInfo()
|
|
|
|
if err != nil {
|
2014-10-29 15:16:43 +01:00
|
|
|
return fmt.Errorf("Couldn't get meminfo: %s", err)
|
2014-06-04 13:12:34 +02:00
|
|
|
}
|
2015-05-28 21:21:44 +02:00
|
|
|
log.Debugf("Set node_mem: %#v", memInfo)
|
2014-06-04 13:12:34 +02:00
|
|
|
for k, v := range memInfo {
|
2014-11-25 03:00:17 +01:00
|
|
|
if _, ok := c.metrics[k]; !ok {
|
|
|
|
c.metrics[k] = prometheus.NewGauge(prometheus.GaugeOpts{
|
2014-06-26 19:20:36 +02:00
|
|
|
Namespace: Namespace,
|
|
|
|
Subsystem: memInfoSubsystem,
|
|
|
|
Name: k,
|
2015-09-26 14:53:46 +02:00
|
|
|
Help: fmt.Sprintf("Memory information field %s.", k),
|
2014-06-26 19:20:36 +02:00
|
|
|
})
|
2014-06-04 13:12:34 +02:00
|
|
|
}
|
2014-11-25 03:00:17 +01:00
|
|
|
c.metrics[k].Set(v)
|
|
|
|
c.metrics[k].Collect(ch)
|
2014-06-04 13:12:34 +02:00
|
|
|
}
|
2014-10-29 15:16:43 +01:00
|
|
|
return err
|
2014-06-04 13:12:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getMemInfo() (map[string]float64, error) {
|
2015-09-26 14:53:46 +02:00
|
|
|
file, err := os.Open(procFilePath("meminfo"))
|
2014-06-04 13:12:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-25 00:30:07 +01:00
|
|
|
defer file.Close()
|
|
|
|
|
2014-06-04 13:12:34 +02:00
|
|
|
return parseMemInfo(file)
|
|
|
|
}
|
|
|
|
|
2014-11-25 00:30:07 +01: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 13:12:34 +02: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:
|
2015-09-26 14:53:46 +02:00
|
|
|
return nil, fmt.Errorf("Invalid line in meminfo: %s", line)
|
2014-06-04 13:12:34 +02:00
|
|
|
}
|
|
|
|
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-25 00:30:07 +01:00
|
|
|
return memInfo, nil
|
2014-06-04 13:12:34 +02:00
|
|
|
}
|