def add_vm(self): r = Resources() project_ids = Resources.get_project_id(r, target=self.target, token=self.token) for vm in Resources.get_virtualmachines(r, target=self.target, token=self.token, parentid=self.uuid): if vm['uuid'] in project_ids: if os.environ['DEBUG'] >= '2': print(vm['name'] + ' has project id: ' + project_ids['project_id']) self.vms.append( VirtualMachine(target=self.target, token=self.token, name=vm['name'], uuid=vm['uuid'], project_id=project_ids['project_id'])) else: self.vms.append( VirtualMachine(target=self.target, token=self.token, name=vm['name'], uuid=vm['uuid'], project_id='default internal'))
def add_datastore(self): r = Resources() for ds in r.get_datastores(target=self.target, token=self.token, parentid=self.uuid): self.datastores.append( Datastore(target=self.target, token=self.token, name=ds['name'], uuid=ds['uuid']))
def add_vm(self): r = Resources() for vm in Resources.get_virtualmachines(r, target=self.target, token=self.token, parentid=self.uuid): self.vms.append( VirtualMachine(target=self.target, token=self.token, name=vm['name'], uuid=vm['uuid']))
def add_host(self): r = Resources() for hosts in Resources.get_hosts(r, target=self.target, token=self.token, parentid=self.uuid): self.hosts.append( Host(target=self.target, token=self.token, name=hosts['name'], uuid=hosts['uuid']))
def add_cluster(self): r = Resources() for cluster in Resources.get_cluster(r, target=self.target, token=self.token, parentid=self.uuid): self.clusters.append( Cluster(target=self.target, token=self.token, name=cluster['name'], uuid=cluster['uuid']))
def add_datacenter(self): r = Resources() for dc in Resources.get_datacenter(r, target=self.target, token=self.token, parentid=self.uuid): self.datacenter.append( Datacenter(target=self.target, token=self.token, name=dc['name'], uuid=dc['uuid']))
def do_metrics(self, target, gauges, infos, states): token = self.get_target_tokens() token = token[target] if not token: print("skipping", target, "in", self.name, ", no token") uuids = self.target_clusters[target] for metric_suffix in gauges: propkey = gauges[metric_suffix]['property'] values = Resources.get_latest_number_properties_multiple(target, token, uuids, propkey) if not values: continue for value_entry in values: if 'data' not in value_entry: continue metric_value = value_entry['data'] cluster_id = value_entry['resourceId'] gauges[metric_suffix]['gauge'].add_metric( labels=[self.clusters[cluster_id]['name'], self.clusters[cluster_id]['parent_dc_name'].lower()], value=metric_value) for metric_suffix in states: propkey = states[metric_suffix]['property'] values = Resources.get_latest_enum_properties_multiple(target, token, uuids, propkey) if not values: continue for value_entry in values: if 'value' not in value_entry: continue metric_value = (1 if states[metric_suffix]['expected'] == value_entry['value'] else 0) cluster_id = value_entry['resourceId'] states[metric_suffix]['state'].add_metric( labels=[self.clusters[cluster_id]['name'], self.clusters[cluster_id]['parent_dc_name'].lower(), value_entry['value']], value=metric_value) for metric_suffix in infos: propkey = infos[metric_suffix]['property'] values = Resources.get_latest_info_properties_multiple(target, token, uuids, propkey) if not values: continue for value_entry in values: if 'data' not in value_entry: continue cluster_id = value_entry['resourceId'] info_value = value_entry['data'] infos[metric_suffix]['info'].add_metric( labels=[self.clusters[cluster_id]['name'], self.clusters[cluster_id]['parent_dc_name'].lower()], value={metric_suffix: info_value})
def create_resource_objects(self, vrops, token): for adapter in Resources.get_adapter(target=vrops, token=token): if os.environ['DEBUG'] >= '2': print("Collecting vcenter: " + adapter['name']) vcenter = Vcenter(target=vrops, token=token, name=adapter['name'], uuid=adapter['uuid']) vcenter.add_datacenter() for dc_object in vcenter.datacenter: if os.environ['DEBUG'] >= '2': print("Collecting Datacenter: " + dc_object.name) dc_object.add_cluster() for cl_object in dc_object.clusters: if os.environ['DEBUG'] >= '2': print("Collecting Cluster: " + cl_object.name) cl_object.add_host() for hs_object in cl_object.hosts: if os.environ['DEBUG'] >= '2': print("Collecting Host: " + hs_object.name) hs_object.add_datastore() for ds_object in hs_object.datastores: if os.environ['DEBUG'] >= '2': print("Collecting Datastore: " + ds_object.name) hs_object.add_vm() for vm_object in hs_object.vms: if os.environ['DEBUG'] >= '2': print("Collecting VM: " + vm_object.name) return vcenter
def collect(self): g = GaugeMetricFamily('vrops_cluster_stats', 'testtest', labels=['datacenter', 'vccluster', 'statkey']) if os.environ['DEBUG'] >= '1': print('ClusterStatsCollector starts with collecting the metrics') for target in self.get_clusters_by_target(): token = self.get_target_tokens() token = token[target] if not token: print("skipping " + target + " in " + self.name + ", no token") uuids = self.target_clusters[target] for statkey_pair in self.statkey_yaml["ClusterStatsCollector"]: statkey_label = statkey_pair['label'] statkey = statkey_pair['statkey'] values = Resources.get_latest_stat_multiple(target, token, uuids, statkey) if not values: print("skipping statkey " + str(statkey) + " in ClusterStatsCollector, no return") continue for value_entry in values: #data = value_entry['data'] metric_value = value_entry['stat-list']['stat'][0]['data'][0] cluster_id = value_entry['resourceId'] g.add_metric( labels=[self.clusters[cluster_id]['parent_dc_name'], self.clusters[cluster_id]['name'], statkey_label], value=metric_value) # self.post_metrics(g.name) yield g
def collect(self): g = GaugeMetricFamily('vrops_datastore_stats', 'testtext', labels=['datacenter', 'vccluster', 'hostsystem', 'datastore', 'statkey']) if os.environ['DEBUG'] >= '1': print(self.__class__.__name__ + " starts with collecting the metrics") #make one big request per stat id with all resource id's in its belly for target in self.get_datastores_by_target(): token = self.get_target_tokens() token = token[target] if not token: print("skipping " + target + " in " + self.__class__.__name__ + " , no token") uuids = self.target_datastores[target] for statkey_pair in self.statkey_yaml[self.__class__.__name__]: statkey_label = statkey_pair['label'] statkey = statkey_pair['statkey'] values = Resources.get_latest_stat_multiple(target, token, uuids, statkey) if not values: print("skipping statkey " + str(statkey) + " in " + self.__class__.__name__ + " , no return") continue for value_entry in values: #there is just one, because we are querying latest only metric_value = value_entry['stat-list']['stat'][0]['data'][0] datastore_id = value_entry['resourceId'] g.add_metric(labels=[self.datastores[datastore_id]['datacenter'], self.datastores[datastore_id]['cluster'], self.datastores[datastore_id]['parent_host_name'], self.datastores[datastore_id]['name'], statkey_label], value=metric_value) # self.post_metrics(self.g.name) yield g
def do_metrics(self, target, gauges): token = self.get_target_tokens() token = token[target] if not token: print("skipping " + target + " in " + self.name + ", no token") uuids = self.target_datastores[target] for metric_suffix in gauges: statkey = gauges[metric_suffix]['statkey'] values = Resources.get_latest_stat_multiple( target, token, uuids, statkey) if not values: print("skipping statkey " + str(statkey) + " in", self.name, ", no return") continue for value_entry in values: metric_value = value_entry['stat-list']['stat'][0]['data'] if metric_value: metric_value = metric_value[0] datastore_id = value_entry['resourceId'] gauges[metric_suffix]['gauge'].add_metric( labels=[ self.datastores[datastore_id]['name'], self.datastores[datastore_id] ['datacenter'].lower(), self.datastores[datastore_id]['cluster'], self.datastores[datastore_id]['parent_host_name'] ], value=metric_value)
def get_project_ids_by_target(self): project_ids = dict() for target in self.get_vms_by_target(): token = self.get_target_tokens() token = token[target] uuids = self.target_vms[target] project_ids[target] = Resources.get_project_ids(target, token, uuids) return project_ids
def query_vrops(self, vrops): if os.environ['DEBUG'] >= '1': print("querying " + vrops) token = Resources.get_token(target=vrops) if not token: return False self.target_tokens[vrops] = token vcenter = self.create_resource_objects(vrops, token) self.vcenter_dict[vrops] = vcenter return True
def do_metrics(self, target, gauges, infos, states): token = self.get_target_tokens() token = token[target] if not token: print("skipping", target, "in", self.name, ", no token") for vc in self.get_vcenters(): uuid = self.vcenters[vc]['uuid'] for metric_suffix in gauges: propkey = gauges[metric_suffix]['property'] metric_value = Resources.get_property(target, token, uuid, propkey) if not metric_value: continue gauges[metric_suffix]['gauge'].add_metric( labels=[self.vcenters[vc]['name']], value=metric_value) for metric_suffix in states: propkey = states[metric_suffix]['property'] value = Resources.get_property(target, token, uuid, propkey) if not value: continue metric_value = (1 if states[metric_suffix]['expected'] == value else 0) states[metric_suffix]['state'].add_metric( labels=[self.vcenters[vc]['name'], value], value=metric_value) for metric_suffix in infos: propkey = infos[metric_suffix]['property'] info_value = Resources.get_property(target, token, uuid, propkey) if not info_value: continue infos[metric_suffix]['info'].add_metric( labels=[self.vcenters[vc]['name']], value={metric_suffix: info_value})
def collect(self): g = GaugeMetricFamily('vrops_vm_stats', 'testtext', labels=[ 'vccluster', 'datacenter', 'virtualmachine', 'hostsystem', 'statkey' ]) if os.environ['DEBUG'] >= '1': print('VMStatsCollector starts with collecting the metrics') # #make one big request per stat id with all resource id's in its belly for target in self.get_vms_by_target(): token = self.get_target_tokens() token = token[target] if not token: print("skipping " + target + " in VMStatsCollector, no token") uuids = self.target_vms[target] for statkey_pair in self.statkey_yaml["VMStatsCollector"]: statkey_label = statkey_pair['label'] statkey = statkey_pair['statkey'] values = Resources.get_latest_stat_multiple( target, token, uuids, statkey) if not values: print("skipping statkey " + str(statkey) + " in VMStatsCollector, no return") continue for value_entry in values: if 'resourceId' not in value_entry: continue #there is just one, because we are querying latest only metric_value = value_entry['stat-list']['stat'][0]['data'] if not metric_value: continue vm_id = value_entry['resourceId'] if vm_id not in self.vms: continue g.add_metric(labels=[ self.vms[vm_id]['cluster'], self.vms[vm_id]['datacenter'], self.vms[vm_id]['name'], self.vms[vm_id]['parent_host_name'], statkey_label ], value=metric_value[0]) # self.post_metrics(g.name) yield g
def do_metrics(self, target, gauges): token = self.get_target_tokens() token = token[target] if not token: print("skipping " + target + " in", self.name, ", no token") for vc in self.get_vcenters(): uuid = self.vcenters[vc]['uuid'] for metric_suffix in gauges: statkey = gauges[metric_suffix]['statkey'] values = Resources.get_latest_stat(target, token, uuid, statkey) if not values: print("skipping statkey " + str(statkey) + " in", self.name, ", no return") continue metric_value = float(values) gauges[metric_suffix]['gauge'].add_metric(labels=[self.vcenters[vc]['name']], value=metric_value)
def do_metrics(self, target, gauges, project_ids): token = self.get_target_tokens() token = token[target] if not token: print("skipping " + target + " in " + self.name + ", no token") uuids = self.target_vms[target] for metric_suffix in gauges: statkey = gauges[metric_suffix]['statkey'] values = Resources.get_latest_stat_multiple( target, token, uuids, statkey) if os.environ['DEBUG'] >= '1': print(target, statkey) print("amount uuids", str(len(uuids))) print("fetched ", str(len(values))) if not values: print("skipping statkey " + str(statkey) + " in", self.name, ", no return") continue for value_entry in values: metric_value = value_entry['stat-list']['stat'][0]['data'] if metric_value: metric_value = metric_value[0] vm_id = value_entry['resourceId'] project_id = "internal" if project_ids: for vm_id_project_mapping in project_ids: if vm_id in vm_id_project_mapping: project_id = vm_id_project_mapping[vm_id] gauges[metric_suffix]['gauge'].add_metric( labels=[ self.vms[vm_id]['name'], self.vms[vm_id]['datacenter'].lower(), self.vms[vm_id]['cluster'], self.vms[vm_id]['parent_host_name'], project_id ], value=metric_value)
def do_metrics(self, target, gauges, infos, states, project_ids): token = self.get_target_tokens() token = token[target] if not token: print("skipping", target, "in", self.name, ", no token") uuids = self.target_vms[target] for metric_suffix in gauges: propkey = gauges[metric_suffix]['property'] values = Resources.get_latest_number_properties_multiple( target, token, uuids, propkey) if not values: continue for value_entry in values: if 'data' not in value_entry: continue metric_value = value_entry['data'] vm_id = value_entry['resourceId'] project_id = "internal" if project_ids: for vm_id_project_mapping in project_ids: if vm_id in vm_id_project_mapping: project_id = vm_id_project_mapping[vm_id] gauges[metric_suffix]['gauge'].add_metric(labels=[ self.vms[vm_id]['name'], self.vms[vm_id]['datacenter'].lower(), self.vms[vm_id]['cluster'], self.vms[vm_id]['parent_host_name'], project_id ], value=metric_value) for metric_suffix in states: propkey = states[metric_suffix]['property'] values = Resources.get_latest_enum_properties_multiple( target, token, uuids, propkey) if not values: continue for value_entry in values: if 'value' not in value_entry: continue data = (1 if states[metric_suffix]['expected'] == value_entry['value'] else 0) vm_id = value_entry['resourceId'] project_id = "internal" if project_ids: for vm_id_project_mapping in project_ids: if vm_id in vm_id_project_mapping: project_id = vm_id_project_mapping[vm_id] states[metric_suffix]['state'].add_metric(labels=[ self.vms[vm_id]['name'], self.vms[vm_id]['datacenter'].lower(), self.vms[vm_id]['cluster'], self.vms[vm_id]['parent_host_name'], value_entry['value'], project_id ], value=data) for metric_suffix in infos: propkey = infos[metric_suffix]['property'] values = Resources.get_latest_info_properties_multiple( target, token, uuids, propkey) if not values: continue for value_entry in values: if 'data' not in value_entry: continue vm_id = value_entry['resourceId'] project_id = "internal" if project_ids: for vm_id_project_mapping in project_ids: if vm_id in vm_id_project_mapping: project_id = vm_id_project_mapping[vm_id] info_value = value_entry['data'] infos[metric_suffix]['info'].add_metric( labels=[ self.vms[vm_id]['name'], self.vms[vm_id]['datacenter'].lower(), self.vms[vm_id]['cluster'], self.vms[vm_id]['parent_host_name'], project_id ], value={metric_suffix: info_value})
def collect(self): g = GaugeMetricFamily('vrops_vm_properties', 'testtest', labels=[ 'vccluster', 'datacenter', 'virtualmachine', 'hostsystem', 'propkey' ]) i = InfoMetricFamily( 'vrops_vm', 'testtest', labels=['vccluster', 'datacenter', 'virtualmachine', 'hostsystem']) if os.environ['DEBUG'] >= '1': print(self.name, 'starts with collecting the metrics') for target in self.get_vms_by_target(): token = self.get_target_tokens() token = token[target] if not token: print("skipping", target, "in", self.name, ", no token") uuids = self.target_vms[target] if 'number_metrics' in self.property_yaml[self.name]: for property_pair in self.property_yaml[ self.name]['number_metrics']: property_label = property_pair['label'] propkey = property_pair['property'] values = Resources.get_latest_number_properties_multiple( target, token, uuids, propkey) if not values: continue for value_entry in values: if 'data' not in value_entry: continue data = value_entry['data'] vm_id = value_entry['resourceId'] if vm_id not in self.vms: continue g.add_metric(labels=[ self.vms[vm_id]['cluster'], self.vms[vm_id]['datacenter'], self.vms[vm_id]['name'], self.vms[vm_id]['parent_host_name'], property_label ], value=data) if 'enum_metrics' in self.property_yaml[self.name]: for property_pair in self.property_yaml[ self.name]['enum_metrics']: property_label = property_pair['label'] propkey = property_pair['property'] expected_state = property_pair['expected'] values = Resources.get_latest_enum_properties_multiple( target, token, uuids, propkey, expected_state) if not values: continue for value_entry in values: if 'data' not in value_entry: continue data = value_entry['data'] vm_id = value_entry['resourceId'] latest_state = value_entry['latest_state'] if vm_id not in self.vms: continue g.add_metric(labels=[ self.vms[vm_id]['cluster'], self.vms[vm_id]['datacenter'], self.vms[vm_id]['name'], self.vms[vm_id]['parent_host_name'], property_label + ": " + latest_state ], value=data) if 'info_metrics' in self.property_yaml[self.name]: for property_pair in self.property_yaml[ self.name]['info_metrics']: property_label = property_pair['label'] propkey = property_pair['property'] values = Resources.get_latest_info_properties_multiple( target, token, uuids, propkey) if not values: continue for value_entry in values: if 'data' not in value_entry: continue vm_id = value_entry['resourceId'] info_value = value_entry['data'] if vm_id not in self.vms: continue i.add_metric(labels=[ self.vms[vm_id]['cluster'], self.vms[vm_id]['datacenter'], self.vms[vm_id]['name'], self.vms[vm_id]['parent_host_name'] ], value={property_label: info_value}) # self.post_metrics(g.name) # self.post_metrics(i.name + '_info') yield g yield i