kiss server monitoring tool with email alerts
go monitoring

feat: init servmon

+625
+15
.github/workflows/build.yml
··· 1 + name: Build 2 + on: 3 + push: 4 + branches: 5 + - main 6 + pull_request: 7 + jobs: 8 + build: 9 + runs-on: ubuntu-latest 10 + steps: 11 + - uses: actions/checkout@v4 12 + - uses: actions/setup-go@v5 13 + with: 14 + go-version: "stable" 15 + - run: make build
+15
.github/workflows/test.yml
··· 1 + name: Test 2 + on: 3 + push: 4 + branches: 5 + - main 6 + pull_request: 7 + jobs: 8 + test: 9 + runs-on: ubuntu-latest 10 + steps: 11 + - uses: actions/checkout@v4 12 + - uses: actions/setup-go@v5 13 + with: 14 + go-version: "stable" 15 + - run: go test -cover ./...
+27
.gitignore
··· 1 + # If you prefer the allow list template instead of the deny list, see community template: 2 + # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore 3 + # 4 + # Binaries for programs and plugins 5 + *.exe 6 + *.exe~ 7 + *.dll 8 + *.so 9 + *.dylib 10 + servmon-bin 11 + 12 + # Test binary, built with `go test -c` 13 + *.test 14 + 15 + # Output of the go coverage tool, specifically when used with LiteIDE 16 + *.out 17 + 18 + # Dependency directories (remove the comment below to include it) 19 + # vendor/ 20 + 21 + # Go workspace file 22 + go.work 23 + go.work.sum 24 + 25 + # env file 26 + .env 27 + .servmon.yaml
+21
LICENSE
··· 1 + MIT License 2 + 3 + Copyright (c) 2025 Julien Robert 4 + 5 + Permission is hereby granted, free of charge, to any person obtaining a copy 6 + of this software and associated documentation files (the "Software"), to deal 7 + in the Software without restriction, including without limitation the rights 8 + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 + copies of the Software, and to permit persons to whom the Software is 10 + furnished to do so, subject to the following conditions: 11 + 12 + The above copyright notice and this permission notice shall be included in all 13 + copies or substantial portions of the Software. 14 + 15 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 + SOFTWARE.
+9
Makefile
··· 1 + #!/usr/bin/make -f 2 + 3 + PWD=$(shell pwd) 4 + 5 + build: 6 + go build -o servmon-bin . 7 + 8 + install: 9 + go install .
+23
README.md
··· 1 + # Servmond 2 + 3 + KISS server monitoring tool with email alerts. 4 + 5 + Monitors: 6 + 7 + - [x] CPU 8 + - [x] Memory 9 + - [x] HTTP Health check 10 + - [ ] Disk 11 + - [ ] Docker 12 + 13 + ## Installation 14 + 15 + ```bash 16 + go install github.com/julienrbrt/servmon@latest 17 + ``` 18 + 19 + ## How to use 20 + 21 + ```bash 22 + servmon --help 23 + ```
+21
config.example.yaml
··· 1 + alert_thresholds: 2 + cpu: 3 + threshold: 90 4 + duration: 5m0s 5 + cooldown: 30m0s 6 + memory: 7 + threshold: 80 8 + cooldown: 30m0s 9 + http: 10 + url: http://localhost:8080/health 11 + timeout: 5s 12 + sample_rate: 10 13 + failure_threshold: 20 14 + check_interval: 1m0s 15 + cooldown: 15m0s 16 + email: 17 + smtp_server: smtp.example.com 18 + from: alerts@example.com 19 + to: admin@example.com 20 + username: alertuser 21 + password: alertpassword
+109
config.go
··· 1 + package main 2 + 3 + import ( 4 + "fmt" 5 + "os" 6 + "time" 7 + 8 + "gopkg.in/yaml.v3" 9 + ) 10 + 11 + // Config is a struct that holds the configuration for the monitoring service. 12 + type Config struct { 13 + AlertThresholds Thresholds `yaml:"alert_thresholds"` 14 + Email Email `yaml:"email"` 15 + } 16 + 17 + type Thresholds struct { 18 + CPU CPU `yaml:"cpu"` 19 + Memory Memory `yaml:"memory"` 20 + HTTP HTTP `yaml:"http"` 21 + } 22 + 23 + type CPU struct { 24 + Threshold float64 `yaml:"threshold"` 25 + Duration time.Duration `yaml:"duration"` 26 + Cooldown time.Duration `yaml:"cooldown"` 27 + } 28 + 29 + type Memory struct { 30 + Threshold float64 `yaml:"threshold"` 31 + Cooldown time.Duration `yaml:"cooldown"` 32 + } 33 + 34 + type HTTP struct { 35 + URL string `yaml:"url"` 36 + Timeout time.Duration `yaml:"timeout"` 37 + SampleRate int `yaml:"sample_rate"` 38 + FailureThreshold float64 `yaml:"failure_threshold"` 39 + CheckInterval time.Duration `yaml:"check_interval"` 40 + Cooldown time.Duration `yaml:"cooldown"` 41 + } 42 + 43 + type Email struct { 44 + SMTPServer string `yaml:"smtp_server"` 45 + From string `yaml:"from"` 46 + To string `yaml:"to"` 47 + Username string `yaml:"username"` 48 + Password string `yaml:"password"` 49 + } 50 + 51 + func (c *Config) Save(path string) error { 52 + out, err := yaml.Marshal(c) 53 + if err != nil { 54 + return fmt.Errorf("failed to marshal config: %w", err) 55 + } 56 + 57 + if err := os.WriteFile(path, out, 0644); err != nil { 58 + return fmt.Errorf("error generating sample config: %w", err) 59 + } 60 + 61 + return nil 62 + } 63 + 64 + // defaultConfig returns a default configuration for the monitoring service. 65 + func defaultConfig() *Config { 66 + return &Config{ 67 + AlertThresholds: Thresholds{ 68 + CPU: CPU{ 69 + Threshold: 90, 70 + Duration: 5 * time.Minute, 71 + Cooldown: 30 * time.Minute, 72 + }, 73 + Memory: Memory{ 74 + Threshold: 80, 75 + Cooldown: 30 * time.Minute, 76 + }, 77 + HTTP: HTTP{ 78 + URL: "http://localhost:8080/health", 79 + Timeout: 5 * time.Second, 80 + SampleRate: 10, 81 + FailureThreshold: 20, 82 + CheckInterval: 1 * time.Minute, 83 + Cooldown: 15 * time.Minute, 84 + }, 85 + }, 86 + Email: Email{ 87 + SMTPServer: "smtp.example.com", 88 + From: "alerts@example.com", 89 + To: "admin@example.com", 90 + Username: "alertuser", 91 + Password: "alertpassword", 92 + }, 93 + } 94 + } 95 + 96 + // loadConfig loads a configuration from a file. 97 + func loadConfig(path string) (*Config, error) { 98 + data, err := os.ReadFile(path) 99 + if err != nil { 100 + return nil, fmt.Errorf("error reading config file: %w", err) 101 + } 102 + 103 + var cfg Config 104 + if err := yaml.Unmarshal(data, &cfg); err != nil { 105 + return nil, fmt.Errorf("error unmarshaling config: %w", err) 106 + } 107 + 108 + return &cfg, nil 109 + }
+42
email.go
··· 1 + package main 2 + 3 + import ( 4 + "fmt" 5 + "log" 6 + 7 + "github.com/wneessen/go-mail" 8 + ) 9 + 10 + // sendEmail sends an alert email using the configuration 11 + func sendEmail(subject, body string, cfg *Config) error { 12 + msg := mail.NewMsg() 13 + if err := msg.From(cfg.Email.From); err != nil { 14 + return fmt.Errorf("failed to set FROM address: %w", err) 15 + } 16 + if err := msg.To(cfg.Email.To); err != nil { 17 + return fmt.Errorf("failed to set TO address: %w", err) 18 + } 19 + 20 + msg.Subject(fmt.Sprintf("[ServMon Alert] %s", subject)) 21 + msg.SetBodyString(mail.TypeTextPlain, body) 22 + 23 + // Create SMTP client with configuration 24 + client, err := mail.NewClient( 25 + cfg.Email.SMTPServer, 26 + mail.WithSMTPAuth(mail.SMTPAuthPlain), 27 + mail.WithTLSPortPolicy(mail.TLSMandatory), 28 + mail.WithUsername(cfg.Email.Username), 29 + mail.WithPassword(cfg.Email.Password), 30 + ) 31 + if err != nil { 32 + return fmt.Errorf("failed to create SMTP client: %w", err) 33 + } 34 + 35 + // Send the email 36 + if err := client.DialAndSend(msg); err != nil { 37 + return fmt.Errorf("failed to send email: %w", err) 38 + } 39 + 40 + log.Printf("Email alert sent successfully: %s", subject) 41 + return nil 42 + }
+25
go.mod
··· 1 + module github.com/julienrbrt/servmon 2 + 3 + go 1.23.4 4 + 5 + require ( 6 + github.com/shirou/gopsutil/v4 v4.24.12 7 + github.com/spf13/cobra v1.8.1 8 + github.com/wneessen/go-mail v0.6.1 9 + gopkg.in/yaml.v3 v3.0.1 10 + ) 11 + 12 + require ( 13 + github.com/ebitengine/purego v0.8.1 // indirect 14 + github.com/go-ole/go-ole v1.2.6 // indirect 15 + github.com/inconshreveable/mousetrap v1.1.0 // indirect 16 + github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect 17 + github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect 18 + github.com/spf13/pflag v1.0.5 // indirect 19 + github.com/tklauser/go-sysconf v0.3.14 // indirect 20 + github.com/tklauser/numcpus v0.8.0 // indirect 21 + github.com/yusufpapurcu/wmi v1.2.4 // indirect 22 + golang.org/x/crypto v0.32.0 // indirect 23 + golang.org/x/sys v0.29.0 // indirect 24 + golang.org/x/text v0.21.0 // indirect 25 + )
+108
go.sum
··· 1 + github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= 2 + github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 3 + github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 + github.com/ebitengine/purego v0.8.1 h1:sdRKd6plj7KYW33EH5As6YKfe8m9zbN9JMrOjNVF/BE= 5 + github.com/ebitengine/purego v0.8.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= 6 + github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= 7 + github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= 8 + github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 9 + github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= 10 + github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 11 + github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= 12 + github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= 13 + github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= 14 + github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= 15 + github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 16 + github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 17 + github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw= 18 + github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= 19 + github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 20 + github.com/shirou/gopsutil/v4 v4.24.12 h1:qvePBOk20e0IKA1QXrIIU+jmk+zEiYVVx06WjBRlZo4= 21 + github.com/shirou/gopsutil/v4 v4.24.12/go.mod h1:DCtMPAad2XceTeIAbGyVfycbYQNBGk2P8cvDi7/VN9o= 22 + github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= 23 + github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= 24 + github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= 25 + github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= 26 + github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= 27 + github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 28 + github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZb78yU= 29 + github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY= 30 + github.com/tklauser/numcpus v0.8.0 h1:Mx4Wwe/FjZLeQsK/6kt2EOepwwSl7SmJrK5bV/dXYgY= 31 + github.com/tklauser/numcpus v0.8.0/go.mod h1:ZJZlAY+dmR4eut8epnzf0u/VwodKmryxR8txiloSqBE= 32 + github.com/wneessen/go-mail v0.6.1 h1:cDGqlGuEEhdILRe53VFzmM9WBk8Xh/QMvbO0oxrNJB4= 33 + github.com/wneessen/go-mail v0.6.1/go.mod h1:G702XlFhzHV0Z4w9j2VsH5K9dJDvj0hx+yOOp1oX9vc= 34 + github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= 35 + github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= 36 + github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= 37 + golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 38 + golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 39 + golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= 40 + golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= 41 + golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= 42 + golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc= 43 + golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc= 44 + golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= 45 + golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= 46 + golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= 47 + golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= 48 + golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= 49 + golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 50 + golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= 51 + golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= 52 + golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= 53 + golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= 54 + golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= 55 + golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= 56 + golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= 57 + golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 58 + golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 59 + golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 60 + golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= 61 + golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= 62 + golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= 63 + golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= 64 + golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 65 + golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 66 + golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 67 + golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 68 + golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 69 + golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 70 + golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 71 + golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 72 + golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 73 + golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 74 + golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 75 + golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 76 + golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= 77 + golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 78 + golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE= 79 + golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 80 + golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= 81 + golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= 82 + golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= 83 + golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= 84 + golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= 85 + golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= 86 + golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek= 87 + golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 88 + golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 89 + golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 90 + golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= 91 + golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= 92 + golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= 93 + golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= 94 + golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= 95 + golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= 96 + golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= 97 + golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 98 + golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 99 + golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= 100 + golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= 101 + golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= 102 + golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= 103 + golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 104 + golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 105 + gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 106 + gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 107 + gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 108 + gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+66
main.go
··· 1 + package main 2 + 3 + import ( 4 + "fmt" 5 + "os" 6 + "path" 7 + 8 + "github.com/spf13/cobra" 9 + ) 10 + 11 + var ( 12 + version = "1.0.0" 13 + flagConfig = "config" 14 + cfgFile string 15 + ) 16 + 17 + func main() { 18 + homeDir, err := os.UserHomeDir() 19 + if err != nil { 20 + fmt.Fprintf(os.Stderr, "error getting user home directory: %v", err) 21 + os.Exit(1) 22 + } 23 + 24 + rootCmd := &cobra.Command{ 25 + Use: "servmon", 26 + Short: "Server Monitoring Tool with TUI and Alerts", 27 + Version: version, 28 + RunE: func(cmd *cobra.Command, args []string) error { 29 + cfgPath, err := cmd.Flags().GetString(flagConfig) 30 + if err != nil { 31 + return fmt.Errorf("error getting flag %s: %v", flagConfig, err) 32 + } 33 + 34 + if _, err := os.Stat(cfgPath); os.IsNotExist(err) { 35 + cfg := defaultConfig() 36 + if err := cfg.Save(cfgFile); err != nil { 37 + return err 38 + } 39 + 40 + cmd.Println("Configuration file generated at", cfgFile) 41 + return nil 42 + } else if err != nil { 43 + return fmt.Errorf("error checking config file: %v", err) 44 + } 45 + 46 + cfg, err := loadConfig(cfgPath) 47 + if err != nil { 48 + return err 49 + } 50 + 51 + go monitorCPU(cfg) 52 + go monitorMemory(cfg) 53 + go monitorHTTP(cfg) 54 + 55 + select {} // keep alive 56 + }, 57 + } 58 + 59 + rootCmd.CompletionOptions.DisableDefaultCmd = true 60 + rootCmd.PersistentFlags().StringVar(&cfgFile, flagConfig, path.Join(homeDir, ".servmon.yaml"), "config file") 61 + 62 + if err := rootCmd.Execute(); err != nil { 63 + fmt.Fprint(os.Stderr, err) 64 + os.Exit(1) 65 + } 66 + }
+144
monitor.go
··· 1 + package main 2 + 3 + import ( 4 + "context" 5 + "fmt" 6 + "log" 7 + "net/http" 8 + "time" 9 + 10 + "github.com/shirou/gopsutil/v4/cpu" 11 + "github.com/shirou/gopsutil/v4/mem" 12 + ) 13 + 14 + func monitorCPU(cfg *Config) { 15 + log.Printf("Monitoring CPU usage with threshold %.2f%% and cooldown %v", cfg.AlertThresholds.CPU.Threshold, cfg.AlertThresholds.CPU.Cooldown) 16 + 17 + alertCooldown := time.NewTimer(cfg.AlertThresholds.CPU.Cooldown) 18 + for { 19 + percent, err := cpu.Percent(time.Duration(1)*time.Second, false) 20 + if err != nil { 21 + log.Printf("Error getting CPU usage: %v", err) 22 + time.Sleep(1 * time.Second) 23 + continue 24 + } 25 + 26 + // Average CPU usage across all cores 27 + var total float64 28 + for _, p := range percent { 29 + total += p 30 + } 31 + 32 + avg := total / float64(len(percent)) 33 + 34 + if avg > cfg.AlertThresholds.CPU.Threshold { 35 + // Check if we're within the cooldown period 36 + select { 37 + case <-alertCooldown.C: 38 + // Cooldown expired, check again 39 + alertCooldown.Reset(cfg.AlertThresholds.CPU.Cooldown) 40 + default: 41 + // Within cooldown, skip alert 42 + time.Sleep(1 * time.Second) 43 + continue 44 + } 45 + 46 + err := sendEmail(fmt.Sprintf("CPU Usage Alert: %.2f%%", avg), 47 + fmt.Sprintf("CPU usage of %.2f%% has exceeded the threshold of %.2f%%", avg, cfg.AlertThresholds.CPU.Threshold), cfg) 48 + if err != nil { 49 + log.Printf("Error sending email: %v", err) 50 + } 51 + } 52 + 53 + time.Sleep(time.Duration(1) * time.Second) 54 + } 55 + } 56 + 57 + func monitorMemory(cfg *Config) { 58 + log.Printf("Monitoring memory usage with threshold %.2f%% and cooldown %v", cfg.AlertThresholds.Memory.Threshold, cfg.AlertThresholds.Memory.Cooldown) 59 + 60 + alertCooldown := time.NewTimer(cfg.AlertThresholds.Memory.Cooldown) 61 + for { 62 + vm, err := mem.VirtualMemory() 63 + if err != nil { 64 + log.Printf("Error getting memory usage: %v", err) 65 + time.Sleep(1 * time.Second) 66 + continue 67 + } 68 + 69 + usedPercent := vm.UsedPercent 70 + 71 + if usedPercent > cfg.AlertThresholds.Memory.Threshold { 72 + // Check if we're within the cooldown period 73 + select { 74 + case <-alertCooldown.C: 75 + // Cooldown expired, check again 76 + alertCooldown.Reset(cfg.AlertThresholds.Memory.Cooldown) 77 + default: 78 + // Within cooldown, skip alert 79 + time.Sleep(1 * time.Second) 80 + continue 81 + } 82 + 83 + err := sendEmail(fmt.Sprintf("Memory Usage Alert: %.2f%%", usedPercent), 84 + fmt.Sprintf("Memory usage of %.2f%% has exceeded the threshold of %.2f%%", usedPercent, cfg.AlertThresholds.Memory.Threshold), cfg) 85 + if err != nil { 86 + log.Printf("Error sending email: %v", err) 87 + } 88 + } 89 + 90 + time.Sleep(time.Duration(1) * time.Second) 91 + } 92 + } 93 + 94 + func monitorHTTP(cfg *Config) { 95 + log.Printf("Monitoring HTTP checks (%s) with threshold %.2f%% and cooldown %v", cfg.AlertThresholds.HTTP.URL, cfg.AlertThresholds.HTTP.FailureThreshold, cfg.AlertThresholds.HTTP.Cooldown) 96 + 97 + alertCooldown := time.NewTimer(cfg.AlertThresholds.HTTP.Cooldown) 98 + client := &http.Client{ 99 + Timeout: cfg.AlertThresholds.HTTP.Timeout, 100 + } 101 + 102 + for { 103 + // Wait for check interval 104 + time.Sleep(cfg.AlertThresholds.HTTP.CheckInterval) 105 + 106 + // Perform HTTP checks 107 + failureCount := 0 108 + for i := 0; i < cfg.AlertThresholds.HTTP.SampleRate; i++ { 109 + req, err := http.NewRequest("GET", cfg.AlertThresholds.HTTP.URL, nil) 110 + if err != nil { 111 + failureCount++ 112 + continue 113 + } 114 + 115 + ctx, cancel := context.WithTimeout(context.Background(), cfg.AlertThresholds.HTTP.Timeout) 116 + defer cancel() 117 + 118 + resp, err := client.Do(req.WithContext(ctx)) 119 + if err != nil || resp.StatusCode >= 400 { 120 + failureCount++ 121 + } 122 + } 123 + 124 + // Calculate failure rate 125 + failureRate := (float64(failureCount) / float64(cfg.AlertThresholds.HTTP.SampleRate)) * 100 126 + if failureRate > cfg.AlertThresholds.HTTP.FailureThreshold { 127 + // Check if we're within the cooldown period 128 + select { 129 + case <-alertCooldown.C: 130 + // Cooldown expired, check again 131 + alertCooldown.Reset(cfg.AlertThresholds.HTTP.Cooldown) 132 + default: 133 + // Within cooldown, skip alert 134 + continue 135 + } 136 + 137 + err := sendEmail(fmt.Sprintf("HTTP Failure Alert: %.2f%%", failureRate), 138 + fmt.Sprintf("HTTP failure rate of %.2f%% has exceeded the threshold of %.2f%%", failureRate, cfg.AlertThresholds.HTTP.FailureThreshold), cfg) 139 + if err != nil { 140 + log.Printf("Error sending email: %v", err) 141 + } 142 + } 143 + } 144 + }