Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to filter events by type #976

Merged
merged 1 commit into from
Dec 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions event/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@ func (m Manager) EventCategory(ctx context.Context, event types.BaseEvent) (stri
}

// Get the events from the specified object(s) and optionanlly tail the event stream
func (m Manager) Events(ctx context.Context, objects []types.ManagedObjectReference, pageSize int32, tail bool, force bool, f func(types.ManagedObjectReference, []types.BaseEvent) error) error {

func (m Manager) Events(ctx context.Context, objects []types.ManagedObjectReference, pageSize int32, tail bool, force bool, f func(types.ManagedObjectReference, []types.BaseEvent) error, kind ...string) error {
// TODO: deprecated this method and add one that uses a single config struct, so we can extend further without breaking the method signature.
if len(objects) >= m.maxObjects && !force {
return fmt.Errorf("Maximum number of objects to monitor (%d) exceeded, refine search", m.maxObjects)
}

proc := newEventProcessor(m, pageSize, f)
proc := newEventProcessor(m, pageSize, f, kind)
for _, o := range objects {
proc.addObject(ctx, o)
}
Expand Down
5 changes: 4 additions & 1 deletion event/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ type tailInfo struct {
type eventProcessor struct {
mgr Manager
pageSize int32
kind []string
tailers map[types.ManagedObjectReference]*tailInfo // tailers by collector ref
callback func(types.ManagedObjectReference, []types.BaseEvent) error
}

func newEventProcessor(mgr Manager, pageSize int32, callback func(types.ManagedObjectReference, []types.BaseEvent) error) *eventProcessor {
func newEventProcessor(mgr Manager, pageSize int32, callback func(types.ManagedObjectReference, []types.BaseEvent) error, kind []string) *eventProcessor {
return &eventProcessor{
mgr: mgr,
tailers: make(map[types.ManagedObjectReference]*tailInfo),
callback: callback,
pageSize: pageSize,
kind: kind,
}
}

Expand All @@ -53,6 +55,7 @@ func (p *eventProcessor) addObject(ctx context.Context, obj types.ManagedObjectR
Entity: obj,
Recursion: types.EventFilterSpecRecursionOptionAll,
},
EventTypeId: p.kind,
}

collector, err := p.mgr.CreateCollectorForEvents(ctx, filter)
Expand Down
33 changes: 29 additions & 4 deletions govc/events/command.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2015 VMware, Inc. All Rights Reserved.
Copyright (c) 2015-2017 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.
Expand All @@ -23,6 +23,7 @@ import (
"fmt"
"io"
"os"
"reflect"
"strings"
"time"

Expand All @@ -40,6 +41,19 @@ type events struct {
Max int32
Tail bool
Force bool
Long bool
Kind kinds
}

type kinds []string

func (e *kinds) String() string {
return fmt.Sprint(*e)
}

func (e *kinds) Set(value string) error {
*e = append(*e, value)
return nil
}

func init() {
Expand All @@ -55,6 +69,8 @@ func (cmd *events) Register(ctx context.Context, f *flag.FlagSet) {
f.Var(flags.NewInt32(&cmd.Max), "n", "Output the last N events")
f.BoolVar(&cmd.Tail, "f", false, "Follow event stream")
f.BoolVar(&cmd.Force, "force", false, "Disable number objects to monitor limit")
f.BoolVar(&cmd.Long, "l", false, "Long listing format")
f.Var(&cmd.Kind, "type", "Include only the specified event types")
}

func (cmd *events) Description() string {
Expand All @@ -63,6 +79,7 @@ func (cmd *events) Description() string {
Examples:
govc events vm/my-vm1 vm/my-vm2
govc events /dc1/vm/* /dc2/vm/*
govc events -type VmPoweredOffEvent -type VmPoweredOnEvent
govc ls -t HostSystem host/* | xargs govc events | grep -i vsan`
}

Expand Down Expand Up @@ -101,6 +118,10 @@ func (cmd *events) printEvents(ctx context.Context, obj *types.ManagedObjectRefe
Message: strings.TrimSpace(event.FullFormattedMessage),
}

if cmd.Long {
r.Type = reflect.TypeOf(e).Elem().Name()
}

// if this is a TaskEvent gather a little more information
if t, ok := e.(*types.TaskEvent); ok {
// some tasks won't have this information, so just use the event message
Expand All @@ -118,6 +139,7 @@ func (cmd *events) printEvents(ctx context.Context, obj *types.ManagedObjectRefe

type record struct {
Object string `json:",omitempty"`
Type string `json:",omitempty"`
CreatedTime time.Time
Category string
Message string
Expand All @@ -129,8 +151,11 @@ func writeEventAsJSON(w io.Writer, r *record) error {

func writeEvent(w io.Writer, r *record) error {
when := r.CreatedTime.Local().Format(time.ANSIC)

_, err := fmt.Fprintf(w, "[%s] [%s] %s\n", when, r.Category, r.Message)
var kind string
if r.Type != "" {
kind = fmt.Sprintf(" [%s]", r.Type)
}
_, err := fmt.Fprintf(w, "[%s] [%s]%s %s\n", when, r.Category, kind, r.Message)
return err
}

Expand Down Expand Up @@ -166,7 +191,7 @@ func (cmd *events) Run(ctx context.Context, f *flag.FlagSet) error {
return err
}
return nil
})
}, cmd.Kind...)

if err != nil {
return err
Expand Down
15 changes: 15 additions & 0 deletions govc/test/events.bats
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ load test_helper
run govc events vm
assert_success
[ ${#lines[@]} -ge $nevents ]

run govc events -type VmPoweredOffEvent -type VmPoweredOnEvent "vm/$vm"
[ ${#lines[@]} -eq 0 ]

run govc vm.power -on "$vm"
assert_success

run govc events -type VmPoweredOffEvent -type VmPoweredOnEvent "vm/$vm"
[ ${#lines[@]} -eq 1 ]

run govc vm.power -off "$vm"
assert_success

run govc events -type VmPoweredOffEvent -type VmPoweredOnEvent "vm/$vm"
[ ${#lines[@]} -eq 2 ]
Copy link
Member

@hickeng hickeng Dec 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could do with an additional VM operation (guest operation or reconfigure or something) so that you can assert that you do not get an event for it. Or use the power events but omit -type VmPoweredOffEvent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Several events such as VmCreatedEvent, VmMacAssignedEvent, etc, are being filtered out here.. but yes could make it more explicit within the test.

}

@test "events json" {
Expand Down