2016-12-05 11:37:35 +01:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/common/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Numerical metric provided by /proc/drbd.
|
|
|
|
type drbdNumericalMetric struct {
|
|
|
|
desc *prometheus.Desc
|
|
|
|
valueType prometheus.ValueType
|
|
|
|
multiplier float64
|
|
|
|
}
|
|
|
|
|
2016-12-22 13:57:19 +01:00
|
|
|
func newDRBDNumericalMetric(name string, desc string, valueType prometheus.ValueType, multiplier float64) drbdNumericalMetric {
|
2016-12-05 11:37:35 +01:00
|
|
|
return drbdNumericalMetric{
|
|
|
|
desc: prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, "drbd", name),
|
2016-12-05 11:37:35 +01:00
|
|
|
desc,
|
|
|
|
[]string{"device"}, nil),
|
|
|
|
valueType: valueType,
|
|
|
|
multiplier: multiplier,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String pair metric provided by /proc/drbd.
|
|
|
|
type drbdStringPairMetric struct {
|
|
|
|
desc *prometheus.Desc
|
|
|
|
valueOkay string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (metric *drbdStringPairMetric) isOkay(value string) float64 {
|
|
|
|
if value == metric.valueOkay {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-12-22 13:57:19 +01:00
|
|
|
func newDRBDStringPairMetric(name string, desc string, valueOkay string) drbdStringPairMetric {
|
2016-12-05 11:37:35 +01:00
|
|
|
return drbdStringPairMetric{
|
|
|
|
desc: prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, "drbd", name),
|
2016-12-05 11:37:35 +01:00
|
|
|
desc,
|
|
|
|
[]string{"device", "node"}, nil),
|
|
|
|
valueOkay: valueOkay,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
drbdNumericalMetrics = map[string]drbdNumericalMetric{
|
2016-12-22 13:57:19 +01:00
|
|
|
"ns": newDRBDNumericalMetric(
|
|
|
|
"network_sent_bytes_total",
|
2016-12-23 15:55:49 +01:00
|
|
|
"Total number of bytes sent via the network.",
|
2016-12-05 11:37:35 +01:00
|
|
|
prometheus.CounterValue,
|
|
|
|
1024),
|
2016-12-22 13:57:19 +01:00
|
|
|
"nr": newDRBDNumericalMetric(
|
|
|
|
"network_received_bytes_total",
|
2016-12-23 15:55:49 +01:00
|
|
|
"Total number of bytes received via the network.",
|
2016-12-05 11:37:35 +01:00
|
|
|
prometheus.CounterValue,
|
|
|
|
1),
|
2016-12-22 13:57:19 +01:00
|
|
|
"dw": newDRBDNumericalMetric(
|
|
|
|
"disk_written_bytes_total",
|
|
|
|
"Net data written on local hard disk; in bytes.",
|
2016-12-05 11:37:35 +01:00
|
|
|
prometheus.CounterValue,
|
|
|
|
1024),
|
2016-12-22 13:57:19 +01:00
|
|
|
"dr": newDRBDNumericalMetric(
|
|
|
|
"disk_read_bytes_total",
|
|
|
|
"Net data read from local hard disk; in bytes.",
|
2016-12-05 11:37:35 +01:00
|
|
|
prometheus.CounterValue,
|
|
|
|
1024),
|
2016-12-22 13:57:19 +01:00
|
|
|
"al": newDRBDNumericalMetric(
|
|
|
|
"activitylog_writes_total",
|
2016-12-05 11:37:35 +01:00
|
|
|
"Number of updates of the activity log area of the meta data.",
|
|
|
|
prometheus.CounterValue,
|
|
|
|
1),
|
2016-12-22 13:57:19 +01:00
|
|
|
"bm": newDRBDNumericalMetric(
|
|
|
|
"bitmap_writes_total",
|
2016-12-05 11:37:35 +01:00
|
|
|
"Number of updates of the bitmap area of the meta data.",
|
|
|
|
prometheus.CounterValue,
|
|
|
|
1),
|
2016-12-22 13:57:19 +01:00
|
|
|
"lo": newDRBDNumericalMetric(
|
2016-12-05 11:37:35 +01:00
|
|
|
"local_pending",
|
|
|
|
"Number of open requests to the local I/O sub-system.",
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
1),
|
2016-12-22 13:57:19 +01:00
|
|
|
"pe": newDRBDNumericalMetric(
|
2016-12-05 11:37:35 +01:00
|
|
|
"remote_pending",
|
2016-12-23 15:55:49 +01:00
|
|
|
"Number of requests sent to the peer, but that have not yet been answered by the latter.",
|
2016-12-05 11:37:35 +01:00
|
|
|
prometheus.GaugeValue,
|
|
|
|
1),
|
2016-12-22 13:57:19 +01:00
|
|
|
"ua": newDRBDNumericalMetric(
|
2016-12-05 11:37:35 +01:00
|
|
|
"remote_unacknowledged",
|
2016-12-23 15:55:49 +01:00
|
|
|
"Number of requests received by the peer via the network connection, but that have not yet been answered.",
|
2016-12-05 11:37:35 +01:00
|
|
|
prometheus.GaugeValue,
|
|
|
|
1),
|
2016-12-22 13:57:19 +01:00
|
|
|
"ap": newDRBDNumericalMetric(
|
2016-12-05 11:37:35 +01:00
|
|
|
"application_pending",
|
|
|
|
"Number of block I/O requests forwarded to DRBD, but not yet answered by DRBD.",
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
1),
|
2016-12-22 13:57:19 +01:00
|
|
|
"ep": newDRBDNumericalMetric(
|
2016-12-05 11:37:35 +01:00
|
|
|
"epochs",
|
|
|
|
"Number of Epochs currently on the fly.",
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
1),
|
2016-12-22 13:57:19 +01:00
|
|
|
"oos": newDRBDNumericalMetric(
|
2016-12-05 11:37:35 +01:00
|
|
|
"out_of_sync_bytes",
|
2016-12-22 13:57:19 +01:00
|
|
|
"Amount of data known to be out of sync; in bytes.",
|
2016-12-05 11:37:35 +01:00
|
|
|
prometheus.GaugeValue,
|
|
|
|
1024),
|
|
|
|
}
|
|
|
|
drbdStringPairMetrics = map[string]drbdStringPairMetric{
|
2016-12-22 13:57:19 +01:00
|
|
|
"ro": newDRBDStringPairMetric(
|
2016-12-05 11:37:35 +01:00
|
|
|
"node_role_is_primary",
|
|
|
|
"Whether the role of the node is in the primary state.",
|
|
|
|
"Primary"),
|
2016-12-22 13:57:19 +01:00
|
|
|
"ds": newDRBDStringPairMetric(
|
2016-12-05 11:37:35 +01:00
|
|
|
"disk_state_is_up_to_date",
|
|
|
|
"Whether the disk of the node is up to date.",
|
|
|
|
"UpToDate"),
|
|
|
|
}
|
|
|
|
|
|
|
|
drbdConnected = prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, "drbd", "connected"),
|
2016-12-23 15:55:49 +01:00
|
|
|
"Whether DRBD is connected to the peer.",
|
2016-12-05 11:37:35 +01:00
|
|
|
[]string{"device"}, nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
type drbdCollector struct{}
|
|
|
|
|
|
|
|
func init() {
|
2017-09-28 15:06:26 +02:00
|
|
|
registerCollector("drbd", defaultDisabled, newDRBDCollector)
|
2016-12-05 11:37:35 +01:00
|
|
|
}
|
|
|
|
|
2016-12-22 13:57:19 +01:00
|
|
|
func newDRBDCollector() (Collector, error) {
|
2016-12-05 11:37:35 +01:00
|
|
|
return &drbdCollector{}, nil
|
|
|
|
}
|
|
|
|
|
2017-02-28 19:47:20 +01:00
|
|
|
func (c *drbdCollector) Update(ch chan<- prometheus.Metric) error {
|
2016-12-05 11:37:35 +01:00
|
|
|
statsFile := procFilePath("drbd")
|
2016-12-16 11:45:14 +01:00
|
|
|
file, err := os.Open(statsFile)
|
2016-12-05 11:37:35 +01:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2016-12-22 13:57:19 +01:00
|
|
|
log.Debugf("Not collecting DRBD statistics, as %s does not exist: %s", statsFile, err)
|
2016-12-05 11:37:35 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2016-12-16 11:45:14 +01:00
|
|
|
defer file.Close()
|
2016-12-05 11:37:35 +01:00
|
|
|
|
2016-12-16 11:45:14 +01:00
|
|
|
scanner := bufio.NewScanner(file)
|
2016-12-05 11:37:35 +01:00
|
|
|
scanner.Split(bufio.ScanWords)
|
|
|
|
device := "unknown"
|
|
|
|
for scanner.Scan() {
|
|
|
|
field := scanner.Text()
|
|
|
|
if kv := strings.Split(field, ":"); len(kv) == 2 {
|
|
|
|
if id, err := strconv.ParseUint(kv[0], 10, 64); err == nil && kv[1] == "" {
|
|
|
|
device = fmt.Sprintf("drbd%d", id)
|
|
|
|
} else if metric, ok := drbdNumericalMetrics[kv[0]]; ok {
|
|
|
|
// Numerical value.
|
2016-12-16 11:36:36 +01:00
|
|
|
value, err := strconv.ParseFloat(kv[1], 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-05 11:37:35 +01:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metric.desc, metric.valueType,
|
|
|
|
value*metric.multiplier, device)
|
|
|
|
} else if metric, ok := drbdStringPairMetrics[kv[0]]; ok {
|
|
|
|
// String pair value.
|
|
|
|
values := strings.Split(kv[1], "/")
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metric.desc, prometheus.GaugeValue,
|
|
|
|
metric.isOkay(values[0]), device, "local")
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
metric.desc, prometheus.GaugeValue,
|
|
|
|
metric.isOkay(values[1]), device, "remote")
|
|
|
|
} else if kv[0] == "cs" {
|
|
|
|
// Connection state.
|
2016-12-22 13:57:19 +01:00
|
|
|
var connected float64
|
2016-12-05 11:37:35 +01:00
|
|
|
if kv[1] == "Connected" {
|
|
|
|
connected = 1
|
|
|
|
}
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
drbdConnected, prometheus.GaugeValue,
|
|
|
|
connected, device)
|
|
|
|
} else {
|
2016-12-23 15:57:46 +01:00
|
|
|
log.Debugf("Don't know how to process key-value pair [%s: %q]", kv[0], kv[1])
|
2016-12-05 11:37:35 +01:00
|
|
|
}
|
|
|
|
} else {
|
2016-12-23 15:57:46 +01:00
|
|
|
log.Debugf("Don't know how to process string %q", field)
|
2016-12-05 11:37:35 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-16 11:36:36 +01:00
|
|
|
return scanner.Err()
|
2016-12-05 11:37:35 +01:00
|
|
|
}
|