Skip to content
Snippets Groups Projects
Verified Commit f4712aa5 authored by meskio's avatar meskio :tent: Committed by Sam Whited
Browse files

Add packets per second metric

- Related: #10
parent cc8ea5f0
No related branches found
No related tags found
1 merge request!11Add a load balancer
......@@ -52,7 +52,8 @@ func main() {
}
agent := lbagent.New(os.Args[1], nil, dialer)
registerBwmon(agent, config)
registerBandwidth(agent, config)
registerPackets(agent, config)
agent.RegisterUtilization("conntrack", lbpb.Utilization_GAUGE, conntrackUtilization)
agent.SetUpdateInterval(updateInterval)
......@@ -66,7 +67,7 @@ func main() {
}
}
func registerBwmon(agent *lbagent.Agent, config Config) {
func registerBandwidth(agent *lbagent.Agent, config Config) {
txBandwidth := config.TxBandwidth
if txBandwidth == 0 {
txBandwidth = config.Bandwidth
......@@ -81,3 +82,11 @@ func registerBwmon(agent *lbagent.Agent, config Config) {
rxBwFunc := getBandwidthUtilization(config.NetDevice, rxBandwidth, RX)
agent.RegisterUtilization("bandwidth_rx", lbpb.Utilization_GAUGE, rxBwFunc)
}
func registerPackets(agent *lbagent.Agent, config Config) {
txPacketsFunc := getPacketUtilization(config.NetDevice, TX)
agent.RegisterUtilization("packets_tx", lbpb.Utilization_COUNTER, txPacketsFunc)
rxPacketsFunc := getPacketUtilization(config.NetDevice, RX)
agent.RegisterUtilization("packets_rx", lbpb.Utilization_COUNTER, rxPacketsFunc)
}
......@@ -36,7 +36,11 @@ const (
func getBandwidthUtilization(device string, max int64, direction int) func() float64 {
var lastRead uint64
return func() float64 {
n, err := parseProcNetDev(device, direction)
field := 1
if direction == TX {
field = 9
}
n, err := parseProcNetDev(device, field)
if err != nil {
log.Printf("Error reading /proc/net/dev: %v", err)
lastRead = 0
......@@ -55,7 +59,22 @@ func getBandwidthUtilization(device string, max int64, direction int) func() flo
}
}
func parseProcNetDev(dev string, direction int) (uint64, error) {
func getPacketUtilization(device string, direction int) func() float64 {
return func() float64 {
field := 2
if direction == TX {
field = 10
}
n, err := parseProcNetDev(device, field)
if err != nil {
log.Printf("Error reading /proc/net/dev: %v", err)
return 0
}
return float64(n)
}
}
func parseProcNetDev(dev string, field int) (uint64, error) {
file, err := os.Open("/proc/net/dev")
if err != nil {
return 0, err
......@@ -65,18 +84,11 @@ func parseProcNetDev(dev string, direction int) (uint64, error) {
// Device name (+ ':') in field 0
devCol := []byte(dev + ":")
// Receive bytes: field 1
// Transmit bytes: field 9
counterIndex := 1
if direction == TX {
counterIndex = 9
}
input := bufio.NewScanner(file)
for input.Scan() {
fields := bytes.Fields(input.Bytes())
if bytes.Equal(fields[0], devCol) {
n, err := strconv.ParseUint(string(fields[counterIndex]), 10, 64)
n, err := strconv.ParseUint(string(fields[field]), 10, 64)
if err != nil {
return 0, err
}
......
......@@ -42,6 +42,16 @@ var limits = []*lb.Limit{
Min: 0,
Max: 1,
},
{
Dimension: "packets_tx",
Min: 0,
Max: 100_000,
},
{
Dimension: "packets_rx",
Min: 0,
Max: 100_000,
},
{
Dimension: "conntrack",
Min: 0,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment