2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2025-05-31 01:56:39 +00:00
xcat-core/xCAT-server/lib/xcat/plugins/switchdiscover.pm
Bin Xu eea661e405 merge from master to 2.13 branch for 2.13.9 release. (1) (#4525)
* fix issueNode range not specified, see man page for syntax. return with no output when site.master is not set #4299

* Fix issue 4246, record rflash process in log file

* dodiscovery: better disksize - ordered by major,minor and no `\n` (#4219)

* better disksize: ordered by major,minor and no `\n`

* nodediscover:`disksize` sent correctly: no need to `split`,`join`

* general sort with explicit key columns, fix search pattern

* dodiscovery: Fix bugs

* dodiscovery: Fix ShellCheck SC2007,SC2004

* dodiscovery: simpler kcmdline parsing, disksize as IEC binary prefix

* Add test case - switch_to_dns_forward_mode

* Modify timeout of login by curl command for OpenBMC

* Add makegocons command

This patch enable goconserver service and manage the node sessions
with a new `makegocons` command.

Implement: #4215

* Refine the Cumulus Linux Section of the Doc (#4249)

* No content in the switches subdirectory in docs, remove

* Change the Network topic to Networking

* Refine and reformat the Cumulus Linux documentation

* Modify the script to build xCAT-genesis-base package (#4292)

* Verify hash ID

* change log messages

* Timeout the ntpd process if ntpd service is not running on ntpserver (#4321)

* enhance nodediscovery process: don't write "NOIP" if the node can not be resloved to an IP (#3995)

* enhance nodediscovery process, if only 1 mac and have \*NOIP\* append, don't write mac table and don't generate dhcp lease entry

* Enhance PR 3995: enhance nodediscovery process: don't write "NOIP" if the node can not be resloved to an IP

* modified depending on comments

* natural_sort_cmp: recursion to iterative implementation (#4314)

* natural_sort_cmp: recursion to iterative implementation

* natural_sort_cmp: correct prototype with parameters, as per `man perlfunc`

* Task 3339, rspconfig ip/netmask/gateway/vlan support

*   * Minor enhance on xcatperftest to put all logs into one file
  * Fix a bug in simulatorctl.sh, and it cause the script cannot be found

* modified depending on comments

* QA list for makehosts (#4284)

* QA list for makehosts

* Fix some bus in makegocons

* Revise test case - switch_to_dns_forward_mode

* Enhancements after the review

* Use IO::Socket to check BMC console port

* Revise test case - switch_to_dns_forward_mode

* modified depending on comments

* Add messages to inform the user of the general action started via xCAT for flashing firmware

* Fix bug, anything in the functional array is the one that's really active, priority will not be 0 if there is pending firmware

* Fix 4338, remove all of  in child process

* fix issue #4354 :The XCATTEST_CN in xcattest can not detect HCP as config file (#4355)

* Fix #4330, close the socket

* Add image name to /opt/xcat/xcatinfo on compute node (#4359)

* Integrate congo console from goconserver with rcons

Enhance the original implement of rcons to support goconserver.
`rcons` will help the user choose from one of the console backend
service based on the console server status of management node.

Implement #4216

* Fix 4363 - discovery broken (#4364)

* do not set exit_code to 1 when the clock is not synced before the timeout (#4362)

* Fix check for MTM values with spaces

* modified depending on comments

* rspconfig admin_password for OpenBMC

* Fix merging in xdcpmerge.sh (#4328)

* Fixes in xdcpmerge.sh

Two fixes:
1. The grep pattern when finding duplicate usernames is missing ":" at the end. So, for example user "test" would also match "test2, etc.". Adding the ":" delimiter fixes the issue.
2. Another issue happens when the file to be merged is a superset of the files on the nodes. For example, if a new user is added and entire passwd file (that is otherwise identical) is sent to be merged. In this case, the $filebackup.nodups file, i.e. the original file with duplicates removed, becomes empty and the condition "if [ -s "$filebackup.nodups" ]" does not execute. Then the merged file ends up being original file with the merge file fully appended, clearly not what was intended.

This is solved by changing the condition to check for file existence "-a" rather then for size. Additionally, I also turn the logic around so that the duplicates are removed from the merge file and then added to the original file. I think this makes logic a bit cleaner and also ensures that existing entries are not reordered or changed in any way.

* Streamlining previous commit

Adjustment to previous commit, streamlining and simplifying logic. Once $mergefile.nodups is created, just concatenate it the original file.

* Update to xdcpmerge

No need to copy $filebackup to $curfile, they are the same.

* Modify for debug conveniently

* add new cases and delete outdated test cases

* Modify genesis build script for centos x86_64

* Add space between at and the time

* Add a print out of the firmware levels for the various UT cases

* Add unit test cases for rspconfig

* rspconfig fix for set hostname

* Enhance the testing case for rspconfig setting hostname

* modified depending on comments

* modified depending on comments

* Add %pretrans script in <lua>. Handle directory to symlink change properly. See comment #3 of https://bugs.launchpad.net/rpm/+bug/633636

* Make xCAT-genesis-base confliects with early version of xCAT-genesis-scripts

* rspconfig dump to allow admins capture logs

* Adding comment

* Improve the error message when BMC does not return a dump ID

* Improve some messages and add timestamp for downloaded dump file

* Improve the message to help Admin figure out where the file is missing

* Leave a log file there when xCAT upgrade in case to debug issue while upgrading (#4389)

* Listen on 0.0.0.0 instead of the hostname

This patch modify the configuration of `makegocons` and `rcons`
for goconserver.

`cat /etc/goconserver/server.conf`
```
global:
  host: 0.0.0.0
  ssl_key_file: /etc/xcat/cert/server-key.pem
  ssl_cert_file: /etc/xcat/cert/server-cert.pem
  ssl_ca_cert_file: /etc/xcat/cert/ca.pem
  logfile: /var/log/goconserver/server.log
api:
  port: 12429
console:
  port: 12430
```

* Support hostname=* for openbmc

* Relay action and snmp configuration support for Coral PDU

* ddns.pm: specify the "directory" option for DNS slaves too (cf. bug #4392)

* Fix issue 4361, modify some sendmsg to message

* 1. add "makeconserver -d" to "rmdef -C", 2. add "makeconserver -C|--cleanup" to remove entries for removed nodes

* When there is a problem with the login, do not hide the message on debug mode. BMCReady does not make sense if the admin does not know how to find that state

* Change function from login_logout_request to login_request, not doing any logout here

* Check that RC is 200 to prevent unknown issues, handle the response generically

* OpenBMC rspconfig dump timeout fixes

* Fix issue 4408, modify error for rspconfig dump

* Clear all BMC Dump logs when BMC firmware flash

* modified depending on comments

* More modifications for pr 4386, to deal with the conflicts

* To handle one case which have 2 implementations, which one is for specific platform, on is for all platforms

* return when current status is RSPCONFIG_DUMP_DOWNLOAD_REQUEST

* fix issue 4417, delete 'clear next_status'

* fix issue 4353: rspconfig needs to support multiple IPs on the BMC and ignore ZeroConfigIPs

* Wait 15 seconds after OpenBMC interface with vlan tag to be activated

* Fix issue #4397: rspconfig <> hostname=xxx show error message when there is multiple network in bmc

* Some sentence modify for makeconservercf -C|--cleanup

* OpenBMC rspconfig dump better dump file name formatting

* Removed the --check and --ipsource option with PR 4258, update the man page

* Improve the message on the HTTP response

* modified depending on comments

* Only handle 404 and 504 in the login request code, defer the rest to deal_with_response()

* rflash stream support

* 1. configure ip/netmask/gateway only on the NIC whose IP match node BMC attribute, 2. add some information for LinkLocal address

* Fix confignetwork bond nic_type detection with multiple bonds

* Modified configonie --ntp command (#4436)

* Add man page for makegocons

This is the guide about how to make goconserver as
a replacement for conserver to help slove the issues reported
for conserver, like: #4043, #3543. For openbmc, the solution of
goconserver is much light-weighted than the conserver which could
help save the system resource. In addition, sshpass is not needed
for openbmc with goconserver.

Implement: #4337

* Add another key for node_info in order not to after the content of $node_info{$node}{bmc}

* enhance rflash stream

* makedhcp does not work well when all service nodes not running dhcp but disjointdhcps=1 (#4426) (#4440)

- if all service nodes not running dhcp, to treat it as disjointdhcps=0
- nodeset will send request to MN by default even if disjointdhcps=1
- Move out of the dhcp service checking from opts pre-check, and do it just before real makedhcp handling.

* rspconfig configure bmc vlan will hung because of PR 4383

* OpenBMC rspconfig dump enhancements

* Changes due to review comments

* Print debug message before login attempt

* Add warning when xCAT throttles SSL connections

* Display first [openbmc_debug] when entering openbmc.pm

* modified error msg

* Make sure credential files have a trailing newline (#4442)

* modified depending on comments

* Fix the typo in the man page of makegocons

* Update the print out based on the review comment, should not use  since the regular expression is removed

* Modify the nodeset disjoint test case accordingly for #4426

* Use short hostname in rcons for goconserver

As the certificate of xcat is signed with short hostname, this
commit force to use the short hostname in  the environment variable for
`congo console`.

* Fix issue 3497, make sense for reventlog msg

* Give summary after flash active when no debugmode

* Fix the issue that the IP configuration will fail if bmc attribute is a hostname

* enhance genimage for sles12sp2 (#4450)

* Add dhcp-client-identifier to lease block (#4429)

Machines that use Infiniband for PXE booting need to have the
dhcp-client-identifier set in the lease block.
Without it, they will not get the lease from the server.

* Support multiple bonds on bring-up

* modified depending on comments

* fix the check for rc to 1 on error cases

* modify response for bmcdiscover when error

* Ignore syslog error in monitorctrl when setNodeStatusAttributes (#4459)

* fix issue https://github.com/xcat2/xcat-core/issues/4411 (#4462)

* fix issue Compute nodes fail to get provisioned #4411: covert imgsrv and xcatmaster to their ip addresses in case the hostname cannot be resolved inside initrd for diskless

* More strict check to tell if it is a chroot env to avoid modify DB (#4463)

when genimage for SN image (#4365)

* issues for install license file on accton switches (#4460)

* Add test cases for rflash regular usage against openbmc

* modify depending on xuwei's comment

* add 2 more cases for option d

* enhance rflash upload message

* Do not display message for clearing dumps when only PNOR

* Display hostname even if multiple IP addresses

* modified depending on comments

* polished message

* Modify the default consoleondemand based on the global setting

This commit fix the bug that consoleondemand works incorrectly.

* modify depending on comments

* modified depending on comments

* enhance rflash error messages

* Modify documenation for servicenode attributes

* build rst file from Schema.pm by db2man

* change status back to starts

* modified depending on comments

* rm openbmcevents

* Usage and man page update for rspconfig dump

* let rflash error message flexible

* Add support for the "file -> (noderange) file" syntax in synclist with ServiceNodes (#4445)

* Add support for "file -  (noderange) file" in synclist when using
hierarchical mode. Fixes #4425

  This patch ensures that:
  1. the synclist is correctly parsed when running on a Service Node
  2. all files are synchronized to SNs in hierarchical mode

* Better test condition for #4425, addresses issue in
https://github.com/xcat2/xcat-core/pull/4445#issuecomment-349472901

* Fix issue 4477, if has node-<mac> will not create node-<mtms> for the same node

* fix issue updatenode -f loses directories when copying files to SN #4456 (#4494)

* comment from ErTao

* Crude attempt at including external configuration files in named.conf

* Fixes after the review

* Fix issue 4490, record any error when rflash active process

* add -d usage and manpage

* updatenode -F not work in hierachy env as the user name is FQDN of MN (#4484)

* updatenode -F not work in hierachy env (#4455)
 - add trace when -V is enabled
 - get the DSH_FROM_USERID from updatenode client

*  - when 'updatenode -F' need to push SN first, using root as non-root does not have permission write to 'SNsyncfiledir'
 - move the set DSH_FROM_USERID code out of the loop, and also cover remote client case.

* fix issue for command rspconfig hostname=*

* enhance rflash

* Adjust the server used for kernel/initrd and imgurl for petitboot (#4416)
 - URL for kernel/initrd, get the value from below value tftpserver -> xcatmaster -> myipfn
 - URL for image, get the value from below value nfsserver -> tftpserver -> xcatmaster -> myipfn

* NODE attribute didn't populate in /opt/xcat/xcatinfo after reboot (#4428)

* NODE attribute didn't populate in /opt/xcat/xcatinfo after reboot

* Get NODE from mypostscripts

* Improve the output message for reventlog, use a global variable to set PolicyFile Path

* If debug_msg is not provided, use an empty string

* Check for LinkLocal as well as 169.254 IP address

* Fix issue 4507, add parameter check for rspconfig admin_passwd

* record more information when rflash upload error

* Fix the error when using array ref in updatenode with old version perl, it is introduced by PR#4484 (#4518)

* Do not restart conserver if goconserver was started

If goconserver was enabled, do not start conserver when restart
xcatd on service node.

* remove the /etc/localtime before copy timezone file

* Use CONGO_CLIENT_TYPE to tell goconserver the source of client (#4501)

goconserver could send back message based on the client type
this commit set CONGO_CLIENT_TYPE to xcat to make the message
from goconserver more friendly.

* add rflash -d doc

* only ignore 169.254.x.x for OpenBMC

* Fix issue 4513, print out better error msg for reventlog -s

* Modify or add openbmc test cases or bundle

* add test cases for updatenode -f/F in hierarchy environment, covers issues #4456,#4455 and PR #4425 (#4500)
2017-12-14 05:03:34 -06:00

1496 lines
49 KiB
Perl

#!/usr/bin/env perl
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
package xCAT_plugin::switchdiscover;
BEGIN
{
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
}
use lib "$::XCATROOT/lib/perl";
use Getopt::Long;
use xCAT::Usage;
use xCAT::NodeRange;
use xCAT::NetworkUtils;
use xCAT::Utils;
use xCAT::SvrUtils;
use xCAT::MacMap;
use xCAT::Table;
use XML::Simple;
no strict;
use Data::Dumper;
use Socket;
use Expect;
use SNMP;
use xCAT::data::switchinfo;
#global variables for this module
my $device;
my $community;
my %globalopt;
my @filternodes;
my @iprange;
my %global_scan_type = (
nmap => "nmap_scan",
lldp => "lldp_scan",
snmp => "snmp_scan"
);
#-------------------------------------------------------------------------------
=head1 xCAT_plugin:switchdiscover
=head2 Package Description
Handles switch discovery functions. It uses lldp, nmap or snmap to scan
the network to find out the switches attached to the network.
=cut
#-------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
=head3 send_msg
Invokes the callback with the specified message
Arguments:
request: request structure for plguin calls
ecode: error code. 0 for succeful.
msg: messages to be displayed.
Returns:
none
=cut
#--------------------------------------------------------------------------------
sub send_msg {
my $request = shift;
my $ecode = shift;
my $msg = shift;
my %output;
#################################################
# Called from child process - send to parent
#################################################
if ( exists( $request->{pipe} )) {
my $out = $request->{pipe};
$output{errorcode} = $ecode;
$output{data} = \@_;
print $out freeze( [\%output] );
print $out "\nENDOFFREEZE6sK4ci\n";
}
#################################################
# Called from parent - invoke callback directly
#################################################
elsif ( exists( $request->{callback} )) {
my $callback = $request->{callback};
$output{errorcode} = $ecode;
$output{data} = $msg;
$callback->( \%output );
}
}
#--------------------------------------------------------------------------------
=head3 handled_commands
It returns a list of commands handled by this plugin.
Arguments:
none
Returns:
a list of commands.
=cut
#--------------------------------------------------------------------------------
sub handled_commands {
return( {switchdiscover=>"switchdiscover"} );
}
#--------------------------------------------------------------------------------
=head3 parse_args
Parse the command line options and operands.
Arguments:
request: the request structure for plugin
Returns:
Usage string or error message.
0 if no user promp needed.
=cut
#--------------------------------------------------------------------------------
sub parse_args {
my $request = shift;
my $args = $request->{arg};
my $cmd = $request->{command};
my %opt;
#############################################
# Responds with usage statement
#############################################
local *usage = sub {
my $usage_string = xCAT::Usage->getUsage($cmd);
return( [$_[0], $usage_string] );
};
#############################################
# No command-line arguments - use defaults
#############################################
if ( !defined( $args )) {
return(0);
}
#############################################
# Checks case in GetOptions, allows opts
# to be grouped (e.g. -vx), and terminates
# at the first unrecognized option.
#############################################
@ARGV = @$args;
$Getopt::Long::ignorecase = 0;
Getopt::Long::Configure( "bundling" );
#############################################
# Process command-line flags
#############################################
if (!GetOptions( \%opt,
qw(h|help V|verbose v|version x z w r n range=s s=s c=s setup pdu))) {
return( usage() );
}
#############################################
# Check for node range
#############################################
if ( scalar(@ARGV) == 1 ) {
my @nodes = xCAT::NodeRange::noderange( @ARGV );
if (nodesmissed) {
return (usage( "The following nodes are not defined in xCAT DB:\n " . join(',', nodesmissed)));
}
foreach (@nodes) {
push @filternodes, $_;
}
unless (@filternodes) {
return(usage( "Invalid Argument: $ARGV[0]" ));
}
if ( exists( $opt{range} )) {
return(usage( "--range flag cannot be used with noderange." ));
}
} elsif ( scalar(@ARGV) > 1 ) {
return(usage( "Invalid flag, please check and retry." ));
}
#############################################
# Option -V for verbose output
#############################################
if ( exists( $opt{V} )) {
$globalopt{verbose} = 1;
}
#############################################
# Check for mutually-exclusive formatting
#############################################
if ( (exists($opt{r}) + exists($opt{x}) + exists($opt{z}) ) > 1 ) {
return( usage() );
}
#############################################
# Check for unsupported scan types
#############################################
if ( exists( $opt{s} )) {
my @stypes = split ',', $opt{s};
my $error;
foreach my $st (@stypes) {
if (! exists($global_scan_type{$st})) {
$error = $error . "Invalide scan type: $st\n";
}
}
if ($error) {
return usage($error);
}
$globalopt{scan_types} = \@stypes;
}
#############################################
# Check the --range ip range option
#############################################
if ( exists( $opt{range} )) {
$globalopt{range} = $opt{range};
my @ips = split /,/, $opt{range};
foreach my $ip (@ips) {
if (($ip =~ /^(\d{1,3})(-\d{1,3})?\.(\d{1,3})(-\d{1,3})?\.(\d{1,3})(-\d{1,3})?\.(\d{1,3})(-\d{1,3})?$/) ||
($ip =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\/(\d+)$/)) {
push @iprange, $ip;
} else {
return usage("Invalid ip or ip range specified: $ip.");
}
}
}
#############################################
# write to the database
#############################################
if ( exists( $opt{w} )) {
$globalopt{w} = 1;
}
#############################################
# list the raw information
#############################################
if ( exists( $opt{r} )) {
$globalopt{r} = 1;
}
#############################################
# list the xml formate data
#############################################
if ( exists( $opt{x} )) {
$globalopt{x} = 1;
}
#############################################
# list the stanza formate data
#############################################
if ( exists( $opt{z} )) {
$globalopt{z} = 1;
}
#########################################################
# only list the nodes that discovered for the first time
#########################################################
if ( exists( $opt{n} )) {
$globalopt{n} = 1;
}
#########################################################
# Accept the community string from user
#########################################################
if ( exists( $opt{c} )) {
$community=$opt{c};
}
#########################################################
# setup discover switch
#########################################################
if ( exists( $opt{setup} )) {
$globalopt{setup} = 1;
}
$device = "switch";
if ( exists( $opt{pdu} )) {
$globalopt{pdu} = 1;
$device = "pdu";
}
return;
}
#--------------------------------------------------------------------------------
=head3 preprocess_request
Parse the arguments and display the usage or the version string.
=cut
#--------------------------------------------------------------------------------
sub preprocess_request {
my $req = shift;
if ($req->{_xcatpreprocessed}->[0] == 1) { return [$req]; }
my $callback=shift;
my $command = $req->{command}->[0];
my $extrargs = $req->{arg};
my @exargs=($req->{arg});
if (ref($extrargs)) {
@exargs=@$extrargs;
}
my $usage_string=xCAT::Usage->parseCommand($command, @exargs);
if ($usage_string) {
$callback->({data=>[$usage_string]});
$req = {};
return;
}
my @result = ();
my $mncopy = {%$req};
push @result, $mncopy;
return \@result;
}
#--------------------------------------------------------------------------------
=head3 process_request
Pasrse the arguments and call the correspondent functions
to do switch discovery.
=cut
#--------------------------------------------------------------------------------
sub process_request {
my $req = shift;
my $callback = shift;
my $sub_req = shift;
###########################################
# Build hash to pass around
###########################################
my %request;
$request{arg} = $req->{arg};
$request{callback} = $callback;
$request{command} = $req->{command}->[0];
####################################
# Process command-specific options
####################################
my $result = parse_args( \%request );
####################################
# Return error
####################################
if ( ref($result) eq 'ARRAY' ) {
send_msg( \%request, 1, @$result );
return(1);
}
# call the relavant functions to start the scan
my @scan_types = ("nmap");
if (exists($globalopt{scan_types})) {
@scan_types = @{$globalopt{scan_types}};
}
my %username_hash = ();
$result = undef;
foreach my $st (@scan_types) {
no strict;
my $fn = $global_scan_type{$st};
my $tmp_result = &$fn(\%request, $callback);
if (ref($tmp_result) eq 'HASH') {
foreach (keys %$tmp_result) {
$result->{$_} = $tmp_result->{$_};
#appending mac address to end of hostname
my $name = $result->{$_}->{name};
if (exists $username_hash{$name}) {
if ($name eq '') {
$name = "$device";
}
my $mac_str = lc($_);
$mac_str =~ s/\://g;
$result->{$_}->{name} = "$name-$mac_str";
} else {
$username_hash{$name} = 1;
}
}
}
}
if (!($result)) {
send_msg( \%request, 0, " No $device found ");
return;
}
my $display_done = 0;
if (exists($globalopt{r})) {
#do nothing since is done by the scan functions.
$display_done = 1;
}
if (exists($globalopt{x})) {
send_msg( \%request, 0, format_xml( $result ));
$display_done = 1;
}
if (exists($globalopt{z})) {
my $stanza_output = format_stanza( $result );
send_msg( \%request, 0, $stanza_output );
$display_done = 1;
}
if (!$display_done) {
#display header
$format = "%-12s\t%-20s\t%-50s\t%-12s";
$header = sprintf $format, "ip", "name","vendor", "mac";
send_msg(\%request, 0, $header);
my $sep = "------------";
send_msg(\%request, 0, sprintf($format, $sep, $sep, $sep, $sep ));
#display switches one by one
foreach my $key (keys(%$result)) {
my $ip = " ";
my $vendor = " ";
my $mac = " ";
if (exists($result->{$key}->{ip})) {
$ip = $result->{$key}->{ip};
}
my $name = get_hostname($result->{$key}->{name}, $ip);
if (exists($result->{$key}->{vendor})) {
$vendor = $result->{$key}->{vendor};
}
if ($key !~ /nomac/) {
$mac = $key;
}
my $msg = sprintf $format, $ip, $name, $vendor, $mac;
send_msg(\%request, 0, $msg);
}
send_msg(\%request, 0,"\n");
}
my ($discoverswitch, $predefineswitch) = matchPredefineSwitch($result, \%request, $sub_req);
# writes the data into xCAT db
if (exists($globalopt{w})) {
send_msg(\%request, 0, "Writing the data into xCAT DB....");
xCATdB($discoverswitch, \%request, $sub_req);
}
if (exists($globalopt{setup})) {
switchsetup($predefineswitch, \%request, $sub_req);
}
return;
}
#--------------------------------------------------------------------------------
=head3 lldp_scan
Use lldpd to scan the subnets to do switch discovery.
Arguments:
request: request structure with callback pointer.
Returns:
A hash containing the swithes discovered.
Each element is a hash of switch attributes. For examples:
{
"AABBCCDDEEFA" =>{name=>"switch1", vendor=>"ibm", ip=>"10.1.2.3"},
"112233445566" =>{name=>"switch2", vendor=>"cisco", ip=>"11.4.5.6"}
}
returns 1 if there are errors occurred.
=cut
#--------------------------------------------------------------------------------
sub lldp_scan {
my $request = shift;
send_msg($request, 0, "Discovering switches using lldp...");
# get the PID of the currently running lldpd if it is running.
if (exists($globalopt{verbose})) {
send_msg($request, 0, "...Checking if lldpd is up and running:\n ps -ef | grep lldpd | grep -v grep | awk '{print \$2}'\n");
}
my $pid;
chomp($pid= `ps -ef | grep lldpd | grep -v grep | awk '{print \$2}'`);
unless($pid){
my $dcmd = "lldpd -c -s -e -f";
#my $outref = xCAT::Utils->runcmd($dcmd, 0);
#if ($::RUNCMD_RC != 0)
#{
# send_msg($request, 1, "Could not start lldpd process. The command was: $dcmd" #);
# return 1;
#}
#xCAT::Utils->runcmd("sleep 30");
send_msg($request, 1, "Warning: lldpd is not running. Please start it with the following flags:\n $dcmd\nThen wait a few minutes before running switchdiscover command again.\n");
return 1;
}
#now run the lldpcli to collect the data
my $ccmd = "lldpcli show neighbors -f xml";
if (exists($globalopt{verbose})) {
send_msg($request, 0, "...Discovering switches using lldpd:\n $ccmd\n");
}
my $result = xCAT::Utils->runcmd($ccmd, 0);
if ($::RUNCMD_RC != 0)
{
send_msg($request, 1, "Could not start lldpd process. The command was: $ccmd" );
return 1;
}
if (exists($globalopt{verbose})) {
send_msg($request, 0, "$result\n");
}
#display the raw output
if (exists($globalopt{r})) {
my $ccmd = "lldpcli show neighbors";
my $raw_result = xCAT::Utils->runcmd($ccmd, 0);
if ($::RUNCMD_RC == 0)
{
send_msg($request, 0, "$raw_result\n\n");
}
}
if (exists($globalopt{verbose})) {
send_msg($request, 0, "...Converting XML output to hash.\n");
}
my $result_ref = XMLin($result, KeyAttr => 'interface', ForceArray => 1);
my $switches;
my $counter=0;
if ($result_ref) {
if (exists($result_ref->{interface})) {
my $ref1 = $result_ref->{interface};
foreach my $interface (@$ref1) {
if (exists($interface->{chassis})) {
my $chassis = $interface->{chassis}->[0];
my $name = $chassis->{name}->[0]->{content};
my $ip = $chassis->{'mgmt-ip'}->[0]->{content};
# resolve the ip from name
if (!$ip) {
if ($name) {
$ip = xCAT::NetworkUtils->getipaddr($name);
}
}
my $id = $chassis->{id}->[0]->{content};
if (!$id) {
$id="nomac_lldp_$counter";
$counter++;
}
my $desc = $chassis->{descr}->[0]->{content};
if ($desc) {
$desc =~ s/\n/ /g;
$desc =~ s/\"//g;
}
if ($id) {
$switches->{$id}->{name} = $name;
$switches->{$id}->{ip} = $ip;
$switches->{$id}->{vendor} = $desc;
}
}
}
}
}
# filter out the uwanted entries if noderange or ip range is specified.
if ((@filternodes> 0) || (@iprange>0)) {
my $ranges = get_ip_ranges($request);
if (exists($globalopt{verbose})) {
send_msg($request, 0, "...Removing the switches that are not within the following ranges:\n @$ranges\n");
}
foreach my $mac (keys %$switches) {
my $ip_r = $switches->{$mac}->{ip};
$match = 0;
foreach my $ip_f (@$ranges) {
my ($net, $mask) = split '/', $ip_f;
if ($mask) { #this is a subnet
$mask = xCAT::NetworkUtils::formatNetmask($mask, 1, 0);
if (xCAT::NetworkUtils->ishostinsubnet($ip_r, $mask, $net)) {
$match = 1;
last;
}
} else { #this is an ip
if ($ip_r eq $net) {
$match = 1;
last;
}
#TODO: handles the case where the range is something like 10.2-3.4.5-6
}
}
if (!$match) {
delete $switches->{$mac};
}
}
}
return $switches
}
#--------------------------------------------------------------------------------
=head3 nmap_scan
Use nmap to scan the subnets to do switch discovery.
Arguments:
request: request structure with callback pointer.
Returns:
A hash containing the swithes discovered.
Each element is a hash of switch attributes. For examples:
{
"AABBCCDDEEFA" =>{name=>"switch1", vendor=>"ibm", ip=>"10.1.2.3"},
"112233445566" =>{name=>"switch2", vendor=>"cisco", ip=>"11.4.5.6"}
}
returns 1 if there are errors occurred.
=cut
#--------------------------------------------------------------------------------
sub nmap_scan {
my $request = shift;
my $ccmd;
#################################################
# If --range options, take iprange, if noderange is defined
# us the ip addresses of the nodes. If none is define, use the
# subnets for all the interfaces.
##################################################
my $ranges = get_ip_ranges($request);
send_msg($request, 0, "Discovering switches using nmap for @$ranges. It may take long time...");
#warning the user if the range is too big
foreach my $r (@$ranges) {
if ($r =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\/(\d+)$/) {
if ($5 < 24) {
send_msg($request, 0, "You can modify the --range parameters to cut down the time.\n" );
last;
}
}
}
# handle ctrl-c
$SIG{TERM} = $SIG{INT} = sub {
#clean up the nmap processes
my $nmap_pid = `ps -ef | grep nmap | grep -v grep | awk '{print \$2}'`;
if ($nmap_pid) {
system("kill -9 $nmap_pid >/dev/null 2>&1");
exit 0;
}
};
my $nmap_version = xCAT::Utils->get_nmapversion();
if (xCAT::Utils->version_cmp($nmap_version,"5.10") < 0) {
$ccmd = "/usr/bin/nmap -sP -oX - @$ranges";
} else {
$ccmd = "/usr/bin/nmap -sn -oX - @$ranges";
}
if (exists($globalopt{verbose})) {
send_msg($request, 0, "Process command: $ccmd\n");
}
my $result = xCAT::Utils->runcmd($ccmd, 0);
if ($::RUNCMD_RC != 0)
{
send_msg($request, 1, "Could not process this command: $ccmd" );
return 1;
}
#################################################
#display the raw output
#################################################
if (defined($globalopt{r}) || defined($globalopt{verbose})) {
send_msg($request, 0, "$result\n" );
}
#################################################
#compose the switch hash
#################################################
my $result_ref = XMLin($result, ForceArray => 1);
my $switches;
my $found;
my $counter=0;
my $osguess_ips=[];
if ($result_ref) {
if (exists($result_ref->{host})) {
my $host_ref = $result_ref->{host};
foreach my $host ( @$host_ref ) {
my $ip;
my $mac;
if (exists($host->{address})) {
my $addr_ref = $host->{address};
$found = 0;
foreach my $addr ( @$addr_ref ) {
my $type = $addr->{addrtype};
if ( $type ne "mac" ) {
$ip = $addr->{addr};
$found = 0;
} else {
$mac = $addr->{addr};
}
if (!$mac) {
$mac="nomac_nmap_$counter";
$counter++;
}
my $vendor = $addr->{vendor};
if ($vendor) {
my $search_string = join '|', keys(%xCAT::data::switchinfo::global_switch_type);
if ($vendor =~ /($search_string)/) {
$found = 1;
}
}
if ( ($found == 0) && ($type eq "mac") ) {
my $search_string = join '|', keys(%xCAT::data::switchinfo::global_mac_identity);
if ($mac =~ /($search_string)/i) {
my $key = $1;
$vendor = $xCAT::data::switchinfo::global_mac_identity{lc $key};
$found = 1;
}
# still not found, used nmap osscan command
if ( $found == 0) {
push(@$osguess_ips, $ip);
}
}
if ($found == 1) {
$switches->{$mac}->{ip} = $ip;
$switches->{$mac}->{vendor} = $vendor;
$switches->{$mac}->{name} = $host->{hostname};
if (exists($globalopt{verbose})) {
send_msg($request, 0, "FOUND Switch: found = $found, ip=$ip, mac=$mac, vendor=$vendor\n");
}
}
} #end for each address
}
} #end for each host
}
}
if ($osguess_ips) {
my $guess_switches = nmap_osguess($request, $osguess_ips);
foreach my $guess_mac ( keys %$guess_switches ) {
$switches->{$guess_mac}->{ip} = $guess_switches->{$guess_mac}->{ip};
$switches->{$guess_mac}->{vendor} = $guess_switches->{$guess_mac}->{vendor};
}
}
return $switches;
}
##########################################################
# If there is no vendor or other than %global_switch_type,
# issue the nmap again to do more aggresively discovery
# Choose best guess from osscan
# only search port 22 and 23 for fast performance
###########################################################
sub nmap_osguess {
my $request = shift;
my $ranges = shift;
my $switches;
my $cmd;
if (exists($globalopt{verbose})) {
send_msg($request, 0, "Couldn't find vendor info, use nmap osscan command to choose best guess");
}
my $nmap_version = xCAT::Utils->get_nmapversion();
if (xCAT::Utils->version_cmp($nmap_version,"5.10") < 0) {
$cmd = "/usr/bin/nmap -O --osscan-guess -A -p 22,23 @$ranges | grep -E 'Interesting ports on|MAC Addres|Device|Running|Aggressive OS guesses' ";
} else {
$cmd = "/usr/bin/nmap -O --osscan-guess -A -p 22,23 @$ranges | grep -E 'Nmap scan report|MAC Addres|Device|Running|Aggressive OS guesses' ";
}
if (exists($globalopt{verbose})) {
send_msg($request, 0, "Process command: $cmd");
}
my $result = xCAT::Utils->runcmd($cmd, 0);
if (defined($globalopt{r}) || defined($globalopt{verbose})) {
send_msg($request, 0, "$result\n" );
}
if ($::RUNCMD_RC == 0)
{
my @lines;
if (xCAT::Utils->version_cmp($nmap_version,"5.10") < 0) {
@lines = split /Interesting ports /, $result;
} else {
@lines = split /Nmap scan /, $result;
}
foreach my $lines_per_ip (@lines) {
my @lines2 = split /\n/, $lines_per_ip;
my $isswitch=0;
my $ip;
my $mac;
my $vendor;
foreach my $line (@lines2) {
if ($line =~ /\b(\d{1,3}(?:\.\d{1,3}){3})\b/)
{
$ip = $1;
}
if ($line =~ /MAC Address/) {
my @array = split / /, $line;
$mac = $array[2];
}
if ( $line =~ /Device type/ ) {
if ( ( $line =~ /switch/) || ( $line =~ /router/) ) {
$isswitch=1;
} else {
last;
}
}
my $search_string = join '|', keys(%xCAT::data::switchinfo::global_switch_type);
if ($line =~ /Running/) {
if ($line =~ /($search_string)/){
$vendor = $1;
last;
}
}
if ($line =~ /Aggressive OS/) {
if ($line =~ /($search_string)/){
$vendor = $1;
$isswitch=1;
}
}
}
if ($isswitch == 1) {
$switches->{$mac}->{ip} = $ip;
$switches->{$mac}->{vendor} = $vendor;
if (exists($globalopt{verbose})) {
send_msg($request, 0, "FOUND switch from osscan-guess: $ip, $mac, $vendor");
}
}
}
}
return $switches;
}
#--------------------------------------------------------------------------------
=head3 snmp_scan
Use snmp to scan the subnets to do switch discovery.
Arguments:
request: request structure with callback pointer.
Returns:
A hash containing the swithes discovered.
Each element is a hash of switch attributes. For examples:
{
"AABBCCDDEEFA" =>{name=>"switch1", vendor=>"ibm", ip=>"10.1.2.3"},
"112233445566" =>{name=>"switch2", vendor=>"cisco", ip=>"11.4.5.6"}
}
returns 1 if there are errors occurred.
=cut
#--------------------------------------------------------------------------------
sub snmp_scan {
my $request = shift;
my $ccmd;
my $result;
my $switches;
my $counter = 0;
#################################################
# If --range options, take iprange, if noderange is defined
# us the ip addresses of the nodes. If none is define, use the
# subnets for all the interfaces.
##################################################
my $ranges = get_ip_ranges($request);
# snmpwalk command has to be available for snmp_scan
if (-x "/usr/bin/snmpwalk" ){
send_msg($request, 0, "Discovering $device using snmpwalk for @$ranges .....");
} else {
send_msg($request, 0, "snmpwalk is not available, please install snmpwalk command first");
return 1;
}
# handle ctrl-c
$SIG{TERM} = $SIG{INT} = sub {
#clean up the nmap processes
my $nmap_pid = `ps -ef | grep /usr/bin/nmap | grep -v grep | grep -v "sh -c" |awk '{print \$2}'`;
if ($nmap_pid) {
system("kill -9 $nmap_pid >/dev/null 2>&1");
exit 0;
}
};
##########################################################
#use nmap to parse the ip range and possible output from the command:
# Nmap scan report for switch-10-5-22-1 (10.5.22.1) 161/udp open snmp
# Nmap scan report for 10.5.23.1 161/udp open snmp
# Nmap scan report for 10.5.24.1 161/udp closed snmp
##########################################################
my $nmap_version = xCAT::Utils->get_nmapversion();
if (xCAT::Utils->version_cmp($nmap_version,"5.10") < 0) {
$ccmd = "/usr/bin/nmap -P0 -v -sU -p 161 -oA snmp_scan @$ranges | grep -E 'appears to be up|^161' | perl -pe 's/\\n/ / if \$. % 2'";
} else {
$ccmd = "/usr/bin/nmap -P0 -v -sU -p 161 -oA snmp_scan @$ranges | grep -v 'host down' | grep -E 'Nmap scan report|^161' | perl -pe 's/\\n/ / if \$. % 2'";
}
if (exists($globalopt{verbose})) {
send_msg($request, 0, "Process command: $ccmd\n");
}
$result = xCAT::Utils->runcmd($ccmd, 0);
if ($::RUNCMD_RC != 0)
{
send_msg($request, 1, "Could not process this command: $ccmd" );
return 1;
}
#################################################
#display the raw output
#################################################
if (defined($globalopt{r}) || defined($globalopt{verbose})) {
send_msg($request, 0, "$result\n" );
}
my @lines = split /\n/, $result;
foreach my $line (@lines) {
my @array = split / /, $line;
if ($line =~ /\b(\d{1,3}(?:\.\d{1,3}){3})\b/)
{
$ip = $1;
}
if (exists($globalopt{verbose})) {
send_msg($request, 0, "Run snmpwalk command to get information for $ip");
}
if ($line =~ /close/) {
send_msg($request, 0, "*** snmp port is disabled for $ip ***");
next;
}
my $vendor = get_snmpvendorinfo($request, $ip);
if ($vendor) {
my $display = "";
my $nopping = "nopping";
my $output = xCAT::SvrUtils->get_mac_by_arp([$ip], $display, $nopping);
my ($desc,$mac) = split /: /, $output->{$ip};
if (exists($globalopt{verbose})) {
send_msg($request, 0, "node: $ip, mac: $mac");
}
if (!$mac) {
$mac="nomac_nmap_$counter";
$counter++;
}
my $hostname = get_snmphostname($request, $ip);
my $stype = get_switchtype($vendor);
$switches->{$mac}->{ip} = $ip;
$switches->{$mac}->{vendor} = $vendor;
$switches->{$mac}->{name} = $hostname;
if (exists($globalopt{verbose})) {
send_msg($request, 0, "found $device: $hostname, $ip, $stype, $vendor");
}
}
}
return $switches;
}
#--------------------------------------------------------------------------------
=head3 get_snmpvendorinfo
return vendor info from snmpwalk command
Arguments:
ip : IP address passed by the switch after scan
Returns:
vendor: vendor info of the switch
=cut
#--------------------------------------------------------------------------------
sub get_snmpvendorinfo {
my $request = shift;
my $ip = shift;
my $snmpwalk_vendor;
my $result;
my $rc=1;
#checking snmp connection
#during discovery phase, xCAT will not know the community string for snmp
#so, will try community string from following way:
# 1) -c options from user input
# 2) snmpc attribute defined in the site table
# 3) default community string for the switches.
my @comm_list;
my $snmpver = '1';
my @snmpcs = xCAT::TableUtils->get_site_attribute("snmpc");
my $snmp_site = $snmpcs[0];
if ($community) {
push @comm_list, $community;
}
if ($snmp_site) {
push @comm_list, $snmp_site;
}
push @comm_list, 'public';
foreach $comms(@comm_list) {
#get sysDescr.0";
my $ccmd = "snmpwalk -Os -v1 -c $comms $ip 1.3.6.1.2.1.1.1";
if (exists($globalopt{verbose})) {
send_msg($request, 0, "Process command: $ccmd\n");
}
$result = xCAT::Utils->runcmd($ccmd, 0);
if ($::RUNCMD_RC == 0)
{
$community = $comms;
$rc=0;
last;
}
}
if ($rc == 1) {
if (exists($globalopt{verbose})) {
send_msg($request, 1, "Could not process snmpwalk command to get sysDescr, please verify community string" );
}
return $snmpwalk_vendor;
}
my ($desc,$model) = split /: /, $result;
if (exists($globalopt{pdu})) {
if ( ($model =~ /pdu/) || ($model =~ /PDU/) ) {
return $model;
}
return $snmpwalk_vendor;
}
if (exists($globalopt{verbose})) {
send_msg($request, 0, "switch model = $model\n" );
}
return $model;
}
#--------------------------------------------------------------------------------
=head3 get_snmpmac
return mac address from snmpwalk command
Arguments:
ip : IP address passed by the switch after scan
Returns:
mac: mac address of the switch
=cut
#--------------------------------------------------------------------------------
sub get_snmpmac {
my $request = shift;
my $ip = shift;
my $mac;
#Ubuntu only takes OID
#get ipNetToMediaPhysAddress;
my $ccmd = "snmpwalk -Os -v1 -c $community $ip 1.3.6.1.2.1.4.22.1.2 | grep $ip";
if (exists($globalopt{verbose})) {
send_msg($request, 0, "Process command: $ccmd\n");
}
my $result = xCAT::Utils->runcmd($ccmd, 0);
if ($::RUNCMD_RC != 0)
{
if (exists($globalopt{verbose})) {
send_msg($request, 1, "Could not process this command: $ccmd" );
}
return $mac;
}
my ($desc,$mac) = split /: /, $result;
#trim the white space at begin and end of mac
$mac =~ s/^\s+|\s+$//g;
#replace space to :
$mac =~ tr/ /:/;
if (exists($globalopt{verbose})) {
send_msg($request, 0, "switch mac = $mac\n" );
}
return $mac;
}
#--------------------------------------------------------------------------------
=head3 get_snmphostname
return hostname from snmpwalk command
Arguments:
ip : IP address passed by the switch after scan
Returns:
mac: hostname of the switch
=cut
#--------------------------------------------------------------------------------
sub get_snmphostname {
my $request = shift;
my $ip = shift;
my $hostname;
#Ubuntu only takes OID
#get sysName info;
my $ccmd = "snmpwalk -Os -v1 -c $community $ip 1.3.6.1.2.1.1.5";
if (exists($globalopt{verbose})) {
send_msg($request, 0, "Process command: $ccmd\n");
}
my $result = xCAT::Utils->runcmd($ccmd, 0);
if ($::RUNCMD_RC != 0)
{
if (exists($globalopt{verbose})) {
send_msg($request, 1, "Could not process this command: $ccmd" );
}
return $hostname;
}
my ($desc,$hostname) = split /: /, $result;
if (exists($globalopt{verbose})) {
send_msg($request, 0, "$device hostname = $hostname\n" );
}
return $hostname;
}
#--------------------------------------------------------------------------------
=head3 get_hostname
return hostname for the switch discovered
Arguments:
host: hostname passed by the switch after scan
ip : IP address passed by the switch after scan
Returns:
host: hostname of the switch
if host is empty, try to lookup use ip address, otherwise format hostname
as switch and ip combination. ex: switch-9-114-5-6
=cut
#--------------------------------------------------------------------------------
sub get_hostname {
my $host = shift;
my $ip = shift;
if ($host) {
return $host;
}
if ( !$host ) {
$host = gethostbyaddr( inet_aton($ip), AF_INET );
if ( !$host ) {
my $ip_str = $ip;
$ip_str =~ s/\./\-/g;
$host = "$device-$ip_str";
}
}
return $host;
}
#--------------------------------------------------------------------------------
=head3 get_switchtype
determine the switch type based on the switch vendor
Arguments:
vendor: switch vendor
Returns:
stype: type of switch, supports Juniper, Cisco, BNT and Mellanox
=cut
#--------------------------------------------------------------------------------
sub get_switchtype {
my $vendor = shift;
my $key = "Not support";
my $search_string = join '|', keys(%xCAT::data::switchinfo::global_switch_type);
if ($vendor =~ /($search_string)/) {
$key = $1;
return $xCAT::data::switchinfo::global_switch_type{$key};
} else {
if (exists($globalopt{pdu})) {
return "irpdu";
}
return $key;
}
}
#--------------------------------------------------------------------------------
=head3 xCATdB
Write discovered switch information to xCAT database.
Arguments:
outhash: A hash containing the swithes discovered.
request: The request structure for plugin.
sub_req: The request structure for runxcmd.
Returns:
none
=cut
#--------------------------------------------------------------------------------
sub xCATdB {
my $outhash = shift;
my $request = shift;
my $sub_req = shift;
my $ret;
#################################################
# write each switch to xcat database
##################################################
foreach my $mac ( keys %$outhash ) {
my $ip = $outhash->{$mac}->{ip};
my $vendor = $outhash->{$mac}->{vendor};
#Get hostname and switch type
my $host = get_hostname($outhash->{$mac}->{name}, $ip);
my $stype = get_switchtype($vendor);
if ($mac =~ /nomac/) {
$mac=" ";
}
#################################################
# use chdef command to make new device or update
# it's attribute
##################################################
if (exists($globalopt{pdu})) {
$ret = xCAT::Utils->runxcmd({ command => ['chdef'], arg => ['-t','node','-o',$host,"groups=$device","ip=$ip","mac=$mac","nodetype=$device","mgt=$device","usercomment=$vendor","pdutype=$stype"] }, $sub_req, 0, 1);
} else {
$ret = xCAT::Utils->runxcmd({ command => ['chdef'], arg => ['-t','node','-o',$host,"groups=$device","ip=$ip","mac=$mac","nodetype=$device","mgt=$device","usercomment=$vendor","switchtype=$stype"] }, $sub_req, 0, 1);
}
}
}
#--------------------------------------------------------------------------------
=head3 get_ip_ranges
Return the an array of ip ranges. If --range is specified, use it. If
noderange is specified, use the ip address of the nodes. Otherwise, use
the subnets for all the live nics on the xCAT mn.
Arguments:
request: request structure with callback pointer.
Returns:
A pointer of an array of ip ranges.
=cut
#--------------------------------------------------------------------------------
sub get_ip_ranges {
$request = shift;
# if --range is defined, just return the ranges specified by the user
if (@iprange > 0) {
return \@iprange;
}
# if noderange is defined, then put the ip addresses of the nodes in
if (@filternodes > 0) {
my @ipranges=();
foreach my $node (@filternodes) {
my $ip = xCAT::NetworkUtils->getipaddr($node);
push(@ipranges, $ip);
}
return \@ipranges;
}
# for default, use the subnets for all the enabled networks
# defined in the networks table.
my $ranges=[];
my $nettab = xCAT::Table->new('networks');
if ($nettab) {
my $netents = $nettab->getAllEntries();
foreach (@$netents) {
my $net = $_->{'net'};
my $nm = $_->{'mask'};
my $fnm = xCAT::NetworkUtils::formatNetmask($nm, 0 , 1);
$net .="/$fnm";
push(@$ranges, $net);
}
}
if (!@$ranges) {
send_msg($request, 1, "ip range is empty, nothing to discover" );
exit 0;
}
return $ranges;
}
#-------------------------------------------------------------------------------
=head3 format_stanza
list the stanza format for swithes
Arguments:
outhash: a hash containing the switches discovered
Returns:
result: return lists as stanza format for swithes
=cut
#--------------------------------------------------------------------------------
sub format_stanza {
my $outhash = shift;
my $result;
#####################################
# Write attributes
#####################################
foreach my $mac ( keys %$outhash ) {
my $ip = $outhash->{$mac}->{ip};
my $vendor = $outhash->{$mac}->{vendor};
#Get hostname and switch type
my $host = get_hostname($outhash->{$mac}->{name}, $ip);
my $stype = get_switchtype($vendor);
if ($mac =~ /nomac/) {
$mac = " ";
}
$result .= "$host:\n\tobjtype=node\n";
$result .= "\tgroups=$device\n";
$result .= "\tip=$ip\n";
$result .= "\tmac=$mac\n";
$result .= "\tmgt=$device\n";
$result .= "\tnodetype=$device\n";
if (!exists($globalopt{pdu})) {
$result .= "\tswitchtype=$stype\n";
} else {
$result .= "\tpdutype=$stype\n";
}
}
return ($result);
}
#--------------------------------------------------------------------------------
=head3 format_xml
list the xml format for swithes
Arguments:
outhash: a hash containing the switches discovered
Returns:
result: return lists as xml format for swithes
=cut
#--------------------------------------------------------------------------------
sub format_xml {
my $outhash = shift;
my $xml;
#####################################
# Write attributes
#####################################
foreach my $mac ( keys %$outhash ) {
my $result;
my $ip = $outhash->{$mac}->{ip};
my $vendor = $outhash->{$mac}->{vendor};
#Get hostname and switch type
my $host = get_hostname($outhash->{$mac}->{name}, $ip);
my $stype = get_switchtype($vendor);
if ($mac =~ /nomac/) {
$mac = " ";
}
$result .= "hostname=$host\n";
$result .= "objtype=node\n";
$result .= "groups=$device\n";
$result .= "ip=$ip\n";
$result .= "mac=$mac\n";
$result .= "mgt=$device\n";
$result .= "nodetype=$device\n";
if (!exists($globalopt{pdu})) {
$result .= "switchtype=$stype\n";
} else {
$result .= "\tpdutype=$stype\n";
}
my $href = {
Switch => { }
};
my @attr = split '\\n', $result;
for (my $i = 0; $i < scalar(@attr); $i++ ){
if( $attr[$i] =~ /(\w+)\=(.*)/){
$href->{Switch}->{$1} = $2;
}
}
$xml.= XMLout($href,
NoAttr => 1,
KeyAttr => [],
RootName => undef );
}
return ($xml);
}
#-------------------------------------------------------------------------------
=head3 matchPredefineSwitch
find discovered switches with predefine switches
for each discovered switches:
Arguments:
outhash: a hash containing the switches discovered
Returns:
result:
=cut
#--------------------------------------------------------------------------------
sub matchPredefineSwitch {
my $outhash = shift;
my $request = shift;
my $sub_req = shift;
my $discoverswitch;
my $configswitch;
#print Dumper($outhash);
my $macmap = xCAT::MacMap->new();
#################################################
# call find_mac to match pre-defined switch and
# discovery switch
##################################################
foreach my $mac ( keys %$outhash ) {
my $ip = $outhash->{$mac}->{ip};
my $vendor = $outhash->{$mac}->{vendor};
# issue makehosts so we can use xdsh
my $dswitch = get_hostname($outhash->{$mac}->{name}, $ip);
my $node = $macmap->find_mac($mac,0,1);
if (!$node) {
send_msg($request, 0, "$device discovered: $dswitch ");
$discoverswitch->{$mac}->{ip} = $ip;
$discoverswitch->{$mac}->{vendor} = $vendor;
$discoverswitch->{$mac}->{name} = $dswitch;
next;
}
my $stype = get_switchtype($vendor);
if (exists($globalopt{pdu})) {
$stype="pdu";
}
send_msg($request, 0, "$device discovered and matched: $dswitch to $node" );
# only write to xcatdb if -w or --setup option specified
if ( (exists($globalopt{w})) || (exists($globalopt{setup})) ) {
if (exists($globalopt{pdu})) {
xCAT::Utils->runxcmd({ command => ['chdef'], arg => ['-t','node','-o',$node,"otherinterfaces=$ip",'status=Matched',"mac=$mac","usercomment=$vendor","pdutype=$stype"] }, $sub_req, 0, 1);
} else {
xCAT::Utils->runxcmd({ command => ['chdef'], arg => ['-t','node','-o',$node,"otherinterfaces=$ip",'status=Matched',"mac=$mac","switchtype=$stype","usercomment=$vendor"] }, $sub_req, 0, 1);
}
}
push (@{$configswitch->{$stype}}, $node);
}
return ($discoverswitch, $configswitch);
}
#--------------------------------------------------------------------------------
=head3 switchsetup
configure the switch
Arguments:
outhash: a hash containing the switches need to configure
Returns:
result:
=cut
#--------------------------------------------------------------------------------
sub switchsetup {
my $nodes_to_config = shift;
my $request = shift;
my $sub_req = shift;
if (exists($globalopt{pdu})) {
my $mytype = "pdu";
my $nodetab = xCAT::Table->new('hosts');
my $nodehash = $nodetab->getNodesAttribs(\@{${nodes_to_config}->{$mytype}},['ip','otherinterfaces']);
# get netmask from network table
my $nettab = xCAT::Table->new("networks");
my @nets;
if ($nettab) {
@nets = $nettab->getAllAttribs('net','mask');
}
foreach my $pdu(@{${nodes_to_config}->{$mytype}}) {
my $cmd = "rspconfig $pdu sshcfg";
xCAT::Utils->runcmd($cmd, 0);
my $ip = $nodehash->{$pdu}->[0]->{ip};
my $mask;
foreach my $net (@nets) {
if (xCAT::NetworkUtils::isInSameSubnet( $net->{'net'}, $ip, $net->{'mask'}, 0)) {
$mask=$net->{'mask'};
}
}
$cmd = "rspconfig $pdu hostname=$pdu ip=$ip netmask=$mask";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC == 0) {
xCAT::Utils->runxcmd({ command => ['chdef'], arg => ['-t','node','-o',$pdu,"ip=$ip","otherinterfaces="] }, $sub_req, 0, 1);
} else {
send_msg($request, 0, "Failed to run rspconfig command to set ip/netmask\n");
}
}
return;
}
foreach my $mytype (keys %$nodes_to_config) {
my $config_script = "$::XCATROOT/share/xcat/scripts/config".$mytype;
if (-r -x $config_script) {
my $switches = join(",",@{${nodes_to_config}->{$mytype}});
if ($mytype eq "onie") {
send_msg($request, 0, "Call to config $switches\n");
my $out = `$config_script --switches $switches --all`;
send_msg($request, 0, "output = $out\n");
} else {
send_msg($request, 0, "call to config $mytype switches $switches\n");
if ($mytype =~ /Mellanox/) {
send_msg($request, 0, "NOTE: If command takes too long for Mellanox IB switch, please CTRL C out of the command\n");
send_msg($request, 0, "then run $config_script --switches $switches --all\n");
}
my $out = `$config_script --switches $switches --all`;
send_msg($request, 0, "output = $out\n");
}
} else {
send_msg($request, 0, "the switch type $mytype is not support yet\n");
}
}
return;
}
1;