@@ -97,7 +97,8 @@ def collect(self, target=None, section='default'):
9797 if section not in self .config .keys ():
9898 print ("{} is not a valid section, using default" .format (section ))
9999 section = 'default'
100- metrics = {
100+ metric_list = {}
101+ metric_list ['vms' ] = {
101102 'vmware_vm_power_state' : GaugeMetricFamily (
102103 'vmware_vm_power_state' ,
103104 'VMWare VM Power state (On / Off)' ,
@@ -117,7 +118,9 @@ def collect(self, target=None, section='default'):
117118 'vmware_vm_num_cpu' : GaugeMetricFamily (
118119 'vmware_vm_num_cpu' ,
119120 'VMWare Number of processors in the virtual machine' ,
120- labels = ['vm_name' , 'host_name' ]),
121+ labels = ['vm_name' , 'host_name' ])
122+ }
123+ metric_list ['datastores' ] = {
121124 'vmware_datastore_capacity_size' : GaugeMetricFamily (
122125 'vmware_datastore_capacity_size' ,
123126 'VMWare Datasore capacity in bytes' ,
@@ -141,7 +144,9 @@ def collect(self, target=None, section='default'):
141144 'vmware_datastore_vms' : GaugeMetricFamily (
142145 'vmware_datastore_vms' ,
143146 'VMWare Virtual Machines number using this datastore' ,
144- labels = ['ds_name' ]),
147+ labels = ['ds_name' ])
148+ }
149+ metric_list ['hosts' ] = {
145150 'vmware_host_power_state' : GaugeMetricFamily (
146151 'vmware_host_power_state' ,
147152 'VMWare Host Power state (On / Off)' ,
@@ -167,6 +172,12 @@ def collect(self, target=None, section='default'):
167172 'VMWare Host Memory Max availability in Mbytes' ,
168173 labels = ['host_name' ]),
169174 }
175+ collect_subsystems = self ._collect_subsystems (section , metric_list .keys ())
176+
177+
178+ metrics = {}
179+ for s in collect_subsystems :
180+ metrics .update (metric_list [s ])
170181
171182 print ("[{0}] Start collecting vcenter metrics for {1}" .format (datetime .utcnow ().replace (tzinfo = pytz .utc ), target ))
172183
@@ -177,28 +188,34 @@ def collect(self, target=None, section='default'):
177188
178189 content = self .si .RetrieveContent ()
179190
180- # Get performance metrics counter information
181- counter_info = self ._vmware_perf_metrics (content )
191+ if 'vms' in collect_subsystems :
192+ # Get performance metrics counter information
193+ counter_info = self ._vmware_perf_metrics (content )
182194
183- # Fill Snapshots (count and age)
184- vm_counts , vm_ages = self ._vmware_get_snapshots (content )
185- for v in vm_counts :
186- metrics ['vmware_vm_snapshots' ].add_metric ([v ['vm_name' ]],
187- v ['snapshot_count' ])
188- for vm_age in vm_ages :
189- for v in vm_age :
190- metrics ['vmware_vm_snapshot_timestamp_seconds' ].add_metric ([v ['vm_name' ],
191- v ['vm_snapshot_name' ]],
192- v ['vm_snapshot_timestamp_seconds' ])
193195
194- # Fill Datastore
195- self ._vmware_get_datastores (content , metrics )
196+ # Fill VM Informations
197+ self ._vmware_get_vms (content , metrics , counter_info )
196198
197- # Fill VM Informations
198- self ._vmware_get_vms (content , metrics , counter_info )
199+ # Fill Snapshots (count and age)
200+ vm_counts , vm_ages = self ._vmware_get_snapshots (content )
201+ for v in vm_counts :
202+ metrics ['vmware_vm_snapshots' ].add_metric ([v ['vm_name' ]],
203+ v ['snapshot_count' ])
204+ for vm_age in vm_ages :
205+ for v in vm_age :
206+ metrics ['vmware_vm_snapshot_timestamp_seconds' ].add_metric ([v ['vm_name' ],
207+ v ['vm_snapshot_name' ]],
208+ v ['vm_snapshot_timestamp_seconds' ])
209+
210+
211+ # Fill Datastore
212+ if 'datastores' in collect_subsystems :
213+ self ._vmware_get_datastores (content , metrics )
199214
200215 # Fill Hosts Informations
201- self ._vmware_get_hosts (content , metrics )
216+ if 'hosts' in collect_subsystems :
217+ self ._vmware_get_hosts (content , metrics )
218+
202219
203220 print ("[{0}] Stop collecting vcenter metrics for {1}" .format (datetime .utcnow ().replace (tzinfo = pytz .utc ), target ))
204221
@@ -207,8 +224,27 @@ def collect(self, target=None, section='default'):
207224 for metricname , metric in metrics .items ():
208225 yield metric
209226
227+ def _collect_subsystems (self , section , valid_subsystems ):
228+ """
229+ Return the list of subsystems to collect - everything by default, a
230+ subset if the config section has collect_only specified
231+ """
232+ collect_subsystems = []
233+
234+ if not self .config [section ].get ('collect_only' ):
235+ collect_subsystems = valid_subsystems
236+ else :
237+ for subsystem in self .config [section ].get ('collect_only' ):
238+ if subsystem in valid_subsystems :
239+ collect_subsystems .append (subsystem )
240+ else :
241+ print ("invalid subsystem specified in collect_only: " + str (subsystem ))
210242
243+ if not collect_subsystems :
244+ print ("no valid subystems specified in collect_only, collecting everything" )
245+ collect_subsystems = valid_subsystems
211246
247+ return collect_subsystems
212248
213249 def _to_unix_timestamp (self , my_date ):
214250 return ((my_date - datetime (1970 ,1 ,1 ,tzinfo = pytz .utc )).total_seconds ())
0 commit comments