github.com/vmware/govmomi@v0.51.0/cli/logs/command.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package logs 6 7 import ( 8 "context" 9 "flag" 10 "time" 11 12 "github.com/vmware/govmomi/cli" 13 "github.com/vmware/govmomi/cli/flags" 14 "github.com/vmware/govmomi/object" 15 ) 16 17 type logs struct { 18 *flags.HostSystemFlag 19 20 Max int32 21 Key string 22 23 follow bool 24 } 25 26 func init() { 27 cli.Register("logs", &logs{}) 28 } 29 30 func (cmd *logs) Register(ctx context.Context, f *flag.FlagSet) { 31 cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx) 32 cmd.HostSystemFlag.Register(ctx, f) 33 34 cmd.Max = 25 // default 35 f.Var(flags.NewInt32(&cmd.Max), "n", "Output the last N log lines") 36 f.StringVar(&cmd.Key, "log", "", "Log file key") 37 f.BoolVar(&cmd.follow, "f", false, "Follow log file changes") 38 } 39 40 func (cmd *logs) Process(ctx context.Context) error { 41 if err := cmd.HostSystemFlag.Process(ctx); err != nil { 42 return err 43 } 44 return nil 45 } 46 47 func (cmd *logs) Description() string { 48 return `View VPX and ESX logs. 49 50 The '-log' option defaults to "hostd" when connected directly to a host or 51 when connected to VirtualCenter and a '-host' option is given. Otherwise, 52 the '-log' option defaults to "vpxd:vpxd.log". The '-host' option is ignored 53 when connected directly to a host. See 'govc logs.ls' for other '-log' options. 54 55 Examples: 56 govc logs -n 1000 -f 57 govc logs -host esx1 58 govc logs -host esx1 -log vmkernel` 59 } 60 61 func (cmd *logs) Run(ctx context.Context, f *flag.FlagSet) error { 62 c, err := cmd.Client() 63 if err != nil { 64 return err 65 } 66 67 defaultKey := "hostd" 68 var host *object.HostSystem 69 70 if c.IsVC() { 71 host, err = cmd.HostSystemIfSpecified() 72 if err != nil { 73 return err 74 } 75 76 if host == nil { 77 defaultKey = "vpxd:vpxd.log" 78 } 79 } 80 81 m := object.NewDiagnosticManager(c) 82 83 key := cmd.Key 84 if key == "" { 85 key = defaultKey 86 } 87 88 l := m.Log(ctx, host, key) 89 90 err = l.Seek(ctx, cmd.Max) 91 if err != nil { 92 return err 93 } 94 95 for { 96 _, err = l.Copy(ctx, cmd.Out) 97 if err != nil { 98 return nil 99 } 100 101 if !cmd.follow { 102 break 103 } 104 105 <-time.After(time.Second) 106 } 107 108 return nil 109 }