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)
Beispiel #2
0
class SampleDiscovery(object):
    """
    Description: Demonstrates all vSphere Stats discovery APIs which
    give current state of the system.
    Sample Prerequisites:
    vCenter 7.0x with 7.0x ESXi hosts.
    """
    def __init__(self):
        parser = sample_cli.build_arg_parser()
        args = sample_util.process_cli_args(parser.parse_args())

        stub_config = get_configuration(args.server, args.username,
                                        args.password, args.skipverification)
        self.counters_client = Counters(stub_config)
        self.providers_client = Providers(stub_config)
        self.resource_types_client = ResourceTypes(stub_config)
        self.counter_metadata_client = CounterMetadata(stub_config)
        self.metrics_client = Metrics(stub_config)
        self.counter_sets_client = CounterSets(stub_config)
        self.resource_address_schemas_client = ResourceAddressSchemas(
            stub_config)

    def run(self):
        """
        Access the Discovery APIs to
        List - counters, countermetadata, providers, resource types,
               metrics, counter sets.
        Get - resource address schema.
        """
        # Counters List.
        counters = self.counters_client.list()
        SampleDiscovery.print_output("Counters List", counters)

        # Choose a random counter and provide cid as input to
        # list CounterMetaData.
        random_cid = random.choice(counters).cid
        # List of counter metadata associated with that counter.
        counter_metadata = self.counter_metadata_client.list(random_cid)
        SampleDiscovery.print_output("Counter Metadata List", counter_metadata)

        # Choose a random counter and provide resource_address_schema_id as
        # input to get Resource Address Schema.
        random_resource_address_schema_id = random.choice(counters).\
            resource_address_schema
        # Get resource address schema associated with that counter.
        resource_address_schema = self.resource_address_schemas_client.get(
            random_resource_address_schema_id)
        SampleDiscovery.print_output("Resource Address Schema",
                                     resource_address_schema)

        # List of vSphere Stats providers connected to vCenter Server.
        providers = self.providers_client.list()
        SampleDiscovery.print_output("Providers List", providers)

        # List of resource types supported by vSphere Stats.
        resource_types = self.resource_types_client.list()
        SampleDiscovery.print_output("Resource Types List", resource_types)

        # List of metrics supported by vSphere Stats.
        metrics = self.metrics_client.list()
        SampleDiscovery.print_output("Metrics List", metrics)

        # List of vSphere Stats defined Counter-sets.
        counter_sets = self.counter_sets_client.list()
        SampleDiscovery.print_output("Counter Sets List", counter_sets)

    @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)