Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
xvertile committed Jul 28, 2024
0 parents commit b454c67
Show file tree
Hide file tree
Showing 18 changed files with 786 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
8 changes: 8 additions & 0 deletions .idea/.gitignore

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

9 changes: 9 additions & 0 deletions .idea/ProxyPulse.iml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 xvertile

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Binary file added cmd/proxypulse/.DS_Store
Binary file not shown.
12 changes: 12 additions & 0 deletions cmd/proxypulse/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
# Build for the current platform
# Set environment variables for cross-compilation
export GOARCH=amd64
export GOOS=linux

# Build for Linux platform and output to build directory
go build -o build/proxypulse

# Reset environment variables (optional)
unset GOARCH
unset GOOS
Binary file added cmd/proxypulse/build/proxypulse
Binary file not shown.
224 changes: 224 additions & 0 deletions cmd/proxypulse/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
package main

import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"

"ProxyPulse/internal/cpu"
"ProxyPulse/internal/filedescriptors"
"ProxyPulse/internal/memory"
"ProxyPulse/internal/network"
"ProxyPulse/internal/sockets"

ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)

var process = "kraken"
var location = "default"

const maxHistoryLength = 100

type Metrics struct {
Location string
TransferRate int
TotalSocketsOpen int32
TotalFileDescriptors int32
TotalCPUUsage float32
MemoryUsage float32
Timestamp time.Time
}

type MetricsHistory struct {
TransferRates []float64
TotalSocketsOpens []float64
TotalFileDescriptors []float64
TotalCPUUsages []float64
MemoryUsages []float64
}

func collect() (Metrics, error) {
var metrics Metrics
metrics.Location = location
_, txRate, err := network.CalculateTransferRate(1 * time.Second)
if err != nil {
return metrics, err
}
metrics.TransferRate = int(txRate)
open, err := sockets.GetTotalOpenSockets()
if err != nil {
return metrics, err
}
metrics.TotalSocketsOpen = open
descriptors, err := filedescriptors.GetTotalFileDescriptors(process)
if err != nil {
return metrics, err
}
metrics.TotalFileDescriptors = descriptors
usage, err := cpu.GetTotalCPUUsage(process)
if err != nil {
return metrics, err
}
metrics.TotalCPUUsage = float32(usage)
totalmemory, err := memory.GetProcessMemoryUsage(process)
if err != nil {
return metrics, err
}
metrics.MemoryUsage = totalmemory
metrics.Timestamp = time.Now()
return metrics, nil
}

