Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/tcp with metrics #14

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
7 changes: 6 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM golang:1.16-alpine
RUN apk add build-base
RUN apk add libpcap-dev
RUN apk add tcpdump

WORKDIR /app

Expand All @@ -11,9 +12,13 @@ RUN go mod download
COPY *.go ./
COPY db ./db
COPY utils ./utils
COPY tcpdump.sh ./
COPY cleanup.sh ./

RUN go build -o /mirroring-api-logging
RUN chmod +x tcpdump.sh;
RUN chmod +x cleanup.sh

EXPOSE 4789/udp

CMD "/mirroring-api-logging"
CMD ["/bin/sh", "-c", "mkdir /app/files | /mirroring-api-logging | /app/tcpdump.sh | /app/cleanup.sh"]
19 changes: 19 additions & 0 deletions cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

max=80
DIR="/app/files"

while true
do
printf "running cleanup\n"
available=$(df -P /app | awk '{ gsub("%",""); capacity = $5 }; END { print capacity }')

if [ "$available" -gt "$max" ]; then
printf "Available value greater than max\n"
find "$DIR" -type f -delete
fi

find "$DIR" -type f -mmin +2 -delete
sleep 30
done

56 changes: 50 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func run(handle *pcap.Handle, apiCollectionId int, source string) {

if !isGcp {
if err := handle.SetBPFFilter("udp and port 4789"); err != nil { // optional
log.Fatal(err)
log.Printf("SetBPFFilter error: %v", err)
return
}
}
Expand Down Expand Up @@ -473,6 +473,12 @@ func main() {
// Handle error
}

defer func() {
if err := client.Disconnect(context.Background()); err != nil {
// Handle error
}
}()

// Set up a ticker to run every 2 minutes
ticker := time.NewTicker(2 * time.Minute)

Expand All @@ -496,15 +502,53 @@ func main() {
interfaceName = "ens4"
}

for {

files, err := ioutil.ReadDir("/app/files/")
log.Println("reading files...")
if err != nil {
log.Printf("Error reading files from directory : %v", err)
continue
}

for _, file := range files {

if file.IsDir() {
continue
}

fileCreationTs, _ := strconv.Atoi(file.Name())
timeNow := time.Now().Unix()
if timeNow-int64(fileCreationTs) < 35 {
continue
}
fileName := "/app/files/" + file.Name()

log.Println("file: ", file.Name())

if handle, err := pcap.OpenOffline(fileName); err != nil {
log.Printf("Error while creating pcap handle %v", err)
} else {
run(handle, -1, "MIRRORING")
flushAll()
}

e := os.Remove(fileName)
if e != nil {
log.Printf("Error while deleting file (%s) : %v", fileName, e)
} else {
log.Printf("Deleted file: %s successfully", fileName)
}

}

time.Sleep(time.Second * 2)
}

if handle, err := pcap.OpenLive(interfaceName, 128*1024, true, pcap.BlockForever); err != nil {
log.Fatal(err)
} else {
run(handle, -1, "MIRRORING")
}

defer func() {
if err := client.Disconnect(context.Background()); err != nil {
// Handle error
}
}()
}
6 changes: 6 additions & 0 deletions tcpdump.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
while :
do
printf "running tcpdump\n"
tcpdump -i eth0 udp port 4789 -w /app/files/%s -W 120 -G 30 -K -n
done
Loading