Ejemplo n.º 1
0
func (cmd *events) Run(ctx context.Context, f *flag.FlagSet) error {
	c, err := cmd.Client()
	if err != nil {
		return err
	}

	objs, err := cmd.ManagedObjects(ctx, f.Args())
	if err != nil {
		return err
	}

	if len(objs) > 0 {
		// need an event manager
		m := event.NewManager(c)

		// get the event stream
		err := m.Events(ctx, objs, cmd.Max, cmd.Tail, cmd.Force, func(ee []types.BaseEvent) error {
			err = cmd.printEvents(ctx, ee, m)
			if err != nil {
				return err
			}
			return nil
		})

		if err != nil {
			return err
		}

	}

	return nil
}
Ejemplo n.º 2
0
func NewCollector(client *vim25.Client, objects ...string) *EventCollector {
	ec := &EventCollector{
		vmwManager: vmwEvents.NewManager(client),
		mos:        monitoredCache{mos: make(map[string]types.ManagedObjectReference)},
	}

	for i := range objects {
		ec.AddMonitoredObject(objects[i])
	}

	return ec
}
Ejemplo n.º 3
0
// Start the event listener
func (mgr *vSphereManager) Start() error {
	// array of managed objects
	refs := mgr.monitoredObjects()

	// only continue if objects were added to monitor
	if len(refs) == 0 {
		return fmt.Errorf("EventManager requires at least one ManagedObject for monitoring, currently %d have been added", 0)
	}

	log.Debugf("EventManager starting event listener for %d managed objects...", len(refs))

	// create the govmomi view manager
	mgr.vmwManager = vmwEvents.NewManager(mgr.session.Client.Client)

	// we don't want the event listener to timeout
	ctx := context.Background()

	// vars used for clarity
	// page size of lastest page of events
	// setting to 1 to avoid having to track last event
	// processed -- this may change in future if too chatty

	// TODO: need to create an "event storm" to ensure we can handle
	// a 1 event page
	pageSize := int32(1)
	// bool to follow the stream
	followStream := true
	// don't exceed the govmomi object limit
	force := false

	//TODO: need a proper way to handle failures / status
	go func(page int32, follow bool, ff bool, refs []types.ManagedObjectReference) error {
		// the govmomi event listener can only be configured once per session -- so if it's already listening it
		// will be replaced
		//
		// the manager & listener will be closed with the session
		err := mgr.vmwManager.Events(ctx, refs, 1, followStream, force, func(page []types.BaseEvent) error {
			evented(mgr, page)
			return nil
		})
		// TODO: this will disappear in the ether
		if err != nil {
			log.Debugf("Error configuring event manager %s \n", err.Error())
			return err
		}
		return nil
	}(pageSize, followStream, force, refs)

	return nil
}
Ejemplo n.º 4
0
func (cmd *events) Run(f *flag.FlagSet) error {
	ctx := context.TODO()

	c, err := cmd.Client()
	if err != nil {
		return err
	}

	m := event.NewManager(c)

	objs, err := cmd.ManagedObjects(ctx, f.Args())
	if err != nil {
		return err
	}

	var events []types.BaseEvent

	for _, o := range objs {
		filter := types.EventFilterSpec{
			Entity: &types.EventFilterSpecByEntity{
				Entity:    o,
				Recursion: types.EventFilterSpecRecursionOptionAll,
			},
		}

		collector, err := m.CreateCollectorForEvents(ctx, filter)
		if err != nil {
			return fmt.Errorf("[%#v] %s", o, err)
		}
		defer collector.Destroy(ctx)

		err = collector.SetPageSize(ctx, cmd.Max)
		if err != nil {
			return err
		}

		page, err := collector.LatestPage(ctx)
		if err != nil {
			return err
		}

		events = append(events, page...)
	}

	event.Sort(events)

	for _, e := range events {
		cat, err := m.EventCategory(ctx, e)
		if err != nil {
			return err
		}

		event := e.GetEvent()
		msg := strings.TrimSpace(event.FullFormattedMessage)

		if t, ok := e.(*types.TaskEvent); ok {
			msg = fmt.Sprintf("%s (target=%s %s)", msg, t.Info.Entity.Type, t.Info.EntityName)
		}

		fmt.Fprintf(os.Stdout, "[%s] [%s] %s\n",
			event.CreatedTime.Local().Format(time.ANSIC),
			cat, msg)
	}

	return nil
}
Ejemplo n.º 5
0
func (cmd *events) Run(f *flag.FlagSet) error {
	ctx := context.TODO()

	finder, err := cmd.Finder()
	if err != nil {
		return err
	}

	c, err := cmd.Client()
	if err != nil {
		return err
	}

	m := event.NewManager(c)

	var objs []list.Element

	args := f.Args()
	if len(args) == 0 {
		args = []string{"."}
	}

	for _, arg := range args {
		es, err := finder.ManagedObjectList(ctx, arg)
		if err != nil {
			return err
		}

		objs = append(objs, es...)
	}

	var events []types.BaseEvent

	for _, o := range objs {
		filter := types.EventFilterSpec{
			Entity: &types.EventFilterSpecByEntity{
				Entity:    o.Object.Reference(),
				Recursion: types.EventFilterSpecRecursionOptionAll,
			},
		}

		collector, err := m.CreateCollectorForEvents(ctx, filter)
		if err != nil {
			return fmt.Errorf("[%s] %s", o.Path, err)
		}
		defer collector.Destroy(ctx)

		err = collector.SetPageSize(ctx, cmd.Max)
		if err != nil {
			return err
		}

		page, err := collector.LatestPage(ctx)
		if err != nil {
			return err
		}

		events = append(events, page...)
	}

	event.Sort(events)

	tw := tabwriter.NewWriter(os.Stdout, 3, 0, 2, ' ', 0)

	for _, e := range events {
		cat, err := m.EventCategory(ctx, e)
		if err != nil {
			return err
		}

		event := e.GetEvent()
		msg := strings.TrimSpace(event.FullFormattedMessage)

		if t, ok := e.(*types.TaskEvent); ok {
			msg = fmt.Sprintf("%s (target=%s %s)", msg, t.Info.Entity.Type, t.Info.EntityName)
		}

		fmt.Fprintf(tw, "[%s]\t[%s]\t%s\n",
			event.CreatedTime.Local().Format(time.ANSIC),
			cat, msg)
	}

	return tw.Flush()
}
Ejemplo n.º 6
0
func main() {

	vcUrl := flag.String("url", GetEnvString(envURL, ""), fmt.Sprintf("vCenter Connect URL [%s]", envURL))
	insecure := flag.Bool("insecure", GetEnvBool(envInsecure, false), fmt.Sprintf("Don't verify Certificate [%s]", envInsecure))

	flag.Parse()

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	url, err := url.Parse(*vcUrl)
	if err != nil {
		exit(err)
	}

	client, err := govmomi.NewClient(ctx, url, *insecure)
	if err != nil {
		exit(err)
	}

	finder := find.NewFinder(client.Client, false)

	dc, err := finder.DefaultDatacenter(ctx)

	if err != nil {
		exit(err)
	}

	filter := types.EventFilterSpec{
		Entity: &types.EventFilterSpecByEntity{
			Entity:    dc.Reference(),
			Recursion: types.EventFilterSpecRecursionOptionAll,
		},
	}

	eventManager := event.NewManager(client.Client)

	collector, err := eventManager.CreateCollectorForEvents(ctx, filter)
	defer collector.Destroy(ctx)

	collector.SetPageSize(ctx, 0)
	collector.Reset(ctx)

	for true {
		items, err := collector.ReadNextEvents(ctx, 10)

		if err != nil {
			exit(err)
		}

		for _, e := range items {
			category, _ := eventManager.EventCategory(ctx, e)
			event := e.GetEvent()
			msg := strings.TrimSpace(event.FullFormattedMessage)
			m := make(map[string]string)
			t := event.CreatedTime
			m["time"] = strconv.FormatInt(t.Unix(), 10)
			m["type"] = "Event"
			if t, ok := e.(*types.TaskEvent); ok {
				m["type"] = "Task"
				m["targetType"] = t.Info.Entity.Type
				m["targetName"] = t.Info.EntityName
			}
			m["message"] = msg
			m["category"] = category
			if event.Host != nil && event.Host.Name != "" {
				m["host"] = event.Host.Name
			}
			if event.Vm != nil && event.Vm.Name != "" {
				m["vm"] = event.Vm.Name
			}
			if event.UserName != "" {
				m["username"] = event.UserName
			}
			s, _ := json.Marshal(m)
			fmt.Printf("%s\n", string(s))
		}
		time.Sleep(10 * 1000 * 1000 * 1000)

	}
}