Skip to content

Commit 889e1f5

Browse files
committed
init
0 parents  commit 889e1f5

File tree

18 files changed

+1045
-0
lines changed

18 files changed

+1045
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
dist/
3+
.idea/

.goreleaser.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# This is an example .goreleaser.yml file with some sensible defaults.
2+
# Make sure to check the documentation at https://goreleaser.com
3+
4+
project_name: "diskscript"
5+
before:
6+
hooks:
7+
# You may remove this if you don't use go modules.
8+
- go mod tidy
9+
# you may remove this if you don't need go generate
10+
- go generate ./...
11+
builds:
12+
- binary: diskscript
13+
main: ./cmd/diskscript/main.go
14+
env:
15+
- CGO_ENABLED=0
16+
goos:
17+
- linux
18+
19+
archives:
20+
- format: tar.gz
21+
# this name template makes the OS and Arch compatible with the results of uname.
22+
name_template: >-
23+
{{ .ProjectName }}_
24+
{{- title .Os }}_
25+
{{- if eq .Arch "amd64" }}x86_64
26+
{{- else if eq .Arch "386" }}i386
27+
{{- else }}{{ .Arch }}{{ end }}
28+
{{- if .Arm }}v{{ .Arm }}{{ end }}
29+
# use zip for windows archives
30+
format_overrides:
31+
- goos: windows
32+
format: zip
33+
checksum:
34+
name_template: 'checksums.txt'
35+
snapshot:
36+
name_template: "{{ incpatch .Version }}-next"
37+
changelog:
38+
sort: asc
39+
filters:
40+
exclude:
41+
- '^docs:'
42+
- '^test:'
43+
44+
# The lines beneath this are called `modelines`. See `:help modeline`
45+
# Feel free to remove those if you don't want/use them.
46+
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
47+
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

Readme.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
自用的一个服务器磁盘监控工具,根据磁盘占用执行对应的脚本,可以结合alertmanager发送报警。
2+
- 挂载设备百分比监控
3+
- 目录大小监控
4+
- 文件大小监控
5+
- 支持发送到alertmanager报警,自定义labels和报警名称
6+
- 二进制直接使用
7+
8+
`config.yml` 配置文件示例
9+
```yml
10+
console: true # 是否输出脚本执行信息
11+
alert: # alertmanager
12+
enable: true # 启用报警
13+
url: "http://localhost:9093"
14+
labels: ["env=dev","sp=aliyun"] # 全局labels,添加到所有报警中
15+
mount: # 挂载设备
16+
- mount: "/" # 根分区超过90%占用报警
17+
threshold: 90
18+
alert: true # 超过阈值报警
19+
labels: ["disk=/"] # 自定义alertmanager labels (可选)
20+
alertname: "根分区磁盘占用过大" # 自定义报警名称(可选)
21+
scripts: # 可选
22+
- "/bin/sh /data/scripts/xxx.sh"
23+
directory: # 挂载目录
24+
- directory: "/data/logs/nginx/"
25+
threshold: 5g # 限制5g 超过5g报警
26+
alert: true
27+
scripts: # 执行脚本
28+
- "/data/scripts/nginx-log.sh"
29+
- directory: "/data/logs/ets2/"
30+
threshold: 5g # 限制
31+
scripts: # 执行脚本
32+
- "find /data/logs/xxx -mtime +7 -delete"
33+
- "find /data/logs/xxx -mtime +7 -delete"
34+
file: # 文件占用检查
35+
- file: "/mnt/d/ISO/Windows.iso"
36+
threshold: 5g # 限制
37+
alert: true
38+
scripts: # 执行脚本
39+
- "find /data/logs/xxx -mtime +7 -delete"
40+
- "find /data/logs/xxx -mtime +7 -delete"
41+
- file: "/mnt/d/ISO/ubuntu-21.10-desktop-amd64.iso"
42+
threshold: 4g # 限制
43+
alert: true
44+
scripts: # 执行脚本
45+
- "cat /dev/null > /data/logs/nginx/blog_access.log"
46+
- "/data/scripts/1.py"
47+
```
48+
可作为定时任务使用
49+
```shell
50+
* * * * * cd /data/xxx && ./diskscript
51+
```
52+
53+
#### 下载使用
54+
```shell
55+
tar xvf diskscript_Linux_x86_64.tar.gz
56+
chmod +x diskscript
57+
# 编辑config.yml文件
58+
# 执行程序
59+
./diskscript
60+
# 指定配置文件
61+
./diskscript -conf xxx.yml
62+
```
63+
![](images/1.png)
64+
![](images/2.png)
65+
![](images/3.png)
66+
![](images/4.png)

