Getting bandWidth data in SL

2020-05-09 17:47发布

I am trying to get a bandwidth usage using Java Client API of Softlayer. The result of the API below is the throughput of bandwidth. Is there any API getting a bandwidth usage data with java ?

List<Data> dataList = Guest.service(client, deviceID).getBandwidthDataByDate(startDate, endDate, "public"); // throuput

Pls advice me why the bandwidth throughput data with this API are different from the data of control.softlayer.com. Is there any critical factor to get precise data?

1条回答
我命由我不由天
2楼-- · 2020-05-09 17:59

Try this code:

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import com.google.gson.Gson;
import com.softlayer.api.ApiClient;
import com.softlayer.api.RestApiClient;
import com.softlayer.api.service.virtual.Guest;
import com.softlayer.api.service.container.metric.data.Type;
import com.softlayer.api.service.metric.tracking.Object;;

public class GetBandwidth {

    public static void main(String[] args) {
        String user = "set me";
        String apikey = "set me";

        // the VSI id
        Long vsiId = new Long(15003381);

        // Declare the API client
        ApiClient client = new RestApiClient().withCredentials(user, apikey);
        Guest.Service vsiService = Guest.service(client, vsiId);

        // Declaring the object mask to get information about the items
        vsiService.setMask("mask[id,metricTrackingObjectId]");

        try {
            Long metrictId = vsiService.getObject().getMetricTrackingObjectId();
            Object.Service objectService = Object.service(client, metrictId); 
            List<Type> validTypes = new ArrayList<Type>();
            Type typeIn = new Type();
            typeIn.setName("PUBLICin");
            typeIn.setKeyName("PUBLICIN");
            typeIn.setSummaryType("sum");
            Type typeOut = new Type();
            typeOut.setName("PUBLICout");
            typeOut.setKeyName("PUBLICOUT");
            typeOut.setSummaryType("sum");
            validTypes.add(typeIn);
            validTypes.add(typeOut);
            GregorianCalendar startDateTime = new GregorianCalendar(2016, 02, 04, 0, 0, 0);
            GregorianCalendar endDateTime = new GregorianCalendar(2016, 02, 18, 11, 59, 59);
            Long summaryPeriod = new Long(1800);



            Gson gson = new Gson();
            System.out.println(gson.toJson(objectService.getSummaryData(startDateTime, endDateTime, validTypes, summaryPeriod)));

        } catch (Exception e) {
            System.out.println("Unable to retrieve the bandwidht. "
                    + e.getMessage());
        }

    }

}
查看更多
登录 后发表回答