2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2025-08-19 17:50:20 +00:00

Merge pull request #1324 from hu-weihua/probe-discovery

Implement discovery probe basic function
This commit is contained in:
Xiaopeng Wang
2016-06-30 11:21:03 +08:00
committed by GitHub
4 changed files with 3607 additions and 93 deletions

View File

@@ -5,7 +5,7 @@ package probe_utils;
use strict;
use File::Path;
use File::Copy;
use Socket;
#-----------------------------------------
=head3
@@ -48,12 +48,12 @@ sub send_msg {
}
if ($output eq "stdout") {
print "$flag $msg\n";
print "$flag$msg\n";
} else {
if (!open(LOGFILE, ">> $output")) {
return 1;
}
print LOGFILE "$flag $msg\n";
print LOGFILE "$flag$msg\n";
close LOGFILE;
}
return 0;
@@ -341,4 +341,98 @@ sub is_dns_ready {
}
}
#------------------------------------------
=head3
Description:
Convert host name to ip address
Arguments:
hostname: The hostname need to convert
Returns:
ip: The ip address
=cut
#------------------------------------------
sub get_ip_from_hostname{
my $hostname = shift;
$hostname=shift if(($hostname) && ($hostname =~ /probe_utils/));
my $ip = "";
my @output = `ping -c 1 $hostname 2>&1`;
if(!$?){
if($output[0] =~ /^PING.+\s+\((\d+\.\d+\.\d+\.\d+)\).+/){
$ip=$1;
}
}
return $ip;
}
#------------------------------------------
=head3
Description:
Calculate network address from ip and netmask
Arguments:
ip: ip address
mask: network mask
Returns:
network : The network address
=cut
#------------------------------------------
sub get_network{
my $ip = shift;
$ip=shift if(($ip) && ($ip =~ /probe_utils/));
my $mask = shift;
my $net="";
return $net if (!is_ip_addr($ip));
return $net if ($mask !~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/);
my $bin_mask = unpack("N", inet_aton($mask));
my $bin_ip = unpack("N", inet_aton($ip));;
my $net_int32 = $bin_mask & $bin_ip;
$net = ($net_int32 >> 24) . "." . (($net_int32 >> 16) & 0xff) . "." . (($net_int32 >> 8) & 0xff) . "." . ($net_int32 & 0xff);
return "$net/$mask";
}
#------------------------------------------
=head3
Description:
Convert ip to hostname
Arguments:
ip: The ip need to convert
Returns:
hostname: hostname or ""
=cut
#------------------------------------------
sub get_hostname_from_ip{
my $ip = shift;
$ip=shift if(($ip) && ($ip =~ /probe_utils/));
my $dns_server = shift;
my $hostname="";
my $output="";
`which nslookup > /dev/null 2>&1`;
if(!$?){
$output = `nslookup $ip $dns_server 2>&1`;
if (!$?) {
chomp($output);
my $rc = $hostname = `echo "$output"|awk -F" " '/name =/ {print \$4}'|awk -F"." '{print \$1}'`;
chomp($hostname);
return $hostname if (!$rc);
}
}
if(($hostname eq "") && (-e "/etc/hosts")){
$output = `cat /etc/hosts 2>&1 |grep $ip`;
if(!$?){
my @splitoutput = split(" ", $output);
$hostname = $splitoutput[1];
}
}
return $hostname;
}
1;

File diff suppressed because it is too large Load Diff

View File

