mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-19 20:40:25 +08:00
[日志审计]增加删除、清理和别的一些设置
This commit is contained in:
158
internal/tasks/log_task.go
Normal file
158
internal/tasks/log_task.go
Normal file
@@ -0,0 +1,158 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
go NewLogTask().Run()
|
||||
})
|
||||
}
|
||||
|
||||
type LogTask struct {
|
||||
}
|
||||
|
||||
func NewLogTask() *LogTask {
|
||||
return &LogTask{}
|
||||
}
|
||||
|
||||
func (this *LogTask) Run() {
|
||||
go this.runClean()
|
||||
go this.runMonitor()
|
||||
}
|
||||
|
||||
func (this *LogTask) runClean() {
|
||||
ticker := utils.NewTicker(24 * time.Hour)
|
||||
for ticker.Wait() {
|
||||
err := this.loopClean(86400)
|
||||
if err != nil {
|
||||
logs.Println("[TASK][LOG]" + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (this *LogTask) loopClean(seconds int64) error {
|
||||
// 检查上次运行时间,防止重复运行
|
||||
settingKey := "logTaskCleanLoop"
|
||||
timestamp := time.Now().Unix()
|
||||
c, err := models.SharedSysSettingDAO.CompareInt64Setting(settingKey, timestamp-seconds)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if c > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 记录时间
|
||||
err = models.SharedSysSettingDAO.UpdateSetting(settingKey, []byte(numberutils.FormatInt64(timestamp)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
configKey := "adminLogConfig"
|
||||
valueJSON, err := models.SharedSysSettingDAO.ReadSetting(configKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(valueJSON) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
config := &systemconfigs.LogConfig{}
|
||||
err = json.Unmarshal(valueJSON, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if config.Days > 0 {
|
||||
err = models.SharedLogDAO.DeleteLogsPermanentlyBeforeDays(config.Days)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *LogTask) runMonitor() {
|
||||
ticker := utils.NewTicker(1 * time.Minute)
|
||||
for ticker.Wait() {
|
||||
err := this.loopClean(60)
|
||||
if err != nil {
|
||||
logs.Println("[TASK][LOG]" + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (this *LogTask) loopMonitor(seconds int64) error {
|
||||
// 检查上次运行时间,防止重复运行
|
||||
settingKey := "logTaskMonitorLoop"
|
||||
timestamp := time.Now().Unix()
|
||||
c, err := models.SharedSysSettingDAO.CompareInt64Setting(settingKey, timestamp-seconds)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if c > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 记录时间
|
||||
err = models.SharedSysSettingDAO.UpdateSetting(settingKey, []byte(numberutils.FormatInt64(timestamp)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
configKey := "adminLogConfig"
|
||||
valueJSON, err := models.SharedSysSettingDAO.ReadSetting(configKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(valueJSON) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
config := &systemconfigs.LogConfig{}
|
||||
err = json.Unmarshal(valueJSON, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if config.Capacity != nil {
|
||||
capacityBytes := config.Capacity.Bytes()
|
||||
if capacityBytes > 0 {
|
||||
sumBytes, err := models.SharedLogDAO.SumLogsSize()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if sumBytes > capacityBytes {
|
||||
err := models.SharedMessageDAO.CreateMessage(0, 0, models.MessageTypeLogCapacityOverflow, models.MessageLevelError, "日志用量已经超出最大限制,当前的用量为"+this.formatBytes(sumBytes)+",而设置的最大容量为"+this.formatBytes(capacityBytes)+"。", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *LogTask) formatBytes(bytes int64) string {
|
||||
sizeHuman := ""
|
||||
if bytes < 1024 {
|
||||
sizeHuman = numberutils.FormatInt64(bytes) + "字节"
|
||||
} else if bytes < 1024*1024 {
|
||||
sizeHuman = fmt.Sprintf("%.2fK", float64(bytes)/1024)
|
||||
} else if bytes < 1024*1024*1024 {
|
||||
sizeHuman = fmt.Sprintf("%.2fM", float64(bytes)/1024/1024)
|
||||
} else {
|
||||
sizeHuman = fmt.Sprintf("%.2fG", float64(bytes)/1024/1024/1024)
|
||||
}
|
||||
return sizeHuman
|
||||
}
|
||||
Reference in New Issue
Block a user