mirror of
				https://github.com/prometheus/node_exporter.git
				synced 2025-08-20 18:33:52 -07:00 
			
		
		
		
	Add network bonding collector
This collector exposes two metrics: - net_bonding_slaves: configured slaves per bonding interface - net_bonding_slaves_active: currently active slaves per bonding interface
This commit is contained in:
		
							parent
							
								
									b03ff7cb9b
								
							
						
					
					
						commit
						0bf0f67503
					
				
							
								
								
									
										93
									
								
								collector/bonding.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								collector/bonding.go
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,93 @@ | |||
| // +build !nobonding
 | ||||
| 
 | ||||
| package collector | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"io/ioutil" | ||||
| 	"path" | ||||
| 	"strings" | ||||
| 
 | ||||
| 	"github.com/prometheus/client_golang/prometheus" | ||||
| ) | ||||
| 
 | ||||
| const ( | ||||
| 	sysfsNet = "/sys/class/net" | ||||
| ) | ||||
| 
 | ||||
| var ( | ||||
| 	bondingSlaves = prometheus.NewGaugeVec( | ||||
| 		prometheus.GaugeOpts{ | ||||
| 			Namespace: Namespace, | ||||
| 			Name:      "net_bonding_slaves", | ||||
| 			Help:      "Number of configured slaves per bonding interface.", | ||||
| 		}, []string{"master"}) | ||||
| 	bondingSlavesActive = prometheus.NewGaugeVec( | ||||
| 		prometheus.GaugeOpts{ | ||||
| 			Namespace: Namespace, | ||||
| 			Name:      "net_bonding_slaves_active", | ||||
| 			Help:      "Number of active slaves per bonding interface.", | ||||
| 		}, []string{"master"}) | ||||
| ) | ||||
| 
 | ||||
| type bondingCollector struct{} | ||||
| 
 | ||||
| func init() { | ||||
| 	Factories["bonding"] = NewBondingCollector | ||||
| } | ||||
| 
 | ||||
| // NewBondingCollector returns a newly allocated bondingCollector.
 | ||||
| // It exposes the number of configured and active slave of linux bonding interfaces.
 | ||||
| func NewBondingCollector(config Config) (Collector, error) { | ||||
| 	c := bondingCollector{} | ||||
| 	if _, err := prometheus.RegisterOrGet(bondingSlaves); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	if _, err := prometheus.RegisterOrGet(bondingSlavesActive); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	return &c, nil | ||||
| } | ||||
| 
 | ||||
| // Update reads and exposes bonding states, implements Collector interface. Caution: This works only on linux.
 | ||||
| func (c *bondingCollector) Update() (int, error) { | ||||
| 	bondingStats, err := readBondingStats(sysfsNet) | ||||
| 	if err != nil { | ||||
| 		return 0, err | ||||
| 	} | ||||
| 	updates := 0 | ||||
| 	for master, status := range bondingStats { | ||||
| 		bondingSlaves.WithLabelValues(master).Set(float64(status[0])) | ||||
| 		updates++ | ||||
| 		bondingSlavesActive.WithLabelValues(master).Set(float64(status[1])) | ||||
| 		updates++ | ||||
| 	} | ||||
| 	return updates, nil | ||||
| } | ||||
| 
 | ||||
| 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)) { | ||||
| 			state, err := ioutil.ReadFile(path.Join(root, master, fmt.Sprintf("slave_%s", slave), "operstate")) | ||||
| 			if err != nil { | ||||
| 				return nil, err | ||||
| 			} | ||||
| 			sstat[0]++ | ||||
| 			if strings.TrimSpace(string(state)) == "up" { | ||||
| 				sstat[1]++ | ||||
| 			} | ||||
| 		} | ||||
| 		status[master] = sstat | ||||
| 	} | ||||
| 	return status, err | ||||
| } | ||||
							
								
								
									
										23
									
								
								collector/bonding_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								collector/bonding_test.go
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,23 @@ | |||
| package collector | ||||
| 
 | ||||
| import ( | ||||
| 	"testing" | ||||
| ) | ||||
| 
 | ||||
| func TestBonding(t *testing.T) { | ||||
| 	bondingStats, err := readBondingStats("fixtures/bonding") | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	if bondingStats["bond0"][0] != 0 || bondingStats["bond0"][1] != 0 { | ||||
| 		t.Fatal("bond0 in unexpected state") | ||||
| 	} | ||||
| 
 | ||||
| 	t.Logf("int: %v", bondingStats["int"]) | ||||
| 	if bondingStats["int"][0] != 2 || bondingStats["int"][1] != 1 { | ||||
| 		t.Fatal("int in unexpected state") | ||||
| 	} | ||||
| 	if bondingStats["dmz"][0] != 2 || bondingStats["dmz"][1] != 2 { | ||||
| 		t.Fatal("dmz in unexpected state") | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										0
									
								
								collector/fixtures/bonding/bond0/bonding/slaves
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								collector/fixtures/bonding/bond0/bonding/slaves
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										1
									
								
								collector/fixtures/bonding/bonding_masters
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								collector/fixtures/bonding/bonding_masters
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1 @@ | |||
| bond0 dmz int | ||||
							
								
								
									
										1
									
								
								collector/fixtures/bonding/dmz/bonding/slaves
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								collector/fixtures/bonding/dmz/bonding/slaves
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1 @@ | |||
| eth0 eth4 | ||||
							
								
								
									
										1
									
								
								collector/fixtures/bonding/dmz/slave_eth0/operstate
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								collector/fixtures/bonding/dmz/slave_eth0/operstate
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1 @@ | |||
| up | ||||
							
								
								
									
										1
									
								
								collector/fixtures/bonding/dmz/slave_eth4/operstate
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								collector/fixtures/bonding/dmz/slave_eth4/operstate
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1 @@ | |||
| up | ||||
							
								
								
									
										1
									
								
								collector/fixtures/bonding/int/bonding/slaves
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								collector/fixtures/bonding/int/bonding/slaves
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1 @@ | |||
| eth5 eth1 | ||||
							
								
								
									
										1
									
								
								collector/fixtures/bonding/int/slave_eth1/operstate
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								collector/fixtures/bonding/int/slave_eth1/operstate
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1 @@ | |||
| down | ||||
							
								
								
									
										1
									
								
								collector/fixtures/bonding/int/slave_eth5/operstate
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								collector/fixtures/bonding/int/slave_eth5/operstate
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1 @@ | |||
| up | ||||
		Loading…
	
		Reference in a new issue