Skip to content

Commit

Permalink
Add '-f' flag to logs command
Browse files Browse the repository at this point in the history
- Support logs '-n' > VC/ESX BrowseLog max

- Add DiagnosticLog helper

- Add doc for logs commands
  • Loading branch information
dougm committed Dec 12, 2016
1 parent ad6e563 commit 53e243f
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### unreleased

* Add DiagnosticLog helper

* Add DatastorePath helper

### 0.12.0 (2016-12-01)
Expand Down
2 changes: 2 additions & 0 deletions govc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### unreleased

* Add '-f' flag to logs command

* Add storage support to vm.migrate

* Add support for file backed serialport devices
Expand Down
27 changes: 24 additions & 3 deletions govc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1604,20 +1604,35 @@ View VPX and ESX logs.
The '-log' option defaults to "hostd" when connected directly to a host or
when connected to VirtualCenter and a '-host' option is given. Otherwise,
the '-log' option defaults to "vpxd:vpxd.log". The '-host' option is ignored
when connected directly to host.
See 'govc logs.ls' for other '-log' options.
when connected directly to a host. See 'govc logs.ls' for other '-log' options.
Examples:
govc logs -n 1000 -f
govc logs -host esx1
govc logs -host esx1 -log vmkernel
Options:
-f=false Follow log file changes
-host= Host system [GOVC_HOST]
-log= Log file key
-n=25 Output the last N logs
-n=25 Output the last N log lines
```

## logs.download

```
Usage: govc logs.download [OPTIONS] [PATH]...
Generate diagnostic bundles.
A diagnostic bundle includes log files and other configuration information.
Use PATH to include a specific set of hosts to include.
Examples:
govc logs.download
govc logs.download host-a host-b
Options:
-default=true Specifies if the bundle should include the default server
```
Expand All @@ -1627,6 +1642,12 @@ Options:
```
Usage: govc logs.ls [OPTIONS]
List diagnostic log keys.
Examples:
govc logs.ls
govc logs.ls -host host-a
Options:
-host= Host system [GOVC_HOST]
```
Expand Down
1 change: 1 addition & 0 deletions govc/emacs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ during initialization.
Keybinding | Description
---------------|------------------------------------------------------------
<kbd>E</kbd> | Events via govc events -n `govc-max-events`
<kbd>L</kbd> | Logs via govc logs -n `govc-max-events`
<kbd>J</kbd> | JSON via govc host
<kbd>N</kbd> | Netstat via `govc-esxcli-netstat-info` with current host id
<kbd>c</kbd> | Connect new session for the current govc mode
Expand Down
11 changes: 10 additions & 1 deletion govc/emacs/govc.el
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ Also fixes the case where user contains an '@'."
(with-current-buffer (get-buffer current-prefix-arg)
(setq govc-session-url url))))

(defcustom govc-max-events 50
(defcustom govc-max-events 100
"Limit events output to the last N events."
:type 'integer
:group 'govc)
Expand All @@ -478,6 +478,14 @@ Also fixes the case where user contains an '@'."
(govc-command "events"
(list "-n" govc-max-events (if current-prefix-arg "-f") (govc-selection)))))

(defun govc-logs ()
"Logs via govc logs -n `govc-max-events'."
(interactive)
(govc-shell-command
(let ((host (govc-selection)))
(govc-command "logs"
(list "-n" govc-max-events (if current-prefix-arg "-f") (if host (list "-host" host)))))))

