Skip to content

Commit

Permalink
(feat) mem limit 100 MB (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 authored Aug 10, 2024
1 parent 8cd45de commit 45db8f5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 11 deletions.
17 changes: 10 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func main() {
watch(filePath)
}
if f.Every > 0 {
if err := gocron.Every(f.Every).Second().Do(pkg.PrintMemUsage); err != nil {
if err := gocron.Every(1).Second().Do(pkg.PrintMemUsage, &f); err != nil {
slog.Error("Error scheduling memory usage", "error", err.Error())
return
}
Expand Down Expand Up @@ -115,16 +115,17 @@ func syncFilePaths() {
}

func sendHealthCheck() {
if f.MSTeamsHook == "" {
return
}
details := pkg.GetHealthCheckDetails(&f, version)
for idx, filePath := range filePaths {
details = append(details, gmt.Details{
Label: fmt.Sprintf("File Path %d", idx+1),
Message: filePath,
})
}
slog.Info("Sending Health Check Notify", "details", details)
if f.MSTeamsHook == "" {
return
}

hostname, _ := os.Hostname()

Expand Down Expand Up @@ -181,13 +182,14 @@ func watch(filePath string) {
}

func notify(result *pkg.ScanResult) {
slog.Info("Sending to MS Teams")
details := pkg.GetAlertDetails(&f, result)
slog.Info("Sending Alert Notify", "details", details)

if f.MSTeamsHook == "" {
return
}

slog.Info("Sending to MS Teams")
details := pkg.GetAlertDetails(&f, result)

hostname, _ := os.Hostname()

err := gmt.Send(hostname, details, f.MSTeamsHook, f.Proxy)
Expand All @@ -208,6 +210,7 @@ func flags() {
flag.Uint64Var(&f.Every, "every", 0, "run every n seconds (0 to run once)")
flag.Uint64Var(&f.HealthCheckEvery, "health-check-every", 0, "run health check every n seconds (0 to disable)")
flag.IntVar(&f.LogLevel, "log-level", 0, "log level (0=info, 1=debug)")
flag.IntVar(&f.MemLimit, "mem-limit", 100, "memory limit in MB (0 to disable)")
flag.IntVar(&f.FilePathsCap, "file-paths-cap", 100, "max number of file paths to watch")
flag.IntVar(&f.Min, "min", 1, "on minimum num of matches, it should notify")
flag.BoolVar(&f.Version, "version", false, "")
Expand Down
25 changes: 25 additions & 0 deletions pkg/card.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,35 @@ package pkg

import (
"fmt"
"runtime"

gmt "github.com/kevincobain2000/go-msteams/src"
)

func GetPanicDetails(f *Flags, m *runtime.MemStats) []gmt.Details {
return []gmt.Details{
{
Label: "File Path",
Message: f.FilePath,
},
{
Label: "Match Pattern",
Message: f.Match,
},
{
Label: "Ignore Pattern",
Message: f.Ignore,
},
{
Label: "Mem Limit (MB) Exceeded",
Message: fmt.Sprintf("%d", f.MemLimit),
},
{
Label: "Alloc (MB)",
Message: fmt.Sprintf("%d", BToMb(m.Alloc)),
},
}
}
func GetHealthCheckDetails(f *Flags, version string) []gmt.Details {
return []gmt.Details{
{
Expand Down
1 change: 1 addition & 0 deletions pkg/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Flags struct {
HealthCheckEvery uint64
Proxy string
LogLevel int
MemLimit int
MSTeamsHook string
Version bool
}
36 changes: 32 additions & 4 deletions pkg/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"os/exec"
"runtime"

gmt "github.com/kevincobain2000/go-msteams/src"
)

func SystemProxy() string {
Expand All @@ -18,16 +20,25 @@ func SystemProxy() string {
return ""
}

func PrintMemUsage() {
func PrintMemUsage(f *Flags) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
slog.Info("Memory Usage",
"Alloc (MB)", bToMb(m.Alloc),
"Sys", bToMb(m.Sys),
"Alloc (MB)", BToMb(m.Alloc),
"Alloc (Bytes)", m.Alloc,
"Sys (MB)", BToMb(m.Sys),
"Sys (Bytes)", m.Sys,
"NumGC", m.NumGC,
"HeapAlloc (MB)", BToMb(m.HeapAlloc),
"HeapSys (Bytes)", m.HeapSys,
)
if f.MemLimit > 0 && m.Alloc > uint64(f.MemLimit*1024*1024) {
SendHealthCheck(f, &m)
panic("Memory Limit Exceeded")
}
}

func bToMb(b uint64) uint64 {
func BToMb(b uint64) uint64 {
return b / 1024 / 1024
}

Expand All @@ -36,3 +47,20 @@ func ExecShell(command string) (string, error) {
out, err := cmd.CombinedOutput()
return string(out), err
}

func SendHealthCheck(f *Flags, m *runtime.MemStats) {
if f.MSTeamsHook == "" {
return
}
details := GetPanicDetails(f, m)
slog.Warn("Sending Panic Notify", "details", details)

hostname, _ := os.Hostname()

err := gmt.Send(hostname, details, f.MSTeamsHook, f.Proxy)
if err != nil {
slog.Error("Error sending to Teams", "error", err.Error())
} else {
slog.Info("Successfully sent to MS Teams")
}
}

0 comments on commit 45db8f5

Please sign in to comment.