Search in sources :

Example 26 with ServiceStat

use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.

the class VSphereAdapterStatsService method mockStats.

private List<ServiceStat> mockStats() {
    List<ServiceStat> res = new ArrayList<>(2);
    ServiceStat m1 = new ServiceStat();
    m1.lastUpdateMicrosUtc = System.currentTimeMillis() * 1000;
    m1.name = "cpu.mock";
    m1.latestValue = 17;
    res.add(m1);
    ServiceStat m2 = new ServiceStat();
    m2.lastUpdateMicrosUtc = System.currentTimeMillis() * 1000;
    m2.name = "memory.mock";
    m2.latestValue = 11;
    res.add(m2);
    return res;
}
Also used : ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) ArrayList(java.util.ArrayList)

Example 27 with ServiceStat

use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.

the class StatsClientTest method test.

@Test
public void test() throws Exception {
    String url = System.getProperty(TestProperties.VC_URL);
    if (url == null) {
        return;
    }
    String username = System.getProperty(TestProperties.VC_USERNAME);
    String password = System.getProperty(TestProperties.VC_PASSWORD);
    BasicConnection conn = new BasicConnection();
    conn.setURI(URI.create(url));
    conn.setUsername(username);
    conn.setPassword(password);
    conn.setIgnoreSslErrors(true);
    conn.setRequestTimeout(30, TimeUnit.SECONDS);
    conn.connect();
    StatsClient client = new StatsClient(conn);
    List<ServiceStat> metrics;
    ManagedObjectReference vm = new ManagedObjectReference();
    vm.setType(VimNames.TYPE_VM);
    vm.setValue("vm-49");
    metrics = client.retrieveMetricsForVm(vm);
    this.logger.info("vm metrics " + metrics);
    ManagedObjectReference host = new ManagedObjectReference();
    host.setType(VimNames.TYPE_HOST);
    host.setValue("host-504");
    metrics = client.retrieveMetricsForHost(host);
    this.logger.info("host metrics " + metrics);
}
Also used : ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) BasicConnection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.BasicConnection) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Test(org.junit.Test) EnumerationClientTest(com.vmware.photon.controller.model.adapters.vsphere.EnumerationClientTest)

Example 28 with ServiceStat

use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.

the class StatsClient method querySingleEntity.

/**
 * See <a href="https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.PerformanceManager.html#queryStats">queryStats method</a>
 * @param ctx
 * @return
 * @throws RuntimeFaultFaultMsg
 */
private List<ServiceStat> querySingleEntity(StatCollectionContext ctx) throws RuntimeFaultFaultMsg {
    List<PerfEntityMetricBase> metrics = getVimPort().queryPerf(PERF_MGR_MOREF, Collections.singletonList(ctx.getSpec()));
    List<ServiceStat> res = new ArrayList<>();
    if (metrics.isEmpty()) {
        // nothing fetched
        return Collections.emptyList();
    }
    // the metrics for the single entity
    PerfEntityMetric m = (PerfEntityMetric) metrics.get(0);
    for (PerfMetricSeries pms : m.getValue()) {
        PerfMetricId metricId = pms.getId();
        PerfMetricIntSeries series = (PerfMetricIntSeries) pms;
        SamplesAggregator factory = ctx.getFactory(metricId.getCounterId());
        PerfCounterInfo counter = this.perfCounterLookup.getCounterByKey(metricId.getCounterId());
        ServiceStat stat = factory.createStat(counter, m.getSampleInfo(), series.getValue());
        if (stat != null) {
            res.add(stat);
        }
    }
    return res;
}
Also used : ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) PerfMetricIntSeries(com.vmware.vim25.PerfMetricIntSeries) PerfCounterInfo(com.vmware.vim25.PerfCounterInfo) ArrayOfPerfCounterInfo(com.vmware.vim25.ArrayOfPerfCounterInfo) PerfMetricSeries(com.vmware.vim25.PerfMetricSeries) PerfMetricId(com.vmware.vim25.PerfMetricId) ArrayList(java.util.ArrayList) PerfEntityMetricBase(com.vmware.vim25.PerfEntityMetricBase) PerfEntityMetric(com.vmware.vim25.PerfEntityMetric)

Example 29 with ServiceStat

use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.

the class SingleResourceStatsAggregationTaskServiceTest method testResourceStatsAggregation.

