Perl ARX Chassis
Problem this snippet solves:
This script is an example of how to use the iControl interfaces provided by an ARX to retrieve chassis configuration, status and statistics on an ARX.
How to use this snippet:
ARXChassisExample.pl --url --user --pass
Prerequisites
- SOAP::Lite perl module
- An F5 ARX system.
- Management access on the ARX must be permitted for HTTPs-API or HTTP-API services.
Code :
#!/usr/bin/perl #------------------------------------------------------------------------------- # The contents of this file are subject to the "END USER LICENSE AGREEMENT # FOR F5 Software Development Kit for iControl"; you may not use this file # except in compliance with the License. The License is included in the # iControl Software Development Kit. # # Software distributed under the License is distributed on an "AS IS" # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See # the License for the specific language governing rights and limitations # under the License. # # The Original Code is iControl Code and related documentation # distributed by F5. # # The Initial Developer of the Original Code is F5 Networks, # Inc. Seattle, WA, USA. Portions created by F5 are Copyright (C) 1996-2012 # F5 Networks, Inc. All Rights Reserved. iControl (TM) is a registered # trademark of F5 Networks, Inc. # # Alternatively, the contents of this file may be used under the terms # of the GNU General Public License (the "GPL"), in which case the # provisions of GPL are applicable instead of those above. If you wish # to allow use of your version of this file only under the terms of the # GPL and not to allow others to use your version of this file under the # License, indicate your decision by deleting the provisions above and # replace them with the notice and other provisions required by the GPL. # If you do not delete the provisions above, a recipient may use your # version of this file under either the License or the GPL. #------------------------------------------------------------------------------- # # Description # # This script is an example of how to use the iControl interfaces provided by # an ARX to retrieve chassis configuration, status and statistics on an ARX. # # Usage: ARXChassisExample.pl --url--user --pass # # Prerequisites: # # This script requires the following: # # * SOAP::Lite perl module # * An F5 ARX system running release V6.02.000 or later. # * Management access on the ARX must be permitted for HTTPs-API or HTTP-API # services. # # For more information on ARX configuration, please consult the # documentation that was provided with your ARX system. #------------------------------------------------------------------------------- # SOAP::Lite lets us send SOAP requests and parse them use SOAP::Lite autotype => 0, default_ns => 'urn:iControl'; # If you need to debug problems with your script, you can use the +trace # option with SOAP::Lite and it will print the XML sent to and received from # the server: # # use SOAP::Lite # autotype => 0, # default_ns => 'urn:iControl' + trace; # Getopt::Long lets us easily parse command line options use Getopt::Long; use POSIX qw(strftime); use Carp; use strict; use warnings; #------------------------------------------------------------------------------- # Main program logic #------------------------------------------------------------------------------- our ($url, $user, $pass); # Load command line options - if the load fails, then we print the usage # instructions and exit. if (!GetOptions("url=s" => \$url, "user=s" => \$user, "pass=s" => \$pass)) { usage(); exit(1); } # If any arguments were skipped, print the usage instructions and exit. if (!defined $url || !defined $user || !defined $pass) { usage(); exit(1); } # The service path for interface "Interface" is this: # # http:// : /api/services/Interface # my $chassisServiceUrl = $url . "/api/services/Chassis"; # In order for SOAP to access a web service, it needs to read the WSDL # for the interface you want to use. The WSDL file for an interface # called "Interface" is available via http/https on the ARX at: # # http:// : /api/services/Interface?wsdl # # If you need a WSDL 2.0 version, that is also available at: # # http:// : /arx-api/wsdl/Interface.wsdl2 # # In this case, we're using the Chassis interface and we're # interested in using the WSDL 1.1 version. # my $chassisWsdlUrl = $chassisServiceUrl . "?wsdl"; # Now we build our SOAP::Lite object using the service and WSDL # URLs my $chassisSoap = SOAP::Lite->new(proxy => $chassisServiceUrl, service => $chassisWsdlUrl); #------------------------------------------------------------------------------- # get_ha_peer #------------------------------------------------------------------------------- print "Calling the \"get_ha_peer\" method of the ARX Chassis interface.\n\n"; # Build a security header our $securityHeader = getSecurityHeader($user, $pass); my $chassisSoapResult = $chassisSoap->get_ha_peer($securityHeader); if (defined $chassisSoapResult->fault && $chassisSoapResult->fault) { print("SOAP request failed:\n" . objdump($chassisSoapResult->fault) . "\n"); } else { print "Printing the results of the call to the \"get_ha_peer\" method of the ARX Chassis interface.\n\n"; print "HA Peer Status:\n"; my $chassisHaPeerStatus = $chassisSoapResult->result; my $peerIp = $chassisHaPeerStatus->{'ip'}; print "ip: ", $peerIp, "\n"; my $peerName = $chassisHaPeerStatus->{'name'}; print "name: ", $peerName, "\n"; my $peerRole = $chassisHaPeerStatus->{'role'}; print "role: ", $peerRole, "\n"; my $peerStatus = $chassisHaPeerStatus->{'status'}; print "status: ", $peerStatus, "\n"; print "\n"; } #------------------------------------------------------------------------------- # get_ha_status #------------------------------------------------------------------------------- print "Calling the \"get_ha_status\" method of the ARX Chassis interface.\n\n"; # Build a security header $securityHeader = getSecurityHeader($user, $pass); $chassisSoapResult = $chassisSoap->get_ha_status($securityHeader); if (defined $chassisSoapResult->fault && $chassisSoapResult->fault) { confess("SOAP request failed:\n" . objdump($chassisSoapResult->fault) . "\n"); } else { print "Printing the results of the call to the \"get_ha_status\" method of the ARX Chassis interface.\n\n"; print "HA Status:\n"; my $chassisHaStatus = $chassisSoapResult->result; my $haStatusName = $chassisHaStatus->{'name'}; print "name: ", $haStatusName, "\n"; my $ip = $chassisHaStatus->{'ip'}; print "ip: ", $ip, "\n"; my $role = $chassisHaStatus->{'role'}; print "role: ", $role, "\n"; my $status = $chassisHaStatus->{'status'}; print "status: ", $status, "\n"; print "\n"; } #------------------------------------------------------------------------------- # get_health #------------------------------------------------------------------------------- print "Calling the \"get_health\" method of the ARX Chassis interface.\n\n"; # Build a security header $securityHeader = getSecurityHeader($user, $pass); $chassisSoapResult = $chassisSoap->get_health($securityHeader); if (defined $chassisSoapResult->fault && $chassisSoapResult->fault) { confess("SOAP request failed:\n" . objdump($chassisSoapResult->fault) . "\n"); } else { print "Printing the results of the call to the \"get_health\" method of the ARX Chassis interface.\n\n"; my @chassisHealths = ($chassisSoapResult->result, $chassisSoapResult->paramsout); foreach my $chassisHealth (@chassisHealths) { my $date = $chassisHealth->{'date'}; print "date: ", $date, "\n"; my $description = $chassisHealth->{'description'}; print "description: ", $description, "\n"; my $event = $chassisHealth->{'event'}; print "event: ", $event, "\n"; my $id = $chassisHealth->{'id'}; print "id: ", $id, "\n"; print "\n"; } print "\n"; } #------------------------------------------------------------------------------- # get_hostname #------------------------------------------------------------------------------- print "Calling the \"get_hostname\" method of the ARX Chassis interface.\n\n"; # Build a security header $securityHeader = getSecurityHeader($user, $pass); $chassisSoapResult = $chassisSoap->get_hostname($securityHeader); if (defined $chassisSoapResult->fault && $chassisSoapResult->fault) { confess("SOAP request failed:\n" . objdump($chassisSoapResult->fault) . "\n"); } else { print "Printing the results of the call to the \"get_hostname\" method of the ARX Chassis interface.\n\n"; my $hostname = $chassisSoapResult->result; print "Hostname: $hostname\n\n"; } #------------------------------------------------------------------------------- # get_hw_version #------------------------------------------------------------------------------- print "Calling the \"get_hw_version\" method of the ARX Chassis interface.\n\n"; # Build a security header $securityHeader = getSecurityHeader($user, $pass); $chassisSoapResult = $chassisSoap->get_hw_version($securityHeader); if (defined $chassisSoapResult->fault && $chassisSoapResult->fault) { confess("SOAP request failed:\n" . objdump($chassisSoapResult->fault) . "\n"); } else { print "Printing the results of the call to the \"get_hw_version\" method of the ARX Chassis interface.\n\n"; my $hwVersion = $chassisSoapResult->result; print "HW Version: $hwVersion\n\n"; } #------------------------------------------------------------------------------- # get_model #------------------------------------------------------------------------------- print "Calling the \"get_model\" method of the ARX Chassis interface.\n\n"; # Build a security header $securityHeader = getSecurityHeader($user, $pass); $chassisSoapResult = $chassisSoap->get_model($securityHeader); if (defined $chassisSoapResult->fault && $chassisSoapResult->fault) { confess("SOAP request failed:\n" . objdump($chassisSoapResult->fault) . "\n"); } else { print "Printing the results of the call to the \"get_model\" method of the ARX Chassis interface.\n\n"; my $model = $chassisSoapResult->result; print "Model: $model\n\n"; } #------------------------------------------------------------------------------- # get_processor_list #------------------------------------------------------------------------------- my @processorList = (); print "Calling the \"get_processor_list\" method of the ARX Chassis interface.\n\n"; # Build a security header $securityHeader = getSecurityHeader($user, $pass); $chassisSoapResult = $chassisSoap->get_processor_list($securityHeader); if (defined $chassisSoapResult->fault && $chassisSoapResult->fault) { confess("SOAP request failed:\n" . objdump($chassisSoapResult->fault) . "\n"); } else { print "Printing the results of the call to the \"get_processor_list\" method of the ARX Chassis interface.\n\n"; @processorList = ($chassisSoapResult->result, $chassisSoapResult->paramsout); foreach my $processor (@processorList) { my $slot = $processor->{'slot'}; print "slot: ", $slot, "\n"; my $proc = $processor->{'processor'}; print "processor: ", $proc, "\n"; print "\n"; } print "\n"; } #------------------------------------------------------------------------------- # get_processor_stats #------------------------------------------------------------------------------- print "Calling the \"get_processor_stats\" method of the ARX Chassis interface.\n\n"; # Build a security header $securityHeader = getSecurityHeader($user, $pass); $chassisSoapResult = $chassisSoap->get_processor_stats($securityHeader); if (defined $chassisSoapResult->fault && $chassisSoapResult->fault) { confess("SOAP request failed:\n" . objdump($chassisSoapResult->fault) . "\n"); } else { print "Printing the results of the call to the \"get_processor_stats\" method of the ARX Chassis interface.\n\n"; my @processorStats = ($chassisSoapResult->result, $chassisSoapResult->paramsout); foreach my $processorStat (@processorStats) { my $processor = $processorStat->{'processor'}; print "processor: ", $processor, "\n"; my $cpu1min = $processorStat->{'cpu1min'}; print "cpu1min: ", $cpu1min, "\n"; my $cpu5min = $processorStat->{'cpu5min'}; print "cpu5min: ", $cpu5min, "\n"; my $freemem = $processorStat->{'freemem'}; print "freemem: ", $freemem, "\n"; my $module_type = $processorStat->{'module_type'}; print "module_type: ", $module_type, "\n"; my $status = $processorStat->{'status'}; print "status: ", $status, "\n"; my $totalmem = $processorStat->{'totalmem'}; print "totalmem: ", $totalmem, "\n"; my $uptime = $processorStat->{'uptime'}; print "uptime: ", $uptime, "\n"; print "\n"; } print "\n"; } #------------------------------------------------------------------------------- # get_processor_status #------------------------------------------------------------------------------- print "Calling the \"get_processor_status\" method of the ARX Chassis interface.\n\n"; if ($#processorList < 0) { print("The list of processors returned from the call to the \"get_list\" method of the ARX Chassis interface was empty.\n"); } else { my @soapProcessorList = ConvertProcessorIdsToSoapData(\@processorList); # Build a security header $securityHeader = getSecurityHeader($user, $pass); $chassisSoapResult = $chassisSoap->get_processor_status(SOAP::Data->name('processors')->value(@soapProcessorList), $securityHeader); if (defined $chassisSoapResult->fault && $chassisSoapResult->fault) { confess("SOAP request failed:\n" . objdump($chassisSoapResult->fault) . "\n"); } else { print "Printing the results of the call to the \"get_processor_status\" method of the ARX Chassis interface.\n\n"; my @processorStatuses = ($chassisSoapResult->result, $chassisSoapResult->paramsout); foreach my $processorStatus (@processorStatuses) { if (exists $processorStatus->{'processor'}) { my $processorStatusProc = $processorStatus->{'processor'}; print "processor:\n"; my $slot = $processorStatusProc->{'slot'}; print " slot: ", $slot, "\n"; my $processor = $processorStatusProc->{'processor'}; print " processor: ", $processor, "\n"; print "\n"; } my $role = $processorStatus->{'role'}; print "role: ", $role, "\n"; my $state = $processorStatus->{'state'}; print "state: ", $state, "\n"; my $uptime = $processorStatus->{'uptime'}; print "uptime: ", $uptime, "\n"; my $free_memory = $processorStatus->{'free_memory'}; print "free_memory: ", $free_memory, "\n"; my $total_memory = $processorStatus->{'total_memory'}; print "total_memory: ", $total_memory, "\n"; my $free_swap = $processorStatus->{'free_swap'}; print "free_swap: ", $free_swap, "\n"; my $total_swap = $processorStatus->{'total_swap'}; print "total_swap: ", $total_swap, "\n"; my $cpu1min = $processorStatus->{'cpu1min'}; print "cpu1min: ", $cpu1min, "\n"; my $cpu5min = $processorStatus->{'cpu5min'}; print "cpu5min: ", $cpu5min, "\n"; print "\n"; } print "\n"; } } #------------------------------------------------------------------------------- # get_serial #------------------------------------------------------------------------------- print "Calling the \"get_serial\" method of the ARX Chassis interface.\n\n"; # Build a security header $securityHeader = getSecurityHeader($user, $pass); $chassisSoapResult = $chassisSoap->get_serial($securityHeader); if (defined $chassisSoapResult->fault && $chassisSoapResult->fault) { confess("SOAP request failed:\n" . objdump($chassisSoapResult->fault) . "\n"); } else { print "Printing the results of the call to the \"get_serial\" method of the ARX Chassis interface.\n\n"; my $serial = $chassisSoapResult->result; print "Serial: $serial\n\n"; } #------------------------------------------------------------------------------- # get_storage_status #------------------------------------------------------------------------------- print "Calling the \"get_storage_status\" method of the ARX Chassis interface.\n\n"; # Build a security header $securityHeader = getSecurityHeader($user, $pass); $chassisSoapResult = $chassisSoap->get_storage_status($securityHeader); if (defined $chassisSoapResult->fault && $chassisSoapResult->fault) { confess("SOAP request failed:\n" . objdump($chassisSoapResult->fault) . "\n"); } else { print "Printing the results of the call to the \"get_storage_status\" method of the ARX Chassis interface.\n\n"; my @storageStatuses = ($chassisSoapResult->result, $chassisSoapResult->paramsout); foreach my $storageStatus (@storageStatuses) { my $storageName = $storageStatus->{'name'}; print "name: ", $storageName, "\n"; my $free_space = $storageStatus->{'free_space'}; print "free_space: ", $free_space, "\n"; my $total_space = $storageStatus->{'total_space'}; print "total_space: ", $total_space, "\n"; my $used_space = $storageStatus->{'used_space'}; print "used_space: ", $used_space, "\n"; print "\n"; } print "\n"; } #------------------------------------------------------------------------------- # get_sw_version #------------------------------------------------------------------------------- print "Calling the \"get_sw_version\" method of the ARX Chassis interface.\n\n"; # Build a security header $securityHeader = getSecurityHeader($user, $pass); $chassisSoapResult = $chassisSoap->get_sw_version($securityHeader); if (defined $chassisSoapResult->fault && $chassisSoapResult->fault) { confess("SOAP request failed:\n" . objdump($chassisSoapResult->fault) . "\n"); } else { print "Printing the results of the call to the \"get_sw_version\" method of the ARX Chassis interface.\n\n"; my $swVersion = $chassisSoapResult->result; print "SW Version: $swVersion\n\n"; } #------------------------------------------------------------------------------- # get_type #------------------------------------------------------------------------------- print "Calling the \"get_type\" method of the ARX Chassis interface.\n\n"; # Build a security header $securityHeader = getSecurityHeader($user, $pass); $chassisSoapResult = $chassisSoap->get_type($securityHeader); if (defined $chassisSoapResult->fault && $chassisSoapResult->fault) { confess("SOAP request failed:\n" . objdump($chassisSoapResult->fault) . "\n"); } else { print "Printing the results of the call to the \"get_type\" method of the ARX Chassis interface.\n\n"; my $type = $chassisSoapResult->result; print "Type: $type\n\n"; } #------------------------------------------------------------------------------- # End of main program logic #------------------------------------------------------------------------------- #------------------------------------------------------------------------------- # sub usage #------------------------------------------------------------------------------- sub usage { print "\nUsage: ARXChassisExample.pl --url --user --pass \n"; print "\n"; print "Argument Description\n"; print "-------- -----------\n"; print "--url The base URL of the web service on the ARX. Both http and https\n"; print " are supported. The format is:\n"; print "\n"; print " http(s):// : \n"; print "\n"; print " : DNS resolvable hostname or IP address\n"; print " : 83 for http or 843 for https\n"; print "\n"; print "--user The username for authentication.\n"; print "--pass The password for authentication.\n"; print "\n"; } #------------------------------------------------------------------------------- # sub getSecurityHeader(user, pass) # # This subroutine builds a security header that will be used for # authentication. This type of security header is required for all calls to # iControl::ARX interfaces, so it makes sense to have this subroutine stored in # a library for common access. #------------------------------------------------------------------------------- sub getSecurityHeader { my $user = shift; my $pass = shift; my $now = time(); my $then = time() + 60; my $created = strftime("%Y-%m-%dT%H:%M:%S", gmtime($now)) . 'Z'; my $expires = strftime("%Y-%m-%dT%H:%M:%S", gmtime($then)) . 'Z'; my $secExt = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'; my $secUtil = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'; my $securityHeader = SOAP::Header->name("wsse:Security")->attr( { 'xmlns:wsse'=> $secExt, 'xmlns:wsu'=> $secUtil } ); my $timestamp = SOAP::Data->name("wsu:Timestamp" => \SOAP::Data->value( SOAP::Data->name('wsu:Created')->value($created) ->type(''), SOAP::Data->name('wsu:Expires')->value($expires) ->type(''))); my $usernameTokenType = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"; my $usernameToken = SOAP::Data->name("wsse:UsernameToken" => \SOAP::Data->value( SOAP::Data->name('wsse:Username')->value($user) ->type(''), SOAP::Data->name('wsse:Password')->value($pass) ->type('') ->attr({'Type'=>$usernameTokenType}))); $securityHeader->value(\SOAP::Data->value($timestamp, $usernameToken)); return $securityHeader; } sub objdump { my ($obj, $indent) = @_; my $content = ''; if (!defined $obj) { return $content; } if (!defined $indent) { $indent = ' '; } my $type = ref $obj; if (!defined $type || $type eq '' || $type eq 'SCALAR') { $content = $content . $indent . $obj . "\n"; } elsif ($type eq 'ARRAY') { foreach my $node (@$obj) { $content = $content . objdump($node, $indent); } } else { my $key; my $value; while (($key, $value) = each %$obj) { my $type2 = ref $value; if (!defined $type2 || $type2 eq '' || $type2 eq 'SCALAR') { $content = $content . $indent . "\'$key\' => $value;\n"; } else { $content = $content . $indent . "\'$key\' => {\n"; $content = $content . objdump($value, $indent.' '); $content = $content . $indent . "}\n"; } } } return $content; } sub ConvertProcessorIdsToSoapData { my ($idListRef) = @_; my @dataArray; foreach my $id (@$idListRef) { my %idHash = %{$id}; my $sid = SOAP::Data->name("processors")->value(\SOAP::Data->value( SOAP::Data->name("slot")->value($idHash{'slot'}), SOAP::Data->name("processor")->value($idHash{'processor'}))); push(@dataArray, $sid); } return @dataArray; }
Updated Jun 06, 2023
Version 2.0wangh_97509
Historic F5 Account
Joined July 01, 2010
No CommentsBe the first to comment