From 872b5161e660daaac9a842b2ec77b5122f47a7db Mon Sep 17 00:00:00 2001 From: Konstantin Lebedev <9497591+kmlebedev@users.noreply.github.com> Date: Tue, 5 Mar 2024 22:09:08 +0500 Subject: [PATCH] [shell] fs log purge (#5344) * fix: install cronie * chore: shell delete old logs * Revert "fix: install cronie" This reverts commit 920832b04b89a0a0c9e12b3d51fe710fc622dad6. * fix: dayAgo * fix: Nit: dayAgo => daysAgo --- weed/shell/command_fs_log.go | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 weed/shell/command_fs_log.go diff --git a/weed/shell/command_fs_log.go b/weed/shell/command_fs_log.go new file mode 100644 index 000000000..5567f76e6 --- /dev/null +++ b/weed/shell/command_fs_log.go @@ -0,0 +1,53 @@ +package shell + +import ( + "flag" + "fmt" + "github.com/seaweedfs/seaweedfs/weed/filer" + "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" + "io" + "time" +) + +func init() { + Commands = append(Commands, &commandFsLogPurge{}) +} + +type commandFsLogPurge struct { +} + +func (c *commandFsLogPurge) Name() string { + return "fs.log.purge" +} + +func (c *commandFsLogPurge) Help() string { + return `purge filer logs + + fs.log.purge [-v] [-modifyDayAgo 365] +` +} + +func (c *commandFsLogPurge) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + fsLogPurgeCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError) + daysAgo := fsLogPurgeCommand.Uint("daysAgo", 365, "purge logs older than N days") + verbose := fsLogPurgeCommand.Bool("v", false, "verbose mode") + + if err = fsLogPurgeCommand.Parse(args); err != nil { + return err + } + + modificationTimeAgo := time.Now().Add(-time.Hour * 24 * time.Duration(*daysAgo)).Unix() + err = filer_pb.ReadDirAllEntries(commandEnv, filer.SystemLogDir, "", func(entry *filer_pb.Entry, isLast bool) error { + if entry.Attributes.Mtime > modificationTimeAgo { + return nil + } + if errDel := filer_pb.Remove(commandEnv, filer.SystemLogDir, entry.Name, true, true, true, false, nil); errDel != nil { + return errDel + } + if *verbose { + fmt.Fprintf(writer, "delete %s\n", entry.Name) + } + return nil + }) + return err +}