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-09-07 13:49:30 +00:00
|
|
|
// +build !nomdadm
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-10-30 20:20:06 +00:00
|
|
|
"github.com/prometheus/common/log"
|
2015-09-07 13:49:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-11-17 15:23:01 +00:00
|
|
|
statuslineRE = regexp.MustCompile(`(\d+) blocks .*\[(\d+)/(\d+)\] \[[U_]+\]`)
|
|
|
|
raid0lineRE = regexp.MustCompile(`(\d+) blocks( super ([0-9\.])*)? \d+k chunks`)
|
|
|
|
buildlineRE = regexp.MustCompile(`\((\d+)/\d+\)`)
|
|
|
|
unknownPersonalityLineRE = regexp.MustCompile(`(\d+) blocks (.*)`)
|
|
|
|
raidPersonalityRE = regexp.MustCompile(`raid[0-9]+`)
|
2015-09-07 13:49:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type mdStatus struct {
|
2017-03-18 17:36:26 +00:00
|
|
|
name string
|
|
|
|
active bool
|
2015-09-07 13:49:30 +00:00
|
|
|
disksActive int64
|
|
|
|
disksTotal int64
|
|
|
|
blocksTotal int64
|
|
|
|
blocksSynced int64
|
|
|
|
}
|
|
|
|
|
|
|
|
type mdadmCollector struct{}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Factories["mdadm"] = NewMdadmCollector
|
|
|
|
}
|
|
|
|
|
|
|
|
func evalStatusline(statusline string) (active, total, size int64, err error) {
|
|
|
|
matches := statuslineRE.FindStringSubmatch(statusline)
|
|
|
|
|
|
|
|
// +1 to make it more obvious that the whole string containing the info is also returned as matches[0].
|
|
|
|
if len(matches) < 3+1 {
|
|
|
|
return 0, 0, 0, fmt.Errorf("too few matches found in statusline: %s", statusline)
|
2017-02-28 16:44:53 +00:00
|
|
|
} else if len(matches) > 3+1 {
|
|
|
|
return 0, 0, 0, fmt.Errorf("too many matches found in statusline: %s", statusline)
|
2015-09-07 13:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size, err = strconv.ParseInt(matches[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, 0, fmt.Errorf("%s in statusline: %s", err, statusline)
|
|
|
|
}
|
|
|
|
|
|
|
|
total, err = strconv.ParseInt(matches[2], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, 0, fmt.Errorf("%s in statusline: %s", err, statusline)
|
|
|
|
}
|
|
|
|
active, err = strconv.ParseInt(matches[3], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, 0, fmt.Errorf("%s in statusline: %s", err, statusline)
|
|
|
|
}
|
|
|
|
|
|
|
|
return active, total, size, nil
|
|
|
|
}
|
|
|
|
|
2016-06-06 21:18:09 +00:00
|
|
|
func evalRaid0line(statusline string) (size int64, err error) {
|
|
|
|
matches := raid0lineRE.FindStringSubmatch(statusline)
|
|
|
|
|
2016-11-15 16:36:56 +00:00
|
|
|
if len(matches) < 2 {
|
2016-06-06 21:18:09 +00:00
|
|
|
return 0, fmt.Errorf("invalid raid0 status line: %s", statusline)
|
|
|
|
}
|
|
|
|
|
|
|
|
size, err = strconv.ParseInt(matches[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("%s in statusline: %s", err, statusline)
|
|
|
|
}
|
|
|
|
|
|
|
|
return size, nil
|
|
|
|
}
|
|
|
|
|
2016-11-17 15:23:01 +00:00
|
|
|
func evalUnknownPersonalitylineRE(statusline string) (size int64, err error) {
|
|
|
|
matches := unknownPersonalityLineRE.FindStringSubmatch(statusline)
|
2016-09-20 14:22:34 +00:00
|
|
|
|
|
|
|
if len(matches) != 2+1 {
|
|
|
|
return 0, fmt.Errorf("invalid unknown personality status line: %s", statusline)
|
|
|
|
}
|
|
|
|
|
|
|
|
size, err = strconv.ParseInt(matches[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("%s in statusline: %s", err, statusline)
|
|
|
|
}
|
|
|
|
|
|
|
|
return size, nil
|
|
|
|
}
|
|
|
|
|
2017-02-28 16:44:53 +00:00
|
|
|
// evalBuildline gets the size that has already been synced out of the sync-line.
|
2015-09-07 13:49:30 +00:00
|
|
|
func evalBuildline(buildline string) (int64, error) {
|
|
|
|
matches := buildlineRE.FindStringSubmatch(buildline)
|
|
|
|
|
|
|
|
// +1 to make it more obvious that the whole string containing the info is also returned as matches[0].
|
|
|
|
if len(matches) < 1+1 {
|
|
|
|
return 0, fmt.Errorf("too few matches found in buildline: %s", buildline)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) > 1+1 {
|
|
|
|
return 0, fmt.Errorf("too many matches found in buildline: %s", buildline)
|
|
|
|
}
|
|
|
|
|
|
|
|
syncedSize, err := strconv.ParseInt(matches[1], 10, 64)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("%s in buildline: %s", err, buildline)
|
|
|
|
}
|
|
|
|
|
|
|
|
return syncedSize, nil
|
|
|
|
}
|
|
|
|
|
2017-02-28 16:44:53 +00:00
|
|
|
// parseMdstat parses an mdstat-file and returns a struct with the relevant infos.
|
2015-09-07 13:49:30 +00:00
|
|
|
func parseMdstat(mdStatusFilePath string) ([]mdStatus, error) {
|
|
|
|
content, err := ioutil.ReadFile(mdStatusFilePath)
|
|
|
|
if err != nil {
|
2017-03-22 13:11:19 +00:00
|
|
|
return []mdStatus{}, err
|
2015-09-07 13:49:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 17:36:26 +00:00
|
|
|
lines := strings.Split(string(content), "\n")
|
2015-09-07 13:49:30 +00:00
|
|
|
// Each md has at least the deviceline, statusline and one empty line afterwards
|
|
|
|
// so we will have probably something of the order len(lines)/3 devices
|
|
|
|
// so we use that for preallocation.
|
2017-03-18 17:36:26 +00:00
|
|
|
mdStates := make([]mdStatus, 0, len(lines)/3)
|
|
|
|
for i, line := range lines {
|
|
|
|
if line == "" {
|
2015-09-07 13:49:30 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-03-18 17:36:26 +00:00
|
|
|
if line[0] == ' ' || line[0] == '\t' {
|
|
|
|
// Lines starting with white space are not the beginning of a md-section.
|
2015-09-07 13:49:30 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-03-18 17:36:26 +00:00
|
|
|
if strings.HasPrefix(line, "Personalities") || strings.HasPrefix(line, "unused") {
|
|
|
|
// These lines contain general information.
|
2015-09-07 13:49:30 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-03-18 17:36:26 +00:00
|
|
|
mainLine := strings.Split(line, " ")
|
2016-06-06 21:18:09 +00:00
|
|
|
if len(mainLine) < 4 {
|
2017-03-18 17:36:26 +00:00
|
|
|
return mdStates, fmt.Errorf("error parsing mdline: %s", line)
|
|
|
|
}
|
|
|
|
md := mdStatus{
|
|
|
|
name: mainLine[0],
|
|
|
|
active: mainLine[2] == "active",
|
2015-09-07 13:49:30 +00:00
|
|
|
}
|
2017-03-18 17:36:26 +00:00
|
|
|
|
|
|
|
if len(lines) <= i+3 {
|
|
|
|
return mdStates, fmt.Errorf("error parsing mdstat: entry for %s has fewer lines than expected", md.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
personality := ""
|
2016-11-17 15:23:01 +00:00
|
|
|
for _, possiblePersonality := range mainLine[3:] {
|
2016-09-20 14:22:34 +00:00
|
|
|
if raidPersonalityRE.MatchString(possiblePersonality) {
|
|
|
|
personality = possiblePersonality
|
2016-11-17 15:23:01 +00:00
|
|
|
break
|
2016-09-20 14:22:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case personality == "raid0":
|
2017-03-18 17:36:26 +00:00
|
|
|
md.disksActive = int64(len(mainLine) - 4) // Get the number of devices from the main line.
|
|
|
|
md.disksTotal = md.disksActive // Raid0 active and total is always the same if active.
|
|
|
|
md.blocksTotal, err = evalRaid0line(lines[i+1])
|
2016-09-20 14:22:34 +00:00
|
|
|
case raidPersonalityRE.MatchString(personality):
|
2017-03-18 17:36:26 +00:00
|
|
|
md.disksActive, md.disksTotal, md.blocksTotal, err = evalStatusline(lines[i+1])
|
2016-09-20 14:22:34 +00:00
|
|
|
default:
|
|
|
|
log.Infof("Personality unknown: %s\n", mainLine)
|
2017-03-18 17:36:26 +00:00
|
|
|
md.blocksTotal, err = evalUnknownPersonalitylineRE(lines[i+1])
|
2016-06-06 21:18:09 +00:00
|
|
|
}
|
2015-09-07 13:49:30 +00:00
|
|
|
if err != nil {
|
2015-09-26 18:07:20 +00:00
|
|
|
return mdStates, fmt.Errorf("error parsing mdstat: %s", err)
|
2015-09-07 13:49:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 17:36:26 +00:00
|
|
|
syncLine := lines[i+2]
|
|
|
|
if strings.Contains(syncLine, "bitmap") {
|
|
|
|
syncLine = lines[i+3]
|
2015-09-07 13:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If device is syncing at the moment, get the number of currently synced bytes,
|
|
|
|
// otherwise that number equals the size of the device.
|
2017-03-18 17:36:26 +00:00
|
|
|
if strings.Contains(syncLine, "recovery") ||
|
|
|
|
strings.Contains(syncLine, "resync") &&
|
|
|
|
!strings.Contains(syncLine, "\tresync=") {
|
|
|
|
md.blocksSynced, err = evalBuildline(syncLine)
|
2015-09-07 13:49:30 +00:00
|
|
|
if err != nil {
|
2015-09-26 18:07:20 +00:00
|
|
|
return mdStates, fmt.Errorf("error parsing mdstat: %s", err)
|
2015-09-07 13:49:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-18 17:36:26 +00:00
|
|
|
md.blocksSynced = md.blocksTotal
|
2015-09-07 13:49:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 17:36:26 +00:00
|
|
|
mdStates = append(mdStates, md)
|
2015-09-07 13:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return mdStates, nil
|
|
|
|
}
|
|
|
|
|
2017-02-28 16:44:53 +00:00
|
|
|
// NewMdadmCollector returns a new Collector exposing raid statistics.
|
2015-09-07 13:49:30 +00:00
|
|
|
func NewMdadmCollector() (Collector, error) {
|
|
|
|
return &mdadmCollector{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
isActiveDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, "md", "is_active"),
|
|
|
|
"Indicator whether the md-device is active or not.",
|
|
|
|
[]string{"device"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
disksActiveDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, "md", "disks_active"),
|
|
|
|
"Number of active disks of device.",
|
|
|
|
[]string{"device"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
disksTotalDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, "md", "disks"),
|
|
|
|
"Total number of disks of device.",
|
|
|
|
[]string{"device"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
blocksTotalDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, "md", "blocks"),
|
|
|
|
"Total number of blocks on device.",
|
|
|
|
[]string{"device"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
blocksSyncedDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, "md", "blocks_synced"),
|
|
|
|
"Number of blocks synced on device.",
|
|
|
|
[]string{"device"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2017-02-28 18:47:20 +00:00
|
|
|
func (c *mdadmCollector) Update(ch chan<- prometheus.Metric) error {
|
2015-09-26 18:07:20 +00:00
|
|
|
statusfile := procFilePath("mdstat")
|
2017-03-22 13:11:19 +00:00
|
|
|
mdstate, err := parseMdstat(statusfile)
|
|
|
|
if err != nil {
|
2017-02-28 18:47:20 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
log.Debugf("Not collecting mdstat, file does not exist: %s", statusfile)
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-26 12:53:46 +00:00
|
|
|
return fmt.Errorf("error parsing mdstatus: %s", err)
|
2015-09-07 13:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, mds := range mdstate {
|
2017-03-18 17:36:26 +00:00
|
|
|
log.Debugf("collecting metrics for device %s", mds.name)
|
2015-09-07 13:49:30 +00:00
|
|
|
|
2017-03-18 17:36:26 +00:00
|
|
|
var active float64
|
|
|
|
if mds.active {
|
|
|
|
active = 1
|
2015-09-07 13:49:30 +00:00
|
|
|
}
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
isActiveDesc,
|
|
|
|
prometheus.GaugeValue,
|
2017-03-18 17:36:26 +00:00
|
|
|
active,
|
|
|
|
mds.name,
|
2015-09-07 13:49:30 +00:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
disksActiveDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(mds.disksActive),
|
2017-03-18 17:36:26 +00:00
|
|
|
mds.name,
|
2015-09-07 13:49:30 +00:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
disksTotalDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(mds.disksTotal),
|
2017-03-18 17:36:26 +00:00
|
|
|
mds.name,
|
2015-09-07 13:49:30 +00:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
blocksTotalDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(mds.blocksTotal),
|
2017-03-18 17:36:26 +00:00
|
|
|
mds.name,
|
2015-09-07 13:49:30 +00:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
blocksSyncedDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(mds.blocksSynced),
|
2017-03-18 17:36:26 +00:00
|
|
|
mds.name,
|
2015-09-07 13:49:30 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|