ServerDensity: creating a new device and getting its access key via the API

31 Jan

I’ve recently been setting up devices that need to have ServerDensity monitoring automatically added to them when I start them up. Setting up the ServerDensity agent itself can be automated through shell commands, but you also need to set the API key in the config. I use a Python file to create a new device in ServerDensity and output the API/agent key for the newly created device.

Here’s the code, not that you will need to install “py-serverdensity” using pip before running this script.

pip install py-serverdensity

And here’s the Python code.

from serverdensity.api import SDApi, SDServiceError
# The ServerDensity account settings
sdaccountURL = ''
sdUsername = "api_user"
sdPassword = "YourPassword"
# The device name, and optionally group it belongs to
deviceName = "TEST"
deviceGroup = ""
# You may not need to change anything below this
# Connect to API
api = SDApi(
 # Add a device through API
 resultSet = api.devices.add({
 'name': deviceName,
 'group': deviceGroup
 # If result is a success
 if resultSet['status'] == 1:
 # Print the device's agent key
 agentKey = resultSet['data']['agentKey']
 print "Agent key = " + agentKey
 # Else output an error
 print "Error: could not create device - " + deviceName
except SDServiceError, e:
 print 'Error:', e
 print 'Response:', e.response

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: