command_fs_log.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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] [-daysAgo 365]
  21. `
  22. }
  23. func (c *commandFsLogPurge) HasTag(CommandTag) bool {
  24. return false
  25. }
  26. func (c *commandFsLogPurge) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  27. fsLogPurgeCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  28. daysAgo := fsLogPurgeCommand.Uint("daysAgo", 365, "purge logs older than N days")
  29. verbose := fsLogPurgeCommand.Bool("v", false, "verbose mode")
  30. if err = fsLogPurgeCommand.Parse(args); err != nil {
  31. return err
  32. }
  33. modificationTimeAgo := time.Now().Add(-time.Hour * 24 * time.Duration(*daysAgo)).Unix()
  34. err = filer_pb.ReadDirAllEntries(commandEnv, filer.SystemLogDir, "", func(entry *filer_pb.Entry, isLast bool) error {
  35. if entry.Attributes.Mtime > modificationTimeAgo {
  36. return nil
  37. }
  38. if errDel := filer_pb.Remove(commandEnv, filer.SystemLogDir, entry.Name, true, true, true, false, nil); errDel != nil {
  39. return errDel
  40. }
  41. if *verbose {
  42. fmt.Fprintf(writer, "delete %s\n", entry.Name)
  43. }
  44. return nil
  45. })
  46. return err
  47. }