func main() {
processName := flag.String("p", "", "Process name to monitor")
flag.Parse()
if *processName == "" {
log.Fatalf("Please provide a process name to monitor using -p flag")
}
process = *processName
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
locationName := flag.String("l", "default", "Location name to monitor")
flag.Parse()
location = *locationName
defer ui.Close()

title := widgets.NewParagraph()
title.Text = `
____ ____ __
/ __ \_________ _ ____ __/ __ \__ __/ /_______
/ /_/ / ___/ __ \| |/_/ / / / /_/ / / / / / ___/ _ \
/ ____/ / / /_/ /> </ /_/ / ____/ /_/ / (__ ) __/
/_/ /_/ \____/_/|_|\__, /_/ \__,_/_/____/\___/
/____/
A ByteZero project | Process: ` + process + `
Press q or <C-c> to quit
`
title.SetRect(0, 0, 70, 12)
title.TextStyle.Fg = ui.ColorCyan

metricsText := widgets.NewParagraph()
metricsText.SetRect(0, 12, 70, 20)
plotHeight := 14
plotWidth := 70

cpuUsagePlot := widgets.NewPlot()
cpuUsagePlot.Title = "CPU Usage (%)"
cpuUsagePlot.SetRect(70, 0, 70+plotWidth, 20)
cpuUsagePlot.AxesColor = ui.ColorWhite
cpuUsagePlot.LineColors[0] = ui.ColorCyan
cpuUsagePlot.MaxVal = 100

transferRatePlot := widgets.NewPlot()
transferRatePlot.Title = "Transfer Rate (MB/s)"
transferRatePlot.SetRect(0, 20, plotWidth, 20+plotHeight)
transferRatePlot.AxesColor = ui.ColorWhite
transferRatePlot.LineColors[0] = ui.ColorCyan

socketsOpenPlot := widgets.NewPlot()
socketsOpenPlot.Title = "Total Sockets Open"
socketsOpenPlot.SetRect(0, 20+plotHeight, plotWidth, 20+2*plotHeight)
socketsOpenPlot.AxesColor = ui.ColorWhite
socketsOpenPlot.LineColors[0] = ui.ColorCyan

fileDescriptorsPlot := widgets.NewPlot()
fileDescriptorsPlot.Title = "Total File Descriptors"
fileDescriptorsPlot.SetRect(plotWidth, 20, 2*plotWidth, 20+plotHeight)
fileDescriptorsPlot.AxesColor = ui.ColorWhite
fileDescriptorsPlot.LineColors[0] = ui.ColorCyan

memoryUsagePlot := widgets.NewPlot()
memoryUsagePlot.Title = "Memory Usage (GB)"
memoryUsagePlot.SetRect(plotWidth, 20+plotHeight, 2*plotWidth, 20+2*plotHeight)
memoryUsagePlot.AxesColor = ui.ColorWhite
memoryUsagePlot.LineColors[0] = ui.ColorCyan

var history MetricsHistory

updateMetrics := func() {
metrics, err := collect()
if err != nil {
metricsText.Text = fmt.Sprintf("Error collecting metrics: %v", err)
return
}
metricsText.Text = fmt.Sprintf(
"Location: %s\nTransfer Rate: %d MB/s\nTotal Sockets Open: %d\nTotal File Descriptors: %d\nTotal CPU Usage: %.2f%%\nMemory Usage: %.2f GB\nTimestamp: %s\n",
metrics.Location,
metrics.TransferRate,
metrics.TotalSocketsOpen,
metrics.TotalFileDescriptors,
metrics.TotalCPUUsage,
metrics.MemoryUsage,
metrics.Timestamp.Format(time.RFC3339),
)

history.TransferRates = append(history.TransferRates, float64(metrics.TransferRate))
history.TotalSocketsOpens = append(history.TotalSocketsOpens, float64(metrics.TotalSocketsOpen))
history.TotalFileDescriptors = append(history.TotalFileDescriptors, float64(metrics.TotalFileDescriptors))
history.TotalCPUUsages = append(history.TotalCPUUsages, float64(metrics.TotalCPUUsage))
history.MemoryUsages = append(history.MemoryUsages, float64(metrics.MemoryUsage))

if len(history.TransferRates) > maxHistoryLength {
history.TransferRates = history.TransferRates[1:]
history.TotalSocketsOpens = history.TotalSocketsOpens[1:]
history.TotalFileDescriptors = history.TotalFileDescriptors[1:]
history.TotalCPUUsages = history.TotalCPUUsages[1:]
history.MemoryUsages = history.MemoryUsages[1:]
}

if len(history.TransferRates) > 5 {
transferRatePlot.Data = [][]float64{history.TransferRates}
cpuUsagePlot.Data = [][]float64{history.TotalCPUUsages}
socketsOpenPlot.Data = [][]float64{history.TotalSocketsOpens}
fileDescriptorsPlot.Data = [][]float64{history.TotalFileDescriptors}
memoryUsagePlot.Data = [][]float64{history.MemoryUsages}

ui.Render(title, metricsText, cpuUsagePlot, transferRatePlot, socketsOpenPlot, fileDescriptorsPlot, memoryUsagePlot)
} else {
ui.Render(title, metricsText, cpuUsagePlot)
}
}

ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()

uiEvents := ui.PollEvents()
sigEvents := make(chan os.Signal, 1)
signal.Notify(sigEvents, os.Interrupt, syscall.SIGTERM)

exitChan := make(chan bool)

go func() {
for {
select {
case e := <-uiEvents:
if e.Type == ui.KeyboardEvent {
switch e.ID {
case "q", "<C-c>":
exitChan <- true
return
}
}
case <-sigEvents:
exitChan <- true
return
}
}
}()

for {
select {
case <-ticker.C:
updateMetrics()
case <-exitChan:
return
}
}
}
24 changes: 24 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module ProxyPulse

go 1.22.4

require (
git.sr.ht/~sbinet/gg v0.5.0 // indirect
github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b // indirect
github.com/campoy/embedmd v1.0.0 // indirect
github.com/gizak/termui v3.1.0+incompatible // indirect
github.com/gizak/termui/v3 v3.1.0 // indirect
github.com/go-fonts/liberation v0.3.1 // indirect
github.com/go-latex/latex v0.0.0-20230307184459-12ec69307ad9 // indirect
github.com/go-pdf/fpdf v0.8.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mum4k/termdash v0.20.0 // indirect
github.com/nsf/termbox-go v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
golang.org/x/image v0.11.0 // indirect
golang.org/x/text v0.14.0 // indirect
gonum.org/v1/plot v0.14.0 // indirect
)
Loading

0 comments on commit b454c67

Please sign in to comment.