@@ -5,39 +5,69 @@ BEGIN { $::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : -d '/opt/xcat' ? '/o
use lib "$::XCATROOT/probe/lib/perl";
use probe_utils;
use xCAT::NetworkUtils;
use File::Basename;
use IO::Select;
use Getopt::Long qw(:config no_ignore_case);
use Data::Dumper;
my $program_name = basename("$0");
my $help;
my $test;
my $output = "stdout";
my $verbose = 0;
my $rst = 0;
my $output = "stdout";
my $verbose = 0;
my $rst = 0;
my $no_pre_check = 0;
my $discovery_type;
my @valid_discovery_type = ("mtms", "switch");
my $valid_discovery_type_str = join(",", @valid_discovery_type);
my $noderange;
my $nics; #reservation attribute, format : xxx|xxx|xxx
#used for discovery monitor
my %rawdata;
my %ipmacmap;
my $terminal = 0;
my %monitor_nodes;
$::USAGE = "Usage:
$program_name -h
$program_name -t
$program_name [-V]
$program_name -s
$program_name -T
$program_name [-V] [-m <discovery_type> -n <node_range>] [--noprecheck]
Description:
Check discovery files or process.
Do probe for discovery process, including pre-check for required configuration and realtime monitor of discovery process.
If all pre-check items pass, $program_name will go to monitor directly, otherwise $program_name exit for error.
Options:
-h : Get usage information of $program_name
-t : To verify if $program_name can work, reserve option for probe framework
-V : Output more information for debug
-s : Discovery static check, check whether genesis files are ready
-h : Get usage information of $program_name.
-T : To verify if $program_name can work, reserve option for probe framework.
-V : Output more information for debug.
-m : The method of discovery, the valid values are $valid_discovery_type_str.
-n : The range of predefined node, must used with option -m.
--noprecheck : skip pre-checking discovery to validate correct configuration.
";
#------------------------------------------
=head3
Description:
Check if all genesis files are available.
Arguments:
arch: valid value are ppc64 and x86_64
Returns:
0 : pass
1 : failed
=cut
#------------------------------------------
sub check_genesis_file {
my $arch = shift;
my $arch = shift;
if (($arch ne "ppc64") and ($arch ne "x86_64")) {
probe_utils->send_msg("$output", "f", "Please input correct arch type");
probe_utils->send_msg("$output", "d", "Please input correct arch type") if ($verbose);
return 1;
}
my $rst_f = 0;
probe_utils->send_msg("$output", "d", "Start to check genesis files for $arch...") if ($verbose);
@@ -128,8 +158,15 @@ sub check_genesis_file {
return 1;
}
my @host_ip_arr = `ifconfig -a |awk -F" " '/inet / {gsub(/\w+:/,"",\$2);print \$2}'`;
my @netmask_arr = `ifconfig -a |awk -F" " '/inet / {gsub(/\w+:/,"",\$4);print \$4}'`;
my @host_ip_arr;
my @netmask_arr;
my @output = `ip addr show|awk -F" " '/inet / {print \$2}'`;
foreach (@output) {
my ($ip, $mask) = split("/", $_);
my $strmask = xCAT::NetworkUtils::formatNetmask($mask, 1, 0);
push(@host_ip_arr, $ip);
push(@netmask_arr, $strmask);
}
@genesis_files = glob("$genesis_folder/*");
foreach (@genesis_files) {
@@ -141,7 +178,7 @@ sub check_genesis_file {
my $netmask_num = 0;
foreach (@host_ip_arr) {
chomp($_);
if (probe_utils->is_ip_belong_to_net($ip_range, $netmask_arr[$netmask_num], $_)) {
if (xCAT::NetworkUtils->ishostinsubnet($_, $netmask_arr[$netmask_num], $ip_range)) {
$host_ip = $_;
}
$netmask_num++;
@@ -229,14 +266,692 @@ sub check_genesis_file {
return $rst_f;
}
#------------------------------------------
=head3
Description:
1. check if there are dynamic range for specific networks defineded in dhcp conf file
2. check if these specific networks have corresponding genesis configuration
Arguments:
networks: A Comma separated list of networks. Every network is combined by network address and mask(i.e. network/mask).
For example: 10.0.0.0/255.0.0.0,50.1.0.0/255.255.0.0
Returns:
0 : pass
1 : failed
=cut
#------------------------------------------
sub dhcp_dynamic_range_check {
my $nets = shift;
my $rst = 0;
return $rst;
}
#------------------------------------------
=head3
Description:
Handle one line log come from tftp log file
Arguments:
msg: one line tftp log
Returns:
0 : pass
1 : failed
=cut
#------------------------------------------
sub handle_tftp_msg {
my $msg = shift;
if ($msg =~ /RRQ\s+from\s+(.+)\s+filename\s+(.+)/i) {
my $ip = $1;
my $file = $2;
my $record = "Via TFTP $ip download $file";
probe_utils->send_msg("$output", "d", "$record");
if (exists($rawdata{"$ipmacmap{$ip}"})) {
push(@{ $rawdata{ $ipmacmap{$ip} }{"history"} }, $record);
}
}
return 0;
}
#------------------------------------------
=head3
Description:
Handle one line log come from http log file
Arguments:
msg: one line http log
Returns:
0 : pass
1 : failed
=cut
#------------------------------------------
sub handle_http_msg {
my $msg = shift;
if ($msg =~ /(\d+\.\d+.\d+.\d+)\s.+GET\s+(.+)\s+HTTP.+/) {
my $ip = $1;
my $file = $2;
my $record = "Via HTTP $ip download $file";
probe_utils->send_msg("$output", "d", "$record");
if (exists($rawdata{"$ipmacmap{$ip}"})) {
push(@{ $rawdata{ $ipmacmap{$ip} }{"history"} }, $record);
}
}
return 0;
}
#------------------------------------------
=head3
Description:
Handle one line log come from dhcp log file
Arguments:
msg: one line http log
nics: target network interfaces
Returns:
0 : pass
1 : failed
=cut
#------------------------------------------
sub handle_dhcp_msg {
my $msg = shift;
if ($msg =~ /.+DHCPDISCOVER\s+from\s+(.+)\s+via\s+([^:]+)(.*)/i) {
my $mac = $1;
my $nic = $2;
if ($3 =~ /no free leases/) {
probe_utils->send_msg("$output", "d", "Receive DHCPDISCOVER from $mac via $nic, no free leases");
return 0;
}
my $record = "Receive DHCPDISCOVER from $mac via $nic";
probe_utils->send_msg("$output", "d", "$record");
push(@{ $rawdata{$mac}{"history"} }, $record);
} elsif ($msg =~ /.+DHCPOFFER\s+on\s+(.+)\s+to\s+(.+)\s+via\s+(.+)/i) {
my $ip = $1;
my $mac = $2;
my $nic = $3;
my $record = "Send DHCPOFFER on $ip back to $mac via $nic";
probe_utils->send_msg("$output", "d", "$record");
if (exists($rawdata{$mac})) {
push(@{ $rawdata{$mac}{"history"} }, $record);
}
} elsif ($msg !~ /unknown lease/ && $msg !~ /ignored/ && $msg =~ /.+DHCPREQUEST\s+for\s+(.+)\s\((.+)\)\s+from\s+(.+)\s+via\s+(.+)/) {
my $ip = $1;
my $server = $2;
my $mac = $3;
my $nic = $4;
my $record = "Receive DHCPREQUEST from $mac for $ip via $nic";
probe_utils->send_msg("$output", "d", "$record");
if (exists($rawdata{$mac})) {
push(@{ $rawdata{$mac}{"history"} }, $record);
}
} elsif ($msg =~ /.+DHCPACK\s+on\s+(.+)\s+to\s+(.+)\s+via\s+(.+)/) {
my $ip = $1;
my $mac = $2;
my $nic = $3;
my $record = "Send DHCPACK on $ip back to $mac via $nic";
probe_utils->send_msg("$output", "d", "$record");
if (exists($rawdata{$mac})) {
$rawdata{$mac}{"ip"} = $ip;
push(@{ $rawdata{$mac}{"history"} }, $record);
$ipmacmap{$ip} = $mac;
}
} elsif ($msg =~ /.+BOOTREQUEST\s+from\s+(.+)\s+via\s+([^:]+)(.*)/) {
my $mac = $1;
my $nic = $2;
if ($3 =~ /no dynamic leases/) {
probe_utils->send_msg("$output", "d", "Receive DHCPDISCOVER from $mac via $nic, no dynamic leases");
return 0;
}
my $record = "Receive BOOTREQUEST from $mac via $nic";
probe_utils->send_msg("$output", "d", "$record");
push(@{ $rawdata{$mac}{"history"} }, $record);
} elsif ($msg =~ /.+BOOTREPLY\s+for\s+(.+)\s+to\s+.+(\w\w:\w\w:\w\w:\w\w:\w\w:\w\w).+via\s+(.+)/) {
my $ip = $1;
my $mac = $2;
my $nic = $3;
my $record = "Send BOOTREPLY on $ip back to $mac via $nic";
probe_utils->send_msg("$output", "d", "$record");
if (exists($rawdata{$mac})) {
$rawdata{$mac}{"ip"} = $ip;
push(@{ $rawdata{$mac}{"history"} }, $record);
$ipmacmap{$ip} = $mac;
}
}
return 0;
}
#------------------------------------------
=head3
Description:
Handle one line log come from computes.log
Arguments:
msg: one line compute log
Returns:
0 : pass
1 : failed
=cut
#------------------------------------------
sub handle_compute_msg {
my $line = shift;
my $sender = "";
my $ip = "";
my $msg;
my @splitline = split(" ", $line);
if (($splitline[4] =~ /^xcat/i) || ($splitline[5] =~ /^xcat/i)) {
$sender = $splitline[3];
splice(@splitline, 0, 5);
$msg = join(" ", @splitline);
if (!xCAT::NetworkUtils->isIpaddr($sender)) {
$ip = xCAT::NetworkUtils->getipaddr($sender);
} else {
$ip = $sender;
}
if ($ip ne "" && defined($ipmacmap{$ip})) {
my $record = "Recv from $ip : $msg";
probe_utils->send_msg("$output", "d", "$record");
push(@{ $rawdata{ $ipmacmap{$ip} }{"history"} }, $record);
}
# There is a node finish discovry process
if ($msg =~ /xcat.genesis.dodiscovery: Restart/) {
my $node = `lsdef -i mac -c 2>&1 | awk -F: '/$ipmacmap{$ip}/ {print \$1}'`;
chomp($node);
$monitor_nodes{$node} = 1 if (defined($monitor_nodes{$node}));
probe_utils->send_msg("$output", "o", "Node $node has finished it's discovery process");
my $output = `lsdef $node 2>&1`;
print "-------------------\n$output-------------------\n";
}
}
return 0;
}
#------------------------------------------
=head3
Description:
Handle one line log come from cluster.log
Arguments:
msg: one line log
Returns:
0 : pass
1 : failed
=cut
#------------------------------------------
sub handle_cluster_msg {
my $line = shift;
my $sender = "";
my $ip = "";
my $msg;
if ($line =~ /.+\d+:\d+:\d+\s+(.+)\s+(xcat.+)/i) {
$sender = $1;
$msg = $2;
if (!xCAT::NetworkUtils->isIpaddr($sender)) {
$ip = xCAT::NetworkUtils->getipaddr($sender);
} else {
$ip = $sender;
}
if ($ip ne "" && defined($ipmacmap{$ip})) {
my $record = "Recv from $ip : $msg";
probe_utils->send_msg("$output", "d", "$record");
push(@{ $rawdata{ $ipmacmap{$ip} }{"history"} }, $record);
}
}
return 0;
}
#------------------------------------------
=head3
Description:
Dump monitor history, categorised by mac address.
Arguments:
NULL
Returns:
=cut
#------------------------------------------
sub dump_history {
my $title = "
=============================================================
= The summary of discovery:
=============================================================
";
print "$title\n";
foreach $mac (keys %rawdata) {
my $nodehostname = `lsdef -i mac -c 2>&1 | awk -F: '/$mac/ {print \$1}'`;
chomp($nodehostname);
if ($nodehostname ne "") {
probe_utils->send_msg("$output", "d", "[$mac ($nodehostname)]");
} else {
probe_utils->send_msg("$output", "d", "[$mac]:");
}
foreach my $line (@{ $rawdata{$mac}{"history"} }) {
probe_utils->send_msg("$output", "d", "\t$line");
}
print "\n";
}
}
#------------------------------------------
=head3
Description:
Check if all predefined node are valid
Arguments:
discovery_type: valid value are mtms and switch
noderange: node range
Returns:
0: pass
1: failed
=cut
#------------------------------------------
sub check_pre_defined_node {
my $discovery_type = shift;
my $noderange = shift;
my $rst = 0;
my @cmdoutput;
my %nodecheckrst;
my $currentnode = "";
@cmdoutput = `lsdef $noderange 2>&1`;
foreach (@cmdoutput) {
if ($_ =~ /^Error: Could not find an object named '(\w+)' .+/i) {
$currentnode = $1;
$nodecheckrst{$currentnode}{"error"} = "Could not find node definition";
$rst = 1;
} elsif ($_ =~ /^\s*Object name: (\w+)/i) {
$currentnode = $1;
$monitor_nodes{$1} = 0;
} elsif ($_ =~ /^\s+(\w+)\s*=\s*(\w+)/) {
$nodecheckrst{$currentnode}{$1} = $2;
}
}
#print Dumper(%nodecheckrst);
foreach my $node (keys %nodecheckrst) {
if (!exists($nodecheckrst{$node}{error})) {
if ($discovery_type eq "mtms") {
if (!(exists($nodecheckrst{$node}{"mtm"}) && exists($nodecheckrst{$node}{"serial"}))) {
$nodecheckrst{$node}{"error"} = "node definition is wrong for '$discovery_type' type discovery";
$rst = 1;
}
} elsif ($discovery_type eq "switch") {
{ #important to hold a block
if (!(exists($nodecheckrst{$node}{"switch"}) && exists($nodecheckrst{$node}{"switchport"}))) {
$nodecheckrst{$node}{"error"} = "Atrribute 'switch' or 'switchport' isn't defined for '$discovery_type' type discovery";
$rst = 1;
last;
}
my $tmpoutput = `lsdef $nodecheckrst{$node}{"switch"} 2>&1`;
if ($?) {
$nodecheckrst{$node}{"error"} = "Miss definition for related switch $nodeswitch";
$rst = 1;
last;
}
if ($tmpoutput !~ /snmpversion=/) {
$nodecheckrst{$node}{"error"} = "Miss attribute 'snmpversion' definition for related switch $nodeswitch";
$rst = 1;
last;
}
if ($tmpoutput !~ /username=/) {
$nodecheckrst{$node}{"error"} = "Miss attribute 'username' definition for related switch $nodeswitch";
$rst = 1;
last;
}
if ($tmpoutput !~ /password=/) {
$nodecheckrst{$node}{"error"} = "Miss attribute 'password' definition for related switch $nodeswitch";
$rst = 1;
last;
}
}
}
}
}
foreach my $node (keys %nodecheckrst) {
probe_utils->send_msg("$output", "d", "$node : $nodecheckrst{$node}{error}") if (exists($nodecheckrst{$node}{error}));
}
return $rst;
}
#------------------------------------------
=head3
Description:
Test if all nodes have finished job
Arguments:
One golble attribute %monitor_nodes;
Returns:
=cut
#------------------------------------------
sub all_monitor_node_done {
my $done = 1;
foreach my $node (keys %monitor_nodes) {
if ($monitor_nodes{$node} == 0) {
$done = 0;
last;
}
}
return $done;
}
#------------------------------------------
=head3
Description:
Do pre_checking
Arguments:
nics: target network interface
if not specified, using the network which master belongs to
Returns:
0: pass
1: failed
=cut
#------------------------------------------
sub do_pre_check {
my $nics = shift;
my @nets = ();
my $rst = 0;
my $msg;
#The block of $nics is ture is a reservation part, this part don't show up in usage
if ($nics) {
if ($nics =~ /[^\w|]/) {
probe_utils->send_msg("$output", "f", "Invalid NIC list");
probe_utils->send_msg("$output", "d", "$::USAGE");
exit 1;
}
$msg = "The input network interfaces $nics exist and are configured well on current server";
my $miss = 0;
my @nic_array = split(",", $nics);
foreach my $nic (@nic_array) {
my $tmp_nic = `ip addr show $nic >/dev/null 2>&1`;
if ($?) {
probe_utils->send_msg("$output", "d", "Network interface $nic doesn't exist on current server") if ($verbose);
$miss = 1;
} else {
my $tmp = `echo $tmp_nic |awk -F" " '/inet / {print \$2}'`;
chomp($tmp);
if (!length($tmp)) {
probe_utils->send_msg("$output", "d", "Network interface $nic isn't set IP address") if ($verbose);
$miss = 1;
} else {
my ($ip, $mask) = split("/", $tmp);
my $strmask = xCAT::NetworkUtils::formatNetmask($mask, 1, 0);
push(@nets, probe_utils->get_network($ip, $strmask));
}
}
}
if ($miss) {
probe_utils->send_msg("$output", "f", $msg);
$rst = 1;
} else {
probe_utils->send_msg("$output", "o", $msg);
}
} else {
$msg = "Attribute 'master' in 'site' table is configured well";
my $masteripinsite = `tabdump site | awk -F',' '/master/ { gsub(/"/, "", \$2) ; print \$2 }'`;
chomp($masteripinsite);
if ($masteripinsite eq "") {
probe_utils->send_msg("$output", "d", "There isn't 'master' definition in 'site' talbe") if ($verbose);
probe_utils->send_msg("$output", "f", $msg);
exit 1;
}
if (!xCAT::NetworkUtils->isIpaddr("$masteripinsite")) {
probe_utils->send_msg("$output", "d", "The value of 'master' in 'site' table isn't a IP address") if ($verbose);
probe_utils->send_msg("$output", "f", $msg);
exit 1;
}
my $tmpoutput = `ip addr 2>&1 |grep $masteripinsite`;
if ($?) {
probe_utils->send_msg("$output", "d", "The IP $masteripinsite of 'master' in 'site' table dosen't belong to any network on current server") if ($verbose);
probe_utils->send_msg("$output", "f", $msg);
exit 1;
}
probe_utils->send_msg("$output", "o", $msg);
chomp($tmpoutput);
my $tmp = `echo $tmpoutput | awk -F" " '{print \$2}'`;
chomp($tmp);
my ($ip, $mask) = split("/", $tmp);
my $strmask = xCAT::NetworkUtils::formatNetmask($mask, 1, 0);
push(@nets, probe_utils->get_network($ip, $strmask));
}
my $arch = `uname -i`;
chomp($arch);
$msg = "Genesis files for $arch are available";
if ($arch =~ /ppc64/i) {
if (check_genesis_file("ppc64")) {
probe_utils->send_msg("$output", "f", $msg);
$rst = 1;
} else {
probe_utils->send_msg("$output", "o", $msg);
if (check_genesis_file("x86_64")) {
probe_utils->send_msg("$output", "w", "Genesis files for x86 are not available");
probe_utils->send_msg("$output", "i", "If don't plan to manage a x86 server, please ignore above warning");
} else {
probe_utils->send_msg("$output", "o", "Genesis files for x86 are available");
}
}
} elsif ($arch =~ /x86/i) {
if (check_genesis_file("x86_64")) {
probe_utils->send_msg("$output", "f", $msg);
$rst = 1;
} else {
probe_utils->send_msg("$output", "o", $msg);
if (check_genesis_file("ppc64")) {
probe_utils->send_msg("$output", "w", "Genesis files for ppc64/ppc64le are not available");
probe_utils->send_msg("$output", "i", "If don't plan to manage a ppc64/ppc64le server, please ignore above warning");
} else {
probe_utils->send_msg("$output", "o", "Genesis files for ppc64/ppc64le are available");
}
}
}
$msg = "DHCP dynamic range is configured well";
if (dhcp_dynamic_range_check(\@nets)) {
probe_utils->send_msg("$output", "f", $msg);
$rst = 1;
} else {
probe_utils->send_msg("$output", "o", $msg);
}
return $rst;
}
#------------------------------------------
=head3
Description:
Monitor the process of discovery
Arguments:
nics: target network interface
if not specified, using the network which master belongs to
Returns:
0: pass
1: failed
=cut
#------------------------------------------
sub do_monitor {
$SIG{TERM} = $SIG{INT} = sub {
$terminal = 1;
};
if (!$nics) {
my $masteripinsite = `tabdump site | awk -F',' '/master/ { gsub(/"/, "", \$2) ; print \$2 }'`;
chomp($masteripinsite);
$nics = `ip addr |grep -B2 $masteripinsite|awk -F" " '/mtu/{gsub(/:/,"",\$2); print \$2}'`;
chomp($nics);
if (!$nics) {
probe_utils->send_msg("$output", "f", "The value of master in site table is $masteripinsite, can't get corresponding network interface");
return 1;
}
}
my $startline = "
-------------------------------------------------------------
___
____ _ _____ _.-| | |\\__/,| (`\\
__ __/ ___| / \\|_ _| { | | |x x |__ _) )
\\ \\/ / | / _ \\ | | \"-.|___| _.( T ) ` /
> <| |___ / ___ \\| | .--'-`-. _((_ `^--' /_< \\
/_/\\_\\\\____/_/ \\_\\_| .+|______|__.-||__)`-'(((/ (((/
-------------------------------------------------------------
";
print "$startline\nStart to capture every message during discovery process......\n";
my $varlogmsg = "/var/log/messages";
my $clusterlog = "/var/log/xcat/cluster.log";
my $computelog = "/var/log/xcat/computes.log";
#http logs are saved in different file in different distro
my $httplog;
if (-e "/var/log/httpd/access_log") {
$httplog = "/var/log/httpd/access_log";
} elsif (-e "/var/log/apache2/access_log") {
$httplog = "/var/log/apache2/access_log";
} elsif (-e "/var/log/apache2/access.log") {
$httplog = "/var/log/apache2/access.log";
}
my $clusterpid;
my $httppid;
my $computerpid;
my $varlogpid;
my $rst = 0;
{ #important to hold a block
# start ot obtain logs from every log file
if (!($varlogpid = open(VARLOGMSGFILE, "tail -f $varlogmsg 2>&1 |"))) {
probe_utils->send_msg("$output", "f", "Can't open $varlogmsg to get logs");
$rst = 1;
last;
}
if (!($clusterpid = open(CLUSTERLOGFILE, "tail -f $clusterlog 2>&1 |"))) {
probe_utils->send_msg("$output", "f", "Can't open $clusterlog to get logs");
$rst = 1;
last;
}
if (!($httppid = open(HTTPLOGFILE, "tail -f $httplog 2>&1 |"))) {
probe_utils->send_msg("$output", "f", "Can't open $httplog to get logs");
$rst = 1;
last;
}
if (!($computerpid = open(COMPUTERFILE, "tail -f $computelog 2>&1 |"))) {
probe_utils->send_msg("$output", "f", "Can't open $computelog to get logs");
$rst = 1;
last;
}
my $select = new IO::Select;
$select->add(\*VARLOGMSGFILE);
$select->add(\*CLUSTERLOGFILE);
$select->add(\*HTTPLOGFILE);
$select->add(\*COMPUTERFILE);
$| = 1;
my $line = "";
my @hdls;
my $hdl;
my $oldlines = 10;
my $varlogmsgcnt = 0;
my $clusterlogcnt = 0;
my $httplogcnt = 0;
my $computercnt = 0;
for (; ;) {
if (@hdls = $select->can_read(0)) {
foreach $hdl (@hdls) {
if ($hdl == \*VARLOGMSGFILE) {
chomp($line = <VARLOGMSGFILE>);
++$varlogmsgcnt;
last if ($varlogmsgcnt <= $oldlines);
my @tmp = split(/ /, $line);
if ($tmp[4] =~ /dhcpd:/i && $line =~ /$nics/) {
handle_dhcp_msg("$line");
} elsif ($tmp[4] =~ /in.tftpd/i) {
handle_tftp_msg("$line");
}
} elsif ($hdl == \*CLUSTERLOGFILE) {
chomp($line = <CLUSTERLOGFILE>);
++$clusterlogcnt;
last if ($clusterlogcnt <= $oldlines);
handle_cluster_msg("$line");
} elsif ($hdl == \*HTTPLOGFILE) {
chomp($line = <HTTPLOGFILE>);
++$httplogcnt;
last if ($httplogcnt <= $oldlines);
handle_http_msg("$line");
} elsif ($hdl == \*COMPUTERFILE) {
chomp($line = <COMPUTERFILE>);
++$computercnt;
last if ($computercnt <= $oldlines);
handle_compute_msg("$line");
}
}
}
if ($terminal || (%monitor_nodes && all_monitor_node_done())) {
if ($terminal) {
probe_utils->send_msg("$output", "d", "Got <Ctrl+c> from STDIN");
} else {
probe_utils->send_msg("$output", "o", "All nodes need to monitor have finished discovery process");
}
last;
} sleep 0.01;
}
&dump_history;
}
kill 'INT', $clusterpid if ($clusterpid);
kill 'INT', $httppid if ($httppid);
kill 'INT', $computerpid if ($computerpid);
kill 'INT', $varlogpid if ($varlogpid);
close(VARLOGMSGFILE) if (VARLOGMSGFILE);
close(CLUSTERLOGFILE) if (CLUSTERLOGFILE);
close(COMPUTERFILE) if (COMPUTERFILE);
close(HTTPLOGFILE) if (HTTPLOGFILE);
return $rst;
}
#-------------------------------------
# main process
## main process
#-------------------------------------
if (
!GetOptions("--help|h|?" => \$help,
"t" => \$test,
"V" => \$verbose,
"s" => \$static))
"T" => \$test,
"V" => \$verbose,
"--noprecheck" => \$no_pre_check,
"m=s" => \$discovery_type,
"n=s" => \$noderange,
"N=s" => \$nics)) #option N is a reservation option, dosen't show up in usage now
{
probe_utils->send_msg("$output", "f", "Invalid parameter for $program_name");
probe_utils->send_msg("$output", "d", "$::USAGE");
@@ -253,23 +968,40 @@ if ($help) {
}
if ($test) {
probe_utils->send_msg("$output", "o", "Discovery Check.");
probe_utils->send_msg("$output", "o", "Do probe for discovery process, including pre-check for required configuration and realtime monitor of discovery process.Before using this command, please install nslookup command ahead.");
exit 0;
}
if ($static) {
$rst = check_genesis_file("ppc64");
if ($rst) {
probe_utils->send_msg("$output", "f", "Genesis files for ppc64/ppc64le failed.");
} else {
probe_utils->send_msg("$output", "o", "Genesis files for ppc64/ppc64le success.");
}
$rst = check_genesis_file("x86_64");
if ($rst) {
probe_utils->send_msg("$output", "f", "Genesis files for x86_64 failed.");
} else {
probe_utils->send_msg("$output", "o", "Genesis files for x86_64 success.");
if (defined($noderange) && !defined($discovery_type)) {
probe_utils->send_msg("$output", "f", "Option '-n' must used with '-m'");
probe_utils->send_msg("$output", "d", "$::USAGE");
exit 1;
}
if (defined($discovery_type)) {
unless (@valid_discovery_type ~~ /^$discovery_type$/) {
probe_utils->send_msg("$output", "f", "Invalid discovery type. the vaild types are $valid_discovery_type_str");
probe_utils->send_msg("$output", "d", "$::USAGE");
exit 1;
}
}
if (defined($noderange) && defined($discovery_type)) {
$msg = "All pre_defined nodes are valid";
my $rc = check_pre_defined_node($discovery_type, $noderange);
if ($rc) {
probe_utils->send_msg("$output", "f", $msg);
return 1;
} else {
probe_utils->send_msg("$output", "o", $msg);
}
}
if (!$no_pre_check) {
$rst = do_pre_check();
exit 1 if ($rst);
}
$rst = do_monitor();
exit $rst;

View File

@@ -10,27 +10,27 @@ $Term::ANSIColor::AUTORESET = 1;
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
my $pro_name = basename($0);
my $program_name = basename($0);
my $pro_dir="$::XCATROOT/probe/";
my $pro_dir = "$::XCATROOT/probe/";
my $plugin_dir = "$pro_dir/subcmds";
my %cmds = ();
my $verbose = 0;
my $nocolor = 0;
my $color = (-t *STDOUT);
my $fullmsg = 0;
my $help = 0;
my $list = 0;
$::USAGE = "Usage:
xcatprobe -h
xcatprobe -l
xcatprobe [-n] [-V] <subcommand> <attrbute_to_subcommand>
xcatprobe [-V] <subcommand> <attrbute_to_subcommand>
Options:
-h : get usage information of $pro_name
-h : get usage information of $program_name
-l : list all valid sub commands
-V : print verbose information of $pro_name
-n : print output without colors
-V : print verbose information of $program_name
";
#-----------------------------------
@@ -52,17 +52,17 @@ sub loadsubcmds {
foreach (@candidate) {
my $cmdname = basename("$_");
$output = `$_ -t 2>&1`;
$output = `$_ -T 2>&1`;
chomp($output);
print "\n-->$_\n[OUTPUT]:\n$output\n" if ($verbose);
if ($output !~ /\[(\w+)\]\s*:\s*(.+)/) {
print "skip $_ for doing '$_ -t' failed, bad format\n" if ($verbose);
print "skip $_ for doing '$_ -T' failed, bad format\n" if ($verbose);
next;
} else {
my $desc = $2;
unless ($1 ~~ /^ok$/) {
print "skip $_ for doing '$_ -t' failed, invalid flag\n" if ($verbose);
print "skip $_ for doing '$_ -T' failed, invalid flag\n" if ($verbose);
next;
}
$cmds{$cmdname} = $desc;
@@ -83,37 +83,93 @@ sub loadsubcmds {
#----------------------------------
sub format_cmd_output {
my $line = shift;
my $nocolor = shift;
my $color = shift;
my $fullmsg = shift;
if ($line =~ /\[(\w+)\]\s*:\s*(.+)/) {
my $flag = $1;
my $msg = $2;
if ($flag =~ /failed/i) {
if ($nocolor) {
print "[FAIL] ";
} else {
print BOLD RED "[FAIL] ";
}
} elsif ($flag =~ /warning/i) {
if ($nocolor) {
print "[WARN] ";
} else {
print BOLD BLUE "[WARN] ";
}
} elsif ($flag =~ /ok/i) {
if ($nocolor) {
print "[ OK ] ";
} else {
print BOLD GREEN "[ OK ] ";
}
} elsif ($flag =~ /debug/i) {
print " ";
} elsif ($flag =~ /info/i) {
print "[INFO] ";
}
print "$msg\n";
my $msg;
my $flag;
if ($line =~ /\[(\w+)\]\s*:(.+)/) {
$flag = $1;
$msg = $2;
} else {
print "$line\n";
$msg = $line;
}
$msg =~ s/\t/ /g;
my $flaglen = 6;
my $desiredwidth = 120;
my $screenwidth = (`tput cols` + 0);
my $maxlen = ($screenwidth > $desiredwidth ? $desiredwidth : $screenwidth);
my @finalmsg = ();
my $msglen = length($msg);
if ($msglen <= $maxlen) {
if (!$fullmsg && $flag) {
my $leftspace = $maxlen - length($msg);
if ($leftspace < $flaglen) {
$msg = substr($msg, 0, $maxlen - $flaglen);
$msg =~ s/(.*).../$1\.\.\./g;
}
}
push @finalmsg, $msg;
} else {
my @tmpmsg = split("", $msg);
my $head = 0;
my $tail = $maxlen;
while ($head < $msglen) {
push @finalmsg, substr($msg, $head, ($tail - $head));
if (!$fullmsg) {
if ($flag) {
$finalmsg[0] =~ s/(.*).........$/$1\.\.\./g;
} else {
$finalmsg[0] =~ s/(.*)...$/$1\.\.\./g;
}
last;
}
$head = $tail;
$tail = $head + ($maxlen <= ($msglen - $tail) ? $maxlen : ($msglen - $tail));
}
}
for (my $i = 0 ; $i < $#finalmsg + 1 ; ++$i) {
if ($i ne $#finalmsg) {
print "$finalmsg[$i]\n";
next;
}
if ($flag) {
my $leftspace = $maxlen - length($finalmsg[$i]);
my $spacenum = (($leftspace >= $flaglen) ? ($leftspace - $flaglen) : ($screenwidth - length($finalmsg[$i]) + $maxlen - $flaglen));
my $spacestr = " " x $spacenum;
print "$finalmsg[$i]$spacestr";
if ($flag =~ /failed/i) {
if ($color) {
print BOLD RED "[FAIL]\n";
} else {
print "[FAIL]\n";
}
} elsif ($flag =~ /warning/i) {
if ($color) {
print BOLD BLUE "[WARN]\n";
} else {
print "[WARN]\n";
}
} elsif ($flag =~ /ok/i) {
if ($color) {
print BOLD GREEN "[ OK ]\n";
} else {
print "[ OK ]\n";
}
} elsif ($flag =~ /debug/i) {
print "\n";
} elsif ($flag =~ /info/i) {
print "[INFO]\n";
}
} else {
print "$finalmsg[$i]\n";
}
}
return 0;
}
@@ -132,32 +188,39 @@ sub listvalidsubcmd {
$maxlen = length($key) if (length($key) > $maxlen);
}
$maxlen += 4;
my $desiredwidth = 120;
my $screenwidth = (`tput cols` + 0);
my $finallen = ($screenwidth > $desiredwidth ? $desiredwidth : $screenwidth);
print "Supported sub commands are:\n";
foreach my $key (keys %cmds) {
my @desc = split(" ", $cmds{$key});
my $str = "";
my @formatdesc = ();
foreach my $word (@desc) {
$str .= $word . " ";
if (length($str) > 100) {
if (length($str) + length($word) > $finallen - $maxlen) {
$str =~ s/([^\s]+)\s$/$1/g;
push @formatdesc, $str;
$str = "";
}
$str .= $word . " ";
}
$str =~ s/([^\s]+)\s$/$1/g;
push @formatdesc, $str;
if ($nocolor) {
print "$key";
} else {
if ($color) {
print BOLD GREEN "$key";
} else {
print "$key";
}
my $space = " " x ($maxlen - length($key));
print "$space $formatdesc[0]\n";
print "$space$formatdesc[0]\n";
delete $formatdesc[0];
$space = " " x $maxlen;
foreach my $line (@formatdesc) {
print "$space $line\n" if (length($line));
print "$space$line\n" if (length($line));
}
}
}
@@ -165,7 +228,7 @@ sub listvalidsubcmd {
# main
#######################################
my @tmpargv = @ARGV;
my @supportopt = ("-V", "-h", "-l", "-n");
my @supportopt = ("-V", "-h", "-l", "-w");
my $pluginname;
my $optnum = 0;
foreach my $attr (@tmpargv) {
@@ -179,13 +242,14 @@ foreach my $attr (@tmpargv) {
$help = 1 if ($attr eq "-h");
$verbose = 1 if ($attr eq "-V");
$list = 1 if ($attr eq "-l");
$nocolor = 1 if ($attr eq "-n");
$fullmsg = 1 if ($attr eq "-w");
} else {
$pluginname = $attr;
last;
}
}
&loadsubcmds;
if (defined($pluginname)) {
my $hit = 0;
@@ -210,22 +274,26 @@ if ($ARGV[0] eq "-l") {
}
if (!defined($pluginname)) {
print "There isn't sub command input from command line\n";
print "There isn't sub command input from command line, please use '-l' to list all valid subcommand\n";
exit 0;
}
for (my $i = 0 ; $i < $optnum + 1 ; $i++) {
shift @tmpargv;
}
splice(@tmpargv, 0, $optnum + 1);
my $pluginattrs = join(" ", @tmpargv);
my $subcmd = "$plugin_dir/$pluginname $pluginattrs";
print "\nsubcmd = $subcmd\n" if ($verbose);
open(PIPE, "$subcmd |");
my $subcmdpid = 0;
$SIG{TERM} = $SIG{INT} = sub {
$subcmdpid or exit 0;
kill 'INT', $subcmdpid;
};
$subcmdpid = open(PIPE, "$subcmd |") or die("Something went wrong while fork()ing to handle subcommand $subcmd: $!");
while (<PIPE>) {
chomp;
format_cmd_output($_, $nocolor);
format_cmd_output($_, $color, $fullmsg);
}
close(PIPE); # This will set the $? properly