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