Can you pull network status information from the Softlayer API?

一曲冷凌霜 提交于 2019-12-12 02:40:41

问题


We are interested in using the API to get the information found on the Network > Status > Local page.

Does the API allow for this?

Thank you


回答1:


Yes it is possible, but there is no a single API call which returns the result. Basically you need to get all the routers in your account.

here a code which could help you (it is using the Python Client)

"""
Get network status local

The script displays the same information as the https://control.softlayer.com/network/status/local page.

Important manual pages
https://sldn.softlayer.com/reference/services/SoftLayer_Account/
https://sldn.softlayer.com/reference/services/SoftLayer_Account/getObject
https://sldn.softlayer.com/reference/datatypes/SoftLayer_Hardware
"""
import SoftLayer
import json


def getRacks(networkHardware, routerStatus):
    """Retrieves the racks information.
       :param SoftLayer_Hardware networkHardware: A network hardware downstream.
       :param SoftLayer_Hardware routerStatus: The routers in the account which contains the network incidents.
    """
    racks = []
    if 'downstreamNetworkHardware' in networkHardware:
        for network in networkHardware['downstreamNetworkHardware']:
            rack = {}
            if 'cs' in network['hostname']:
                rack['name'] = network['hostname'] + '.' + network['domain']
                rack['type'] = network['hardwareChassis']['hardwareFunction']['code']
                rack['status'] = getAggregationRackStatus(network, routerStatus)
                racks.append(rack)
    return racks


def getAggregationRackStatus(networkHardware, routerStatus):
    """Retrieves the status for racks and aggregation hardware.
       :param SoftLayer_Hardware networkHardware: A network hardware downstream.
       :param SoftLayer_Hardware routerStatus: The routers in the account which contains the network incidents.
    """
    if 'downstreamNetworkHardwareWithIncidents' in routerStatus:
        for incident in routerStatus['downstreamNetworkHardwareWithIncidents']:
            if incident['hostname'] == networkHardware['hostname']:
                return incident['networkStatus']
    else:
        return routerStatus['networkStatus']


def getAggregation(router, routerStatus):
    """Retrieves the racks information.
       :param SoftLayer_Hardware router: A router hardware.
       :param SoftLayer_Hardware routerStatus: The routers in the account which contains the network incidents.
    """
    aggregations = []
    if 'downstreamNetworkHardware' in router:
        for networkHardware in router['downstreamNetworkHardware']:
            aggregation= {}
            if 'as' in networkHardware['hostname']:
                aggregation['name'] = networkHardware['hostname'] + '.' + networkHardware['domain']
                aggregation['type'] = networkHardware['hardwareChassis']['hardwareFunction']['code']
                aggregation['status'] = getAggregationRackStatus(networkHardware, routerStatus)
                aggregation['rack'] = getRacks(networkHardware, routerStatus)
                aggregations.append(aggregation)
    return aggregations


USERNAME = 'set me'
API_KEY = 'set me'

# Declares the API client
client = SoftLayer.Client()
accountService = client['SoftLayer_Account']

objectMask = "mask[id,routers[id,hardwareChassis[manufacturer,name,hardwareFunction[code,description]],hostname,domain,networkStatus,networkMonitorAttachedDownVirtualGuestCount,networkMonitorAttachedDownHardwareCount,downstreamNetworkHardware[downstreamNetworkHardware,hostname,domain,hardwareChassis[manufacturer,name,hardwareFunction[code,description]]]]]"
objectMaskStatus = "mask[networkMonitorDownVirtualGuestCount,networkMonitorDownHardwareCount,routers[downstreamNetworkHardwareWithIncidents[hardwareChassis[hardwareFunction], networkStatus]]]"

try:
    response = accountService.getObject(mask=objectMask)
    responseStatus = accountService.getObject(mask=objectMask)
except SoftLayer.SoftLayerAPIError as e:
    print("Unable to get the account. faultCode=%s, faultString=%s" % (e.faultCode, e.faultString))


routers = []
for i in range(0, len(response['routers'])):
    router = response['routers'][i]
    routerStatus = responseStatus['routers'][i]
    device = {}
    device['name'] = router['hostname'] + "." + router['domain']
    device['type'] = router['hardwareChassis']['hardwareFunction']['code']
    device['status'] = router['networkStatus']
    device['aggregation'] = getAggregation(router, routerStatus)
    device['routerstatus'] = routerStatus['id']
    device['id'] = router['id']
    routers.append(device)

print(json.dumps(routers, sort_keys=True, indent=2, separators=(',', ': ')))



回答2:


This request may help you:

https://[username]:[apikey]@api.softlayer.com/rest/v3/SoftLayer_Account/getRouters?objectMask=mask[downlinkNetworkHardware,networkStatus,hardwareChassis.hardwareFunction,networkMonitorAttachedDownHardware.latestNetworkMonitorIncident, networkMonitorAttachedDownVirtualGuests[latestNetworkMonitorIncident,primaryIpAddress],downstreamNetworkHardwareWithIncidents.networkStatus,downlinkVirtualGuests.latestNetworkMonitorIncident, downlinkServers[hardwareChassis.hardwareFunction, latestNetworkMonitorIncident]]
Method: GET

Reference:

https://forums.softlayer.com/forum/softlayer-developer-network/implementations/84851-network-local-status-infrastructure-overview-via-api

(Note: This reference will be outdated soon, but it can help you now)



来源:https://stackoverflow.com/questions/34914945/can-you-pull-network-status-information-from-the-softlayer-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!