@Test
public void testResourceStatsAggregation() throws Throwable {
    // create a resource pool
    ResourcePoolState rpState = new ResourcePoolState();
    rpState.name = "testName";
    ResourcePoolState rpReturnState = postServiceSynchronously(ResourcePoolService.FACTORY_LINK, rpState, ResourcePoolState.class);
    ComputeState[] computeStateArray = new ComputeState[NUM_COMPUTE_RESOURCES];
    for (int i = 0; i < NUM_COMPUTE_RESOURCES; i++) {
        ComputeDescription cDesc = new ComputeDescription();
        cDesc.name = rpState.name;
        cDesc.statsAdapterReference = UriUtils.buildUri(this.host, MockStatsAdapter.SELF_LINK);
        ComputeDescription descReturnState = postServiceSynchronously(ComputeDescriptionService.FACTORY_LINK, cDesc, ComputeDescription.class);
        ComputeState computeState = new ComputeState();
        computeState.name = rpState.name;
        computeState.descriptionLink = descReturnState.documentSelfLink;
        computeState.resourcePoolLink = rpReturnState.documentSelfLink;
        computeStateArray[i] = postServiceSynchronously(ComputeService.FACTORY_LINK, computeState, ComputeState.class);
    }
    StatsCollectionTaskState collectionTaskState = new StatsCollectionTaskState();
    collectionTaskState.resourcePoolLink = rpReturnState.documentSelfLink;
    collectionTaskState.taskInfo = TaskState.createDirect();
    int counter = 0;
    // executing stats collection multiple times
    while (counter < NUM_COLLECTIONS) {
        this.postServiceSynchronously(StatsCollectionTaskService.FACTORY_LINK, collectionTaskState, StatsCollectionTaskState.class);
        counter++;
    }
    // wait for stats to be populated
    this.host.waitFor("Error waiting for stats", () -> {
        boolean returnStatus = false;
        for (int i = 0; i < NUM_COMPUTE_RESOURCES; i++) {
            String statsUriPath = UriUtils.buildUriPath(computeStateArray[i].documentSelfLink, ServiceHost.SERVICE_URI_SUFFIX_STATS);
            ServiceStats resStats = getServiceSynchronously(statsUriPath, ServiceStats.class);
            for (ServiceStat stat : resStats.entries.values()) {
                if (stat.name.startsWith(UriUtils.getLastPathSegment(MockStatsAdapter.SELF_LINK))) {
                    returnStatus = true;
                    break;
                }
            }
        }
        return returnStatus;
    });
    // number of keys = 2, number of lastCollectionTimeMetrics = 2
    int numberOfRawMetrics = NUM_COLLECTIONS * NUM_COMPUTE_RESOURCES * 2 * 2;
    // kick off an aggregation task
    SingleResourceStatsAggregationTaskState aggregationTaskState = new SingleResourceStatsAggregationTaskState();
    aggregationTaskState.resourceLink = computeStateArray[0].documentSelfLink;
    aggregationTaskState.metricNames = new HashSet<>(Arrays.asList(MockStatsAdapter.KEY_1, MockStatsAdapter.KEY_2, MockStatsAdapter.KEY_3));
    postServiceSynchronously(SingleResourceStatsAggregationTaskService.FACTORY_LINK, aggregationTaskState, SingleResourceStatsAggregationTaskState.class);
    long expectedExpirationTime = Utils.getNowMicrosUtc() + TimeUnit.DAYS.toMicros(DEFAULT_RETENTION_LIMIT_DAYS);
    this.host.waitFor("Error waiting for rolled up stats", () -> {
        ServiceDocumentQueryResult result = this.host.getExpandedFactoryState(UriUtils.buildUri(this.host, ResourceMetricsService.FACTORY_LINK));
        String randomDocumentLink = result.documentLinks.get(0);
        ResourceMetrics metric = Utils.fromJson(result.documents.get(randomDocumentLink), ResourceMetrics.class);
        // Make sure all the documents have expiration time set.
        Assert.assertTrue("Expiration time is not correctly set.", metric.documentExpirationTimeMicros < expectedExpirationTime);
        return (result.documentCount == 2 + numberOfRawMetrics);
    });
    String statsUriPath = UriUtils.buildUriPath(computeStateArray[0].documentSelfLink, ServiceHost.SERVICE_URI_SUFFIX_STATS);
    ServiceStats resStats = getServiceSynchronously(statsUriPath, ServiceStats.class);
    int statCount = 0;
    for (ServiceStat stat : resStats.entries.values()) {
        if (stat.name.startsWith(MockStatsAdapter.KEY_1) || stat.name.startsWith(MockStatsAdapter.KEY_2)) {
            statCount++;
        }
    }
    Assert.assertEquals("Did not find in-memory stats", 2, statCount);
    // verify that the aggregation tasks have been deleted
    this.host.waitFor("Timeout waiting for task to expire", () -> {
        ServiceDocumentQueryResult res = this.host.getFactoryState(UriUtils.buildUri(this.host, SingleResourceStatsAggregationTaskService.FACTORY_LINK));
        return res.documentLinks.size() == 0;
    });
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) SingleResourceStatsAggregationTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsAggregationTaskService.SingleResourceStatsAggregationTaskState) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) ServiceStats(com.vmware.xenon.common.ServiceStats) StatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.StatsCollectionTaskService.StatsCollectionTaskState) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Example 30 with ServiceStat

