Skip to content

Commit

Permalink
minor fixes; docs
Browse files Browse the repository at this point in the history
  • Loading branch information
shayanderson committed Jul 5, 2024
1 parent 6efd0c5 commit 64fbac9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A simple tool to notify you when your system memory usage is too high.
Features:

- uses `notify-send` to send notifications when memory usage reaches a certain threshold
- major Linux distributions are supported
- built for Linux (tested on Ubuntu 22.04 X11)
- configurable threshold, check memory usage interval and resend notification delay

Example desktop notification:
Expand All @@ -14,7 +14,7 @@ Example desktop notification:

## Usage

Example usage that monitors memory usage by checking every 2 seconds, sends a notification when memory usage is above 80% and will not send another notification until after 10 seconds.
Example usage that monitors memory usage by checking every 2 seconds, sends a notification when memory usage is above 80% and will not resend another notification until after 10 seconds.

```bash
notifymem -threshold 80 -delay 10 -interval 2
Expand Down
11 changes: 7 additions & 4 deletions monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func NewMonitor(notifier Notifier, opts MonitorOptions) (*Monitor, error) {
threshold: opts.Threshold,
}

if m.interval < 100*time.Millisecond {
return nil, errors.New("interval must be at least 100ms")
if m.interval < 1*time.Second {
return nil, errors.New("interval must be at least 1s")
}

if m.notifier == nil {
Expand All @@ -57,8 +57,8 @@ func NewMonitor(notifier Notifier, opts MonitorOptions) (*Monitor, error) {
return nil, errors.New("resend delay must be at least 5s")
}

if m.threshold < 0 || m.threshold > 100 {
return nil, errors.New("threshold must be between 0 and 100")
if m.threshold < 1 || m.threshold > 100 {
return nil, errors.New("threshold must be between 1 and 100")
}

return m, nil
Expand Down Expand Up @@ -157,6 +157,9 @@ func memoryUsage(m memory) (int, error) {
if m.total == 0 {
return 0, errors.New("total memory is 0")
}
if m.avail == 0 {
return 0, errors.New("available memory is 0")
}

return int((float64(m.total-m.avail) / float64(m.total)) * 100), nil
}
Expand Down

0 comments on commit 64fbac9

Please sign in to comment.