December 20, 2019


Working with Metric_Tracking_Object

A few examples on interacting with Metric tracking object

Metric_Tracking_Object

Get the all metric tracking ids

import SoftLayer
import json

client = SoftLayer.create_client_from_env()
account_service = client['Account']

mask = 'id,metricTrackingObject[id]'

try:
    response = account_service.getVirtualGuests(mask = mask)
    print(json.dumps(response, sort_keys=True, indent=2, separators=(',', ': ')))
except SoftLayer.SoftLayerAPIError as e:
    print("Unable to list the response for the package: \nfaultCode= %s, \n \nfaultString= %s"
          % (e.faultCode, e.faultString))

Get bandwidth data

import SoftLayer
import json

client = SoftLayer.create_client_from_env()
metric_service = client['Metric_Tracking_Object']

startDate = '2019-07-09T19:06:11-06:00'
endDate = '2019-07-09T19:06:11-16:00'

body = 'public'

"""
Set with Metric_Tracking_Object id. 
"""
id = '123456'

try:
    response = metric_service.getBandwidthData(startDate, endDate, body, id = id)
    print(json.dumps(response, sort_keys=True, indent=2, separators=(',', ': ')))
except SoftLayer.SoftLayerAPIError as e:
    print("Unable to list the response for the package: \nfaultCode= %s, \n \nfaultString= %s"
          % (e.faultCode, e.faultString))

Get custom graph data

import SoftLayer

client = SoftLayer.create_client_from_env()
metric_service = client['Metric_Tracking_Object']

body = {
    "baseUnit": "",
    "endDatetime": "2019-07-09T19:06:11-06:00",
    "metrics": [{
        "keyName": "VIRTUAL_DEDICATED_RACK",
        "name": "Bandwidth Allotment",
        "unit": "unit"}
]}

"""
Set with Metric_Tracking_Object id. 
"""
id = '123456'

try:
    response = metric_service.getCustomGraphData(body, id = id)
    print(response)
except SoftLayer.SoftLayerAPIError as e:
    print("Unable to list the response for the package: \nfaultCode= %s, \n \nfaultString= %s"
          % (e.faultCode, e.faultString))

Get details for date range

import SoftLayer
import json

client = SoftLayer.create_client_from_env()
metric_service = client['Metric_Tracking_Object']

startDate = '2019-07-09T19:06:11-06:00'
endDate = '2019-07-09T19:06:11-16:00'

body = ['InstanceCount','HostMemoryUsage']

"""
Set with Metric_Tracking_Object id. 
"""
id = '123456'

try:
    response = metric_service.getDetailsForDateRange(startDate, endDate, body, id = id)
    print(json.dumps(response, sort_keys=True, indent=2, separators=(',', ': ')))
except SoftLayer.SoftLayerAPIError as e:
    print("Unable to list the response for the package: \nfaultCode= %s, \n \nfaultString= %s"
          % (e.faultCode, e.faultString))

Get metric data types

import SoftLayer
import json

client = SoftLayer.create_client_from_env()
metric_service = client['Metric_Tracking_Object']

"""
Set with Metric_Tracking_Object id. 
"""
id = '123456'

try:
    response = metric_service.getMetricDataTypes(id = id)
    print(json.dumps(response, sort_keys=True, indent=2, separators=(',', ': ')))
except SoftLayer.SoftLayerAPIError as e:
    print("Unable to list the response for the package: \nfaultCode= %s, \n \nfaultString= %s"
          % (e.faultCode, e.faultString))

Get object

import SoftLayer
import json

client = SoftLayer.create_client_from_env()
metric_service = client['Metric_Tracking_Object']

"""
Set with Metric_Tracking_Object id. 
"""
id = '123456'

try:
    response = metric_service.getObject(id = id)
    print(json.dumps(response, sort_keys=True, indent=2, separators=(',', ': ')))
except SoftLayer.SoftLayerAPIError as e:
    print("Unable to list the response for the package: \nfaultCode= %s, \n \nfaultString= %s"
          % (e.faultCode, e.faultString))

Get summary data

import SoftLayer
import json

client = SoftLayer.create_client_from_env()
metric_service = client['Metric_Tracking_Object']

startDate = '2019-07-09T19:06:11-06:00'
endDate = '2019-07-09T19:06:11-16:00'

body = [{
    'keyName': 'CPU0',
    'summaryType': 'max'},
    {
        'keyName': 'MEMORY_USAGE',
        'summaryType': 'max'},
    {
        'keyName': 'CPU1',
        'summaryType': 'max'},
    {
        'keyName': 'CPU2',
        'summaryType': 'max'},
    {
        'keyName': 'CPU3',
        'summaryType': 'max'
    }]
    
"""
Set with Metric_Tracking_Object id. 
"""
id = '123456'    

try:
    response = metric_service.getSummaryData(startDate,
                                              endDate, body, 600, id = id)
    print(json.dumps(response, sort_keys=True, indent=2, separators=(',', ': ')))
except SoftLayer.SoftLayerAPIError as e:
    print("Unable to list the response for the package: \nfaultCode= %s, \nfaultString= %s"
          % (e.faultCode, e.faultString))

Get Metric Tracking Object type

import SoftLayer
import json

client = SoftLayer.create_client_from_env()
metric_service = client['Metric_Tracking_Object']

"""
Set with Metric_Tracking_Object id. 
"""
id = '123456'

try:
    response = metric_service.getType(id = id)
    print(json.dumps(response, sort_keys=True, indent=2, separators=(',', ': ')))
except SoftLayer.SoftLayerAPIError as e:
    print("Unable to list the response for the package: \nfaultCode= %s, \n \nfaultString= %s"
          % (e.faultCode, e.faultString))

Feedback?

We would love to hear it
Open an issue