-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (91 loc) · 2.58 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go count count.c
import (
"bufio"
"fmt"
"log"
"net/http"
"time"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/rlimit"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
UPDATE_INTERVAL = 1 // sec
)
var (
mapElemCountGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "ebpf_map_curr_elem_count",
Help: "Current number of elements in eBPF maps, labeled by map ID and name",
},
[]string{"id", "name"},
)
mapPressureGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "ebpf_map_pressure",
Help: "Current pressure of eBPF maps (currElements / maxElements), labeled by map ID and name",
},
[]string{"id", "name"},
)
)
func main() {
reg := prometheus.NewRegistry()
reg.MustRegister(mapElemCountGauge)
reg.MustRegister(mapPressureGauge)
if err := rlimit.RemoveMemlock(); err != nil {
log.Fatalf("Failed to remove memlock limit: %v", err)
}
objs := countObjects{}
if err := loadCountObjects(&objs, nil); err != nil {
log.Fatalf("Failed to load objects: %v", err)
}
defer objs.Close()
// Attach the program to the Iterator hook.
iterLink, err := link.AttachIter(link.IterOptions{
Program: objs.DumpBpfMap,
})
if err != nil {
log.Fatalf("Failed to attach eBPF program: %v", err)
}
defer iterLink.Close()
log.Println("eBPF program attached successfully.")
// Start HTTP server for Prometheus metrics
handler := promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
http.Handle("/metrics", handler)
go func() {
log.Fatal(http.ListenAndServe(":2112", nil))
}()
log.Println("Prometheus HTTP server started on :2112")
// Keep the program running.
for {
time.Sleep(UPDATE_INTERVAL * time.Second)
reader, err := iterLink.Open()
if err != nil {
log.Fatalf("Failed to open BPF iterator: %v", err)
}
defer reader.Close()
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
// Variables to store the parsed values
var id int
var name string
var maxElements int
var currElements int64
// Parse the line
line := scanner.Text()
length, err := fmt.Sscanf(line, "%4d %s %10d %10d", &id, &name, &maxElements, &currElements)
if err != nil || length != 4 {
log.Fatal(err)
}
// Update the metrics
idStr := fmt.Sprintf("%d", id)
mapElemCountGauge.WithLabelValues(idStr, name).Set(float64(currElements))
mapPressureGauge.WithLabelValues(idStr, name).Set(float64(currElements) / float64(maxElements))
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
}