cmd/diskscript/config.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
console: true # script 脚本输出
2+
alert: # alertmanager
3+
enable: true
4+
url: "http://192.168.100.192:9093"
5+
labels: ["env2=dev"] # 全局labels
6+
mount: # 挂载设备
7+
- mount: "/"
8+
threshold: 60 # 限制 60%
9+
alert: true
10+
alertname: "根磁盘挂载超过限制"
11+
labels: ["env=dev"]
12+
scripts: # 执行脚本
13+
- "/tmp/1.sh args1"
14+
- "/tmp/2.sh"
15+
- mount: "/boot"
16+
threshold: 60
17+
scripts:
18+
- "/tmp/1.sh pars"
19+
- mount: "/home"
20+
threshold: 60
21+
scripts:
22+
- "/tmp/1.sh pars"
23+
directory:
24+
- directory: "/data/logs/nginx/89.log"
25+
threshold: 20m # 限制
26+
alert: true
27+
scripts: # 执行脚本
28+
- "cat /dev/null > /data/logs/nginx/blog_access.log && cat /dev/null > /data/logs/nginx/access_music.log"
29+
- "/data/scripts/1.py"
30+
- directory: "/data/logs"
31+
threshold: 1g # 限制
32+
scripts: # 执行脚本
33+
- "/tmp/1.sh pars"
34+
- "/data/scripts/1.py"
35+
- directory: "/data/mysql-backu"
36+
threshold: 100m
37+
scripts:
38+
- "/tmp/1.sh pars"
39+
- "/data/scripts/1.py"
40+
- "/data/scripts/logs-rm.sh"
41+
- directory: "/data/mysql-backup"
42+
threshold: 100m
43+
scripts:
44+
- "/tmp/1.sh pars"
45+
- "/data/scripts/1.py"
46+
- "/data/scripts/logs-rm.sh"
47+
file:
48+
- file: "/mnt/d/ISO/Windows.iso"
49+
threshold: 5g # 限制
50+
alert: true
51+
scripts: # 执行脚本
52+
- "cat /dev/null > /data/logs/nginx/blog_access.log && cat /dev/null > /data/logs/nginx/access_music.log"
53+
- "/data/scripts/1.py"
54+
- file: "/mnt/d/ISO/ubuntu-21.10-desktop-amd64.iso"
55+
threshold: 4g # 限制
56+
alert: true
57+
scripts: # 执行脚本
58+
- "cat /dev/null > /data/logs/nginx/blog_access.log && cat /dev/null > /data/logs/nginx/access_music.log"
59+
- "/data/scripts/1.py"

cmd/diskscript/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"diskscript/pkg/diskscript"
5+
"flag"
6+
)
7+
8+
var configfile string
9+
10+
func init() {
11+
flag.StringVar(&configfile, "conf", "config.yml", "指定配置文件")
12+
}
13+
14+
func main() {
15+
flag.Parse()
16+
diskscript.Execute(configfile)
17+
}

go.mod

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
module diskscript
2+
3+
go 1.18
4+
5+
require (
6+
github.com/go-openapi/runtime v0.25.0
7+
github.com/go-openapi/strfmt v0.21.3
8+
github.com/prometheus/alertmanager v0.25.0
9+
github.com/prometheus/common v0.39.0
10+
github.com/shirou/gopsutil v3.21.11+incompatible
11+
golang.org/x/mod v0.7.0
12+
gopkg.in/alecthomas/kingpin.v2 v2.2.6
13+
gopkg.in/yaml.v3 v3.0.1
14+
)
15+
16+
require (
17+
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
18+
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
19+
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
20+
github.com/beorn7/perks v1.0.1 // indirect
21+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
22+
github.com/go-logr/logr v1.2.3 // indirect
23+
github.com/go-logr/stdr v1.2.2 // indirect
24+
github.com/go-ole/go-ole v1.2.6 // indirect
25+
github.com/go-openapi/analysis v0.21.4 // indirect
26+
github.com/go-openapi/errors v0.20.3 // indirect
27+
github.com/go-openapi/jsonpointer v0.19.5 // indirect
28+
github.com/go-openapi/jsonreference v0.20.0 // indirect
29+
github.com/go-openapi/loads v0.21.2 // indirect
30+
github.com/go-openapi/spec v0.20.7 // indirect
31+
github.com/go-openapi/swag v0.22.3 // indirect
32+
github.com/go-openapi/validate v0.22.0 // indirect
33+
github.com/golang/protobuf v1.5.2 // indirect
34+
github.com/josharian/intern v1.0.0 // indirect
35+
github.com/jpillora/backoff v1.0.0 // indirect
36+
github.com/mailru/easyjson v0.7.7 // indirect
37+
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
38+
github.com/mitchellh/mapstructure v1.5.0 // indirect
39+
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
40+
github.com/oklog/ulid v1.3.1 // indirect
41+
github.com/opentracing/opentracing-go v1.2.0 // indirect
42+
github.com/pkg/errors v0.9.1 // indirect
43+
github.com/prometheus/client_golang v1.14.0 // indirect
44+
github.com/prometheus/client_model v0.3.0 // indirect
45+
github.com/prometheus/procfs v0.8.0 // indirect
46+
github.com/yusufpapurcu/wmi v1.2.2 // indirect
47+
go.mongodb.org/mongo-driver v1.11.0 // indirect
48+
go.opentelemetry.io/otel v1.11.1 // indirect
49+
go.opentelemetry.io/otel/trace v1.11.1 // indirect
50+
golang.org/x/net v0.4.0 // indirect
51+
golang.org/x/oauth2 v0.3.0 // indirect
52+
golang.org/x/sys v0.3.0 // indirect
53+
golang.org/x/text v0.5.0 // indirect
54+
google.golang.org/appengine v1.6.7 // indirect
55+
google.golang.org/protobuf v1.28.1 // indirect
56+
gopkg.in/yaml.v2 v2.4.0 // indirect
57+
)

0 commit comments

Comments
 (0)