Skip to content

Commit

Permalink
feat: install version, refactor on vars, added latest and appended fi…
Browse files Browse the repository at this point in the history
…leinfo
  • Loading branch information
kevincobain2000 committed Dec 27, 2024
1 parent 4598080 commit 49c7b70
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 11 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/glebarez/go-sqlite v1.22.0
github.com/gravwell/gravwell/v3 v3.8.34
github.com/jasonlvhit/gocron v0.0.1
github.com/k0kubun/pp v3.0.1+incompatible
github.com/kevincobain2000/go-msteams v1.1.1
github.com/mattn/go-isatty v0.0.20
github.com/natefinch/lumberjack v2.0.0+incompatible
Expand All @@ -19,6 +20,7 @@ require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ github.com/gravwell/gravwell/v3 v3.8.34/go.mod h1:FsIn6mNCcY7wEswbhxRpLchB9cF5jj
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jasonlvhit/gocron v0.0.1 h1:qTt5qF3b3srDjeOIR4Le1LfeyvoYzJlYpqvG7tJX5YU=
github.com/jasonlvhit/gocron v0.0.1/go.mod h1:k9a3TV8VcU73XZxfVHCHWMWF9SOqgoku0/QlY2yvlA4=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 h1:uC1QfSlInpQF+M0ao65imhwqKnz3Q2z/d8PWZRMQvDM=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40=
github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/kevincobain2000/go-msteams v1.1.1 h1:vZ8AYvVmiCdC+VZwsw7RFhb89RG/GasX9kvbdKheFN4=
github.com/kevincobain2000/go-msteams v1.1.1/go.mod h1:+HowoQQHg9HLfx3CYQGImGGYw20+kN9rFmUXgxrqBzo=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand Down
9 changes: 8 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ THIS_PROJECT_NAME='go-watch-logs'
THISOS=$(uname -s)
ARCH=$(uname -m)

INSTALL_VERSION=${1:-latest}

case $THISOS in
Linux*)
case $ARCH in
Expand Down Expand Up @@ -49,7 +51,12 @@ if [ -z "$THE_ARCH_BIN" ]; then
exit 1
fi

curl -kL --progress-bar https://github.com/rakutentech/$THIS_PROJECT_NAME/releases/latest/download/$THE_ARCH_BIN -o "$BIN_DIR"/$THIS_PROJECT_NAME
DOWNLOAD_URL="https://github.com/rakutentech/$THIS_PROJECT_NAME/releases/download/$INSTALL_VERSION/$THE_ARCH_BIN"
if [ "$INSTALL_VERSION" = "latest" ]; then
DOWNLOAD_URL="https://github.com/kevincobain2000/$THIS_PROJECT_NAME/releases/$INSTALL_VERSION/download/$THE_ARCH_BIN"
fi

curl -kL --progress-bar "$DOWNLOAD_URL" -o "$BIN_DIR"/$THIS_PROJECT_NAME

chmod +x "$BIN_DIR"/$THIS_PROJECT_NAME

Expand Down
15 changes: 11 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ func watch(filePath string) {
slog.Error("Error scanning file", "error", err.Error(), "filePath", filePath)
return
}
slog.Info("1st line", "date", result.FirstDate, "line", pkg.Truncate(result.FirstLine, pkg.TruncateMax))
slog.Info("Preview line", "line", pkg.Truncate(result.PreviewLine, pkg.TruncateMax))
slog.Info("Last line", "date", result.LastDate, "line", pkg.Truncate(result.LastLine, pkg.TruncateMax))
slog.Info("1st line (truncated to 200 chars)", "date", result.FirstDate, "line", pkg.Truncate(result.FirstLine, pkg.TruncateMax))
slog.Info("Preview line (truncated to 200 chars)", "line", pkg.Truncate(result.PreviewLine, pkg.TruncateMax))
slog.Info("Last line (truncated to 200 chars)", "date", result.LastDate, "line", pkg.Truncate(result.LastLine, pkg.TruncateMax))
slog.Info("Error count", "percent", fmt.Sprintf("%d (%.2f)", result.ErrorCount, result.ErrorPercent)+"%")

