Skip to content

Commit ca12dd0

Browse files
committed
feat: organize code into packages
1 parent 6c43f98 commit ca12dd0

11 files changed

Lines changed: 475 additions & 483 deletions

File tree

README.md

Lines changed: 46 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,71 @@
1-
# System Monitoring
1+
# Appwrite System Monitoring
22

3-
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.
3+
A system monitoring tool for Appwrite servers that tracks CPU, memory, and disk usage with alerting via BetterStack.
44

55
## Features
66

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

14-
## Command Line Usage
13+
## Project Structure
1514

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

18-
```bash
19-
monitoring [flags]
20-
21-
Flags:
22-
-url string
23-
BetterStack webhook URL (required)
24-
-interval int
25-
Check interval in seconds (default: 300)
26-
-cpu-limit float
27-
CPU usage threshold percentage (default: 90)
28-
-memory-limit float
29-
Memory usage threshold percentage (default: 90)
30-
-disk-limit float
31-
Disk usage threshold percentage (default: 85)
32-
-help
33-
Display help information
34-
```
35-
36-
### Examples
37-
38-
```bash
39-
# Basic usage with required URL
40-
monitoring --url=https://betterstack.com/webhook/xyz
41-
42-
# Custom thresholds
43-
monitoring --url=https://betterstack.com/webhook/xyz \
44-
--cpu-limit=95 \
45-
--memory-limit=85 \
46-
--disk-limit=80
47-
48-
# More frequent checks (every minute)
49-
monitoring --url=https://betterstack.com/webhook/xyz --interval=60
5017
```
51-
52-
## Docker Deployment
53-
54-
### Using Docker Run
55-
56-
```bash
57-
docker run -d \
58-
--name monitoring \
59-
--privileged \
60-
--pid=host \
61-
-v /:/host:ro \
62-
ghcr.io/appwrite/monitoring:latest \
63-
monitoring \
64-
--url=https://betterstack.com/webhook/xyz \
65-
--interval=300 \
66-
--cpu-limit=90 \
67-
--memory-limit=90 \
68-
--disk-limit=85
69-
```
70-
71-
### Using Docker Compose
72-
73-
The docker-compose.yml file is configured with default parameters that you can modify as needed:
74-
75-
```bash
76-
docker-compose up -d
18+
monitoring/
19+
├── main.go # Entry point with CLI argument parsing
20+
├── pkg/
21+
│ ├── logger.go # Logging functionality at package level
22+
│ └── monitor/ # Core monitoring functionality
23+
│ ├── monitor.go # Main monitoring struct and Metric model
24+
│ ├── cpu.go # CPU-specific monitoring
25+
│ ├── memory.go # Memory-specific monitoring
26+
│ └── disk.go # Disk-specific monitoring
27+
├── go.mod # Go module definition
28+
└── README.md # Documentation
7729
```
7830

79-
To modify the parameters, edit the command section in docker-compose.yml:
80-
```yaml
81-
command:
82-
- monitoring
83-
- "--url=https://betterstack.com/webhook/xyz"
84-
- "--interval=10"
85-
- "--cpu-limit=90"
86-
- "--memory-limit=80"
87-
- "--disk-limit=85"
88-
```
31+
## Usage
8932

90-
## Building from Source
33+
### Building
9134

92-
1. Clone the repository:
9335
```bash
36+
# Clone the repository
9437
git clone https://github.com/appwrite/monitoring.git
9538
cd monitoring
96-
```
97-
98-
2. Build the binary:
99-
```bash
100-
go build -o monitoring
101-
```
10239

103-
3. Run the monitoring tool:
104-
```bash
105-
monitoring --url=https://betterstack.com/webhook/xyz
40+
# Build the binary
41+
go build -o appwrite-monitor main.go
10642
```
10743

108-
## Development
109-
110-
### Requirements
111-
- Go 1.21 or later
112-
- Docker and Docker Compose (for containerized deployment)
44+
### Running
11345

