2015-05-12 07:16:44 +00:00
|
|
|
// +build linux,!nobonding
|
2014-08-14 11:22:35 +00:00
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2014-10-03 11:16:37 +00:00
|
|
|
"os"
|
2014-08-14 11:22:35 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
sysfsNet = "/sys/class/net"
|
|
|
|
)
|
|
|
|
|
2014-11-25 02:00:17 +00:00
|
|
|
type bondingCollector struct {
|
|
|
|
slaves, active *prometheus.GaugeVec
|
|
|
|
}
|
2014-08-14 11:22:35 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
Factories["bonding"] = NewBondingCollector
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
|
|
|
slaves: prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Name: "net_bonding_slaves",
|
|
|
|
Help: "Number of configured slaves per bonding interface.",
|
|
|
|
},
|
|
|
|
[]string{"master"},
|
|
|
|
),
|
|
|
|
active: prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Name: "net_bonding_slaves_active",
|
|
|
|
Help: "Number of active slaves per bonding interface.",
|
|
|
|
},
|
|
|
|
[]string{"master"},
|
|
|
|
),
|
|
|
|
}, nil
|
2014-08-14 11:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update reads and exposes bonding states, implements Collector interface. Caution: This works only on linux.
|
2014-10-29 14:16:43 +00:00
|
|
|
func (c *bondingCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
2014-08-14 11:22:35 +00:00
|
|
|
bondingStats, err := readBondingStats(sysfsNet)
|
|
|
|
if err != nil {
|
2014-10-29 14:16:43 +00:00
|
|
|
return err
|
2014-08-14 11:22:35 +00:00
|
|
|
}
|
|
|
|
for master, status := range bondingStats {
|
2014-11-25 02:00:17 +00:00
|
|
|
c.slaves.WithLabelValues(master).Set(float64(status[0]))
|
|
|
|
c.active.WithLabelValues(master).Set(float64(status[1]))
|
2014-08-14 11:22:35 +00:00
|
|
|
}
|
2014-11-25 02:00:17 +00:00
|
|
|
c.slaves.Collect(ch)
|
|
|
|
c.active.Collect(ch)
|
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{}
|
|
|
|
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 11:16:37 +00: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 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
|
|
|
|
}
|