use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.

the class StatsCollectionTaskServiceTest method testCustomStatsAdapter.

@Test
public void testCustomStatsAdapter() throws Throwable {
    ResourcePoolState rpState = new ResourcePoolState();
    rpState.name = UUID.randomUUID().toString();
    ResourcePoolState rpReturnState = postServiceSynchronously(ResourcePoolService.FACTORY_LINK, rpState, ResourcePoolState.class);
    ComputeDescription desc = new ComputeDescription();
    desc.name = rpState.name;
    desc.statsAdapterReferences = Collections.singleton(UriUtils.buildUri(this.host, CustomStatsAdapter.SELF_LINK));
    ComputeDescription descReturnState = postServiceSynchronously(ComputeDescriptionService.FACTORY_LINK, desc, ComputeDescription.class);
    ComputeState computeState = new ComputeState();
    computeState.name = rpState.name;
    computeState.descriptionLink = descReturnState.documentSelfLink;
    computeState.resourcePoolLink = rpReturnState.documentSelfLink;
    List<String> computeLinks = new ArrayList<>();
    for (int i = 0; i < this.numResources; i++) {
        ComputeState res = postServiceSynchronously(ComputeService.FACTORY_LINK, computeState, ComputeState.class);
        computeLinks.add(res.documentSelfLink);
    }
    // create a stats collection scheduler task
    StatsCollectionTaskState statCollectionState = new StatsCollectionTaskState();
    statCollectionState.resourcePoolLink = rpReturnState.documentSelfLink;
    statCollectionState.statsAdapterReference = UriUtils.buildUri(this.host, CustomStatsAdapter.SELF_LINK);
    statCollectionState.options = EnumSet.of(TaskOption.SELF_DELETE_ON_COMPLETION);
    ScheduledTaskState statsCollectionTaskState = new ScheduledTaskState();
    statsCollectionTaskState.factoryLink = StatsCollectionTaskService.FACTORY_LINK;
    statsCollectionTaskState.initialStateJson = Utils.toJson(statCollectionState);
    statsCollectionTaskState.intervalMicros = TimeUnit.MILLISECONDS.toMicros(500);
    statsCollectionTaskState = postServiceSynchronously(ScheduledTaskService.FACTORY_LINK, statsCollectionTaskState, ScheduledTaskState.class);
    ServiceDocumentQueryResult res = this.host.getFactoryState(UriUtils.buildExpandLinksQueryUri(UriUtils.buildUri(this.host, ScheduledTaskService.FACTORY_LINK)));
    assertTrue(res.documents.size() > 0);
    // get stats from resources
    for (int i = 0; i < computeLinks.size(); i++) {
        String statsUriPath = UriUtils.buildUriPath(computeLinks.get(i), ServiceHost.SERVICE_URI_SUFFIX_STATS);
        this.host.waitFor("Error waiting for stats", () -> {
            ServiceStats resStats = getServiceSynchronously(statsUriPath, ServiceStats.class);
            boolean returnStatus = false;
            // was populated in the in memory stats
            for (ServiceStat stat : resStats.entries.values()) {
                if (stat.name.startsWith(UriUtils.getLastPathSegment(CustomStatsAdapter.SELF_LINK))) {
                    returnStatus = true;
                    break;
                }
            }
            return returnStatus;
        });
    }
    // clean up
    deleteServiceSynchronously(statsCollectionTaskState.documentSelfLink);
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) ArrayList(java.util.ArrayList) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) ServiceStats(com.vmware.xenon.common.ServiceStats) ScheduledTaskState(com.vmware.photon.controller.model.tasks.ScheduledTaskService.ScheduledTaskState) StatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.StatsCollectionTaskService.StatsCollectionTaskState) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Aggregations

ServiceStat (com.vmware.xenon.common.ServiceStats.ServiceStat)37 ArrayList (java.util.ArrayList)21 ComputeStats (com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats)16 SingleResourceStatsCollectionTaskState (com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState)15 List (java.util.List)11 Map (java.util.Map)11 ComputeStatsRequest (com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest)10 ResourceMetrics (com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics)10 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)9 Operation (com.vmware.xenon.common.Operation)9 QueryTask (com.vmware.xenon.services.common.QueryTask)9 HashMap (java.util.HashMap)9 ServiceStats (com.vmware.xenon.common.ServiceStats)8 UriUtils (com.vmware.xenon.common.UriUtils)8 URI (java.net.URI)8 PhotonModelConstants (com.vmware.photon.controller.model.constants.PhotonModelConstants)7 ComputeStateWithDescription (com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription)7 ServiceDocument (com.vmware.xenon.common.ServiceDocument)7 StatelessService (com.vmware.xenon.common.StatelessService)7 Utils (com.vmware.xenon.common.Utils)7