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