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