Bandwidth summary per server

2019-03-02 03:32发布

问题:

I am trying to get the bandwidth data for all the softlayer servers under my account.

Thanks to account_servers.rb I am able to get the server id for all the servers. Now I would like to get the Bandwidth used by the servers for a particular time frame. The data that I am interested is

http://sldn.softlayer.com/reference/datatypes/SoftLayer_Metric_Tracking_Object_Bandwidth_Summary .

I tried to get information using softlayer_client.service_named("Metric_Tracking_Object_Bandwidth_Summary"). Unfortunately I am not able to get the details.

I did find a java code, but I am interested in ruby code. Can someone please guide me to get the server bandwith summary?

Getting bandWidth data in SL

回答1:

Please, try the following Ruby examples:

require 'rubygems'
require 'softlayer_api'


server_id = 11498369
# Your SoftLayer API username.
SL_API_USERNAME = 'set me'

# Your SoftLayer API key.
SL_API_KEY = 'set me'

softlayer_client = SoftLayer::Client.new(:username => SL_API_USERNAME,
                                         :api_key => SL_API_KEY)

vsi_service = softlayer_client.service_named('SoftLayer_Virtual_Guest')
metric_tracking_object_id = vsi_service.object_with_id(server_id).getMetricTrackingObjectId

metric_service = softlayer_client.service_named('SoftLayer_Metric_Tracking_Object')
service_ref = metric_service.object_with_id(metric_tracking_object_id)

begin
  object_template = [{
      'keyName' => 'PUBLICOUT',
      'summaryType' => 'sum'
  }]

  result = service_ref.getSummaryData('2016-03-29T00:00:00','2016-03-30T00:00:00',object_template,600)
  puts result.inspect

rescue => e
  puts 'Error when executing the script...'

  $stdout.print(e.inspect)
end

References:

SoftLayer_Metric_Tracking_Object::getSummaryData

SoftLayer_Virtual_Guest::getMetricTrackingObjectId

Second example using SoftLayer_Virtual_Gues::getBandwidthDataByDate:

require 'rubygems'
require 'softlayer_api'
require 'pp'
require 'date'

# Set the server id that you wish to get Bandwidth information.
server_id = 11498369

softlayer_client = SoftLayer::Client.new(:username => 'set me',
                                         :api_key => 'set me')

server = SoftLayer::VirtualServer.server_with_id(server_id, :client => softlayer_client)


get_bandwidth_data_by_date = server.service.getBandwidthDataByDate('2016-03-29T00:00:00','2016-03-30T00:00:00','public')
pp('getBandwidthDataByDate: ', get_bandwidth_data_by_date)

References:

SoftLayer_Virtual_Guest::getBandwidthDataByDate



回答2:

Disclamer: I created my own Ruby SoftLayer client, you can check it at http://github.com/zertico/softlayer specially for situations like this one where you want to access some specific data (and I'm not SoftLayer staff ;) )

If you'd like to give it a try the code that solves your problem is

ps: I'm considering you are manipulating a SoftLayer_Hardware_Server, right?

hardware = Softlayer::Hardware::Server.find(123)
hardware.get_current_bandwidth_summary

mask = 'mask[currentBandwidthSummary]'
hardware = Softlayer::Hardware::Server.mask(mask).find(123)
hardware.current_bandwidth_summary

You will access a ruby object like this one:

=> #<Softlayer::Metric::Tracking::Object::Bandwidth::Summary:0x007ff74b683540
 @allocation_amount="1234",
 @allocation_id=111111,
 @amount_out="12.34567",
 @average_daily_usage="1.23",
 @currently_over_allocation_flag=0,
 @id=1111111,
 @outbound_bandwidth_amount="123.45678",
 @projected_bandwidth_usage="123.45",
 @projected_over_allocation_flag=0>

Hope it can helps you, comment if you have any doubt about the client usage