2014-02-07 16:09:39 +00:00
|
|
|
// +build !nonative
|
|
|
|
|
2014-02-18 11:35:11 +00:00
|
|
|
package collector
|
2013-05-07 14:40:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-02-17 16:35:23 +00:00
|
|
|
"os"
|
2013-05-07 14:40:10 +00:00
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2014-02-18 11:35:11 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2013-05-07 14:40:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-02-17 17:36:12 +00:00
|
|
|
procLoad = "/proc/loadavg"
|
|
|
|
procMemInfo = "/proc/meminfo"
|
|
|
|
procInterrupts = "/proc/interrupts"
|
2014-02-18 10:26:33 +00:00
|
|
|
procNetDev = "/proc/net/dev"
|
2014-02-18 10:54:12 +00:00
|
|
|
procDiskStats = "/proc/diskstats"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
diskStatsHeader = []string{
|
|
|
|
"reads_completed", "reads_merged",
|
|
|
|
"sectors_read", "read_time_ms",
|
|
|
|
"writes_completed", "writes_merged",
|
|
|
|
"sectors_written", "write_time_ms",
|
|
|
|
"io_now", "io_time_ms", "io_time_weighted",
|
|
|
|
}
|
2013-05-07 14:40:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type nativeCollector struct {
|
|
|
|
loadAvg prometheus.Gauge
|
|
|
|
attributes prometheus.Gauge
|
|
|
|
lastSeen prometheus.Gauge
|
2014-02-17 16:35:23 +00:00
|
|
|
memInfo prometheus.Gauge
|
2014-02-17 17:36:12 +00:00
|
|
|
interrupts prometheus.Counter
|
2014-02-18 10:26:33 +00:00
|
|
|
netStats prometheus.Counter
|
2014-02-18 10:54:12 +00:00
|
|
|
diskStats prometheus.Counter
|
2013-05-07 14:40:10 +00:00
|
|
|
name string
|
2014-02-18 11:35:11 +00:00
|
|
|
config Config
|
2013-05-07 14:40:10 +00:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:09:39 +00:00
|
|
|
func init() {
|
2014-02-18 11:35:11 +00:00
|
|
|
Factories = append(Factories, NewNativeCollector)
|
2014-02-07 16:09:39 +00:00
|
|
|
}
|
|
|
|
|
2013-05-07 14:40:10 +00:00
|
|
|
// Takes a config struct and prometheus registry and returns a new Collector exposing
|
|
|
|
// load, seconds since last login and a list of tags as specified by config.
|
2014-02-18 11:35:11 +00:00
|
|
|
func NewNativeCollector(config Config, registry prometheus.Registry) (Collector, error) {
|
2014-02-07 16:09:39 +00:00
|
|
|
c := nativeCollector{
|
2013-05-07 14:40:10 +00:00
|
|
|
name: "native_collector",
|
|
|
|
config: config,
|
|
|
|
loadAvg: prometheus.NewGauge(),
|
|
|
|
attributes: prometheus.NewGauge(),
|
|
|
|
lastSeen: prometheus.NewGauge(),
|
2014-02-17 16:35:23 +00:00
|
|
|
memInfo: prometheus.NewGauge(),
|
2014-02-17 17:36:12 +00:00
|
|
|
interrupts: prometheus.NewCounter(),
|
2014-02-18 10:26:33 +00:00
|
|
|
netStats: prometheus.NewCounter(),
|
2014-02-18 10:54:12 +00:00
|
|
|
diskStats: prometheus.NewCounter(),
|
2013-05-07 14:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
registry.Register(
|
|
|
|
"node_load",
|
|
|
|
"node_exporter: system load.",
|
2013-07-24 13:53:30 +00:00
|
|
|
prometheus.NilLabels,
|
2014-02-07 16:09:39 +00:00
|
|
|
c.loadAvg,
|
2013-05-07 14:40:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
registry.Register(
|
|
|
|
"node_last_login_seconds",
|
|
|
|
"node_exporter: seconds since last login.",
|
2013-07-24 13:53:30 +00:00
|
|
|
prometheus.NilLabels,
|
2014-02-07 16:09:39 +00:00
|
|
|
c.lastSeen,
|
2013-05-07 14:40:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
registry.Register(
|
|
|
|
"node_attributes",
|
|
|
|
"node_exporter: system attributes.",
|
2013-07-24 13:53:30 +00:00
|
|
|
prometheus.NilLabels,
|
2014-02-07 16:09:39 +00:00
|
|
|
c.attributes,
|
2013-05-07 14:40:10 +00:00
|
|
|
)
|
|
|
|
|
2014-02-17 16:35:23 +00:00
|
|
|
registry.Register(
|
|
|
|
"node_mem",
|
|
|
|
"node_exporter: memory details.",
|
|
|
|
prometheus.NilLabels,
|
|
|
|
c.memInfo,
|
|
|
|
)
|
|
|
|
|
2014-02-17 17:36:12 +00:00
|
|
|
registry.Register(
|
|
|
|
"node_interrupts",
|
|
|
|
"node_exporter: interrupt details.",
|
|
|
|
prometheus.NilLabels,
|
|
|
|
c.interrupts,
|
|
|
|
)
|
|
|
|
|
2014-02-18 10:26:33 +00:00
|
|
|
registry.Register(
|
|
|
|
"node_net",
|
|
|
|
"node_exporter: network stats.",
|
|
|
|
prometheus.NilLabels,
|
|
|
|
c.netStats,
|
|
|
|
)
|
|
|
|
|
2014-02-18 10:54:12 +00:00
|
|
|
registry.Register(
|
|
|
|
"node_disk",
|
|
|
|
"node_exporter: disk stats.",
|
|
|
|
prometheus.NilLabels,
|
|
|
|
c.diskStats,
|
|
|
|
)
|
2014-02-07 16:09:39 +00:00
|
|
|
return &c, nil
|
2013-05-07 14:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *nativeCollector) Name() string { return c.name }
|
|
|
|
|
|
|
|
func (c *nativeCollector) Update() (updates int, err error) {
|
|
|
|
last, err := getSecondsSinceLastLogin()
|
|
|
|
if err != nil {
|
|
|
|
return updates, fmt.Errorf("Couldn't get last seen: %s", err)
|
|
|
|
}
|
2014-02-17 16:35:23 +00:00
|
|
|
updates++
|
|
|
|
debug(c.Name(), "Set node_last_login_seconds: %f", last)
|
|
|
|
c.lastSeen.Set(nil, last)
|
2013-05-07 14:40:10 +00:00
|
|
|
|
|
|
|
load, err := getLoad()
|
|
|
|
if err != nil {
|
|
|
|
return updates, fmt.Errorf("Couldn't get load: %s", err)
|
|
|
|
}
|
2014-02-17 16:35:23 +00:00
|
|
|
updates++
|
|
|
|
debug(c.Name(), "Set node_load: %f", load)
|
|
|
|
c.loadAvg.Set(nil, load)
|
|
|
|
|
2013-05-07 14:40:10 +00:00
|
|
|
debug(c.Name(), "Set node_attributes{%v}: 1", c.config.Attributes)
|
|
|
|
c.attributes.Set(c.config.Attributes, 1)
|
2014-02-17 16:35:23 +00:00
|
|
|
|
|
|
|
memInfo, err := getMemInfo()
|
|
|
|
if err != nil {
|
|
|
|
return updates, fmt.Errorf("Couldn't get meminfo: %s", err)
|
|
|
|
}
|
|
|
|
debug(c.Name(), "Set node_mem: %#v", memInfo)
|
|
|
|
for k, v := range memInfo {
|
|
|
|
updates++
|
|
|
|
fv, err := strconv.ParseFloat(v, 64)
|
|
|
|
if err != nil {
|
|
|
|
return updates, fmt.Errorf("Invalid value in meminfo: %s", err)
|
|
|
|
}
|
|
|
|
c.memInfo.Set(map[string]string{"type": k}, fv)
|
|
|
|
}
|
2014-02-17 17:36:12 +00:00
|
|
|
|
|
|
|
interrupts, err := getInterrupts()
|
|
|
|
if err != nil {
|
|
|
|
return updates, fmt.Errorf("Couldn't get interrupts: %s", err)
|
|
|
|
}
|
|
|
|
for name, interrupt := range interrupts {
|
|
|
|
for cpuNo, value := range interrupt.values {
|
|
|
|
updates++
|
|
|
|
fv, err := strconv.ParseFloat(value, 64)
|
|
|
|
if err != nil {
|
2014-02-18 11:35:11 +00:00
|
|
|
return updates, fmt.Errorf("Invalid value %s in interrupts: %s", value, err)
|
2014-02-17 17:36:12 +00:00
|
|
|
}
|
|
|
|
labels := map[string]string{
|
|
|
|
"CPU": strconv.Itoa(cpuNo),
|
|
|
|
"type": name,
|
|
|
|
"info": interrupt.info,
|
|
|
|
"devices": interrupt.devices,
|
|
|
|
}
|
|
|
|
c.interrupts.Set(labels, fv)
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 10:26:33 +00:00
|
|
|
|
|
|
|
netStats, err := getNetStats()
|
|
|
|
if err != nil {
|
|
|
|
return updates, fmt.Errorf("Couldn't get netstats: %s", err)
|
|
|
|
}
|
|
|
|
for direction, devStats := range netStats {
|
|
|
|
for dev, stats := range devStats {
|
|
|
|
for t, value := range stats {
|
|
|
|
updates++
|
|
|
|
v, err := strconv.ParseFloat(value, 64)
|
|
|
|
if err != nil {
|
|
|
|
return updates, fmt.Errorf("Invalid value %s in interrupts: %s", value, err)
|
|
|
|
}
|
|
|
|
labels := map[string]string{
|
|
|
|
"device": dev,
|
|
|
|
"direction": direction,
|
|
|
|
"type": t,
|
|
|
|
}
|
|
|
|
c.netStats.Set(labels, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 10:54:12 +00:00
|
|
|
|
|
|
|
diskStats, err := getDiskStats()
|
|
|
|
if err != nil {
|
|
|
|
return updates, fmt.Errorf("Couldn't get diskstats: %s", err)
|
|
|
|
}
|
|
|
|
for dev, stats := range diskStats {
|
|
|
|
for k, value := range stats {
|
|
|
|
updates++
|
|
|
|
v, err := strconv.ParseFloat(value, 64)
|
|
|
|
if err != nil {
|
|
|
|
return updates, fmt.Errorf("Invalid value %s in diskstats: %s", value, err)
|
|
|
|
}
|
|
|
|
labels := map[string]string{"device": dev, "type": k}
|
|
|
|
c.diskStats.Set(labels, v)
|
|
|
|
}
|
|
|
|
}
|
2013-05-07 14:40:10 +00:00
|
|
|
return updates, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLoad() (float64, error) {
|
|
|
|
data, err := ioutil.ReadFile(procLoad)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2014-02-18 12:17:40 +00:00
|
|
|
return parseLoad(string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseLoad(data string) (float64, error) {
|
|
|
|
parts := strings.Fields(data)
|
2013-05-07 14:40:10 +00:00
|
|
|
load, err := strconv.ParseFloat(parts[0], 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("Could not parse load '%s': %s", parts[0], err)
|
|
|
|
}
|
|
|
|
return load, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSecondsSinceLastLogin() (float64, error) {
|
|
|
|
who := exec.Command("who", "/var/log/wtmp", "-l", "-u", "-s")
|
|
|
|
|
|
|
|
output, err := who.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = who.Start()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reader := bufio.NewReader(output)
|
|
|
|
|
|
|
|
var last time.Time
|
|
|
|
for {
|
|
|
|
line, isPrefix, err := reader.ReadLine()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if isPrefix {
|
|
|
|
return 0, fmt.Errorf("line to long: %s(...)", line)
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := strings.Fields(string(line))
|
|
|
|
lastDate := fields[2]
|
|
|
|
lastTime := fields[3]
|
|
|
|
|
|
|
|
dateParts, err := splitToInts(lastDate, "-") // 2013-04-16
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("Couldn't parse date in line '%s': %s", fields, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
timeParts, err := splitToInts(lastTime, ":") // 11:33
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("Couldn't parse time in line '%s': %s", fields, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
last_t := time.Date(dateParts[0], time.Month(dateParts[1]), dateParts[2], timeParts[0], timeParts[1], 0, 0, time.UTC)
|
|
|
|
last = last_t
|
|
|
|
}
|
|
|
|
err = who.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return float64(time.Now().Sub(last).Seconds()), nil
|
|
|
|
}
|
2014-02-17 16:35:23 +00:00
|
|
|
|
|
|
|
func getMemInfo() (map[string]string, error) {
|
2014-02-18 12:17:40 +00:00
|
|
|
file, err := os.Open(procMemInfo)
|
2014-02-17 16:35:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-02-18 12:17:40 +00:00
|
|
|
return parseMemInfo(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseMemInfo(r io.ReadCloser) (map[string]string, error) {
|
|
|
|
defer r.Close()
|
|
|
|
memInfo := map[string]string{}
|
|
|
|
scanner := bufio.NewScanner(r)
|
2014-02-17 16:35:23 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
parts := strings.Fields(string(line))
|
|
|
|
key := ""
|
|
|
|
switch len(parts) {
|
|
|
|
case 2: // no unit
|
|
|
|
key = parts[0][:len(parts[0])-1] // remove trailing : from key
|
|
|
|
case 3: // has unit
|
|
|
|
key = fmt.Sprintf("%s_%s", parts[0][:len(parts[0])-1], parts[2])
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Invalid line in %s: %s", procMemInfo, line)
|
|
|
|
}
|
|
|
|
memInfo[key] = parts[1]
|
|
|
|
}
|
|
|
|
return memInfo, nil
|
|
|
|
|
|
|
|
}
|
2014-02-17 17:36:12 +00:00
|
|
|
|
|
|
|
type interrupt struct {
|
|
|
|
info string
|
|
|
|
devices string
|
|
|
|
values []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func getInterrupts() (map[string]interrupt, error) {
|
2014-02-18 12:17:40 +00:00
|
|
|
file, err := os.Open(procInterrupts)
|
2014-02-17 17:36:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-02-18 12:17:40 +00:00
|
|
|
return parseInterrupts(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseInterrupts(r io.ReadCloser) (map[string]interrupt, error) {
|
|
|
|
defer r.Close()
|
|
|
|
interrupts := map[string]interrupt{}
|
|
|
|
scanner := bufio.NewScanner(r)
|
2014-02-17 17:36:12 +00:00
|
|
|
if !scanner.Scan() {
|
|
|
|
return nil, fmt.Errorf("%s empty", procInterrupts)
|
|
|
|
}
|
|
|
|
cpuNum := len(strings.Fields(string(scanner.Text()))) // one header per cpu
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
parts := strings.Fields(string(line))
|
|
|
|
if len(parts) < cpuNum+2 { // irq + one column per cpu + details,
|
|
|
|
continue // we ignore ERR and MIS for now
|
|
|
|
}
|
|
|
|
intName := parts[0][:len(parts[0])-1] // remove trailing :
|
|
|
|
intr := interrupt{
|
|
|
|
values: parts[1:cpuNum],
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := strconv.Atoi(intName); err == nil { // numeral interrupt
|
|
|
|
intr.info = parts[cpuNum+1]
|
|
|
|
intr.devices = strings.Join(parts[cpuNum+2:], " ")
|
|
|
|
} else {
|
|
|
|
intr.info = strings.Join(parts[cpuNum+1:], " ")
|
|
|
|
}
|
|
|
|
interrupts[intName] = intr
|
|
|
|
}
|
|
|
|
return interrupts, nil
|
|
|
|
}
|
2014-02-18 10:26:33 +00:00
|
|
|
|
|
|
|
func getNetStats() (map[string]map[string]map[string]string, error) {
|
2014-02-18 12:17:40 +00:00
|
|
|
file, err := os.Open(procNetDev)
|
2014-02-18 10:26:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-02-18 12:17:40 +00:00
|
|
|
return parseNetStats(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseNetStats(r io.ReadCloser) (map[string]map[string]map[string]string, error) {
|
|
|
|
defer r.Close()
|
|
|
|
netStats := map[string]map[string]map[string]string{}
|
|
|
|
netStats["transmit"] = map[string]map[string]string{}
|
|
|
|
netStats["receive"] = map[string]map[string]string{}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(r)
|
2014-02-18 10:26:33 +00:00
|
|
|
scanner.Scan() // skip first header
|
|
|
|
scanner.Scan()
|
|
|
|
parts := strings.Split(string(scanner.Text()), "|")
|
|
|
|
if len(parts) != 3 { // interface + receive + transmit
|
|
|
|
return nil, fmt.Errorf("Invalid header line in %s: %s",
|
|
|
|
procNetDev, scanner.Text())
|
|
|
|
}
|
|
|
|
header := strings.Fields(parts[1])
|
|
|
|
for scanner.Scan() {
|
|
|
|
parts := strings.Fields(string(scanner.Text()))
|
|
|
|
if len(parts) != 2*len(header)+1 {
|
|
|
|
return nil, fmt.Errorf("Invalid line in %s: %s",
|
|
|
|
procNetDev, scanner.Text())
|
|
|
|
}
|
|
|
|
|
|
|
|
dev := parts[0][:len(parts[0])-1]
|
|
|
|
receive, err := parseNetDevLine(parts[1:len(header)+1], header)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
transmit, err := parseNetDevLine(parts[len(header)+1:], header)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
netStats["transmit"][dev] = transmit
|
|
|
|
netStats["receive"][dev] = receive
|
|
|
|
}
|
|
|
|
return netStats, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseNetDevLine(parts []string, header []string) (map[string]string, error) {
|
|
|
|
devStats := map[string]string{}
|
|
|
|
for i, v := range parts {
|
|
|
|
devStats[header[i]] = v
|
|
|
|
}
|
|
|
|
return devStats, nil
|
|
|
|
}
|
2014-02-18 10:54:12 +00:00
|
|
|
|
|
|
|
func getDiskStats() (map[string]map[string]string, error) {
|
2014-02-18 12:17:40 +00:00
|
|
|
file, err := os.Open(procDiskStats)
|
2014-02-18 10:54:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-02-18 12:17:40 +00:00
|
|
|
return parseDiskStats(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseDiskStats(r io.ReadCloser) (map[string]map[string]string, error) {
|
|
|
|
defer r.Close()
|
|
|
|
diskStats := map[string]map[string]string{}
|
|
|
|
scanner := bufio.NewScanner(r)
|
2014-02-18 10:54:12 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
parts := strings.Fields(string(scanner.Text()))
|
|
|
|
if len(parts) != len(diskStatsHeader)+3 { // we strip major, minor and dev
|
|
|
|
return nil, fmt.Errorf("Invalid line in %s: %s", procDiskStats, scanner.Text())
|
|
|
|
}
|
|
|
|
dev := parts[2]
|
|
|
|
diskStats[dev] = map[string]string{}
|
|
|
|
for i, v := range parts[3:] {
|
|
|
|
diskStats[dev][diskStatsHeader[i]] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return diskStats, nil
|
|
|
|
}
|