-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
129 lines (106 loc) · 2.89 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
@author: Saad Zaher
*/
package main
import (
"context"
"flag"
"fmt"
"os"
"net/url"
"reflect"
"time"
"github.com/vmware/govmomi/event"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/types"
)
var (
begin time.Duration
end time.Duration
follow bool
vcenterUrl string
insecure bool
username string
password string
eventCount int
)
func init(){
flag.DurationVar(&begin, "b", 10 * time.Minute, "Start time of events to be streamed")
flag.DurationVar(&end, "e", 0, "End time of events to be streamed")
flag.BoolVar(&follow, "f", false, "Follow event stream")
flag.StringVar(&vcenterUrl, "url", "", "Vcenter URL. i.e. https://localhost/sdk")
flag.StringVar(&username, "u", "[email protected]", "Vcenter Username")
flag.StringVar(&password, "p", "", "Vcenter password")
flag.BoolVar(&insecure, "i", true, "Insecure")
flag.IntVar(&eventCount, "c", 100,"Number of events to fetch every time.")
}
func main() {
// example use against simulator: go run main.go -b 8h -f
// example use against vCenter with optional event filters:
// go run main.go -url $GOVMOMI_URL -insecure $GOVMOMI_INSECURE -b 8h -f VmEvent UserLoginSessionEvent
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
flag.Parse()
if vcenterUrl == ""{
fmt.Fprintf(os.Stderr, "-url vCenter url is Required\n")
os.Exit(1)
}
if password == "" {
fmt.Fprintf(os.Stderr, "-p Password is required\n")
os.Exit(1)
}
u, _ :=url.Parse(vcenterUrl)
u.User = url.UserPassword(username, password)
c, err := govmomi.NewClient(ctx, u, true)
if err != nil{
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
c.Login(ctx, u.User)
m := event.NewManager(c.Client)
ref := c.ServiceContent.RootFolder
now, err := methods.GetCurrentTime(ctx, c) // vCenter server time (UTC)
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
filter := types.EventFilterSpec{
EventTypeId: flag.Args(), // e.g. VmEvent
Entity: &types.EventFilterSpecByEntity{
Entity: ref,
Recursion: types.EventFilterSpecRecursionOptionAll,
},
Time: &types.EventFilterSpecByTime{
BeginTime: types.NewTime(now.Add(begin * -1)),
},
}
if end != 0 {
filter.Time.EndTime = types.NewTime(now.Add(end * -1))
}
collector, err := m.CreateCollectorForEvents(ctx, filter)
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
defer collector.Destroy(ctx)
for {
events, err := collector.ReadNextEvents(ctx, int32(eventCount))
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
if len(events) == 0 {
if follow {
time.Sleep(time.Second)
continue
}
break
}
for i := range events {
event := events[i].GetEvent()
kind := reflect.TypeOf(events[i]).Elem().Name()
fmt.Printf("%d [%s] [%s] %s\n", event.Key, event.CreatedTime.Format(time.ANSIC), kind, event.FullFormattedMessage)
}
}
}