2020-04-05 07:51:16 +00:00
package command
import (
"fmt"
2022-08-17 19:42:03 +00:00
"github.com/seaweedfs/seaweedfs/weed/filer"
2022-07-29 07:17:28 +00:00
"github.com/seaweedfs/seaweedfs/weed/pb"
2021-01-22 06:39:25 +00:00
"os"
2020-08-29 02:43:04 +00:00
"path/filepath"
"strings"
2020-04-28 06:49:46 +00:00
"time"
2020-04-05 07:51:16 +00:00
2022-07-29 07:17:28 +00:00
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/security"
"github.com/seaweedfs/seaweedfs/weed/util"
2020-04-05 07:51:16 +00:00
)
func init ( ) {
2021-01-13 02:48:01 +00:00
cmdFilerMetaTail . Run = runFilerMetaTail // break init cycle
2020-04-05 07:51:16 +00:00
}
2021-01-13 02:48:01 +00:00
var cmdFilerMetaTail = & Command {
2021-03-03 07:07:29 +00:00
UsageLine : "filer.meta.tail [-filer=localhost:8888] [-pathPrefix=/]" ,
2021-03-01 00:21:09 +00:00
Short : "see continuous changes on a filer" ,
Long : ` See continuous changes on a filer .
2020-04-05 07:51:16 +00:00
2021-01-22 06:39:25 +00:00
weed filer . meta . tail - timeAgo = 30 h | grep truncate
weed filer . meta . tail - timeAgo = 30 h | jq .
2022-05-31 03:24:59 +00:00
weed filer . meta . tail - timeAgo = 30 h - untilTimeAgo = 20 h | jq .
2021-01-22 06:39:25 +00:00
weed filer . meta . tail - timeAgo = 30 h | jq . eventNotification . newEntry . name
2021-11-01 01:01:33 +00:00
weed filer . meta . tail - timeAgo = 30 h - es = http : //<elasticSearchServerHost>:<port> -es.index=seaweedfs
2020-04-05 07:51:16 +00:00
` ,
}
var (
2021-01-13 08:31:19 +00:00
tailFiler = cmdFilerMetaTail . Flag . String ( "filer" , "localhost:8888" , "filer hostname:port" )
2021-03-03 07:07:29 +00:00
tailTarget = cmdFilerMetaTail . Flag . String ( "pathPrefix" , "/" , "path to a folder or common prefix for the folders or files on filer" )
2021-01-13 08:31:19 +00:00
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\"" )
2022-05-30 23:16:23 +00:00
tailStop = cmdFilerMetaTail . Flag . Duration ( "untilTimeAgo" , 0 , "read until this time ago. \"300ms\", \"1.5h\" or \"2h45m\". Valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\"" )
2021-01-13 08:31:19 +00:00
tailPattern = cmdFilerMetaTail . Flag . String ( "pattern" , "" , "full path or just filename pattern, ex: \"/home/?opher\", \"*.pdf\", see https://golang.org/pkg/path/filepath/#Match " )
2021-01-13 11:31:37 +00:00
esServers = cmdFilerMetaTail . Flag . String ( "es" , "" , "comma-separated elastic servers http://<host:port>" )
esIndex = cmdFilerMetaTail . Flag . String ( "es.index" , "seaweedfs" , "ES index name" )
2020-04-05 07:51:16 +00:00
)
2021-01-13 02:48:01 +00:00
func runFilerMetaTail ( cmd * Command , args [ ] string ) bool {
2020-04-05 07:51:16 +00:00
2021-08-03 08:30:35 +00:00
util . LoadConfiguration ( "security" , false )
2020-04-05 07:51:16 +00:00
grpcDialOption := security . LoadClientTLS ( util . GetViper ( ) , "grpc.client" )
2021-12-30 08:23:57 +00:00
clientId := util . RandomInt32 ( )
2020-04-05 07:51:16 +00:00
2020-08-29 02:43:04 +00:00
var filterFunc func ( dir , fname string ) bool
2021-01-13 08:31:19 +00:00
if * tailPattern != "" {
if strings . Contains ( * tailPattern , "/" ) {
println ( "watch path pattern" , * tailPattern )
2020-08-29 02:43:04 +00:00
filterFunc = func ( dir , fname string ) bool {
2021-01-13 08:31:19 +00:00
matched , err := filepath . Match ( * tailPattern , dir + "/" + fname )
2020-08-29 02:43:04 +00:00
if err != nil {
fmt . Printf ( "error: %v" , err )
}
return matched
}
} else {
2021-01-13 08:31:19 +00:00
println ( "watch file pattern" , * tailPattern )
2020-08-29 02:43:04 +00:00
filterFunc = func ( dir , fname string ) bool {
2021-01-13 08:31:19 +00:00
matched , err := filepath . Match ( * tailPattern , fname )
2020-08-29 02:43:04 +00:00
if err != nil {
fmt . Printf ( "error: %v" , err )
}
return matched
}
}
}
shouldPrint := func ( resp * filer_pb . SubscribeMetadataResponse ) bool {
2022-02-25 09:17:26 +00:00
if filer_pb . IsEmpty ( resp ) {
2020-08-29 02:43:04 +00:00
return false
}
2021-09-25 08:04:51 +00:00
if filterFunc == nil {
return true
}
2020-08-29 02:43:04 +00:00
if resp . EventNotification . OldEntry != nil && filterFunc ( resp . Directory , resp . EventNotification . OldEntry . Name ) {
return true
}
if resp . EventNotification . NewEntry != nil && filterFunc ( resp . EventNotification . NewParentPath , resp . EventNotification . NewEntry . Name ) {
return true
}
return false
}
2021-01-11 10:08:26 +00:00
eachEntryFunc := func ( resp * filer_pb . SubscribeMetadataResponse ) error {
2022-08-17 19:42:03 +00:00
filer . ProtoToText ( os . Stdout , resp )
2021-01-22 06:39:25 +00:00
fmt . Fprintln ( os . Stdout )
2021-01-11 10:08:26 +00:00
return nil
2021-01-11 08:03:13 +00:00
}
2021-01-13 11:31:37 +00:00
if * esServers != "" {
var err error
eachEntryFunc , err = sendToElasticSearchFunc ( * esServers , * esIndex )
if err != nil {
fmt . Printf ( "create elastic search client to %s: %+v\n" , * esServers , err )
return false
}
}
2021-01-11 08:03:13 +00:00
2022-05-30 23:16:23 +00:00
var untilTsNs int64
if * tailStop != 0 {
untilTsNs = time . Now ( ) . Add ( - * tailStop ) . UnixNano ( )
}
2023-03-22 06:01:49 +00:00
metadataFollowOption := & pb . MetadataFollowOption {
ClientName : "tail" ,
ClientId : clientId ,
ClientEpoch : 0 ,
SelfSignature : 0 ,
PathPrefix : * tailTarget ,
AdditionalPathPrefixes : nil ,
DirectoriesToWatch : nil ,
StartTsNs : time . Now ( ) . Add ( - * tailStart ) . UnixNano ( ) ,
StopTsNs : untilTsNs ,
EventErrorType : pb . TrivialOnError ,
}
tailErr := pb . FollowMetadata ( pb . ServerAddress ( * tailFiler ) , grpcDialOption , metadataFollowOption , func ( resp * filer_pb . SubscribeMetadataResponse ) error {
if ! shouldPrint ( resp ) {
2021-08-04 23:25:46 +00:00
return nil
2023-03-22 06:01:49 +00:00
}
if err := eachEntryFunc ( resp ) ; err != nil {
return err
}
return nil
} )
2020-04-05 07:51:16 +00:00
2021-01-13 08:31:19 +00:00
if tailErr != nil {
fmt . Printf ( "tail %s: %v\n" , * tailFiler , tailErr )
2020-04-05 07:51:16 +00:00
}
return true
}