(defun govc-parse-info (output)
"Parse govc info command OUTPUT."
(let* ((entries)
Expand Down Expand Up @@ -656,6 +664,7 @@ Also fixes the case where user contains an '@'."
(defvar govc-host-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "E" 'govc-events)
(define-key map "L" 'govc-logs)
(define-key map "J" 'govc-host-json-info)
(define-key map "N" 'govc-host-esxcli-netstat)
(define-key map "c" 'govc-mode-new-session)
Expand Down
38 changes: 24 additions & 14 deletions govc/logs/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ package logs
import (
"context"
"flag"
"fmt"
"math"
"time"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
Expand All @@ -32,6 +31,8 @@ type logs struct {

Max int32
Key string

follow bool
}

func init() {
Expand All @@ -43,8 +44,9 @@ func (cmd *logs) Register(ctx context.Context, f *flag.FlagSet) {
cmd.HostSystemFlag.Register(ctx, f)

cmd.Max = 25 // default
f.Var(flags.NewInt32(&cmd.Max), "n", "Output the last N logs")
f.Var(flags.NewInt32(&cmd.Max), "n", "Output the last N log lines")
f.StringVar(&cmd.Key, "log", "", "Log file key")
f.BoolVar(&cmd.follow, "f", false, "Follow log file changes")
}

func (cmd *logs) Process(ctx context.Context) error {
Expand All @@ -60,8 +62,12 @@ func (cmd *logs) Description() string {
The '-log' option defaults to "hostd" when connected directly to a host or
when connected to VirtualCenter and a '-host' option is given. Otherwise,
the '-log' option defaults to "vpxd:vpxd.log". The '-host' option is ignored
when connected directly to host.
See 'govc logs.ls' for other '-log' options.`
when connected directly to a host. See 'govc logs.ls' for other '-log' options.
Examples:
govc logs -n 1000 -f
govc logs -host esx1
govc logs -host esx1 -log vmkernel`
}

func (cmd *logs) Run(ctx context.Context, f *flag.FlagSet) error {
Expand Down Expand Up @@ -91,20 +97,24 @@ func (cmd *logs) Run(ctx context.Context, f *flag.FlagSet) error {
key = defaultKey
}

// get LineEnd without any LineText
h, err := m.BrowseLog(ctx, host, key, math.MaxInt32, 0)
if err != nil {
return err
}
l := m.Log(ctx, host, key)

start := h.LineEnd - cmd.Max
h, err = m.BrowseLog(ctx, host, key, start, 0)
err = l.Seek(ctx, cmd.Max)
if err != nil {
return err
}

for _, line := range h.LineText {
fmt.Println(line)
for {
_, err = l.Copy(ctx, cmd.Out)
if err != nil {
return nil
}

if !cmd.follow {
break
}

<-time.After(time.Second)
}

return nil
Expand Down
12 changes: 12 additions & 0 deletions govc/logs/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ func (cmd *download) Usage() string {
return "[PATH]..."
}

func (cmd *download) Description() string {
return `Generate diagnostic bundles.
A diagnostic bundle includes log files and other configuration information.
Use PATH to include a specific set of hosts to include.
Examples:
govc logs.download
govc logs.download host-a host-b`
}

func (cmd *download) DownloadFile(c *vim25.Client, b string) error {
u, err := c.Client.ParseURL(b)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions govc/logs/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ func (cmd *ls) Process(ctx context.Context) error {
return nil
}

func (cmd *ls) Description() string {
return `List diagnostic log keys.
Examples:
govc logs.ls
govc logs.ls -host host-a`
}

func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
c, err := cmd.Client()
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion govc/test/logs.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ load test_helper
[ $nlogs -ge 1 ]

# test -n flag
run govc logs -n $((nlogs - 1))
run govc logs -n $((nlogs - 10))
assert_success
[ ${#lines[@]} -le $nlogs ]

Expand All @@ -20,6 +20,10 @@ load test_helper
# there should be plenty more than 1 line of vmkernel logs
[ $nlogs -ge 1 ]

# test > 1 call to BrowseLog()
run govc logs -n 2002
assert_success

# -host ignored against ESX
run govc logs -host enoent
assert_success
Expand Down
76 changes: 76 additions & 0 deletions object/diagnostic_log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright (c) 2015 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package object

import (
"context"
"fmt"
"io"
"math"
)

// DiagnosticLog wraps DiagnosticManager.BrowseLog
type DiagnosticLog struct {
m DiagnosticManager

Key string
Host *HostSystem

Start int32
}

// Seek to log position starting at the last nlines of the log
func (l *DiagnosticLog) Seek(ctx context.Context, nlines int32) error {
h, err := l.m.BrowseLog(ctx, l.Host, l.Key, math.MaxInt32, 0)
if err != nil {
return err
}

l.Start = h.LineEnd - nlines

return nil
}

// Copy log starting from l.Start to the given io.Writer
// Returns on error or when end of log is reached.
func (l *DiagnosticLog) Copy(ctx context.Context, w io.Writer) (int, error) {
const max = 500 // VC max == 500, ESX max == 1000
written := 0

for {
h, err := l.m.BrowseLog(ctx, l.Host, l.Key, l.Start, max)
if err != nil {
return 0, err
}

for _, line := range h.LineText {
n, err := fmt.Fprintln(w, line)
written += n
if err != nil {
return written, err
}
}

l.Start += int32(len(h.LineText))

if l.Start >= h.LineEnd {
break
}
}

return written, nil
}
8 changes: 8 additions & 0 deletions object/diagnostic_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func NewDiagnosticManager(c *vim25.Client) *DiagnosticManager {
return &m
}

func (m DiagnosticManager) Log(ctx context.Context, host *HostSystem, key string) *DiagnosticLog {
return &DiagnosticLog{
m: m,
Key: key,
Host: host,
}
}

func (m DiagnosticManager) BrowseLog(ctx context.Context, host *HostSystem, key string, start, lines int32) (*types.DiagnosticManagerLogHeader, error) {
req := types.BrowseDiagnosticLog{
This: m.Reference(),
Expand Down

0 comments on commit 53e243f

Please sign in to comment.