class SampleQueryDataPointsSetID(object): """ Description: Demonstrates querying data points filtered by resource by creating Acquisition Specification using Counter SetId. Sample Prerequisites: vCenter 7.0x with 7.0x ESXi hosts. """ def __init__(self): args = sample_util.process_cli_args(parser.parse_args()) self.interval = int(args.interval) self.expiration = int(args.expiration) stub_config = get_configuration(args.server, args.username, args.password, args.skipverification) self.counter_sets_client = CounterSets(stub_config) self.acq_specs_client = AcqSpecs(stub_config) self.data_client = Data(stub_config) session = get_unverified_session() if args.skipverification else None self.vsphere_client = create_vsphere_client(server=args.server, username=args.username, password=args.password, session=session) def run(self): wait_time = 30 vm_type = "VM" memo = "user definition of acquisition spec" # Get Counter-set ID of VM counters which is provided as # Acquisition Specification setId. counter_sets = self.counter_sets_client.list() set_id = list( filter(lambda cs: cs.counters[0].cid.endswith("." + vm_type), counter_sets))[0].id counter_spec_obj = self.acq_specs_client.CounterSpec(set_id=set_id) # Choose a random VM from which stats data needs to be collected. vm_list = self.vsphere_client.vcenter.VM.list() vm_obj = random.choice(vm_list) vm_id = vm_obj.vm rsrc_obj = RsrcId(id_value=vm_id, type=vm_type) # Create an Acquisition Specification for all the VM counters using # "setId" in CreateSpec. acq_spec_obj = self.acq_specs_client.CreateSpec( counters=counter_spec_obj, resources=[rsrc_obj], interval=self.interval, expiration=self.expiration, memo_=memo) acq_spec_id = self.acq_specs_client.create(acq_spec_obj) SampleQueryDataPointsSetID.print_output( "Acquisition Specification created with ID: " + acq_spec_id) # Get the Acquisition Specification. acq_spec_info = self.acq_specs_client.get(acq_spec_id) SampleQueryDataPointsSetID.print_output( "Details of Acquisition Specification with ID: " + acq_spec_id, acq_spec_info) # Wait for 30 seconds for data collection to happen. time.sleep(wait_time) # Query for data points filtered by resource. resource = "type." + vm_type + "=" + vm_id filter_spec = self.data_client.FilterSpec(resources=[resource]) data_points = self.data_client.query_data_points(filter=filter_spec) SampleQueryDataPointsSetID.print_output("Data Points collected", data_points) # CleanUp. # Delete the Acquisition Specification. self.acq_specs_client.delete(acq_spec_id) SampleQueryDataPointsSetID.print_output( "Acquisition Specification with ID: " + acq_spec_id + " is deleted") @staticmethod def print_output(*argv): print("------------------------------------") for arg in argv: print(arg)
class SampleQueryDataPoints(object): """ Description: Demonstrates end to end workflow of vSphere Stats. Step 1: Creation of an Acquisition Specification. Step 2: Query for data points filtered by cid. Sample Prerequisites: vCenter 7.0x with 7.0x ESXi hosts. """ def __init__(self): args = sample_util.process_cli_args(parser.parse_args()) self.interval = int(args.interval) self.expiration = int(args.expiration) stub_config = get_configuration(args.server, args.username, args.password, args.skipverification) self.acq_specs_client = AcqSpecs(stub_config) self.data_client = Data(stub_config) session = get_unverified_session() if args.skipverification else None self.vsphere_client = create_vsphere_client(server=args.server, username=args.username, password=args.password, session=session) def run(self): cid = "disk.throughput.usage.VM" wait_time = 30 vm_type = "VM" memo = "user definition of acquisition spec" cid_mid_obj = CidMid(cid=cid) counter_spec_obj = self.acq_specs_client.CounterSpec( cid_mid=cid_mid_obj) # Choose a random VM from which stats data needs to be collected. vm_list = self.vsphere_client.vcenter.VM.list() vm_obj = random.choice(vm_list) vm_id = vm_obj.vm rsrc_obj = RsrcId(id_value=vm_id, type=vm_type) # Create an Acquisition Specification for VM counter. acq_spec_obj = self.acq_specs_client.CreateSpec( counters=counter_spec_obj, resources=[rsrc_obj], interval=self.interval, expiration=self.expiration, memo_=memo) acq_spec_id = self.acq_specs_client.create(acq_spec_obj) SampleQueryDataPoints.print_output( "Acquisition Specification created with ID: " + acq_spec_id) # Get the Acquisition Specification. acq_spec_info = self.acq_specs_client.get(acq_spec_id) SampleQueryDataPoints.print_output( "Details of Acquisition Specification with ID " + acq_spec_id, acq_spec_info) # Wait for 30 seconds for data collection to happen. time.sleep(wait_time) # Query for data points filtered by cid. filter_spec = self.data_client.FilterSpec(cid=cid) data_points = self.data_client.query_data_points(filter=filter_spec) SampleQueryDataPoints.print_output("Data Points collected", data_points) # CleanUp. # Delete the Acquisition Specification. self.acq_specs_client.delete(acq_spec_id) SampleQueryDataPoints.print_output( "Acquisition Specification with ID: " + acq_spec_id + " is deleted") @staticmethod def print_output(*argv): print("------------------------------------") for arg in argv: print(arg)
class SampleQueryDataPointsPredicate(object): """ Description: Demonstrates querying data points filtered by cid by creating Acquisition Specification using Query Predicate "ALL". Sample Prerequisites: vCenter 7.0x with 7.0x ESXi hosts. """ def __init__(self): args = sample_util.process_cli_args(parser.parse_args()) self.interval = int(args.interval) self.expiration = int(args.expiration) stub_config = get_configuration(args.server, args.username, args.password, args.skipverification) self.acq_specs_client = AcqSpecs(stub_config) self.data_client = Data(stub_config) self.providers_client = Providers(stub_config) def run(self): cid = "disk.throughput.usage.VM" wait_time = 30 vm_type = "VM" host_type = "HOST" memo = "user definition of acquisition spec" cid_mid_obj = CidMid(cid=cid) counter_spec_obj = self.acq_specs_client.CounterSpec( cid_mid=cid_mid_obj) # To collect stats data from all VMs on a particular host, provide # QueryPredicate "ALL" for VM resourceId. rsrc_obj1 = RsrcId(id_value="", type=vm_type, predicate="ALL") # Choose a random host. providers = self.providers_client.list() random_host_id = random.choice(providers).id_value rsrc_obj2 = RsrcId(id_value=random_host_id, type=host_type) # Create an Acquisition Specification to collect stats data from all # VMs on a host using QueryPredicate "ALL". acq_spec_obj = self.acq_specs_client.CreateSpec( counters=counter_spec_obj, resources=[rsrc_obj1, rsrc_obj2], interval=self.interval, expiration=self.expiration, memo_=memo) acq_spec_id = self.acq_specs_client.create(acq_spec_obj) SampleQueryDataPointsPredicate.print_output( "Acquisition Specification created with ID: " + acq_spec_id) # Get the Acquisition Specification. acq_spec_info = self.acq_specs_client.get(acq_spec_id) SampleQueryDataPointsPredicate.print_output( "Details of Acquisition Specification with ID: " + acq_spec_id, acq_spec_info) # Wait for 30 seconds for data collection to happen. time.sleep(wait_time) # Query for data points filtered by cid. filter_spec = self.data_client.FilterSpec(cid=cid) data_points = self.data_client.query_data_points(filter=filter_spec) SampleQueryDataPointsPredicate.print_output("Data points collected", data_points) # CleanUp. # Delete the Acquisition Specification. self.acq_specs_client.delete(acq_spec_id) SampleQueryDataPointsPredicate.print_output( "Acquisition Specification with ID: " + acq_spec_id + " is deleted") @staticmethod def print_output(*argv): print("------------------------------------") for arg in argv: print(arg)
class SampleAcquisitionSpecLifecycle(object): """ Demonstrates create, get, list, update and delete operations of Acquisition Specifications. Sample Prerequisites: vCenter 7.0x with 7.0x ESXi hosts. """ def __init__(self): args = sample_util.process_cli_args(parser.parse_args()) self.interval = int(args.interval) self.expiration = int(args.expiration) stub_config = get_configuration(args.server, args.username, args.password, args.skipverification) self.providers_client = Providers(stub_config) self.acq_specs_client = AcqSpecs(stub_config) def run(self): cid = "cpu.capacity.demand.HOST" new_cid = "mem.capacity.usage.HOST" host_type = "HOST" memo = "user definition of acquisition spec" # The counter and associated resources can be chosen using discovery # APIs. Please refer to samples in discovery package to obtain this # metadata. In this sample, we create an Acquisition # Specification for a HOST counter. cid_mid_obj = CidMid(cid=cid) counter_spec_obj = self.acq_specs_client.CounterSpec( cid_mid=cid_mid_obj) # Choose a random host from which stats data needs to be collected. providers = self.providers_client.list() random_host_id = random.choice(providers).id_value host_resource_id_obj = RsrcId(id_value=random_host_id, type=host_type) acq_spec_obj = self.acq_specs_client.CreateSpec( counters=counter_spec_obj, resources=[host_resource_id_obj], interval=self.interval, expiration=self.expiration, memo_=memo) # Create an Acquisition Specification. acq_spec_id = self.acq_specs_client.create(acq_spec_obj) SampleAcquisitionSpecLifecycle.print_output( "Acquisition Specification created with ID: " + acq_spec_id) # List Acquisition Specifications. SampleAcquisitionSpecLifecycle.print_output( "List of Acquisition Specifications", self.acq_specs_client.list()) # Update the existing Acquisition Specification by only modifying the # intended field in UpdateSpec, keeping all other fields as it is. cid_mid_obj = CidMid(cid=new_cid) counter_spec_obj = self.acq_specs_client.CounterSpec( cid_mid=cid_mid_obj) updated_acq_spec_obj = self.acq_specs_client.UpdateSpec( counters=counter_spec_obj, resources=[host_resource_id_obj], interval=self.interval, expiration=self.expiration, memo_=memo) self.acq_specs_client.update(acq_spec_id, updated_acq_spec_obj) SampleAcquisitionSpecLifecycle.print_output( "Updated Acquisition Specification", self.acq_specs_client.get(acq_spec_id)) # Get the Acquisition Specification. acq_spec_info = self.acq_specs_client.get(acq_spec_id) SampleAcquisitionSpecLifecycle.print_output( "Details of Acquisition Specification with ID " + acq_spec_id, acq_spec_info) # Delete Acquisition Specification. self.acq_specs_client.delete(acq_spec_id) SampleAcquisitionSpecLifecycle.print_output( "Acquisition Specification with ID: " + acq_spec_id + " is deleted") @staticmethod def print_output(*argv): print("------------------------------------") for arg in argv: print(arg)