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