Skip to content

Commit 8cd6776

Browse files
Merge pull request #1 from SentinalFS/big-bang-init
Go logger
2 parents 7997eba + 7a398e2 commit 8cd6776

File tree

17 files changed

+442
-1
lines changed

17 files changed

+442
-1
lines changed

.github/workflows/ci.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: Release Workflow
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
permissions:
12+
contents: write
13+
14+
jobs:
15+
release:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
21+
- name: Set up Go
22+
uses: actions/setup-go@v5
23+
with:
24+
go-version: '1.23'
25+
26+
- name: Install GoReleaser
27+
run: go install github.com/goreleaser/goreleaser@latest
28+
29+
- name: Check version
30+
run: |
31+
VERSION=$(cat VERSION.txt)
32+
echo "version=$VERSION" >> $GITHUB_ENV
33+
echo "Version: $VERSION"
34+
35+
- name: Run build using goreleaser on local
36+
run: goreleaser release --snapshot --skip=publish --clean
37+
38+
- name: Create Tag
39+
if: |
40+
(github.event_name == 'push' && github.ref == 'refs/heads/main') ||
41+
(github.event_name == 'pull_request' &&
42+
github.event.action == 'closed' &&
43+
github.event.pull_request.merged == true &&
44+
github.event.pull_request.base.ref == 'main')
45+
run: |
46+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
47+
git config --global user.name "github-actions[bot]"
48+
git tag "v${{ env.version }}"
49+
git push origin "v${{ env.version }}"
50+
51+
- name: Run GoReleaser Release
52+
if: |
53+
(github.event_name == 'push' && github.ref == 'refs/heads/main') ||
54+
(github.event_name == 'pull_request' &&
55+
github.event.action == 'closed' &&
56+
github.event.pull_request.merged == true &&
57+
github.event.pull_request.base.ref == 'main')
58+
run: |
59+
export GORELEASER_CURRENT_TAG="v${{ env.version }}"
60+
export GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
61+
goreleaser release --clean --rm-dist
62+
env:
63+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ go.work.sum
2323

2424
# env file
2525
.env
26+
27+
# build
28+
dist

.goreleaser.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
project_name: go-ebpf-logger
2+
3+
builds:
4+
- main: ./main.go
5+
goos:
6+
- linux
7+
- darwin
8+
- windows
9+
goarch:
10+
- amd64
11+
- arm64
12+
13+
archives:
14+
- format: tar.gz
15+
name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}'
16+
17+
release:
18+
github:
19+
owner: siddh34
20+
name: go-ebpf-logger

Dockerfile.amd64

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM debian:stable-slim
2+
3+
ARG TARGETARCH
4+
5+
WORKDIR /app
6+
7+
COPY dist/go-ebpf-logger_linux_${TARGETARCH}_v1/go-ebpf-logger /app/go-ebpf-logger
8+
9+
COPY monitor.bpf.o /app/monitor.bpf.o
10+
11+
RUN chmod +x /app/go-ebpf-logger
12+
13+
ENTRYPOINT ["/app/go-ebpf-logger"]

Dockerfile.arm64

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM debian:stable-slim
2+
3+
ARG TARGETARCH
4+
5+
WORKDIR /app
6+
7+
COPY dist/go-ebpf-logger_linux_${TARGETARCH}/go-ebpf-logger /app/go-ebpf-logger
8+
9+
COPY monitor.bpf.o /app/monitor.bpf.o
10+
11+
RUN chmod +x /app/go-ebpf-logger
12+
13+
ENTRYPOINT ["/app/go-ebpf-logger"]

