-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
68 lines (56 loc) · 1.29 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
package main
import (
"context"
"flag"
"fmt"
"net/url"
"os"
"runtime"
"time"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/vim25/soap"
"github.com/vsphere/cron"
"github.com/vsphere/g"
)
func main() {
cfg := flag.String("c", "cfg.json", "configuration file")
version := flag.Bool("v", false, "show version")
flag.Parse()
if *version {
fmt.Println(g.VERSION)
os.Exit(0)
}
g.InitLog("info")
g.ParseConfig(*cfg)
g.ParseExtendConfig(g.Config().Extend)
go g.ReloadConfig(*cfg)
go g.ReloadExtendConfig(g.Config().Extend)
g.InitRPCClients()
t := time.NewTicker(time.Duration(g.Config().Transfer.Interval) * time.Second)
defer t.Stop()
for {
for _, vc := range g.Config().Vsphere {
go vcCollect(vc)
}
g.Log.Debugln("[main.go] the number of goroutine:", runtime.NumGoroutine())
<-t.C
}
}
func vcCollect(vc *g.VsphereConfig) {
ctx := context.Background()
u, err := soap.ParseURL(vc.Addr)
if err != nil {
g.Log.Errorln("[main.go]", err)
}
u.User = url.UserPassword(vc.User, vc.Pwd)
c, err := govmomi.NewClient(ctx, u, true)
if err != nil {
g.Log.Warnln("[main.go] the vc", vc.Addr, "connection error:", err)
return
}
if c.IsVC() {
g.Log.Infoln("[main.go] the vc", vc.Addr, "connection successful!")
}
cron.Collect(ctx, c, vc)
defer c.Logout(ctx)
}