slog.Info("Lines read", "count", result.LinesRead)
Expand All @@ -189,7 +189,13 @@ func watch(filePath string) {
if result.ErrorCount < f.Min {
return
}
notify(result)
if !f.NotifyOnlyRecent {
notify(result)
}

if f.NotifyOnlyRecent && pkg.IsRecentlyModified(result.FileInfo, f.Every) {
notify(result)
}
if f.PostMin != "" {
if _, err := pkg.ExecShell(f.PostMin); err != nil {
slog.Error("Error running post command", "error", err.Error())
Expand Down Expand Up @@ -238,6 +244,7 @@ func flags() {
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.Anomaly, "anomaly", false, "")
flag.BoolVar(&f.NotifyOnlyRecent, "notify-only-recent", true, "Notify on latest file only by timestamp based on --every")
flag.BoolVar(&f.Version, "version", false, "")
flag.BoolVar(&f.Test, "test", false, `Quickly test paths or regex
# will test if the input matches the regex
Expand Down
1 change: 1 addition & 0 deletions pkg/anomaly.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package pkg
8 changes: 8 additions & 0 deletions pkg/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pkg
import (
"os"
"path/filepath"
"time"
"unicode/utf8"
)

Expand Down Expand Up @@ -66,3 +67,10 @@ func GetHomedir() string {
}
return home
}

func IsRecentlyModified(fileInfo os.FileInfo, within uint64) bool {
modTime := fileInfo.ModTime().Unix()
currentTime := time.Now().Unix()
bufferMins := int64(within) * 60
return currentTime-modTime < bufferMins
}
1 change: 1 addition & 0 deletions pkg/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Flags struct {
MemLimit int
MSTeamsHook string
Anomaly bool
NotifyOnlyRecent bool
Test bool
Version bool
}
2 changes: 1 addition & 1 deletion pkg/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func SetupLoggingStdout(logLevel int, logFile string) error {
}
slog.SetDefault(slog.New(slogcolor.NewHandler(&lumberjack.Logger{
Filename: logFile,
MaxSize: 1, // megabytes
MaxSize: 10, // megabytes
MaxBackups: 3,
MaxAge: 3, // days
LocalTime: true,
Expand Down
16 changes: 11 additions & 5 deletions pkg/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func NewWatcher(

type ScanResult struct {
FilePath string
FileInfo os.FileInfo
ErrorCount int
ErrorPercent float64
LinesRead int
Expand Down Expand Up @@ -97,11 +98,11 @@ func (w *Watcher) Scan() (*ScanResult, error) {
return nil, err
}

re, err := regexp.Compile(w.matchPattern)
regMatch, err := regexp.Compile(w.matchPattern)
if err != nil {
return nil, err
}
ri, err := regexp.Compile(w.ignorePattern)
regIgnore, err := regexp.Compile(w.ignorePattern)
if err != nil {
return nil, err
}
Expand All @@ -111,6 +112,8 @@ func (w *Watcher) Scan() (*ScanResult, error) {
linesRead := 0
bytesRead := w.lastFileSize

counter := make(map[string]int)

for scanner.Scan() {
lines = append(lines, scanner.Text())
line := scanner.Bytes()
Expand All @@ -122,24 +125,26 @@ func (w *Watcher) Scan() (*ScanResult, error) {
linesRead = -linesRead
}
// slog.Debug("Scanning line", "line", string(line), "lineNum", currentLineNum, "linesRead", linesRead)
if w.ignorePattern != "" && ri.Match(line) {
if w.ignorePattern != "" && regIgnore.Match(line) {
continue
}

// anomaly insertion
if w.anomaly {
match := re.FindAllString(string(line), -1)
match := regMatch.FindAllString(string(line), -1)
var exactMatch string
if len(match) >= 1 {
exactMatch = match[0]
}
if exactMatch != "" {
slog.Info("Match found", "line", string(line), "match", exactMatch)
counter[exactMatch]++
}
// pp.Println(counter)

}

if re.Match(line) {
if regMatch.Match(line) {
lineStr := string(line)
if firstLine == "" {
firstLine = lineStr
Expand Down Expand Up @@ -184,6 +189,7 @@ func (w *Watcher) Scan() (*ScanResult, error) {
LastLine: lastLine,
LastDate: SearchDate(lastLine),
FilePath: w.filePath,
FileInfo: fileInfo,
ErrorPercent: matchPercentage,
LinesRead: linesRead,
}, nil
Expand Down

0 comments on commit 49c7b70

Please sign in to comment.