From 1efde241468b05e0dcc8ad7b421fbf3cfb1f2835 Mon Sep 17 00:00:00 2001 From: gotoeasy Date: Sun, 10 Jul 2022 14:47:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E4=BB=93=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E7=BB=B4=E6=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 ++- glc/cmn/cmn.go | 44 ++++++++++++++++++++ glc/conf/config.go | 7 ++++ glc/ldb/sysmnt/storage_data.go | 36 ++++++++++++++++ glc/www/controller/storage_mnt_controller.go | 7 ++++ 5 files changed, 97 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 73b91fe..d6fbd71 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ docker run -d -p 8080:8080 -v /glc:/glogcenter gotoeasy/glc ## `docker`启动环境变量 - [x] `GLC_STORE_NAME_AUTO_ADD_DATE`日志仓是否自动按日存储,默认`true` +- [x] `GLC_SAVE_DAYS`日志仓按日存储自动维护时的保留天数(`0~180`),`0`表示不自动删除,默认`180`天 - [x] `GLC_STORE_ROOT`存储根目录,默认`/glogcenter` - [x] `GLC_MAX_IDLE_TIME`最大闲置时间(秒),超过闲置时间将自动关闭日志仓,`0`时不关闭,默认`180`秒 - [x] `GLC_ENABLE_SECURITY_KEY`日志添加的接口是否开启API秘钥校验,默认`false` @@ -108,8 +109,8 @@ docker run -d -p 8080:8080 -v /glc:/glogcenter gotoeasy/glc ### 开发版`latest` - [ ] TODO -- [ ] 界面显示当前版本 -- [ ] 日志按日分仓存储时,默认保存最多180天 +- [x] 日志按日分仓存储时,默认自动维护保存最多180天,自动维护时不能手动删除日志仓 +- [x] 改善日志仓管理页面的展示 ### 版本`0.4.0` diff --git a/glc/cmn/cmn.go b/glc/cmn/cmn.go index 06f29d7..7da00f3 100644 --- a/glc/cmn/cmn.go +++ b/glc/cmn/cmn.go @@ -131,6 +131,22 @@ func LeftRune(str string, length int) string { return rs } +func RightRune(str string, length int) string { + lenr := LenRune(str) + if lenr <= length { + return str + } + + var rs string + start := lenr - length + for i, s := range str { + if i >= start { + rs = rs + string(s) + } + } + return rs +} + func PathSeparator() string { return string(os.PathSeparator) } @@ -161,6 +177,29 @@ func GeyStoreNameByDate(name string) string { return name } +func StartwithsRune(str string, startstr string) bool { + + if startstr == "" || str == startstr { + return true + } + + strs := []rune(str) + tmps := []rune(startstr) + lens := len(strs) + lentmp := len([]rune(tmps)) + if lens < lentmp { + return false + } + + for i := 0; i < lentmp; i++ { + if tmps[i] != strs[i] { + return false + } + } + + return true +} + func EndwithsRune(str string, endstr string) bool { if endstr == "" || str == endstr { @@ -253,3 +292,8 @@ func GetSizeInfo(size uint64) string { } return fmt.Sprintf("%.1fM", float64(size)/1024/1024) } + +// 当前日期加减天数后的yyyymmdd格式 +func GetYyyymmdd(days int) string { + return time.Now().AddDate(0, 0, days).Format("20060102") +} diff --git a/glc/conf/config.go b/glc/conf/config.go index 639ec02..900d5b9 100644 --- a/glc/conf/config.go +++ b/glc/conf/config.go @@ -26,6 +26,7 @@ var enableWebGzip bool var amqpAddr string var amqpQueueName string var amqpJsonFormat bool +var saveDays int func init() { UpdateConfigByEnv() @@ -47,6 +48,12 @@ func UpdateConfigByEnv() { amqpAddr = Getenv("GLC_AMQP_ADDR", "") // rabbitMq连接地址,例:"amqp://user:password@ip:port/" amqpQueueName = Getenv("GLC_AMQP_QUEUE_NAME", "glc-log-queue") // rabbitMq队列名 amqpJsonFormat = GetenvBool("GLC_AMQP_JSON_FORMAT", true) // rabbitMq消息文本是否为json格式,默认true + saveDays = GetenvInt("GLC_SAVE_DAYS", 180) // 日志分仓时的保留天数(0~180),0表示不自动删除,默认180天 +} + +// 取配置: 日志分仓时的保留天数(0~180),0表示不自动删除,可通过环境变量“GLC_SAVE_DAYS”设定,默认180天 +func GetSaveDays() int { + return saveDays } // 取配置: rabbitMq消息文本是否为json格式,可通过环境变量“GLC_AMQP_JSON_FORMAT”设定,默认值“true” diff --git a/glc/ldb/sysmnt/storage_data.go b/glc/ldb/sysmnt/storage_data.go index 79aa6a2..2beed61 100644 --- a/glc/ldb/sysmnt/storage_data.go +++ b/glc/ldb/sysmnt/storage_data.go @@ -7,7 +7,9 @@ import ( "fmt" "glc/cmn" "glc/conf" + "log" "os" + "time" "github.com/shirou/gopsutil/disk" ) @@ -24,6 +26,19 @@ type StorageModel struct { TotalSize string `json:"totalSize"` // 占用空间 } +func init() { + go func() { + if conf.IsStoreNameAutoAddDate() && conf.GetSaveDays() > 0 { + removeStorageByDays() + ticker := time.NewTicker(time.Hour) // 一小时检查一次是否有待删除的日志仓 + for { + <-ticker.C + removeStorageByDays() + } + } + }() +} + func GetStorageList() *StorageResult { var datas []*StorageModel @@ -76,3 +91,24 @@ func DeleteStorage(name string) error { } return NewSysmntStorage().DeleteStorageInfo(name) } + +func removeStorageByDays() { + // 日志按日期分仓存储时,按保存天数自动删除 + minYmd := cmn.GetYyyymmdd(-1 * conf.GetSaveDays()) + dirs := cmn.GetStorageNames(conf.GetStorageRoot(), ".sysmnt") + for _, dir := range dirs { + ymd := cmn.RightRune(dir, 8) + if !cmn.StartwithsRune(ymd, "20") { + continue + } + + if ymd < minYmd { + err := DeleteStorage(dir) + if err != nil { + log.Println("日志仓最多保存", conf.GetSaveDays(), "天,", "删除", dir, "失败", err) + } else { + log.Println("日志仓最多保存", conf.GetSaveDays(), "天,", "已删除", dir) + } + } + } +} diff --git a/glc/www/controller/storage_mnt_controller.go b/glc/www/controller/storage_mnt_controller.go index 1770877..21c3d9f 100644 --- a/glc/www/controller/storage_mnt_controller.go +++ b/glc/www/controller/storage_mnt_controller.go @@ -1,6 +1,7 @@ package controller import ( + "fmt" "glc/cmn" "glc/conf" "glc/gweb" @@ -24,6 +25,12 @@ func StorageListController(req *gweb.HttpRequest) *gweb.HttpResult { // 删除指定日志仓 func StorageDeleteController(req *gweb.HttpRequest) *gweb.HttpResult { name := req.GetFormParameter("storeName") + + if conf.IsStoreNameAutoAddDate() && conf.GetSaveDays() > 0 { + msg := fmt.Sprintf("当前是日志仓自动维护模式,最多保存 %d 天,不能手动删除", conf.GetSaveDays()) + return gweb.Error500(msg) + } + if status.IsStorageOpening(name) { return gweb.Error500("日志仓 " + name + " 正在使用,不能删除") }