Skip to content

Commit b59095a

Browse files
committed
add(logs) 增加日志扫描和日志清理的功能
1 parent be5124a commit b59095a

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

modules/waf_master_site.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4250,3 +4250,65 @@ func SyncRenewalCert() {
42504250
}
42514251

42524252
}
4253+
4254+
func (s *Wafmastersite) GetSiteLogsSize(request *http.Request) core.Response {
4255+
// 备份网站日志
4256+
list, err := public.GetHistorySiteLogs("/www/cloud_waf/vhost/history_backups/logs")
4257+
if err != nil {
4258+
list = make(map[string]int64)
4259+
}
4260+
//当前的网站日志
4261+
list2, err := public.GetHistorySiteLogs("/www/cloud_waf/nginx/logs")
4262+
if err != nil {
4263+
list2 = make(map[string]int64)
4264+
}
4265+
return core.Success(map[string]interface{}{
4266+
"history": list,
4267+
"site": list2,
4268+
})
4269+
}
4270+
4271+
// 清理日志
4272+
func (s *Wafmastersite) ClearSiteLogs(request *http.Request) core.Response {
4273+
//参数是一个list ["路径1", "路径2", ...]
4274+
params := struct {
4275+
Paths []string `json:"paths"`
4276+
}{}
4277+
if err := core.GetParamsFromRequestToStruct(request, &params); err != nil {
4278+
return core.Fail(err)
4279+
}
4280+
if len(params.Paths) == 0 {
4281+
return core.Fail("参数错误,路径不能为空")
4282+
}
4283+
for _, path := range params.Paths {
4284+
if !strings.HasSuffix(path, ".log") && !strings.HasSuffix(path, ".zip") {
4285+
continue
4286+
}
4287+
4288+
abs, err := filepath.Abs(path)
4289+
if err != nil {
4290+
continue
4291+
}
4292+
if !strings.HasPrefix(abs, "/www/cloud_waf/vhost/history_backups/logs/") && !strings.HasPrefix(abs, "/www/cloud_waf/nginx/logs/") {
4293+
continue
4294+
}
4295+
//如果zip文件则直接删除
4296+
if strings.HasSuffix(path, ".zip") {
4297+
if public.FileExists(abs) {
4298+
err = os.Remove(abs)
4299+
if err != nil {
4300+
return core.Fail("删除文件失败:" + err.Error())
4301+
}
4302+
}
4303+
continue
4304+
}
4305+
//如果是.log 的文件则内容直接清空
4306+
if public.FileExists(abs) {
4307+
err = os.Truncate(abs, 0)
4308+
if err != nil {
4309+
return core.Fail("网站日志清空失败:" + err.Error())
4310+
}
4311+
}
4312+
}
4313+
return core.Success("网站日志清理成功")
4314+
}

public/waf_logs.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package public
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
)
8+
9+
func GetHistorySiteLogs(path string) (map[string]int64, error) {
10+
result := make(map[string]int64)
11+
12+
if !FileExists(path) {
13+
return result, fmt.Errorf("目录不存在: %s", path)
14+
}
15+
16+
err := filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error {
17+
if err != nil {
18+
return nil
19+
}
20+
21+
if info.IsDir() {
22+
return nil
23+
}
24+
25+
relativePath, err := filepath.Rel(path, filePath)
26+
if err != nil {
27+
relativePath = info.Name()
28+
}
29+
30+
result[relativePath] = info.Size()
31+
32+
return nil
33+
})
34+
35+
if err != nil {
36+
return result, fmt.Errorf("遍历目录失败: %v", err)
37+
}
38+
39+
return result, nil
40+
}

0 commit comments

Comments
 (0)