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.
|
|
|
|
|
2015-05-12 11:06:41 +00:00
|
|
|
// +build !nobonding
|
2014-08-14 11:22:35 +00:00
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
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"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-04-24 19:19:17 +00:00
|
|
|
"github.com/prometheus/common/log"
|
2014-08-14 11:22:35 +00:00
|
|
|
)
|
|
|
|
|
2014-11-25 02:00:17 +00:00
|
|
|
type bondingCollector struct {
|
2016-12-28 14:21:31 +00:00
|
|
|
slaves, active typedDesc
|
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.
|
2015-05-20 18:04:49 +00:00
|
|
|
func NewBondingCollector() (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},
|
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 {
|
2017-04-24 19:19:17 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
log.Debugf("Not collecting bonding, file does not exist: %s", statusfile)
|
|
|
|
return nil
|
|
|
|
}
|
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{}
|
2019-02-05 15:37:27 +00:00
|
|
|
masters, err := ioutil.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)) {
|
2019-02-05 15:37:27 +00:00
|
|
|
slaves, err := ioutil.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)) {
|
2019-02-05 15:37:27 +00:00
|
|
|
state, err := ioutil.ReadFile(filepath.Join(root, master, fmt.Sprintf("lower_%s", slave), "operstate"))
|
2014-10-03 11:16:37 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// some older? kernels use slave_ prefix
|
2019-02-05 15:37:27 +00:00
|
|
|
state, err = ioutil.ReadFile(filepath.Join(root, master, fmt.Sprintf("slave_%s", slave), "operstate"))
|
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
|
|
|
|
}
|