README.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,62 @@
1-
# go-ebpf-logger
1+
# go-ebpf-logger
2+
3+
The golang code that actually runs the file monitor ebpf code
4+
5+
## Pre-requisite
6+
7+
Install golang, visit [link](https://go.dev/doc/install)
8+
9+
Install goreleaser, visit [link](https://goreleaser.com/install/#aur)
10+
11+
Install gh, visit [link](https://cli.github.com/)
12+
13+
## Screenshots
14+
15+
It works!
16+
17+
![Screenshot showing go-ebpf-logger in action](./docs/assets/working.png)
18+
19+
20+
## Run it on Local
21+
22+
Get file monitor binary from the repo
23+
24+
Put the version accordingly here in the below command at `vX.Y.Z`
25+
26+
```sh
27+
gh release download vX.Y.Z --repo SentinalFS/file-monitor --pattern "monitor.bpf.o"
28+
```
29+
30+
Run it
31+
32+
```sh
33+
sudo go run main.go
34+
```
35+
36+
## Run it on docker
37+
38+
Get file monitor binary from the repo
39+
40+
Put the version accordingly here in the below command at `vX.Y.Z`
41+
42+
```sh
43+
gh release download vX.Y.Z --repo SentinalFS/file-monitor --pattern "monitor.bpf.o"
44+
```
45+
46+
Run go releaser on local
47+
48+
```sh
49+
goreleaser release --snapshot --skip=publish --clean
50+
```
51+
52+
Build it
53+
54+
```sh
55+
docker build --build-arg TARGETARCH=amd64 -t go-ebpf-logger -f Dockerfile.amd64 .
56+
```
57+
58+
Run it
59+
60+
```sh
61+
sudo docker run --rm -it --privileged -v /sys/fs/bpf:/sys/fs/bpf:rw go-ebpf-logger
62+
```

VERSION.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.0.1

bpf/pin_maps.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package bpf
2+
3+
import (
4+
"os"
5+
6+
"github.com/cilium/ebpf"
7+
8+
"fmt"
9+
10+
"go-ebp-logger/constants"
11+
)
12+
13+
func pinMaps(m *ebpf.Map) error {
14+
if m != nil {
15+
path := "/sys/fs/bpf/" + constants.InodeMapName
16+
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
17+
fmt.Printf("Warning: failed to remove existing pin at %s: %v", path, err)
18+
}
19+
if err := m.Pin(path); err != nil {
20+
fmt.Printf("Failed to pin map %s to %s: %v", constants.InodeMapName,path, err)
21+
}
22+
fmt.Printf("Map %s pinned succesfully", constants.InodeMapName)
23+
return nil
24+
}
25+
26+
fmt.Printf("Map %s not found", constants.InodeMapName)
27+
return fmt.Errorf("no map was pinned")
28+
}

bpf/setup.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package bpf
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/cilium/ebpf"
7+
"github.com/cilium/ebpf/link"
8+
9+
"go-ebp-logger/constants"
10+
)
11+
12+
func SetupBPF(bpfObj string) (*ebpf.Map, *ebpf.Map, func()) {
13+
spec, err := ebpf.LoadCollectionSpec(bpfObj)
14+
if err != nil {
15+
fmt.Printf("Failed to load BPF collection spec: %v\n", err)
16+
}
17+
18+
coll, err := ebpf.NewCollection(spec)
19+
if err != nil {
20+
fmt.Printf("Failed to load BPF collection: %v\n", err)
21+
}
22+
23+
var links []*link.Link
24+
for progName, fn := range constants.ProgsToFuncs {
25+
prog := coll.Programs[progName]
26+
if prog == nil {
27+
fmt.Printf("Program '%s' not found\n", progName)
28+
}
29+
kp, err := link.Kprobe(fn, prog, nil)
30+
if err != nil {
31+
fmt.Printf("Failed to attach kprobe to %s: %v\n", fn, err)
32+
}
33+
links = append(links, &kp)
34+
}
35+
36+
monitored_inode_map := coll.Maps["monitored_inodes"]
37+
err = pinMaps(monitored_inode_map)
38+
if err != nil {
39+
panic(err)
40+
}
41+
42+
events := coll.Maps["events"]
43+
if events == nil {
44+
fmt.Printf("Map 'events' not found\n")
45+
}
46+
47+
renameEvents := coll.Maps["rename_events"]
48+
if renameEvents == nil {
49+
fmt.Printf("Map 'rename_events' not found\n")
50+
}
51+
52+
cleanup := func() {
53+
for _, l := range links {
54+
(*l).Close()
55+
}
56+
coll.Close()
57+
}
58+
59+
return events, renameEvents, cleanup
60+
}

constants/bpf.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package constants
2+
3+
4+
var ProgsToFuncs = map[string]string{
5+
"trace_read": "vfs_read",
6+
"trace_write": "vfs_write",
7+
"trace_rename": "vfs_rename",
8+
"trace_delete": "vfs_unlink",
9+
}
10+
11+
var InodeMapName = "monitored_inodes"
12+
13+
var BpfObjName = "monitor.bpf.o"

0 commit comments

Comments
 (0)