Skip to content

Instantly share code, notes, and snippets.

@rhodgin
Created July 10, 2015 15:48
Show Gist options
  • Save rhodgin/340054a1a373a859b989 to your computer and use it in GitHub Desktop.
Save rhodgin/340054a1a373a859b989 to your computer and use it in GitHub Desktop.
python module to get basic information about virtual and hardware servers in a SoftLayer account
'''
Code example of how to get information on the servers in a
SoftLayer account. You can call the script from the command line
and provide the userid and api key that you'd like to use to check
the account with.
You can run the script with the user ID you'd like to check under
the account you have configured:
find_active_softlayer_servers sl_username sl_api_key
The output of the script is a csv format with commas as the
column separator.
'''
from __future__ import print_function
import sys
import SoftLayer
def find_active_softlayer_servers(sl_username=None, sl_api_key=None):
"""
Get information on the active servers in an account
Keyword arguments:
sl_username -- the SoftLayer username used to retrieve the data (default: None)
sl_api_key -- the API key for the SoftLayer user ID used to load the data (default: None)
"""
# create a client to the SoftLayer_Account API service.
if sl_username is not None and sl_api_key is not None:
client = SoftLayer.Client(sl_username, sl_api_key)
else:
# if no connection information is passed in it will try to
# use the default values set up in the CLI
client = SoftLayer.Client()
# start by getting VM information
# number of results that we expect at a time
chunk = 200
vm_mask = "id,fullyQualifiedDomainName,notes,maxCpu,maxMemory,datacenter[name]"
# get the data from the SoftLayer APIs
vms = client.iter_call("Account", "getVirtualGuests", chunk=chunk, mask=vm_mask)
# add table header
print('\'ID', ',', 'Server Name', ',', 'Server Type', ',', 'Description', ',', 'CPU', ',', 'Memory', ',', 'Location')
for vsi in vms:
# navigate to the creator property under the VMs billing item
print(str(vsi['id']), ',', vsi['fullyQualifiedDomainName'], ',',
'VM', ',', vsi.get('notes', ''), ',', vsi['maxCpu'], ',',
vsi['maxMemory'], ',', vsi['datacenter']['name'])
# next get the information on bare metal servers
bm_mask = "id,fullyQualifiedDomainName,notes,processorCoreAmount,memoryCapacity,datacenter[name]"
# get the data from the SoftLayer APIs
bms = client.iter_call("Account", "getHardware", chunk=chunk, mask=bm_mask)
for bm in bms:
# navigate to the creator property under the VMs billing item
if bm.get('processorCoreAmount') > 0 and bm.get('memoryCapacity') > 0:
print(str(bm['id']), ',', bm['fullyQualifiedDomainName'], ',',
'BM', ',', bm.get('notes', ''), ',', bm['processorCoreAmount'], ',',
bm['memoryCapacity'], ',', bm['datacenter']['name'])
if __name__ == '__main__':
if len(sys.argv) == 1:
find_active_softlayer_servers()
else:
find_active_softlayer_servers(sys.argv[1], sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment