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-09-07 15:49:30 +02:00
|
|
|
// +build !nomdadm
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2019-12-31 17:19:37 +01:00
|
|
|
"github.com/go-kit/kit/log"
|
|
|
|
"github.com/go-kit/kit/log/level"
|
2015-09-07 15:49:30 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2019-07-01 11:56:06 +02:00
|
|
|
"github.com/prometheus/procfs"
|
2015-09-07 15:49:30 +02:00
|
|
|
)
|
|
|
|
|
2019-12-31 17:19:37 +01:00
|
|
|
type mdadmCollector struct {
|
|
|
|
logger log.Logger
|
|
|
|
}
|
2015-09-07 15:49:30 +02:00
|
|
|
|
|
|
|
func init() {
|
2017-09-28 15:06:26 +02:00
|
|
|
registerCollector("mdadm", defaultEnabled, NewMdadmCollector)
|
2015-09-07 15:49:30 +02:00
|
|
|
}
|
|
|
|
|
2017-02-28 17:44:53 +01:00
|
|
|
// NewMdadmCollector returns a new Collector exposing raid statistics.
|
2019-12-31 17:19:37 +01:00
|
|
|
func NewMdadmCollector(logger log.Logger) (Collector, error) {
|
|
|
|
return &mdadmCollector{logger}, nil
|
2015-09-07 15:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2019-07-01 11:56:06 +02:00
|
|
|
activeDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "md", "state"),
|
|
|
|
"Indicates the state of md-device.",
|
2015-09-07 15:49:30 +02:00
|
|
|
[]string{"device"},
|
2019-07-01 11:56:06 +02:00
|
|
|
prometheus.Labels{"state": "active"},
|
2015-09-07 15:49:30 +02:00
|
|
|
)
|
2019-07-01 11:56:06 +02:00
|
|
|
inActiveDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "md", "state"),
|
|
|
|
"Indicates the state of md-device.",
|
|
|
|
[]string{"device"},
|
|
|
|
prometheus.Labels{"state": "inactive"},
|
|
|
|
)
|
|
|
|
recoveringDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "md", "state"),
|
|
|
|
"Indicates the state of md-device.",
|
|
|
|
[]string{"device"},
|
|
|
|
prometheus.Labels{"state": "recovering"},
|
|
|
|
)
|
|
|
|
resyncDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "md", "state"),
|
|
|
|
"Indicates the state of md-device.",
|
2015-09-07 15:49:30 +02:00
|
|
|
[]string{"device"},
|
2019-07-01 11:56:06 +02:00
|
|
|
prometheus.Labels{"state": "resync"},
|
|
|
|
)
|
|
|
|
|
|
|
|
disksDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "md", "disks"),
|
|
|
|
"Number of active/failed/spare disks of device.",
|
|
|
|
[]string{"device", "state"},
|
2015-09-07 15:49:30 +02:00
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
disksTotalDesc = prometheus.NewDesc(
|
2019-07-01 11:56:06 +02:00
|
|
|
prometheus.BuildFQName(namespace, "md", "disks_required"),
|
2015-09-07 15:49:30 +02:00
|
|
|
"Total number of disks of device.",
|
|
|
|
[]string{"device"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
blocksTotalDesc = prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, "md", "blocks"),
|
2015-09-07 15:49:30 +02:00
|
|
|
"Total number of blocks on device.",
|
|
|
|
[]string{"device"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
blocksSyncedDesc = prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, "md", "blocks_synced"),
|
2015-09-07 15:49:30 +02:00
|
|
|
"Number of blocks synced on device.",
|
|
|
|
[]string{"device"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2017-02-28 19:47:20 +01:00
|
|
|
func (c *mdadmCollector) Update(ch chan<- prometheus.Metric) error {
|
2019-07-01 11:56:06 +02:00
|
|
|
fs, errFs := procfs.NewFS(*procPath)
|
|
|
|
|
|
|
|
if errFs != nil {
|
2019-11-29 14:51:31 +01:00
|
|
|
return fmt.Errorf("failed to open procfs: %w", errFs)
|
2019-07-01 11:56:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mdStats, err := fs.MDStat()
|
|
|
|
|
2017-03-22 14:11:19 +01:00
|
|
|
if err != nil {
|
2017-02-28 19:47:20 +01:00
|
|
|
if os.IsNotExist(err) {
|
2019-12-31 17:19:37 +01:00
|
|
|
level.Debug(c.logger).Log("msg", "Not collecting mdstat, file does not exist", "file", *procPath)
|
2017-02-28 19:47:20 +01:00
|
|
|
return nil
|
|
|
|
}
|
2019-07-01 11:56:06 +02:00
|
|
|
|
2015-09-26 14:53:46 +02:00
|
|
|
return fmt.Errorf("error parsing mdstatus: %s", err)
|
2015-09-07 15:49:30 +02:00
|
|
|
}
|
|
|
|
|
2019-07-01 11:56:06 +02:00
|
|
|
for _, mdStat := range mdStats {
|
2019-12-31 17:19:37 +01:00
|
|
|
level.Debug(c.logger).Log("msg", "collecting metrics for device", "device", mdStat.Name)
|
2019-07-01 11:56:06 +02:00
|
|
|
|
|
|
|
stateVals := make(map[string]float64)
|
|
|
|
stateVals[mdStat.ActivityState] = 1
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
disksTotalDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(mdStat.DisksTotal),
|
|
|
|
mdStat.Name,
|
|
|
|
)
|
2015-09-07 15:49:30 +02:00
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2019-07-01 11:56:06 +02:00
|
|
|
disksDesc,
|
2015-09-07 15:49:30 +02:00
|
|
|
prometheus.GaugeValue,
|
2019-07-01 11:56:06 +02:00
|
|
|
float64(mdStat.DisksActive),
|
|
|
|
mdStat.Name,
|
|
|
|
"active",
|
2015-09-07 15:49:30 +02:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2019-07-01 11:56:06 +02:00
|
|
|
disksDesc,
|
2015-09-07 15:49:30 +02:00
|
|
|
prometheus.GaugeValue,
|
2019-07-01 11:56:06 +02:00
|
|
|
float64(mdStat.DisksFailed),
|
|
|
|
mdStat.Name,
|
|
|
|
"failed",
|
2015-09-07 15:49:30 +02:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
2019-07-01 11:56:06 +02:00
|
|
|
disksDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(mdStat.DisksSpare),
|
|
|
|
mdStat.Name,
|
|
|
|
"spare",
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
activeDesc,
|
2015-09-07 15:49:30 +02:00
|
|
|
prometheus.GaugeValue,
|
2019-07-01 11:56:06 +02:00
|
|
|
stateVals["active"],
|
|
|
|
mdStat.Name,
|
2015-09-07 15:49:30 +02:00
|
|
|
)
|
2019-07-01 11:56:06 +02:00
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
inActiveDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
stateVals["inactive"],
|
|
|
|
mdStat.Name,
|
|
|
|
)
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
recoveringDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
stateVals["recovering"],
|
|
|
|
mdStat.Name,
|
|
|
|
)
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
resyncDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
stateVals["resyncing"],
|
|
|
|
mdStat.Name,
|
|
|
|
)
|
|
|
|
|
2015-09-07 15:49:30 +02:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
blocksTotalDesc,
|
|
|
|
prometheus.GaugeValue,
|
2019-07-01 11:56:06 +02:00
|
|
|
float64(mdStat.BlocksTotal),
|
|
|
|
mdStat.Name,
|
2015-09-07 15:49:30 +02:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
blocksSyncedDesc,
|
|
|
|
prometheus.GaugeValue,
|
2019-07-01 11:56:06 +02:00
|
|
|
float64(mdStat.BlocksSynced),
|
|
|
|
mdStat.Name,
|
2015-09-07 15:49:30 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|