2015-09-26 15:36:40 +00: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 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
|
|
|
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 {
|
2015-10-11 15:00:48 +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,
|
2015-09-26 12:53:46 +00:00
|
|
|
Help: fmt.Sprintf("Memory information field %s.", k),
|
2014-06-26 17:20:36 +00:00
|
|
|
})
|
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) {
|
2015-09-26 12:53:46 +00:00
|
|
|
file, err := os.Open(procFilePath("meminfo"))
|
2014-06-04 11:12:34 +00:00
|
|
|
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 {
|
2015-10-11 15:00:48 +00:00
|
|
|
return nil, fmt.Errorf("invalid value in meminfo: %s", err)
|
2014-06-04 11:12:34 +00:00
|
|
|
}
|
|
|
|
switch len(parts) {
|
|
|
|
case 2: // no unit
|
|
|
|
case 3: // has unit, we presume kB
|
|
|
|
fv *= 1024
|
|
|
|
default:
|
2015-10-11 15:00:48 +00:00
|
|
|
return nil, fmt.Errorf("invalid line in meminfo: %s", line)
|
2014-06-04 11:12:34 +00: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-24 23:30:07 +00:00
|
|
|
return memInfo, nil
|
2014-06-04 11:12:34 +00:00
|
|
|
}
|