浏览代码

管理主机内存清理工具

huangyan 1 月之前
当前提交
13e9da4573
共有 11 个文件被更改,包括 145 次插入0 次删除
  1. 8 0
      .idea/.gitignore
  2. 9 0
      .idea/MemoryCleanup.iml
  3. 8 0
      .idea/modules.xml
  4. 4 0
      .idea/vcs.xml
  5. 9 0
      build.bat
  6. 7 0
      go.mod
  7. 4 0
      go.sum
  8. 15 0
      install.sh
  9. 二进制
      log-cleaner
  10. 11 0
      log-cleaner.service
  11. 70 0
      main.go

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 9 - 0
.idea/MemoryCleanup.iml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="WEB_MODULE" version="4">
+  <component name="Go" enabled="true" />
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/MemoryCleanup.iml" filepath="$PROJECT_DIR$/.idea/MemoryCleanup.iml" />
+    </modules>
+  </component>
+</project>

+ 4 - 0
.idea/vcs.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings" defaultProject="true" />
+</project>

+ 9 - 0
build.bat

@@ -0,0 +1,9 @@
+cd %~dp0
+set GOARCH=arm
+set GOOS=linux
+set GOARM=6
+set GOPATH=E:\gopath
+set GO111MODULE=auto
+
+
+go build -o log-cleaner main.go

+ 7 - 0
go.mod

@@ -0,0 +1,7 @@
+module MemoryCleanup
+
+go 1.21
+
+require github.com/fsnotify/fsnotify v1.7.0
+
+require golang.org/x/sys v0.4.0 // indirect

+ 4 - 0
go.sum

@@ -0,0 +1,4 @@
+github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
+github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
+golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
+golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

+ 15 - 0
install.sh

@@ -0,0 +1,15 @@
+#!/bin/bash
+
+# 复制二进制文件到/usr/local/bin/
+cp log-cleaner /usr/local/bin/
+chmod +x /usr/local/bin/log-cleaner
+
+# 复制服务文件到/etc/systemd/system/
+cp log-cleaner.service /etc/systemd/system/
+
+# 重新加载systemd并启用服务
+systemctl daemon-reload
+systemctl enable log-cleaner.service
+systemctl start log-cleaner.service
+
+echo "安装完成,服务已启动"

二进制
log-cleaner


+ 11 - 0
log-cleaner.service

@@ -0,0 +1,11 @@
+[Unit]
+Description=Log Cleaner Service
+After=network.target
+
+[Service]
+Type=simple
+ExecStart=/usr/local/bin/log-cleaner
+Restart=always
+
+[Install]
+WantedBy=multi-user.target

+ 70 - 0
main.go

@@ -0,0 +1,70 @@
+package main
+
+import (
+	"log"
+	"os"
+	"path/filepath"
+
+	"github.com/fsnotify/fsnotify"
+)
+
+const (
+	maxSize = 500 * 1024 * 1024 // 500MB
+	logDir  = "/var/log"
+)
+
+func checkAndTruncate(filePath string) {
+	fileInfo, err := os.Stat(filePath)
+	if err != nil {
+		log.Printf("获取文件信息失败 %s: %v", filePath, err)
+		return
+	}
+
+	if fileInfo.Size() > maxSize {
+		err := os.Truncate(filePath, 0)
+		if err != nil {
+			log.Printf("截断文件失败 %s: %v", filePath, err)
+		} else {
+			log.Printf("成功截断文件 %s", filePath)
+		}
+	}
+}
+
+func main() {
+	// 初始检查所有文件
+	files, err := os.ReadDir(logDir)
+	if err != nil {
+		log.Fatalf("读取日志目录失败: %v", err)
+	}
+
+	for _, file := range files {
+		if file.IsDir() {
+			continue
+		}
+		checkAndTruncate(filepath.Join(logDir, file.Name()))
+	}
+
+	// 创建文件监控
+	watcher, err := fsnotify.NewWatcher()
+	if err != nil {
+		log.Fatalf("创建文件监控失败: %v", err)
+	}
+	defer watcher.Close()
+
+	err = watcher.Add(logDir)
+	if err != nil {
+		log.Fatalf("添加监控目录失败: %v", err)
+	}
+
+	log.Println("开始监控日志目录", logDir)
+	for {
+		select {
+		case event := <-watcher.Events:
+			if event.Op&fsnotify.Write == fsnotify.Write {
+				checkAndTruncate(event.Name)
+			}
+		case err := <-watcher.Errors:
+			log.Printf("监控错误: %v", err)
+		}
+	}
+}