Skip to content

Commit

Permalink
Prometheus metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
containerscrew committed Oct 12, 2024
1 parent 93287b6 commit 0f1f1ea
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 94 deletions.
16 changes: 9 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ repos:
- id: check-json
#- id: check-docstring-first
# - id: check-xml
# - repo: https://github.com/dnephin/pre-commit-golang
# rev: v0.5.1
# hooks:
# - id: go-fmt
# - id: go-imports
# - id: golangci-lint
# args: [--fix]
- repo: https://github.com/dnephin/pre-commit-golang
rev: v0.5.1
hooks:
- id: go-fmt
# - id: go-imports
# - id: go-critic
- id: go-mod-tidy
- id: golangci-lint
args: [--fix]
- repo: https://github.com/containerscrew/mtoc
rev: v0.4.1
hooks:
Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ require (
github.com/cilium/ebpf v0.16.0
github.com/containerscrew/devstdout v0.3.0
github.com/pelletier/go-toml v1.9.5
github.com/prometheus/client_golang v1.20.4
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.60.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af // indirect
google.golang.org/protobuf v1.35.1 // indirect
)

require (
Expand Down
32 changes: 30 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cilium/ebpf v0.16.0 h1:+BiEnHL6Z7lXnlGUsXQPPAE7+kenAd4ES8MQ5min0Ok=
github.com/cilium/ebpf v0.16.0/go.mod h1:L7u2Blt2jMM/vLAVgjxluxtBKlz3/GWjB0dMOEngfwE=
github.com/containerscrew/devstdout v0.3.0 h1:WvL3ezp8Ul4B+BRRAP0+txcZWfzqlZZxq6gNiVrmNCo=
github.com/containerscrew/devstdout v0.3.0/go.mod h1:qivX4vQCCaDLhLXXma2qzQMFAvF6xJQOqa4//2IDSSo=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4 h1:ygs9POGDQpQGLJPlq4+0LBUmMBNox1N4JSpw+OETcvI=
Expand All @@ -18,10 +24,14 @@ github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtL
github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=
github.com/jsimonetti/rtnetlink/v2 v2.0.1 h1:xda7qaHDSVOsADNouv7ukSuicKZO7GgVUCXxpaIEIlM=
github.com/jsimonetti/rtnetlink/v2 v2.0.1/go.mod h1:7MoNYNbb3UaDHtF8udiJo/RH6VsTKP1pqKLUTVCvToE=
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand All @@ -31,21 +41,39 @@ github.com/mdlayher/netlink v1.7.2 h1:/UtM3ofJap7Vl4QWCPDGXY8d3GIY2UGSDbK+QWmY8/
github.com/mdlayher/netlink v1.7.2/go.mod h1:xraEF7uJbxLhc5fpHL4cPe221LI2bdttWlU+ZGLfQSw=
github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U=
github.com/mdlayher/socket v0.4.1/go.mod h1:cAqeGjoufqdxWkD7DkpyS+wcefOtmu5OQ8KuoJGIReA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.20.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zIq5+qNN23vI=
github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
github.com/prometheus/common v0.60.0 h1:+V9PAREWNvJMAuJ1x1BaWl9dewMW4YrHZQbx0sJNllA=
github.com/prometheus/common v0.60.0/go.mod h1:h0LYf1R1deLSKtD4Vdg8gy4RuOvENW2J/h19V5NADQw=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af h1:6yITBqGTE2lEeTPG04SN9W+iWHCRyHqlVYILiSXziwk=
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af/go.mod h1:4F09kP5F+am0jAwlQLddpoMDM+iewkxxt6nxUQ5nq5o=
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c h1:7dEasQXItcW1xKJ2+gg5VOiBnqWrJc+rq0DPKyvvdbY=
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9 changes: 8 additions & 1 deletion internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ import (
"context"

"github.com/cilium/ebpf/rlimit"
"github.com/containerscrew/kernelsnoop/internal/dto"
)

// Retrieve context data (log and config) from the context
func GetContextData(ctx context.Context) *dto.ContextData {
contextData, _ := ctx.Value(dto.ContextDataKey).(*dto.ContextData)
return contextData
}

// RemoveMemLock removes the memory lock limit for the current process.
func RemoveMemLock(ctx context.Context) error {
func RemoveMemLock() error {
if err := rlimit.RemoveMemlock(); err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions internal/dto/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ type Config struct {
Type string `toml:"type"`
}
Networking struct {
Enable_udp_tracing bool `toml:"enable_udp_tracing"`
Enable_tcp_tracing bool `toml:"enable_tcp_tracing"`
Udp_filter_ports []string `toml:"udp_filter_ports"`
Tcp_filter_ports []string `toml:"tcp_filter_ports"`
EnableUDPTracing bool `toml:"enable_udp_tracing"`
EnableTCPTracing bool `toml:"enable_tcp_tracing"`
UDPFilterPorts []string `toml:"udp_filter_ports"`
TCPFilterPorts []string `toml:"tcp_filter_ports"`
}
}

Expand Down
9 changes: 7 additions & 2 deletions internal/dto/context_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package dto
import devstdout "github.com/containerscrew/devstdout/pkg"

type ContextData struct {
Log *devstdout.CustomLogger
Config *Config
Log *devstdout.CustomLogger
Config *Config
}

// Define a custom key type to avoid string-based key issues
type contextKey string

const ContextDataKey = contextKey("contextData")
28 changes: 21 additions & 7 deletions internal/ipchecker/ip_checker.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Get geo location of an IP address using custom API https://github.com/containerscrew/iproxy
package ipchecker

import (
"encoding/json"
"fmt"
"net"
"net/http"
"time"
)

// GeoLocation represents the response structure for the IP info API
type GeoLocation struct {
Status string `json:"status"`
City string `json:"city"`
Expand All @@ -25,28 +26,41 @@ type GeoLocation struct {
Query string `json:"query"`
}

// Get ip info from custom API
// GetIPInfo retrieves geo-location information for a given IP address from a custom API
func GetIPInfo(ip string) (GeoLocation, error) {
url := fmt.Sprintf("http://iproxy:8000/api/v1/%s", ip)
resp, err := http.Get(url)

// Create a custom HTTP client with a timeout
client := &http.Client{
Timeout: 10 * time.Second,
}

// Perform the GET request
resp, err := client.Get(url)
if err != nil {
return GeoLocation{}, err
return GeoLocation{}, fmt.Errorf("failed to make request: %v", err)
}
defer resp.Body.Close()

// Check for non-OK status
if resp.StatusCode != http.StatusOK {
return GeoLocation{}, fmt.Errorf("API request failed with status: %s", resp.Status)
}

// Parse the response body into the GeoLocation struct
var info GeoLocation
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
return GeoLocation{}, err
return GeoLocation{}, fmt.Errorf("failed to decode response: %v", err)
}

return info, nil
}

// PrivateIPCheck checks if the given IP is private (non-routable)
func PrivateIPCheck(ip string) bool {
ipAddress := net.ParseIP(ip)
return ipAddress.IsPrivate()
ipAddress := net.ParseIP(ip)
if ipAddress == nil {
return false // Invalid IP
}
return ipAddress.IsPrivate()
}
52 changes: 52 additions & 0 deletions internal/monitoring/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package monitoring

import (
"github.com/prometheus/client_golang/prometheus"
)

// Define the Prometheus metrics for TCP and UDP connections
var (
TCPConnections = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "tcp_connections_total",
Help: "Total number of TCP connections.",
},
[]string{"src_addr", "dst_addr", "src_port", "dst_port", "comm"},
)

UDPConnections = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "udp_connections_total",
Help: "Total number of UDP connections.",
},
[]string{"src_addr", "dst_addr", "src_port", "dst_port", "comm"},
)
)

// InitPrometheus registers the Prometheus metrics
func InitPrometheus() {
prometheus.MustRegister(TCPConnections)
prometheus.MustRegister(UDPConnections)
}

// TrackTCPEvent updates the Prometheus metrics for a TCP event
func TrackTCPEvent(srcAddr, dstAddr, srcPort, dstPort, comm string) {
TCPConnections.With(prometheus.Labels{
"src_addr": srcAddr,
"dst_addr": dstAddr,
"src_port": srcPort,
"dst_port": dstPort,
"comm": comm,
}).Inc()
}

// TrackUDPEvent updates the Prometheus metrics for a UDP event
func TrackUDPEvent(srcAddr, dstAddr, srcPort, dstPort, comm string) {
UDPConnections.With(prometheus.Labels{
"src_addr": srcAddr,
"dst_addr": dstAddr,
"src_port": srcPort,
"dst_port": dstPort,
"comm": comm,
}).Inc()
}
2 changes: 1 addition & 1 deletion internal/trackers/net_track/bpf_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/trackers/net_track/bpf_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0f1f1ea

Please sign in to comment.