|
| 1 | +package notifymem |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "context" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "runtime" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +type memory struct { |
| 14 | + total int |
| 15 | + avail int |
| 16 | +} |
| 17 | + |
| 18 | +type MonitorOptions struct { |
| 19 | + DebugFunc func(string) |
| 20 | + Interval time.Duration |
| 21 | + ResendDelay time.Duration |
| 22 | + Threshold int |
| 23 | +} |
| 24 | + |
| 25 | +type Monitor struct { |
| 26 | + debugFunc func(string) |
| 27 | + interval time.Duration |
| 28 | + lastSentAt time.Time |
| 29 | + notifier Notifier |
| 30 | + resendDelay time.Duration |
| 31 | + threshold int |
| 32 | +} |
| 33 | + |
| 34 | +func NewMonitor(notifier Notifier, opts MonitorOptions) (*Monitor, error) { |
| 35 | + if runtime.GOOS != "linux" { |
| 36 | + return nil, errors.New("notifymem only supports Linux") |
| 37 | + } |
| 38 | + |
| 39 | + m := &Monitor{ |
| 40 | + debugFunc: opts.DebugFunc, |
| 41 | + interval: opts.Interval, |
| 42 | + notifier: notifier, |
| 43 | + resendDelay: opts.ResendDelay, |
| 44 | + threshold: opts.Threshold, |
| 45 | + } |
| 46 | + |
| 47 | + if m.interval < 100*time.Millisecond { |
| 48 | + return nil, errors.New("interval must be at least 100ms") |
| 49 | + } |
| 50 | + |
| 51 | + if m.notifier == nil { |
| 52 | + return nil, errors.New("notifier is required") |
| 53 | + } |
| 54 | + |
| 55 | + if m.resendDelay < 5*time.Second { |
| 56 | + return nil, errors.New("resend delay must be at least 5s") |
| 57 | + } |
| 58 | + |
| 59 | + if m.threshold < 0 || m.threshold > 100 { |
| 60 | + return nil, errors.New("threshold must be between 0 and 100") |
| 61 | + } |
| 62 | + |
| 63 | + return m, nil |
| 64 | +} |
| 65 | + |
| 66 | +func (m *Monitor) debug(msg string) { |
| 67 | + if m.debugFunc != nil { |
| 68 | + m.debugFunc(msg) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func (m *Monitor) isThresholdReached() (bool, int, error) { |
| 73 | + mem, err := readMemory() |
| 74 | + if err != nil { |
| 75 | + return false, 0, err |
| 76 | + } |
| 77 | + |
| 78 | + usage, err := memoryUsage(mem) |
| 79 | + if err != nil { |
| 80 | + return false, 0, err |
| 81 | + } |
| 82 | + |
| 83 | + if usage >= m.threshold { |
| 84 | + return true, usage, nil |
| 85 | + } |
| 86 | + |
| 87 | + return false, usage, nil |
| 88 | +} |
| 89 | + |
| 90 | +func (m *Monitor) notify(usage int) error { |
| 91 | + if time.Since(m.lastSentAt) < m.resendDelay { |
| 92 | + m.debug("resend delay not reached") |
| 93 | + return nil |
| 94 | + } |
| 95 | + |
| 96 | + m.debug("sending notification") |
| 97 | + m.lastSentAt = time.Now() |
| 98 | + return m.notifier.Notify( |
| 99 | + "Memory Usage Threshold Reached [notifymem]", |
| 100 | + fmt.Sprintf("Memory usage at %d%%", usage), |
| 101 | + ) |
| 102 | +} |
| 103 | + |
| 104 | +func (m *Monitor) Run(ctx context.Context) error { |
| 105 | + m.debug("staring monitor") |
| 106 | + for { |
| 107 | + select { |
| 108 | + case <-ctx.Done(): |
| 109 | + m.debug("stopping monitor") |
| 110 | + return ctx.Err() |
| 111 | + |
| 112 | + case <-time.After(m.interval): |
| 113 | + reached, usage, err := m.isThresholdReached() |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + m.debug(fmt.Sprintf("memory usage: %d%%", usage)) |
| 118 | + |
| 119 | + if reached { |
| 120 | + m.debug(fmt.Sprintf("threshold reached: %d%%", usage)) |
| 121 | + if err := m.notify(usage); err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func memoryUsage(m memory) (int, error) { |
| 131 | + if m.total == 0 { |
| 132 | + return 0, errors.New("total memory is 0") |
| 133 | + } |
| 134 | + |
| 135 | + return int((float64(m.total-m.avail) / float64(m.total)) * 100), nil |
| 136 | +} |
| 137 | + |
| 138 | +func readMemory() (memory, error) { |
| 139 | + m := memory{} |
| 140 | + f, err := os.Open("/proc/meminfo") |
| 141 | + if err != nil { |
| 142 | + return m, err |
| 143 | + } |
| 144 | + defer f.Close() |
| 145 | + |
| 146 | + s := bufio.NewScanner(f) |
| 147 | + for s.Scan() { |
| 148 | + var k string |
| 149 | + var v int |
| 150 | + _, err := fmt.Sscanf(s.Text(), "%s %d", &k, &v) |
| 151 | + if err != nil { |
| 152 | + continue |
| 153 | + } |
| 154 | + switch k { |
| 155 | + case "MemTotal:": |
| 156 | + m.total = v |
| 157 | + case "MemAvailable:": |
| 158 | + m.avail = v |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return m, nil |
| 163 | +} |
0 commit comments