我正在尝试获取帐户下所有软件层服务器的带宽数据。
感谢account_servers.rb
我能够获取所有服务器的服务器ID。现在我想获得服务器在特定时间范围内使用的带宽。我感兴趣的数据是
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Metric_Tracking_Object_Bandwidth_Summary
我尝试使用softlayer_client.service_named("Metric_Tracking_Object_Bandwidth_Summary")
获取信息。不幸的是我无法获得详细信息。
我确实找到了一个java代码,但我对ruby代码很感兴趣。有人可以指导我获取服务器带宽摘要吗?
答案 0 :(得分:2)
请尝试以下Ruby
示例:
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
<强>参考文献:强>
SoftLayer_Metric_Tracking_Object::getSummaryData
SoftLayer_Virtual_Guest::getMetricTrackingObjectId
使用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)
<强>参考文献:强>
答案 1 :(得分:0)
Disclamer:我创建了自己的Ruby SoftLayer客户端,您可以在http://github.com/zertico/softlayer专门检查它,例如您想要访问某些特定数据的情况(我不是SoftLayer员工;))
如果您想尝试解决问题的代码
ps:我在考虑你正在操纵SoftLayer_Hardware_Server,对吗?
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
您将访问类似这样的ruby对象:
=> #<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>
希望它可以帮助您,如果您对客户端使用有任何疑问,请发表评论