114-
### Local Development
115-
1. Install dependencies:
11646
```bash
117-
go mod download
47+
# Basic usage
48+
./appwrite-monitor --url="https://betterstack-webhook-url"
49+
50+
# With custom thresholds
51+
./appwrite-monitor \
52+
--url="https://betterstack-webhook-url" \
53+
--interval=60 \
54+
--cpu-limit=80 \
55+
--memory-limit=85 \
56+
--disk-limit=90
11857
```
11958

120-
2. Build and run:
121-
```bash
122-
go build -o monitoring
123-
monitoring --url=https://betterstack.com/webhook/xyz
124-
```
59+
### Command Line Options
12560

126-
### Docker Development
127-
```
128-
docker compose up -d
129-
```
61+
- `--url`: BetterStack webhook URL (required)
62+
- `--interval`: Check interval in seconds (default: 300)
63+
- `--cpu-limit`: CPU usage threshold percentage (default: 90)
64+
- `--memory-limit`: Memory usage threshold percentage (default: 90)
65+
- `--disk-limit`: Disk usage threshold percentage (default: 85)
66+
67+
## How It Works
13068

131-
## License
69+
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.
13270

133-
MIT License - see the [LICENSE](LICENSE) file for details
71+
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.

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ module github.com/appwrite/monitoring
22

33
go 1.19
44

5-
require github.com/shirou/gopsutil/v3 v3.24.1
5+
require github.com/shirou/gopsutil/v3 v3.23.7
66

77
require (
88
github.com/go-ole/go-ole v1.2.6 // indirect
99
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
1010
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
1111
github.com/shoenig/go-m1cpu v0.1.6 // indirect
12-
github.com/tklauser/go-sysconf v0.3.12 // indirect
13-
github.com/tklauser/numcpus v0.6.1 // indirect
12+
github.com/tklauser/go-sysconf v0.3.11 // indirect
13+
github.com/tklauser/numcpus v0.6.0 // indirect
1414
github.com/yusufpapurcu/wmi v1.2.3 // indirect
15-
golang.org/x/sys v0.16.0 // indirect
15+
golang.org/x/sys v0.10.0 // indirect
1616
)

go.sum

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
44
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
55
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
66
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
7+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
78
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
8-
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
9-
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
109
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
1110
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
1211
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1312
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1413
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
1514
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
16-
github.com/shirou/gopsutil/v3 v3.24.1 h1:R3t6ondCEvmARp3wxODhXMTLC/klMa87h2PHUw5m7QI=
17-
github.com/shirou/gopsutil/v3 v3.24.1/go.mod h1:UU7a2MSBQa+kW1uuDq8DeEBS8kmrnQwsv2b5O513rwU=
15+
github.com/shirou/gopsutil/v3 v3.23.7 h1:C+fHO8hfIppoJ1WdsVm1RoI0RwXoNdfTK7yWXV0wVj4=
16+
github.com/shirou/gopsutil/v3 v3.23.7/go.mod h1:c4gnmoRC0hQuaLqvxnx1//VXQ0Ms/X9UnJF8pddY5z4=
1817
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
1918
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
2019
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
@@ -26,18 +25,17 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
2625
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
2726
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
2827
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
29-
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
30-
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
31-
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
32-
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
28+
github.com/tklauser/go-sysconf v0.3.11 h1:89WgdJhk5SNwJfu+GKyYveZ4IaJ7xAkecBo+KdJV0CM=
29+
github.com/tklauser/go-sysconf v0.3.11/go.mod h1:GqXfhXY3kiPa0nAXPDIQIWzJbMCB7AmcWpGR8lSZfqI=
30+
github.com/tklauser/numcpus v0.6.0 h1:kebhY2Qt+3U6RNK7UqpYNA+tJ23IBEGKkB7JQBfDYms=
31+
github.com/tklauser/numcpus v0.6.0/go.mod h1:FEZLMke0lhOUG6w2JadTzp0a+Nl8PF/GFkQ5UVIcaL4=
3332
github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw=
3433
github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
3534
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3635
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
37-
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
38-
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
39-
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
40-
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
36+
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
37+
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
38+
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4139
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
4240
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4341
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)