From aad25d8cb5699ddc4d14419b990d167681a9d485 Mon Sep 17 00:00:00 2001 From: Casandra Qiu Date: Fri, 2 Sep 2016 12:18:21 -0400 Subject: [PATCH 1/3] Checking nodetype to distinguish between node/switch discovery --- perl-xCAT/xCAT/MacMap.pm | 23 ++++++++++++++++++- .../lib/xcat/plugins/switchdiscover.pm | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/perl-xCAT/xCAT/MacMap.pm b/perl-xCAT/xCAT/MacMap.pm index 23eb69e91..2cb3de0d0 100644 --- a/perl-xCAT/xCAT/MacMap.pm +++ b/perl-xCAT/xCAT/MacMap.pm @@ -288,6 +288,10 @@ sub dump_mac_info { $self->{callback} = $callback; } my $community = "public"; + my @snmpcs = xCAT::TableUtils->get_site_attribute("snmpc"); + my $tmp = $snmpcs[0]; + if (defined($tmp)) { $community = $tmp } + my $dump_all_switches = 0; my %switches_to_dump = (); if (!defined($noderange)) { @@ -366,6 +370,7 @@ sub find_mac { my $self = shift; my $mac = shift; my $cachedonly = shift; + my $discover_switch=shift; # For now HARDCODE (TODO, configurable?) a cache as stale after five minutes # Also, if things are changed in the config, our cache could be wrong, @@ -390,7 +395,7 @@ sub find_mac { #If requesting a cache only check or the cache is a mere 20 seconds old #don't bother querying switches if ($cachedonly or ($self->{timestamp} > (time() - 20))) { return undef; } - $self->refresh_table; #not cached or stale cache, refresh + $self->refresh_table($discover_switch); #not cached or stale cache, refresh if ($self->{mactable}->{ lc($mac) }) { return $self->{mactable}->{ lc($mac) }; } @@ -426,6 +431,7 @@ sub fill_switchparms { sub refresh_table { my $self = shift; + my $discover_switch = shift; my $curswitch; $self->{mactable} = {}; $self->{switchtab} = xCAT::Table->new('switch', -create => 1); @@ -469,7 +475,22 @@ sub refresh_table { #Build hash of switch port names per switch $self->{switches} = {}; + + #get nodetype from nodetype + my $ntable = xCAT::Table->new('nodetype'); + my @typeentries = $ntable->getAllNodeAttribs(['node', 'nodetype']); + foreach my $ntnode (@typeentries) { + if ($ntnode->{nodetype} eq "switch") { + $self->{switches}->{$ntnode->{node} }->{nodetype} = $ntnode->{nodetype}; + xCAT::MsgUtils->message("S", "refresh_table: $ntnode->{node} is $self->{switches}->{$ntnode->{node} }->{nodetype}"); + } + } foreach my $entry (@entries) { + if ( (($discover_switch) and ($self->{switches}->{$entry->{node}}->{nodetype} ne "switch")) + or ( !($discover_switch) and ($self->{switches}->{$entry->{node}}->{nodetype} eq "switch")) ){ + xCAT::MsgUtils->message("S", "refresh_table: skip $entry->{node} and $entry->{switch}"); + next; + } if (defined($entry->{switch}) and $entry->{switch} ne "" and defined($entry->{port}) and $entry->{port} ne "") { if (!$self->{switches}->{ $entry->{switch} }->{ $entry->{port} }) diff --git a/xCAT-server/lib/xcat/plugins/switchdiscover.pm b/xCAT-server/lib/xcat/plugins/switchdiscover.pm index 2d4682ecf..01285a046 100644 --- a/xCAT-server/lib/xcat/plugins/switchdiscover.pm +++ b/xCAT-server/lib/xcat/plugins/switchdiscover.pm @@ -1383,7 +1383,7 @@ sub switchsetup { # issue makehosts so we can use xdsh my $dswitch = get_hostname($outhash->{$mac}->{name}, $ip); - my $node = $macmap->find_mac($mac,0); + my $node = $macmap->find_mac($mac,0,1); if (!$node) { send_msg($request, 0, "NO predefined switch matched this switch $dswitch with ip address $ip and mac address $mac"); next; From 7db31b82a5c3ad58493b70ebccee4a4fa4c1722a Mon Sep 17 00:00:00 2001 From: Casandra Qiu Date: Wed, 7 Sep 2016 12:59:46 -0400 Subject: [PATCH 2/3] create temp nodetype hash type --- perl-xCAT/xCAT/MacMap.pm | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/perl-xCAT/xCAT/MacMap.pm b/perl-xCAT/xCAT/MacMap.pm index 2cb3de0d0..a70e4eaaf 100644 --- a/perl-xCAT/xCAT/MacMap.pm +++ b/perl-xCAT/xCAT/MacMap.pm @@ -439,9 +439,6 @@ sub refresh_table { my @switchentries = $self->{switchestab}->getAllNodeAttribs([qw(switch snmpversion username password privacy auth)]); my $community = "public"; - #$self->{sitetab} = xCAT::Table->new('site'); - #my $tmp = $self->{sitetab}->getAttribs({key=>'snmpc'},'value'); - #if ($tmp and $tmp->{value}) { $community = $tmp->{value} } my @snmpcs = xCAT::TableUtils->get_site_attribute("snmpc"); my $tmp = $snmpcs[0]; if (defined($tmp)) { $community = $tmp } @@ -476,18 +473,23 @@ sub refresh_table { #Build hash of switch port names per switch $self->{switches} = {}; - #get nodetype from nodetype + #get nodetype from nodetype table, build a temp nodetype hash + my %typehash; + my @typeentries; my $ntable = xCAT::Table->new('nodetype'); - my @typeentries = $ntable->getAllNodeAttribs(['node', 'nodetype']); - foreach my $ntnode (@typeentries) { - if ($ntnode->{nodetype} eq "switch") { - $self->{switches}->{$ntnode->{node} }->{nodetype} = $ntnode->{nodetype}; - xCAT::MsgUtils->message("S", "refresh_table: $ntnode->{node} is $self->{switches}->{$ntnode->{node} }->{nodetype}"); - } + if ($ntable) { + @typeentries = $ntable->getAllNodeAttribs(['node', 'nodetype']); } + for my $typeentry (@typeentries) { + $typehash{ $typeentry->{node} } = $typeentry->{nodetype}; + } + foreach my $entry (@entries) { - if ( (($discover_switch) and ($self->{switches}->{$entry->{node}}->{nodetype} ne "switch")) - or ( !($discover_switch) and ($self->{switches}->{$entry->{node}}->{nodetype} eq "switch")) ){ + # if we are doing switch discovery and the node is not a switch, skip + # if we are NOT doing switch discovery, and the node is a switch, skip + my $ntype = $typehash{$entry->{node}}; + if ( (($discover_switch) and ( $ntype ne "switch")) + or ( !($discover_switch) and ( $ntype eq "switch")) ){ xCAT::MsgUtils->message("S", "refresh_table: skip $entry->{node} and $entry->{switch}"); next; } From 8e834c5e45f37ff4fbc5da4e478eac1b3260335e Mon Sep 17 00:00:00 2001 From: Casandra Qiu Date: Thu, 8 Sep 2016 10:37:42 -0400 Subject: [PATCH 3/3] using hashtable for nodetype --- perl-xCAT/xCAT/MacMap.pm | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/perl-xCAT/xCAT/MacMap.pm b/perl-xCAT/xCAT/MacMap.pm index a70e4eaaf..077c4d21b 100644 --- a/perl-xCAT/xCAT/MacMap.pm +++ b/perl-xCAT/xCAT/MacMap.pm @@ -439,6 +439,9 @@ sub refresh_table { my @switchentries = $self->{switchestab}->getAllNodeAttribs([qw(switch snmpversion username password privacy auth)]); my $community = "public"; + #$self->{sitetab} = xCAT::Table->new('site'); + #my $tmp = $self->{sitetab}->getAttribs({key=>'snmpc'},'value'); + #if ($tmp and $tmp->{value}) { $community = $tmp->{value} } my @snmpcs = xCAT::TableUtils->get_site_attribute("snmpc"); my $tmp = $snmpcs[0]; if (defined($tmp)) { $community = $tmp } @@ -473,21 +476,17 @@ sub refresh_table { #Build hash of switch port names per switch $self->{switches} = {}; - #get nodetype from nodetype table, build a temp nodetype hash - my %typehash; - my @typeentries; + #get nodetype from nodetype table, build a temp hash + my $typehash; my $ntable = xCAT::Table->new('nodetype'); if ($ntable) { - @typeentries = $ntable->getAllNodeAttribs(['node', 'nodetype']); - } - for my $typeentry (@typeentries) { - $typehash{ $typeentry->{node} } = $typeentry->{nodetype}; + $typehash = $ntable->getAllNodeAttribs(['node','nodetype'], 1); } foreach my $entry (@entries) { # if we are doing switch discovery and the node is not a switch, skip # if we are NOT doing switch discovery, and the node is a switch, skip - my $ntype = $typehash{$entry->{node}}; + my $ntype = $typehash->{$entry->{node}}->[0]->{nodetype}; if ( (($discover_switch) and ( $ntype ne "switch")) or ( !($discover_switch) and ( $ntype eq "switch")) ){ xCAT::MsgUtils->message("S", "refresh_table: skip $entry->{node} and $entry->{switch}");