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.
|
|
|
|
|
2021-10-03 13:35:24 +02:00
|
|
|
//go:build !nobonding
|
2015-05-12 13:06:41 +02:00
|
|
|
// +build !nobonding
|
2014-08-14 13:22:35 +02:00
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
2020-06-15 22:27:14 +02:00
|
|
|
"errors"
|
2014-08-14 13:22:35 +02:00
|
|
|
"fmt"
|
2024-09-11 10:51:28 +02:00
|
|
|
"log/slog"
|
2014-10-03 13:16:37 +02:00
|
|
|
"os"
|
2019-02-05 16:37:27 +01:00
|
|
|
"path/filepath"
|
2014-08-14 13:22:35 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
2014-11-25 03:00:17 +01:00
|
|
|
type bondingCollector struct {
|
2016-12-28 15:21:31 +01:00
|
|
|
slaves, active typedDesc
|
2024-09-11 10:51:28 +02:00
|
|
|
logger *slog.Logger
|
2014-11-25 03:00:17 +01:00
|
|
|
}
|
2014-08-14 13:22:35 +02:00
|
|
|
|
|
|
|
func init() {
|
2018-03-29 16:18:12 +02:00
|
|
|
registerCollector("bonding", defaultEnabled, NewBondingCollector)
|
2014-08-14 13:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBondingCollector returns a newly allocated bondingCollector.
|
|
|
|
// It exposes the number of configured and active slave of linux bonding interfaces.
|
2024-09-11 10:51:28 +02:00
|
|
|
func NewBondingCollector(logger *slog.Logger) (Collector, error) {
|
2014-11-25 03:00:17 +01:00
|
|
|
return &bondingCollector{
|
2016-12-28 15:21:31 +01:00
|
|
|
slaves: typedDesc{prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, "bonding", "slaves"),
|
2016-12-28 15:21:31 +01:00
|
|
|
"Number of configured slaves per bonding interface.",
|
|
|
|
[]string{"master"}, nil,
|
|
|
|
), prometheus.GaugeValue},
|
|
|
|
active: typedDesc{prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, "bonding", "active"),
|
2016-12-28 15:21:31 +01:00
|
|
|
"Number of active slaves per bonding interface.",
|
|
|
|
[]string{"master"}, nil,
|
|
|
|
), prometheus.GaugeValue},
|
2019-12-31 17:19:37 +01:00
|
|
|
logger: logger,
|
2014-11-25 03:00:17 +01:00
|
|
|
}, nil
|
2014-08-14 13:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update reads and exposes bonding states, implements Collector interface. Caution: This works only on linux.
|
2017-02-28 19:47:20 +01:00
|
|
|
func (c *bondingCollector) Update(ch chan<- prometheus.Metric) error {
|
2017-04-24 21:19:17 +02:00
|
|
|
statusfile := sysFilePath("class/net")
|
|
|
|
bondingStats, err := readBondingStats(statusfile)
|
2014-08-14 13:22:35 +02:00
|
|
|
if err != nil {
|
2020-06-15 22:27:14 +02:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
2024-09-11 10:51:28 +02:00
|
|
|
c.logger.Debug("Not collecting bonding, file does not exist", "file", statusfile)
|
2020-02-19 16:11:29 +01:00
|
|
|
return ErrNoData
|
2017-04-24 21:19:17 +02:00
|
|
|
}
|
2014-10-29 15:16:43 +01:00
|
|
|
return err
|
2014-08-14 13:22:35 +02:00
|
|
|
}
|
|
|
|
for master, status := range bondingStats {
|
2016-12-28 15:21:31 +01:00
|
|
|
ch <- c.slaves.mustNewConstMetric(float64(status[0]), master)
|
|
|
|
ch <- c.active.mustNewConstMetric(float64(status[1]), master)
|
2014-08-14 13:22:35 +02:00
|
|
|
}
|
2014-10-29 15:16:43 +01:00
|
|
|
return nil
|
2014-08-14 13:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func readBondingStats(root string) (status map[string][2]int, err error) {
|
|
|
|
status = map[string][2]int{}
|
2022-07-27 20:59:39 +02:00
|
|
|
masters, err := os.ReadFile(filepath.Join(root, "bonding_masters"))
|
2014-08-14 13:22:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, master := range strings.Fields(string(masters)) {
|
2022-07-27 20:59:39 +02:00
|
|
|
slaves, err := os.ReadFile(filepath.Join(root, master, "bonding", "slaves"))
|
2014-08-14 13:22:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sstat := [2]int{0, 0}
|
|
|
|
for _, slave := range strings.Fields(string(slaves)) {
|
2022-07-27 20:59:39 +02:00
|
|
|
state, err := os.ReadFile(filepath.Join(root, master, fmt.Sprintf("lower_%s", slave), "bonding_slave", "mii_status"))
|
2020-06-15 22:27:14 +02:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
2014-10-03 13:16:37 +02:00
|
|
|
// some older? kernels use slave_ prefix
|
2022-07-27 20:59:39 +02:00
|
|
|
state, err = os.ReadFile(filepath.Join(root, master, fmt.Sprintf("slave_%s", slave), "bonding_slave", "mii_status"))
|
2014-10-03 13:16:37 +02:00
|
|
|
}
|
2014-08-14 13:22:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sstat[0]++
|
|
|
|
if strings.TrimSpace(string(state)) == "up" {
|
|
|
|
sstat[1]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status[master] = sstat
|
|
|
|
}
|
|
|
|
return status, err
|
|
|
|
}
|