Skip to content

Instantly share code, notes, and snippets.

@softlayer
Created April 30, 2010 20:02
Show Gist options
  • Save softlayer/385680 to your computer and use it in GitHub Desktop.
Save softlayer/385680 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# Retrieve a list of hardware and print a report with server hostname, domain,
# login info, network, CPU, and RAM details.
#
# This script makes a single call to the getHardware() method in the
# SoftLayer_Account API service
# (http://sldn.softlayer.com/reference/services/SoftLayer_Account/getHardware)
# and uses an object mask to retrieve related information. It uses the
# SoftLayer API Perl client
# <http://github.com/softlayer/softlayer-api-perl-client> to handle API calls.
# See below for more details.
#
# License: http://sldn.softlayer.com/article/License
# Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
# Set this to the path of the SoftLayer API Perl client.
use lib '/path/to/client/';
use SoftLayer::API::SOAP;
use strict;
# Your SoftLayer API username and key. Generate an API key at the SoftLayer
# Customer Portal: https://manage.softlayer.com/Administrative/apiKeychain
my $apiUsername = 'set me!';
my $apiKey = 'set me too!';
# Declare a new API service object for the SoftLayer_Account API service.
my $client = SoftLayer::API::SOAP->new('SoftLayer_Account', undef, $apiUsername, $apiKey);
# Add an object mask to retrieve our hardwares' related items such as its
# operating system, hardware components, and network components. Object masks
# can retrieve any information related to your object. See
# http://sldn.softlayer.com/reference/datatypes/SoftLayer_Hardware_Server
# for a list of the relational properties you can retrieve along with hardware.
$client->setObjectMask({
'hardware' => {
'operatingSystem' => {
'passwords' => {},
},
'processors' => {},
'processorCount' => {},
'memory' => {},
'memoryCount' => {},
'networkComponents' => {},
'primaryIpAddress' => {},
'privateIpAddress' => {},
}
});
# Make the call to retrieve our hardware records.
my $hardwareRecords = $client->getHardware();
# If there was an error returned from the SoftLayer API then bomb out with the
# error message.
if ($hardwareRecords->fault) {
die "Unable to retrieve hardware. " . $hardwareRecords->faultstring;
}
# Otherwise loop through the returned hardware records, get the information we
# need, and print the server's information.
$hardwareRecords = $hardwareRecords->result;
for my $i (0 .. $#{$hardwareRecords}) {
# Separate the hardware record into variables for easier processing later.
my $hardware = $hardwareRecords->[$i];
my $passwords = $hardware->{operatingSystem}->{passwords};
my $networkComponents = $hardware->{networkComponents};
my $processors = $hardware->{processors};
my $memory = $hardware->{memory};
# Find the root user out of a list of recorded OS users. If we can't find
# the root user then default to "*not found*".
my $rootUsername = '*not found*';
my $rootPassword = '*not found*';
for my $j (0 .. $#{$passwords}) {
my $user = $passwords->[$j];
if ($user->{username} eq 'root' or $user->{username} eq 'Administrator') {
$rootUsername = $user->{username};
$rootPassword = $user->{password};
}
}
# Go through the hardware's network components to get it's public and
# private network ports. Save MAC addresses.
my $publicMacAddress = '*not found*';
my $privateMacAddress = '*not found*';
for my $j (0 .. $#{$networkComponents}) {
my $component = $networkComponents->[$j];
# SoftLayer uses eth0 on the private network and eth1 on the public
# network.
if ($component->{name} eq 'eth' and $component->{port} eq '0') {
$privateMacAddress = $component->{macAddress};
} elsif ($component->{name} eq 'eth' and $component->{port} eq '1') {
$publicMacAddress = $component->{macAddress};
}
}
# Hardware can only have like processors in them, so use the first item in
# the processors array to get the type of processor in the server.
my $processorType = $processors->[0]->{hardwareComponentModel}->{hardwareGenericComponentModel}->{capacity}
. $processors->[0]->{hardwareComponentModel}->{hardwareGenericComponentModel}->{units} . " "
. $processors->[0]->{hardwareComponentModel}->{hardwareGenericComponentModel}->{description};
# Treat memory the same way we did processors.
my $memoryType = $memory->[0]->{hardwareComponentModel}->{hardwareGenericComponentModel}->{capacity}
. $memory->[0]->{hardwareComponentModel}->{hardwareGenericComponentModel}->{units} . " "
. $memory->[0]->{hardwareComponentModel}->{hardwareGenericComponentModel}->{description};
# All done! Print hardware info.
print <<EOT;
Hostname: $hardware->{hostname}
Domain: $hardware->{domain}
Login: $rootUsername / $rootPassword
Public IP Address: $hardware->{primaryIpAddress}
Public MAC Address: $publicMacAddress
Private IP Address: $hardware->{privateIpAddress}
Private MAC Address: $privateMacAddress
CPUs: $hardware->{processorCount}x $processorType
RAM: $hardware->{memoryCount}x $memoryType
EOT
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment