You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
4 years ago
  1. package command
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/pb"
  5. "github.com/golang/protobuf/jsonpb"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/security"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. )
  14. func init() {
  15. cmdFilerMetaTail.Run = runFilerMetaTail // break init cycle
  16. }
  17. var cmdFilerMetaTail = &Command{
  18. UsageLine: "filer.meta.tail [-filer=localhost:8888] [-pathPrefix=/]",
  19. Short: "see continuous changes on a filer",
  20. Long: `See continuous changes on a filer.
  21. weed filer.meta.tail -timeAgo=30h | grep truncate
  22. weed filer.meta.tail -timeAgo=30h | jq .
  23. weed filer.meta.tail -timeAgo=30h | jq .eventNotification.newEntry.name
  24. weed filer.meta.tail -timeAgo=30h -es=http://<elasticSearchServerHost>:<port> -es.index=seaweedfs
  25. `,
  26. }
  27. var (
  28. tailFiler = cmdFilerMetaTail.Flag.String("filer", "localhost:8888", "filer hostname:port")
  29. tailTarget = cmdFilerMetaTail.Flag.String("pathPrefix", "/", "path to a folder or common prefix for the folders or files on filer")
  30. tailStart = cmdFilerMetaTail.Flag.Duration("timeAgo", 0, "start time before now. \"300ms\", \"1.5h\" or \"2h45m\". Valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\"")
  31. tailPattern = cmdFilerMetaTail.Flag.String("pattern", "", "full path or just filename pattern, ex: \"/home/?opher\", \"*.pdf\", see https://golang.org/pkg/path/filepath/#Match ")
  32. esServers = cmdFilerMetaTail.Flag.String("es", "", "comma-separated elastic servers http://<host:port>")
  33. esIndex = cmdFilerMetaTail.Flag.String("es.index", "seaweedfs", "ES index name")
  34. )
  35. func runFilerMetaTail(cmd *Command, args []string) bool {
  36. util.LoadConfiguration("security", false)
  37. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  38. clientId := util.RandomInt32()
  39. var filterFunc func(dir, fname string) bool
  40. if *tailPattern != "" {
  41. if strings.Contains(*tailPattern, "/") {
  42. println("watch path pattern", *tailPattern)
  43. filterFunc = func(dir, fname string) bool {
  44. matched, err := filepath.Match(*tailPattern, dir+"/"+fname)
  45. if err != nil {
  46. fmt.Printf("error: %v", err)
  47. }
  48. return matched
  49. }
  50. } else {
  51. println("watch file pattern", *tailPattern)
  52. filterFunc = func(dir, fname string) bool {
  53. matched, err := filepath.Match(*tailPattern, fname)
  54. if err != nil {
  55. fmt.Printf("error: %v", err)
  56. }
  57. return matched
  58. }
  59. }
  60. }
  61. shouldPrint := func(resp *filer_pb.SubscribeMetadataResponse) bool {
  62. if filer_pb.IsEmpty(resp) {
  63. return false
  64. }
  65. if filterFunc == nil {
  66. return true
  67. }
  68. if resp.EventNotification.OldEntry != nil && filterFunc(resp.Directory, resp.EventNotification.OldEntry.Name) {
  69. return true
  70. }
  71. if resp.EventNotification.NewEntry != nil && filterFunc(resp.EventNotification.NewParentPath, resp.EventNotification.NewEntry.Name) {
  72. return true
  73. }
  74. return false
  75. }
  76. jsonpbMarshaler := jsonpb.Marshaler{
  77. EmitDefaults: false,
  78. }
  79. eachEntryFunc := func(resp *filer_pb.SubscribeMetadataResponse) error {
  80. jsonpbMarshaler.Marshal(os.Stdout, resp)
  81. fmt.Fprintln(os.Stdout)
  82. return nil
  83. }
  84. if *esServers != "" {
  85. var err error
  86. eachEntryFunc, err = sendToElasticSearchFunc(*esServers, *esIndex)
  87. if err != nil {
  88. fmt.Printf("create elastic search client to %s: %+v\n", *esServers, err)
  89. return false
  90. }
  91. }
  92. tailErr := pb.FollowMetadata(pb.ServerAddress(*tailFiler), grpcDialOption, "tail", clientId,
  93. *tailTarget, nil, time.Now().Add(-*tailStart).UnixNano(), 0,
  94. func(resp *filer_pb.SubscribeMetadataResponse) error {
  95. if !shouldPrint(resp) {
  96. return nil
  97. }
  98. if err := eachEntryFunc(resp); err != nil {
  99. return err
  100. }
  101. return nil
  102. }, false)
  103. if tailErr != nil {
  104. fmt.Printf("tail %s: %v\n", *tailFiler, tailErr)
  105. }
  106. return true
  107. }