command_fs_log.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package shell
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/filer"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  7. "io"
  8. "time"
  9. )
  10. func init() {
  11. Commands = append(Commands, &commandFsLogPurge{})
  12. }
  13. type commandFsLogPurge struct {
  14. }
  15. func (c *commandFsLogPurge) Name() string {
  16. return "fs.log.purge"
  17. }
  18. func (c *commandFsLogPurge) Help() string {
  19. return `purge filer logs
  20. fs.log.purge [-v] [-modifyDayAgo 365]
  21. `
  22. }
  23. func (c *commandFsLogPurge) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  24. fsLogPurgeCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  25. daysAgo := fsLogPurgeCommand.Uint("daysAgo", 365, "purge logs older than N days")
  26. verbose := fsLogPurgeCommand.Bool("v", false, "verbose mode")
  27. if err = fsLogPurgeCommand.Parse(args); err != nil {
  28. return err
  29. }
  30. modificationTimeAgo := time.Now().Add(-time.Hour * 24 * time.Duration(*daysAgo)).Unix()
  31. err = filer_pb.ReadDirAllEntries(commandEnv, filer.SystemLogDir, "", func(entry *filer_pb.Entry, isLast bool) error {
  32. if entry.Attributes.Mtime > modificationTimeAgo {
  33. return nil
  34. }
  35. if errDel := filer_pb.Remove(commandEnv, filer.SystemLogDir, entry.Name, true, true, true, false, nil); errDel != nil {
  36. return errDel
  37. }
  38. if *verbose {
  39. fmt.Fprintf(writer, "delete %s\n", entry.Name)
  40. }
  41. return nil
  42. })
  43. return err
  44. }