This commit is contained in:
zoe
2023-05-01 18:26:36 +08:00
parent 2833a74007
commit d56f5c910b
4 changed files with 67 additions and 25 deletions
+15
View File
@@ -0,0 +1,15 @@
module clear
go 1.20
require (
github.com/shirou/gopsutil/v3 v3.23.4
gopkg.in/yaml.v2 v2.4.0
)
require (
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
golang.org/x/sys v0.7.0 // indirect
)
+16 -25
View File
@@ -2,16 +2,12 @@ package main
import (
"bytes"
"time"
)
//https://www.jianshu.com/p/2ea010a9691e
import (
"clear/utils"
"clear/utils/color"
"flag"
"fmt"
"github.com/fatih/color"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/v3/disk"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
@@ -19,9 +15,9 @@ import (
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"syscall"
"time"
)
type Conf struct {
@@ -41,7 +37,7 @@ type FileType struct {
ModTime time.Time
}
// 邮件内容
// EmailContent 邮件内容
var EmailContent bytes.Buffer
// 清理大小总计
@@ -61,6 +57,7 @@ var safeExt = []string{".log", ".json", ".gz", ".tgz", ".tar", ".zip", ".rar", "
func init() {
MinRecountFileSize = 10 * 1024 * 1024
}
func main() {
var minSize float64
var configDir string
@@ -68,7 +65,7 @@ func main() {
var email string
var help bool
flag.BoolVar(&help, "help", false, "显示帮助")
flag.Float64Var(&minSize, "min-size", 90, "阈值比例,例如:磁盘使用率大于90%后执行")
flag.Float64Var(&minSize, "min-size", 10, "阈值比例,例如:磁盘使用率大于90%后执行")
flag.StringVar(&configDir, "config-dir", "./rules/", "配置目录")
flag.StringVar(&configFile, "config-file", "", "指定配置文件")
flag.StringVar(&email, "email", "", "指定邮箱收件人")
@@ -97,7 +94,7 @@ func main() {
UsedPercent := fmt.Sprintf("%.1f", data.UsedPercent)
DiskTotalSize := int64(data.Total)
LeftSize := int64(data.Free)
diskData = "挂载点: " + data.Path + ", 使用比例: " + UsedPercent + "%, 磁盘容量:" + FormatFileSize(DiskTotalSize) + ", 剩余容量: " + FormatFileSize(LeftSize) + ", 磁盘类型:" + data.Fstype
diskData = "挂载点: " + data.Path + ", 使用比例: " + UsedPercent + "%, 磁盘容量:" + utils.FormatFileSize(DiskTotalSize) + ", 剩余容量: " + utils.FormatFileSize(LeftSize) + ", 磁盘类型:" + data.Fstype
fmt.Println(data)
break
}
@@ -145,8 +142,8 @@ func main() {
endTime := time.Now().Format(TimeFormat)
fmt.Println("+-----------------------------------------------------------------------+")
successClearSize := FormatFileSize(ClearSize)
estimateSize := FormatFileSize(EstimateSize)
successClearSize := utils.FormatFileSize(ClearSize)
estimateSize := utils.FormatFileSize(EstimateSize)
fmt.Println("[成功清理]", successClearSize)
fmt.Println("[预估清理]", estimateSize)
@@ -173,21 +170,16 @@ func main() {
EmailContent.WriteString("\n 磁盘容量已下降,可忽略本邮件 \n")
}
//content := "<pre>" + EmailContent.String() + "</pre>"
content := "<pre>" + EmailContent.String() + "</pre>"
if isCheck == true {
//utils.SendEmailApi(email, "【磁盘容量不足警告】🔥", content, "日志清理")
utils.SendEmailApi(email, "【磁盘容量不足警告】🔥", content, "日志清理")
} else {
//utils.SendEmailApi(email, "磁盘清理完成通知", content, "日志清理")
utils.SendEmailApi(email, "磁盘清理完成通知", content, "日志清理")
}
}
fmt.Println("[完成时间]", endTime)
}
func FormatFileSize(DiskTotalSize int64) string {
return strconv.FormatInt(DiskTotalSize<<2, 10)
}
func findConf(filename string) error {
var confList []Conf
@@ -346,11 +338,10 @@ func ListDir(config Conf, folder string) error {
return nil
}
func RunRule(config Conf, file FileType, minFileSize int64, beforeTime int64) error {
filesize := FormatFileSize(file.Size)
//colorSize := color.Cyan(filesize)
filesize := utils.FormatFileSize(file.Size)
colorSize := color.Cyan(filesize)
if minFileSize > 0 && file.Size < minFileSize {
color.Green("sdfsd", "")
fmt.Println(color.Green("[忽略]"), file.ModTime.Format(TimeFormat), "➜", file.Path, colorSize)
return nil
}
+17
View File
@@ -0,0 +1,17 @@
package color
func Magenta(xxx string) string {
return xxx
}
func Green(xxx string) string {
return xxx
}
func Red(xxx string) string {
return xxx
}
func Cyan(xxx string) string {
return xxx
}
+19
View File
@@ -0,0 +1,19 @@
package utils
import "strconv"
func FormatFileSize(fileSize int64) string {
return strconv.FormatInt(fileSize>>30, 10)
}
func GetIp() string {
return ""
}
func SendEmailApi(email, xx, content, title string) {
}
func InArray(ext string, safeExt []string) bool {
return true
}