master
go 161 lines 4.08 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package vsphere
4
5 import (
6 "errors"
7 "fmt"
8
9 "github.com/netdata/netdata/go/plugins/plugin/go.d/collector/vsphere/client"
10 "github.com/netdata/netdata/go/plugins/plugin/go.d/collector/vsphere/discover"
11 "github.com/netdata/netdata/go/plugins/plugin/go.d/collector/vsphere/match"
12 "github.com/netdata/netdata/go/plugins/plugin/go.d/collector/vsphere/scrape"
13 )
14
15 func (c *Collector) validateConfig() error {
16 const minRecommendedUpdateEvery = 20
17
18 if c.URL == "" {
19 return errors.New("config option url is required")
20 }
21 if c.Username == "" || c.Password == "" {
22 return errors.New("config options username and password are required")
23 }
24 if c.UpdateEvery < minRecommendedUpdateEvery {
25 c.Warningf("config option update_every=%d is lower than recommended minimum %d", c.UpdateEvery, minRecommendedUpdateEvery)
26 }
27 if c.DiscoveryInterval.Duration() <= 0 {
28 return errors.New("config option discovery_interval must be greater than zero")
29 }
30 if len(c.TagCategories) > 0 {
31 m, err := match.NewPatternListMatcher("tag_categories", c.TagCategories)
32 if err != nil {
33 return err
34 }
35 c.vsphereTagCategoryMatcher = m
36 }
37 if len(c.CustomAttributes) > 0 {
38 m, err := match.NewPatternListMatcher("custom_attributes", c.CustomAttributes)
39 if err != nil {
40 return err
41 }
42 c.customAttributeMatcher = m
43 }
44 if err := c.validateDatastoreClusterConfig(); err != nil {
45 return err
46 }
47 if err := c.validateVSANConfig(); err != nil {
48 return err
49 }
50 return nil
51 }
52
53 func (c *Collector) validateDatastoreClusterConfig() error {
54 if !c.CollectDatastoreClusters {
55 return nil
56 }
57 if len(c.DatastoreClustersInclude) == 0 {
58 c.DatastoreClustersInclude = match.DatastoreClusterIncludes{"/*"}
59 }
60 m, err := c.DatastoreClustersInclude.Parse()
61 if err != nil {
62 return err
63 }
64 c.datastoreClusterMatcher = m
65 return nil
66 }
67
68 func (c *Collector) validateVSANConfig() error {
69 if !c.CollectVSAN {
70 return nil
71 }
72 if len(c.VSANClustersInclude) == 0 {
73 c.VSANClustersInclude = match.VSANClusterIncludes{"/*"}
74 }
75 vsanClusterMatcher, err := c.VSANClustersInclude.Parse()
76 if err != nil {
77 return err
78 }
79 c.vsanClusterMatcher = vsanClusterMatcher
80 if len(c.VSANHostsInclude) == 0 {
81 c.VSANHostsInclude = match.VSANHostIncludes{"/*"}
82 }
83 vsanHostMatcher, err := c.VSANHostsInclude.Parse()
84 if err != nil {
85 return err
86 }
87 c.vsanHostMatcher = vsanHostMatcher
88 if len(c.VSANVMsInclude) == 0 {
89 c.VSANVMsInclude = match.VSANVMIncludes{"/*"}
90 }
91 vsanVMMatcher, err := c.VSANVMsInclude.Parse()
92 if err != nil {
93 return err
94 }
95 c.vsanVMMatcher = vsanVMMatcher
96 return nil
97 }
98
99 func (c *Collector) initClient() (*client.Client, error) {
100 config := client.Config{
101 URL: c.URL,
102 User: c.Username,
103 Password: c.Password,
104 Timeout: c.Timeout.Duration(),
105 TLSConfig: c.ClientConfig.TLSConfig,
106 }
107 return client.New(config)
108 }
109
110 func (c *Collector) initDiscoverer(cli *client.Client) error {
111 d := discover.New(cli)
112 d.Logger = c.Logger
113 d.CollectDatastoreClusters = c.CollectDatastoreClusters
114 d.CollectVSAN = c.CollectVSAN
115 d.CollectNetworkTopology = c.CollectNetworkTopology
116 d.DatastoreClusterMatcher = c.datastoreClusterMatcher
117 d.TagCategoryMatcher = c.vsphereTagCategoryMatcher
118 d.CustomAttributeMatcher = c.customAttributeMatcher
119
120 hm, err := c.HostsInclude.Parse()
121 if err != nil {
122 return fmt.Errorf("parse config option host_include: %w", err)
123 }
124 if hm != nil {
125 d.HostMatcher = hm
126 }
127 vmm, err := c.VMsInclude.Parse()
128 if err != nil {
129 return fmt.Errorf("parse config option vm_include: %w", err)
130 }
131 if vmm != nil {
132 d.VMMatcher = vmm
133 }
134 dsm, err := c.DatastoresInclude.Parse()
135 if err != nil {
136 return fmt.Errorf("parse config option datastore_include: %w", err)
137 }
138 if dsm != nil {
139 d.DatastoreMatcher = dsm
140 }
141
142 cm, err := c.ClustersInclude.Parse()
143 if err != nil {
144 return fmt.Errorf("parse config option cluster_include: %w", err)
145 }
146 if cm != nil {
147 d.ClusterMatcher = cm
148 }
149
150 c.discoverer = d
151 return nil
152 }
153
154 func (c *Collector) initScraper(cli *client.Client) {
155 ms := scrape.New(cli)
156 ms.Logger = c.Logger
157 c.scraper = ms
158 c.dsPropertyCollector = cli
159 c.clusterPropertyCollector = cli
160 c.rpPropertyCollector = cli
161 }