Skip to content

Instantly share code, notes, and snippets.

@softlayer
Created June 14, 2010 21:59
Show Gist options
  • Save softlayer/438379 to your computer and use it in GitHub Desktop.
Save softlayer/438379 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# Retrieve a list of switchport statistics for a server's network interfaces.
#
# This script makes a single call to the getPortStatistics() method in the
# SoftLayer_Network_Component API service
# (http://sldn.softlayer.com/reference/services/SoftLayer_NetworkComponent/getPortStatistics)
# for each of a server's network components to query port statistics for that
# interface from SoftLayer's switches. Port statistics are modeled by the
# SoftLayer__Container_Network_Port_Statistic data type
# (http://sldn.softlayer.com/reference/datatypes/SoftLayer_Container_Network_Port_Statistic).
# 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!';
# Your server's id. Call the getHardware() method in the SoftLayer_Account API
# service (http://sldn.softlayer.com/reference/services/SoftLayer_Account/getHardware)
# to get a list of your account's hardware records.
my $serverId = 1234;
# Declare a new API service object for the SoftLayer_Hardware_Server API
# service.
my $client = SoftLayer::API::SOAP->new('SoftLayer_Hardware_Server', $serverId, $apiUsername, $apiKey);
# Switchport statistics are measured off the server's network components. Use
# an object mask to network component records along with our server record.
$client->setObjectMask({
'networkComponents' => {},
});
# Make the call to retrieve our hardware record. Once we have that we can query
# the server's network components.
my $server = $client->getObject();
# If there was an error returned from the SoftLayer API then die out with the
# error message.
if ($server->fault) {
die "Unable to retrieve server record. " . $server->faultstring;
}
# Separate our network components for easier processing later.
$server = $server->result;
my $networkComponents = $server->{networkComponents};
# Print out a simple report header.
print "\nSwitchport statistics for " . $server->{fullyQualifiedDomainName} . "\n\n";
# Loop through our server's network components. For each NIC make a call to the
# SoftLayer_Network_Component API service method getPortStatics() to get a list
# of switchport statistics retrieved from the switch on the other side of your
# NIC. Print a simple report per NIC.
for my $i (0 .. $#{$networkComponents}) {
my $component = $networkComponents->[$i];
# Skip the management network component.
if ($component->{name} eq 'mgmt') {
next;
}
# It's all right to redeclare your API object.
$client = SoftLayer::API::SOAP->new('SoftLayer_Network_Component', $component->{id}, $apiUsername, $apiKey);
# Retrieve switchport statistics for the NIC.
my $stats = $client->getPortStatistics();
# Make sure we didn't run into an error.
if ($stats->fault) {
die "Unable to retrieve switchport statics for " . $component->{name} . $component->{port} . ". " . $stats->faultstring;
}
# Make boolean up/down responses easier to read.
$stats = $stats->result;
$stats->{administrativeStatus} = ($stats->{administrativeStatus} eq '1') ? 'Up' : 'Down';
$stats->{operationalStatus} = ($stats->{operationalStatus} eq '1') ? 'Up' : 'Down';
# Print our port's statistics report.
print <<EOT;
Statistics for $component->{name}$component->{port} ($component->{primaryIpAddress})
Administrative status: $stats->{administrativeStatus}
Operational status: $stats->{operationalStatus}
MTU: $stats->{maximumTransmissionUnit}
Inbound octets (from server): $stats->{inOctets}
Outbound octets (to server): $stats->{outOctets}
Inbound unicast packets: $stats->{inUnicastPackets}
Outbound unicast packets: $stats->{outUnicastPackets}
Inbound discarded packets: $stats->{inDiscardPackets}
Outbound discarded packets: $stats->{outDiscardPackets}
Inbound error packets: $stats->{inErrorPackets}
Outbound error packets: $stats->{outErrorPackets}
EOT
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment