Skip to content

feat: add ema for more smoother metrics calculations #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Go Tests

on:
push:
branches: [ main, master ]
paths:
- '**.go'
- 'go.mod'
- 'go.sum'
pull_request:
branches: [ main, master ]
paths:
- '**.go'
- 'go.mod'
- 'go.sum'

jobs:
test:
name: Run Tests
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'
check-latest: true

- name: Get dependencies
run: go mod tidy

- name: Run tests
run: go test -v -coverprofile=coverage.out ./...

- name: Display coverage
run: go tool cover -func=coverage.out

- name: Upload coverage report
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: coverage.out
if-no-files-found: error
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.env
.env
coverage.out
154 changes: 46 additions & 108 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,133 +1,71 @@
# System Monitoring
# Appwrite System Monitoring

A lightweight system monitoring tool that tracks CPU, memory, and disk usage across your infrastructure. When resource usage exceeds defined thresholds, it creates incidents in BetterStack.
A system monitoring tool for Appwrite servers that tracks CPU, memory, and disk usage with alerting via BetterStack.

## Features

- CPU usage monitoring
- Memory usage monitoring
- Disk usage monitoring (root and mounted volumes)
- Automatic incident creation and resolution
- Configurable thresholds via CLI
- Docker-based deployment
- Monitors CPU usage with configurable thresholds
- Monitors memory usage with configurable thresholds
- Monitors disk usage (root and mounted volumes) with configurable thresholds
- Uses Exponential Moving Average (EMA) to smooth out short-term spikes
- Sends alerts to BetterStack when thresholds are exceeded

## Command Line Usage
## Project Structure

The monitoring tool is configured through command-line flags:
The project follows a simplified package structure:

```bash
monitoring [flags]

Flags:
-url string
BetterStack webhook URL (required)
-interval int
Check interval in seconds (default: 300)
-cpu-limit float
CPU usage threshold percentage (default: 90)
-memory-limit float
Memory usage threshold percentage (default: 90)
-disk-limit float
Disk usage threshold percentage (default: 85)
-help
Display help information
```

### Examples

```bash
# Basic usage with required URL
monitoring --url=https://betterstack.com/webhook/xyz

# Custom thresholds
monitoring --url=https://betterstack.com/webhook/xyz \
--cpu-limit=95 \
--memory-limit=85 \
--disk-limit=80

# More frequent checks (every minute)
monitoring --url=https://betterstack.com/webhook/xyz --interval=60
```

## Docker Deployment

### Using Docker Run

```bash
docker run -d \
--name monitoring \
--privileged \
--pid=host \
-v /:/host:ro \
ghcr.io/appwrite/monitoring:latest \
monitoring \
--url=https://betterstack.com/webhook/xyz \
--interval=300 \
--cpu-limit=90 \
--memory-limit=90 \
--disk-limit=85
```

### Using Docker Compose

The docker-compose.yml file is configured with default parameters that you can modify as needed:

```bash
docker-compose up -d
monitoring/
├── main.go # Entry point with CLI argument parsing
├── pkg/
│ ├── logger.go # Logging functionality at package level
│ └── monitor/ # Core monitoring functionality
│ ├── monitor.go # Main monitoring struct and Metric model
│ ├── cpu.go # CPU-specific monitoring
│ ├── memory.go # Memory-specific monitoring
│ └── disk.go # Disk-specific monitoring
├── go.mod # Go module definition
└── README.md # Documentation
```

To modify the parameters, edit the command section in docker-compose.yml:
```yaml
command:
- monitoring
- "--url=https://betterstack.com/webhook/xyz"
- "--interval=10"
- "--cpu-limit=90"
- "--memory-limit=80"
- "--disk-limit=85"
```
## Usage

## Building from Source
### Building

1. Clone the repository:
```bash
# Clone the repository
git clone https://github.com/appwrite/monitoring.git
cd monitoring
```

2. Build the binary:
```bash
go build -o monitoring
```

3. Run the monitoring tool:
```bash
monitoring --url=https://betterstack.com/webhook/xyz
# Build the binary
go build -o appwrite-monitor main.go
```

## Development

### Requirements
- Go 1.21 or later
- Docker and Docker Compose (for containerized deployment)
### Running

### Local Development
1. Install dependencies:
```bash
go mod download
# Basic usage
./appwrite-monitor --url="https://betterstack-webhook-url"

# With custom thresholds
./appwrite-monitor \
--url="https://betterstack-webhook-url" \
--interval=60 \
--cpu-limit=80 \
--memory-limit=85 \
--disk-limit=90
```

2. Build and run:
```bash
go build -o monitoring
monitoring --url=https://betterstack.com/webhook/xyz
```
### Command Line Options

### Docker Development
```
docker compose up -d
```
- `--url`: BetterStack webhook URL (required)
- `--interval`: Check interval in seconds (default: 300)
- `--cpu-limit`: CPU usage threshold percentage (default: 90)
- `--memory-limit`: Memory usage threshold percentage (default: 90)
- `--disk-limit`: Disk usage threshold percentage (default: 85)

## How It Works

## License
The monitoring tool uses Exponential Moving Average (EMA) to track resource usage over time, which helps prevent false alerts from momentary spikes. When the EMA of a resource exceeds the configured threshold, an alert is sent to BetterStack.

MIT License - see the [LICENSE](LICENSE) file for details
The EMA smoothing factor is automatically calculated based on the check interval to provide roughly 5 minutes of smoothing. This means that sudden spikes will have less impact on the reported values, while sustained high usage will still trigger alerts.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ module github.com/appwrite/monitoring

go 1.19

require github.com/shirou/gopsutil/v3 v3.24.1
require github.com/shirou/gopsutil/v3 v3.23.7

require (
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/sys v0.10.0 // indirect
)
22 changes: 10 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/shirou/gopsutil/v3 v3.24.1 h1:R3t6ondCEvmARp3wxODhXMTLC/klMa87h2PHUw5m7QI=
github.com/shirou/gopsutil/v3 v3.24.1/go.mod h1:UU7a2MSBQa+kW1uuDq8DeEBS8kmrnQwsv2b5O513rwU=
github.com/shirou/gopsutil/v3 v3.23.7 h1:C+fHO8hfIppoJ1WdsVm1RoI0RwXoNdfTK7yWXV0wVj4=
github.com/shirou/gopsutil/v3 v3.23.7/go.mod h1:c4gnmoRC0hQuaLqvxnx1//VXQ0Ms/X9UnJF8pddY5z4=
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
Expand All @@ -26,18 +25,17 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
github.com/tklauser/go-sysconf v0.3.11 h1:89WgdJhk5SNwJfu+GKyYveZ4IaJ7xAkecBo+KdJV0CM=
github.com/tklauser/go-sysconf v0.3.11/go.mod h1:GqXfhXY3kiPa0nAXPDIQIWzJbMCB7AmcWpGR8lSZfqI=
github.com/tklauser/numcpus v0.6.0 h1:kebhY2Qt+3U6RNK7UqpYNA+tJ23IBEGKkB7JQBfDYms=
github.com/tklauser/numcpus v0.6.0/go.mod h1:FEZLMke0lhOUG6w2JadTzp0a+Nl8PF/GFkQ5UVIcaL4=
github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw=
github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
Loading