From d29bf877defccec9a14b17d698c9a67110b8ef66 Mon Sep 17 00:00:00 2001 From: hu-weihua Date: Thu, 26 May 2016 05:45:20 -0400 Subject: [PATCH 1/7] New code for xcat probe component --- xCAT-probe/lib/perl/ProbeUtils.pm | 317 ++++++++++++++++++++ xCAT-probe/subcmds/xcatmn | 475 ++++++++++++++++++++++++++++++ xCAT-probe/xcatprobe | 258 ++++++++++++++++ 3 files changed, 1050 insertions(+) create mode 100644 xCAT-probe/lib/perl/ProbeUtils.pm create mode 100755 xCAT-probe/subcmds/xcatmn create mode 100755 xCAT-probe/xcatprobe diff --git a/xCAT-probe/lib/perl/ProbeUtils.pm b/xCAT-probe/lib/perl/ProbeUtils.pm new file mode 100644 index 000000000..8304fb76b --- /dev/null +++ b/xCAT-probe/lib/perl/ProbeUtils.pm @@ -0,0 +1,317 @@ +package ProbeUtils; +use strict; +use File::Path; +use File::Copy; + +#----------------------------------------- +=head3 + Description: + Format output message depending on probe framework requirement + Format is [] : + The valid are debug, warning, failed and ok + Attribute: list by input sequence + output: where should output the message + num: the number of + 0: debug + 1: warning + 2: failed + 3: ok + msg: the information need to output + Return value: + 1 : Failed + 0 : success +=cut +#---------------------------------------- +sub send_msg { + my $output=shift; + $output=shift if(($output) && ($output =~ /ProbeUtils/)); + my $num = shift; + my $msg = shift; + my $flag; + + if ($num == 0) { + $flag = "[debug] :"; + }elsif($num == 1) { + $flag = "[warning]:"; + }elsif($num == 2) { + $flag = "[failed] :"; + }elsif($num == 3){ + $flag = "[ok] :"; + } + if($output eq "stdout"){ + print "$flag $msg\n"; + }else{ + if (!open (LOGFILE, ">> $output") ) { + return 1; + } + print LOGFILE "$flag $msg\n"; + close LOGFILE; + } + return 0; +} + +#------------------------------------------ +=head3 + Description: + Judge if a string is a IP address + Attribute: list by input sequence + addr: the string want to be judged + Return value: + 1 : yes + 0 : no +=cut +#------------------------------------------ +sub isIpAddr{ + my $addr = shift; + $addr=shift if(($addr) && ($addr =~ /ProbeUtils/)); + return 0 unless($addr); + return 0 if($addr !~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); + return 0 if ($1 > 255 || $1 == 0 || $2 > 255 || $3 > 255 || $4 > 255); + return 1; +} + +#------------------------------------------ +=head3 + Description: + Judge if a IP address belongs to a network + Attribute: list by input sequence + net : network address, such like 10.10.10.0 + mask: network mask. suck like 255.255.255.0 + ip: a ip address + Return value: + 1 : yes + 0 : no +=cut +#------------------------------------------ +sub isIpBelongToNet{ + my $net=shift; + $net=shift if(($net) && ($net =~ /ProbeUtils/)); + my $mask=shift; + my $targetip=shift; + + return 0 if($net !~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); + return 0 if($mask !~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); + return 0 if(! isIpAddr($targetip)); + + my $bin_mask=0; + $bin_mask=(($1+0)<<24)+(($2+0)<<16)+(($3+0)<<8)+($4+0) if($mask =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); + + my $bin_ip=0; + $bin_ip=(($1+0)<<24)+(($2+0)<<16)+(($3+0)<<8)+($4+0) if($targetip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); + + my $tmp_net = $bin_mask&$bin_ip; + + my $bin_net=0; + $bin_net=(($1+0)<<24)+(($2+0)<<16)+(($3+0)<<8)+($4+0) if($net =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); + + return 0 if( $tmp_net != $bin_net); + return 1; +} + +#------------------------------------------ +=head3 + Description: + Get distro name of current operating system + Attribute: list by input sequence + None + Return value: + A string, include value are sles, redhat and ubuntu +=cut +#------------------------------------------ +sub getOS{ + my $os="unknown"; + my $output = `cat /etc/*release* 2>&1`; + if($output =~ /suse/i){ + $os="sles"; + }elsif($output =~ /Red Hat/i){ + $os="redhat"; + }elsif($output =~ /ubuntu/i){ + $os="ubuntu"; + } + + return $os; +} + +#------------------------------------------ +=head3 + Description: + Judge if a IP address is a static IP address + Attribute: list by input sequence + ip: a ip address + nic: the network adapter which ip belongs to + Return value: + 1 : yes + 0 : no +=cut +#------------------------------------------ +sub isStaticIp{ + my $ip=shift; + $ip=shift if(($ip) && ($ip =~ /ProbeUtils/)); + my $nic=shift; + my $os = getOS(); + my $rst=0; + + if($os =~ /redhat/){ + my $output1=`cat /etc/sysconfig/network-scripts/ifcfg-$nic 2>&1 |grep -i IPADDR`; + my $output2=`cat /etc/sysconfig/network-scripts/ifcfg-$nic 2>&1 |grep -i BOOTPROTO`; + $rst=1 if(($output1 =~ /$ip/) && ($output2 =~ /static/i)); + }elsif($os =~ /sles/){ + my $output1=`cat /etc/sysconfig/network/ifcfg-$nic 2>&1 |grep -i IPADDR`; + my $output2=`cat /etc/sysconfig/network/ifcfg-$nic 2>&1 |grep -i BOOTPROTO`; + $rst=1 if(($output1 =~ /$ip/) && ($output2 =~ /static/i)); + }elsif($os =~/ubuntu/){ + my $output=`cat /etc/network/interfaces 2>&1|grep -E "iface\s+$nic"`; + $rst=1 if($output =~ /static/i); + } + return $rst; +} + +#------------------------------------------ +=head3 + Description: + Judge if SELinux is opened in current operating system + Attribute: list by input sequence + None + Return value: + 1 : yes + 0 : no +=cut +#------------------------------------------ +sub isSelinuxEnable{ + if (-e "/usr/sbin/selinuxenabled") { + `/usr/sbin/selinuxenabled`; + if ($? == 0) { + return 1; + } else { + return 0; + } + } else { + return 0; + } +} + +#------------------------------------------ +=head3 + Description: + Judge if firewall is opened in current operating system + Attribute: list by input sequence + None + Return value: + 1 : yes + 0 : no +=cut +#------------------------------------------ +sub isFirewallOpen{ + my $os =getOS(); + my $output; + my $rst=0; + + if($os =~ /redhat/){ + $output=`service iptables status 2>&1`; + $rst=1 if($output =~ /running/i); + }elsif($os =~ /sles/){ + $output=`service SuSEfirewall2_setup status`; + $rst=1 if($output =~ /running/i); + }elsif($os =~/ubuntu/){ + $output=`ufw status`; + $rst=1 if($output =~ /Status: active/i); + } + return $rst; +} + +#------------------------------------------ +=head3 + Description: + Judge if http service is ready to use in current operating system + Attribute: list by input sequence + ip: http server's ip + Return value: + 1 : yes + 0 : no +=cut +#------------------------------------------ +sub isHttpReady{ + my $mnip=shift; + $mnip=shift if(($mnip) && ($mnip =~ /ProbeUtils/)); + + my $http = "http://$mnip/install/postscripts/efibootmgr"; + rename("./efibootmgr", "./efibootmgr.org") if(-e "./efibootmgr"); + + my $outputtmp = `wget $http 2>&1`; + my $rst =$?; + if(($outputtmp =~ /200 OK/) && (!$rst) && (-e "./efibootmgr")){ + unlink("./efibootmgr"); + rename("./efibootmgr.org", "./efibootmgr") if(-e "./efibootmgr.org"); + return 1; + }else{ + rename("./efibootmgr.org", "./efibootmgr") if(-e "./efibootmgr.org"); + return 0; + } +} + +#------------------------------------------ +=head3 + Description: + Judge if tftp service is ready to use in current operating system + Attribute: list by input sequence + ip: tftp server's ip + Return value: + 1 : yes + 0 : no +=cut +#------------------------------------------ +sub isTftpReady{ + my $mnip=shift; + $mnip=shift if(($mnip) && ($mnip =~ /ProbeUtils/)); + + rename("/tftpboot/tftptestt.tmp", "/tftpboot/tftptestt.tmp.old") if(-e "/tftpboot/tftptestt.tmp"); + rename("./tftptestt.tmp", "./tftptestt.tmp.old") if(-e "./tftptestt.tmp"); + + system("touch /tftpboot/tftptestt.tmp"); + my $output = `tftp -4 -v $mnip -c get tftptestt.tmp`; + if((!$?) && (-e "./tftptestt.tmp")){ + unlink("./tftptestt.tmp"); + rename("./tftptestt.tmp.old", "./tftptestt.tmp") if(-e "./tftptestt.tmp.old"); + rename("/tftpboot/tftptestt.tmp.old","/tftpboot/tftptestt.tmp") if(-e "/tftpboot/tftptestt.tmp.old"); + return 1; + }else{ + rename("./tftptestt.tmp.old", "./tftptestt.tmp") if(-e "./tftptestt.tmp.old"); + rename("/tftpboot/tftptestt.tmp.old","/tftpboot/tftptestt.tmp") if(-e "/tftpboot/tftptestt.tmp.old"); + return 0; + } +} + + +#------------------------------------------ +=head3 + Description: + Judge if DNS service is ready to use in current operating system + Attribute: list by input sequence + ip: DNS server's ip + Return value: + 1 : yes + 0 : no +=cut +#------------------------------------------ +sub isDnsReady{ + my $mnip=shift; + $mnip=shift if(($mnip) && ($mnip =~ /ProbeUtils/)); + my $hostname=shift; + my $domain=shift; + + my $output = `nslookup $mnip $mnip 2>&1`; + + if($?){ + return 0; + }else{ + chomp($output); + my $tmp=`echo "$output" |grep "Server:[\t\s]*$mnip" >/dev/null 2>&1`; + print "$tmp"; + return 0 if($?); + $tmp = `echo "$output"|grep "name ="|grep "$hostname\.$domain" >/dev/null 2>&1`; + return 0 if($?); + return 1; + } +} + +1; diff --git a/xCAT-probe/subcmds/xcatmn b/xCAT-probe/subcmds/xcatmn new file mode 100755 index 000000000..7e9c3c21c --- /dev/null +++ b/xCAT-probe/subcmds/xcatmn @@ -0,0 +1,475 @@ +#! /usr/bin/perl + +#use lib "/home/probe/lib/perl"; +use lib "/opt/xcat/probe/lib/perl"; +use ProbeUtils; +use File::Basename; +use Getopt::Long; + +my $proname=basename("$0"); +my $help; +my $mnip; +my $test; +my $output="stdout"; +my $verbose=0; +my $rst=0; + +$::USAGE = "Usage: + $proname -h + $proname -t + $proname [-m] [-f] [-v] + +Description: + After xcat installation, use this command to check if xcat has been installed correctly and is ready for use. + +Options: + -h : get usage information of $proname + -t : to verify if $proname can work, reserve option for probe framework + -m : specify current MN's IP address, if not specified, use master ip in site table + -f : specify where should $proname pass its output to. if not specified, pass output to STDOUT by default. + -v : output more information for debug +"; + +sub returncmdoutput{ + my $rst=shift; + chomp($rst); + my @lines=split("[\n\r]", $rst); + foreach my $line (@lines){ + ProbeUtils->send_msg("$output",0, "$line"); + } +} + + +#------------------------------------- +# main process +#------------------------------------- +if ( + !GetOptions("--help|h|?" => \$help, + "t"=> \$test, + "v"=> \$verbose, + "f=s" => \$output, + "m=s" => \$mnip)) +{ + ProbeUtils->send_msg("$output",2, "Invalid parameter for $proname"); + ProbeUtils->send_msg("$output",0, "$::USAGE"); + exit 1; +} + +if($help){ + if($output ne "stdout"){ + ProbeUtils->send_msg("$output",0, "$::USAGE"); + }else{ + print "$::USAGE"; + } + exit 0; +} + +if($test){ + ProbeUtils->send_msg("$output",3,"After xcat installation, use this command to check if xcat has been installed correctly and is ready for use. Before using this command , please install tftp, nslookup and wget commands ahead. The platform supported are redhat, sles and ubuntu."); + exit 0; +} + +my $hostname=`hostname -s`; +chomp($hostname); +my $hostipaddr=`hostname -i`; +chomp($hostipaddr); +my $othername=`hostname -a`; +chomp($othername); + +ProbeUtils->send_msg("$output",0,"Short hostname of this server is $hostname") if($verbose); +ProbeUtils->send_msg("$output",0,"The address of hostname $hostname is $hostipaddr") if($verbose); +ProbeUtils->send_msg("$output",0,"The other names of $hostname is $othername") if($verbose && ($othername ne "")); + +my $msg="Sub process 'xcatd: SSL listener' is running"; +my $xcatdproc = `ps aux|grep -v grep|grep xcatd`; +chomp($xcatdproc); +if($xcatdproc =~ /xcatd: SSL listener/){ + ProbeUtils->send_msg("$output",3,"$msg"); +}else{ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; +} + +$msg="Sub process 'xcatd: DB Access' is running"; +if($xcatdproc =~ /xcatd: DB Access/){ + ProbeUtils->send_msg("$output",3,"$msg"); +}else{ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; +} + +$msg="Sub process 'xcatd: UDP listener' is running"; +if($xcatdproc =~ /xcatd: UDP listener/){ + ProbeUtils->send_msg("$output",3,"$msg"); +}else{ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; +} + +$msg="Sub process 'xcatd: install monitor' is running"; +if($xcatdproc =~ /xcatd: install monitor/){ + ProbeUtils->send_msg("$output",3,"$msg"); +}else{ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; +} + +$msg="Sub process 'xcatd: Discovery worker' is running"; +if($xcatdproc =~ /xcatd: Discovery worker/){ + ProbeUtils->send_msg("$output",3,"$msg"); +}else{ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; +} + +$msg="Sub process 'xcatd: Command log writer' is running"; +if($xcatdproc =~ /xcatd: Command log writer/){ + ProbeUtils->send_msg("$output",3,"$msg"); +}else{ + ProbeUtils->send_msg("$output",1,"Sub process 'xcatd: Command log writer' isn't running"); +} +exit 1 if($rst); + + +my $xcatdport=`tabdump site 2>&1 | awk -F',' '/xcatdport/ { gsub(/"/, "", \$2) ; print \$2 }'`; +chomp($xcatdport); +ProbeUtils->send_msg("$output",0,"The port used by the xcatd daemon for client/server communication is $xcatdport") if($verbose); +$msg="xcatd is listening on port $xcatdport"; +my $cmdoutput = `netstat -ant|grep LISTEN|grep $xcatdport`; +if($?){ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; +}else{ + ProbeUtils->send_msg("$output",3,"$msg"); +} + +my $xcatiport=`tabdump site 2>&1| awk -F',' '/xcatiport/ { gsub(/"/, "", \$2) ; print \$2 }'`; +chomp($xcatiport); +ProbeUtils->send_msg("$output",0,"The port used by xcatd to receive install status updates from nodes is $xcatiport") if($verbose); +$msg="xcatd is listening on port $xcatiport"; +$cmdoutput = `netstat -ant|grep LISTEN|grep $xcatiport`; +if($?){ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; +}else{ + ProbeUtils->send_msg("$output",3,"$msg"); +} +exit 1 if($rst); + +$msg ="'lsxcatd -a' works"; +$cmdoutput=`lsxcatd -a 2>&1`; +$rst=$?; +returncmdoutput($cmdoutput) if($verbose); +if($rst){ + ProbeUtils->send_msg("$output",2,"$msg"); + exit $rst; +}else{ + ProbeUtils->send_msg("$output",3,"$msg"); +} + +my $masteripinsite=`tabdump site | awk -F',' '/master/ { gsub(/"/, "", \$2) ; print \$2 }'`; +chomp($masteripinsite); +ProbeUtils->send_msg("$output",0, "The value of 'master' in 'site' table is $masteripinsite") if($verbose); +ProbeUtils->send_msg("$output",2, "There isn't 'master' definition in 'site' talbe") if($masteripinsite eq ""); + +$msg="The value of 'master' in 'site' table is a IP address"; +if(ProbeUtils->isIpAddr("$masteripinsite")){ + ProbeUtils->send_msg("$output",3,"$msg"); +}else{ + ProbeUtils->send_msg("$output",2,"$msg"); + exit 1; +} + +if($mnip){ + $msg="The IP passed by command line equals the value of 'master' in 'site' table"; + if($mnip eq $masteripinsite){ + ProbeUtils->send_msg("$output",3,"$msg"); + }else{ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; + } +}else{ + ProbeUtils->send_msg("$output",0,"Use the value of 'master' of 'site' table as MNIP") if($verbose); + $mnip=$masteripinsite; +} + +ProbeUtils->send_msg("$output",0,"MNIP used in below checking is $mnip") if($verbose); + +$msg="There is configuration in 'passwd' table for 'system' for node provision"; +my $passwd=`tabdump passwd |awk -F',' '/system/ { gsub(/"/, "", \$2); gsub(/"/, "", \$3); print \$2,\$3 }'`; +chomp($passwd); +my ($username, $pw)=split(" ", $passwd); +if($username eq "" || $pw eq ""){ + ProbeUtils->send_msg("$output",0,"There isn't username or password for 'system' in 'passwd' table"); + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; +}else{ + ProbeUtils->send_msg("$output",3,"$msg"); +} + +$msg="There is a network adapter on current server is configured by IP $mnip"; +my $nic=`ip addr show |grep -B2 $mnip|grep mtu|awk '{print \$2}'|sed -e 's/:\$//'`; +chomp($nic); +if("$nic" eq ""){ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; +}else{ + ProbeUtils->send_msg("$output",0,"IP $mnip belongs to network adapter $nic") if($verbose); + ProbeUtils->send_msg("$output",3,"$msg"); +} + +$msg="IP $mnip is a static IP on current server"; +if(ProbeUtils->isStaticIp("$mnip", "$nic")){ + ProbeUtils->send_msg("$output",3,"$msg"); +}else{ + ProbeUtils->send_msg("$output",1,"IP $mnip is not a static ip on current server"); +} + +$msg="$mnip belongs to one of networks defined in 'networks' table"; +my $networks=`tabdump networks|grep -v "^#"`; +$networks=~s/\"//g; +my $netcnt = `echo "$networks"|wc -l`; +my $hit=0; +for(my $i=1;$i<$netcnt+1;$i++){ + my $line=`echo "$networks" |sed -n ${i}p |awk -F"," '{print \$2,\$3,\$4}'`; + chomp($line); + if($line =~ /(.+) (.+) (.+)/){ + $hit=1 if(ProbeUtils->isIpBelongToNet("$1", "$2", $mnip) && ("$3" eq "$nic")); + } +} +if($hit){ + ProbeUtils->send_msg("$output",3,"$msg"); +}else{ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; +} + +$msg="There is domain definition in 'site' table"; +my $domain=`tabdump site | awk -F',' '/domain/ { gsub(/"/, "", \$2) ; print \$2 }'`; +chomp($domain); +if($domain){ + ProbeUtils->send_msg("$output",0,"The value of 'domain' in 'site' table is $domain") if($verbose); + ProbeUtils->send_msg("$output",3,"$msg"); +}else{ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; +} + +$msg="There are /install and /install/postscripts directory on current server"; +if(-e "/install/postscripts/"){ + ProbeUtils->send_msg("$output",3,"$msg"); +}else{ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; +} + +$msg="There is /tftpboot directory on current server"; +if(-e "/tftpboot/"){ + ProbeUtils->send_msg("$output",3,"$msg"); +}else{ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; +} + +my $expected=10; +$msg="The free space of / directory is more than $expected G"; +my $diskspace=`df -h|awk '{print \$4,\$6}'|grep -E "/\$"`; +if($?){ + ProbeUtils->send_msg("$output",0,"There isn't any filesystem mount on / directory"); + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; +}else{ + chomp($diskspace); + my ($size, $dir) = split(" ", $diskspace); + $size=~s/G//g; + ProbeUtils->send_msg("$output",0,"The free space of / is $size G") if($verbose); + if($size<$expected){ + ProbeUtils->send_msg("$output",1,"The free space of / is less than $expected G"); + }else{ + ProbeUtils->send_msg("$output",3,"$msg"); + } +} + +$expected=1; +$msg="The free space of /var directory is more than $expected G"; +$diskspace=`df -h|awk '{print \$4,\$6}'|grep -E "/var\$"`; +if(!$?){ + chomp($diskspace); + my ($size, $dir) = split(" ", $diskspace); + $size=~s/G//g; + ProbeUtils->send_msg("$output",0,"The free space of /var is $size G") if($verbose); + if($size<$expected){ + ProbeUtils->send_msg("$output",1,"The free space of /var is less than $expected G"); + }else{ + ProbeUtils->send_msg("$output",3,"$msg"); + } +} + +$expected=1; +$msg="The free space of /tmp directory is more than $expected G"; +$diskspace=`df -h|awk '{print \$4,\$6}'|grep -E "/tmp\$"`; +if(!$?){ + chomp($diskspace); + my ($size, $dir) = split(" ", $diskspace); + $size=~s/G//g; + ProbeUtils->send_msg("$output",0,"The free space of /tmp is $size G") if($verbose); + if($size<$expected){ + ProbeUtils->send_msg("$output",1,"The free space of /tmp is less than $expected G"); + }else{ + ProbeUtils->send_msg("$output",3,"$msg"); + } +} + + +$expected=10; +$msg="The free space of /install directory is more than $expected G"; +$diskspace=`df -h|awk '{print \$4,\$6}'|grep -E "/install\$"`; +if(!$?){ + chomp($diskspace); + my ($size, $dir) = split(" ", $diskspace); + $size=~s/G//g; + ProbeUtils->send_msg("$output",0,"The free space of /install is $size G") if($verbose); + if($size<$expected){ + ProbeUtils->send_msg("$output",1,"The free space of /install is less than $expected G"); + }else{ + ProbeUtils->send_msg("$output",3,"$msg"); + } +} + +$msg="SELinux is disabled on current server"; +if(ProbeUtils->isSelinuxEnable()){ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; +}else{ + ProbeUtils->send_msg("$output",3,"$msg"); +} + +$msg="Firewall is closed on current server"; +if(ProbeUtils->isFirewallOpen()){ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; +}else{ + ProbeUtils->send_msg("$output",3,"$msg"); +} + +`which wget > /dev/null 2>&1`; +if($?){ + ProbeUtils->send_msg("$output",1,"wget command isn't installed on current server, skip checking HTTP service"); +}else{ + $msg="HTTP service is ready on $mnip"; + if(ProbeUtils->isHttpReady("$mnip")){ + ProbeUtils->send_msg("$output",3,"$msg"); + }else{ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; + } +} + +`which tftp > /dev/null 2>&1`; +if($?){ + ProbeUtils->send_msg("$output",1,"tftp command isn't installed in current server, skip checking tftp service"); +}else{ + $msg="TFTP service is ready on $mnip"; + if(ProbeUtils->isTftpReady("$mnip")){ + ProbeUtils->send_msg("$output",3,"$msg"); + }else{ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst =1; + } +} + +`which nslookup > /dev/null 2>&1`; +if($?){ + ProbeUtils->send_msg("$output",1,"nslookup command isn't installed in current server, skip checking DNS service"); +}else{ + $msg="DNS server is ready on $mnip"; + ProbeUtils->send_msg("$output",0,"hostname used to check DNS is $hostname, domain used to check DNS is $domain") if($verbose); + if(ProbeUtils->isDnsReady("$mnip","$hostname", "$domain")){ + ProbeUtils->send_msg("$output",3,"$msg"); + }else{ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst =1; + } +} + +my $os=ProbeUtils->getOS(); +my $leasefile=""; +$leasefile="/var/lib/dhcpd/dhcpd.leases" if($os =~ /redhat/i); +$leasefile="/var/lib/dhcp/db/dhcpd.leases" if($os =~ /sles/i); +$leasefile="/var/lib/dhcp/dhcpd.leases" if($os =~ /ubuntu/i); +$msg="The size of $leasefile is less than 100M"; +my $filesizetmp=`du -sb $leasefile`; +if($?){ + returncmdoutput($filesizetmp) if($verbose); + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; +}else{ + chomp($filesizetmp); + my ($size, $file) = split(" ", $filesizetmp); + ProbeUtils->send_msg("$output",0,"The size of $leasefile is $size bytes") if($verbose); + if($size > 104857600){ + ProbeUtils->send_msg("$output",1,"The size of $leasefile is more than 100M"); + }else{ + ProbeUtils->send_msg("$output",3,"$msg"); + } +} + +my $msg="DHCP service is ready on $mnip"; +my $rc=0; +{ #very important brace to create a block + my $tmp=`chdef xcatmntest groups=all ip=$mnip mac=aa:aa:aa:aa:aa:aa`; + if($?){ + returncmdoutput($tmp) if($verbose); + ProbeUtils->send_msg("$output",0,"Simulate a node by chdef failed") if($verbose); + $rc=1; + last; + }else{ + ProbeUtils->send_msg("$output",0,"Simulate a node xcatmntest to do dhcp test") if($verbose); + } + + $tmp=`makehosts xcatmntest`; + if($?){ + returncmdoutput($tmp) if($verbose); + ProbeUtils->send_msg("$output",0,"makehosts xcatmntest failed") if($verbose); + $rc=1; + `rmdef xcatmntest`; + last; + } + + $tmp = `makedhcp xcatmntest 2>&1`; + if($?){ + returncmdoutput($tmp) if($verbose); + ProbeUtils->send_msg("$output",0,"makedhcp xcatmntest failed") if($verbose); + $rc=1; + `makehosts -d xcatmntest && rmdef xcatmntest`; + last; + } + + $tmp = `makedhcp -q xcatmntest`; + if($?){ + returncmdoutput($tmp) if($verbose); + ProbeUtils->send_msg("$output",0,"makedhcp -q xcatmntest failed") if($verbose); + $rc=1; + `makedhcp -d xcatmntest && makehosts -d xcatmntest && rmdef xcatmntest`; + last; + } + chomp($tmp); + if($tmp !~ /xcatmntest: ip-address = $mnip, hardware-address = aa:aa:aa:aa:aa:aa/){ + returncmdoutput($tmp) if($verbose); + ProbeUtils->send_msg("$output",0,"DHCP server's reply is wrong") if($verbose); + $rc=1; + `makedhcp -d xcatmntest && makehosts -d xcatmntest && rmdef xcatmntest`; + last; + } + + ProbeUtils->send_msg("$output",0,"Start to clear simulate information for dhcp test") if($verbose); + $tmp=`makedhcp -d xcatmntest && makehosts -d xcatmntest && rmdef xcatmntest`; + returncmdoutput($tmp) if($verbose); +} +if($rc){ + ProbeUtils->send_msg("$output",2,"$msg"); + $rst=1; +}else{ + ProbeUtils->send_msg("$output",3,"$msg"); +} +exit $rst ; diff --git a/xCAT-probe/xcatprobe b/xCAT-probe/xcatprobe new file mode 100755 index 000000000..1e602b35f --- /dev/null +++ b/xCAT-probe/xcatprobe @@ -0,0 +1,258 @@ +#!/usr/bin/env perl + +use File::Basename; +use Data::Dumper; +use File::Path; +use POSIX qw(WNOHANG setsid :errno_h); +use Term::ANSIColor qw(:constants); +$Term::ANSIColor::AUTORESET=1; + +my $pro_name=basename($0); +my $pro_dir=dirname($0); +#my $plugin_dir="$pro_dir/subcmds"; +my $plugin_dir="/opt/xcat/probe/subcmds"; +my %cmds=(); + +my $verbose=0; +my $nocolor=0; +my $help=0; +my $list=0; + +$::USAGE = "Usage: +xcatprobe -h +xcatprobe -l +xcatprobe -v +xcatprobe [-n] + +Options: + -h : get usage information of $pro_name + -l : list all valid sub commands + -v : print verbose information of $pro_name + -n : print output without colors +"; + +#----------------------------------- +=head3 + Description: + Load sub commands from ~/subcmds directory + Using -t option of command to judge if it is valid. + If command in ~/subcmds has syntax error, or doesn't follow interface specification, this command will be skipped +=cut +#----------------------------------- +sub loadsubcmds{ + my @candidate=glob("$plugin_dir/*"); + my @subcmds=(); + my @validflag=("debug", "warning", "failed", "ok"); + my $output; + + print "Starting to load sub command form ~/subcmds.............\n" if($verbose); + + foreach (@candidate) { + my $cmdname = basename("$_"); + $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); + next; + }else{ + my $desc =$2; + unless(@validflag ~~ /$1/){ + print "skip $_ for doing '$_ -t' failed, invalid flag\n" if($verbose); + next; + } + $cmds{$cmdname} = $desc; + print "load $_ \n" if($verbose); + } + } + print "\nLoad sub command.........[done]\n" if($verbose); +} + + +#----------------------------------- +=head3 + Description: + Format the output of sub command, make them colorfully. +=cut +#---------------------------------- +sub format_cmd_output{ + my $line=shift; + my $nocolor=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 " "; + } + print "$msg\n"; + }else{ + print "$line\n"; + } + return 0; +} + +#----------------------------------- +=head3 + Description: + List all valid sub command in ~/subcmds directory +=cut +#---------------------------------- +sub listvalidsubcmd{ + my $maxlen=0; + foreach my $key (keys %cmds) { + $maxlen=length($key) if(length($key)>$maxlen); + } + $maxlen+=4; + 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){ + push @formatdesc, $str; + $str=""; + } + } + push @formatdesc, $str; + if($nocolor){ + print "$key"; + }else{ + print BOLD GREEN "$key"; + } + my $space=" " x ($maxlen-length($key)); + print "$space $formatdesc[0]\n"; + delete $formatdesc[0]; + $space=" " x $maxlen; + foreach my $line (@formatdesc){ + print "$space $line\n" if(length($line)); + } + + } +} + +####################################### +# main +####################################### +my @tmpargv=@ARGV; +my @supportopt=("-v","-h","-l","-n"); +my $pluginname; +my $optnum=0; +foreach my $attr (@tmpargv){ + if($attr =~ /^-/){ + unless(@supportopt ~~ /$attr/){ + print "Unsupported attribute: $attr\n"; + print $::USAGE; + exit 1; + } + $optnum++; + $help=1 if($attr eq "-h"); + $verbose=1 if($attr eq "-v"); + $list=1 if($attr eq "-l"); + $nocolor=1 if($attr eq "-n"); + }else{ + $pluginname=$attr; + last; + } +} + +&loadsubcmds; +if(defined ($pluginname)){ + my $hit=0; + foreach my $key (keys %cmds){ + $hit=1 if($pluginname eq $key); + } + unless($hit){ + print "Unsupported sub command: $pluginname\n"; + &listvalidsubcmd; + exit 1; + } +} + +if($help){ + print $::USAGE; + exit 0; +} + +if($ARGV[0] eq "-l"){ + &listvalidsubcmd; + exit 0; +} + +if(!defined ($pluginname)){ + print "There isn't sub command input from command line\n"; + exit 0; +} + +for(my $i =0; $i<$optnum+1; $i++){ + shift @tmpargv; +} +my $pluginattrs = join(" ", @tmpargv); + +my $date = `date +"%Y%m%d%H%M%S"`; +chomp($date); +my $cmmpath="/tmp/probe"; +my $cmmfile="$cmmpath/$pluginname.$date"; +mkpath("$cmmpath") unless(-d "$cmmpath"); +`touch $cmmfile`; +my $subcmd="$plugin_dir/$pluginname -f $cmmfile $pluginattrs"; + +print "\n[main] subcmd = $subcmd\n" if($verbose); + +my $subproc = fork(); +my $subrst=0; +if(!defined($subproc)){ + print "Unable to fork new process to run subcommand\n"; + exit 1; +}elsif($subproc==0){ + $SIG{INT} = sub { + print "Sub proc $$ recrive INT signal to exit"; + exit 0; + }; + print "[sub $$] run $subcmd\n----------------------\n" if($verbose); + `$subcmd`; + exit $? ; +} + +if(!open (CMMFILE, "$cmmfile") ) { + print "[main] Failed to open $cmmfile\n"; + exit 1; +} +my $offset = 0; +my $prst=0; +if($subproc){ + do{ + seek(CMMFILE,$offset,0); + while(my $line = ){ + chomp($line); + format_cmd_output($line, $nocolor); + } + $offset=tell; + }while(waitpid($subproc, WNOHANG)==0) +} +close(CMMFILE); + +unlink($cmmfile); +exit 0; + From d2e469f919506a5992d07c349938baccab54e20b Mon Sep 17 00:00:00 2001 From: hu-weihua Date: Tue, 31 May 2016 03:24:37 -0400 Subject: [PATCH 2/7] change code logic and format depending on comments come from colleagues --- xCAT-probe/xcatprobe | 249 +++++++++++++++++++------------------------ 1 file changed, 112 insertions(+), 137 deletions(-) diff --git a/xCAT-probe/xcatprobe b/xCAT-probe/xcatprobe index 1e602b35f..366a00686 100755 --- a/xCAT-probe/xcatprobe +++ b/xCAT-probe/xcatprobe @@ -1,152 +1,159 @@ #!/usr/bin/env perl +# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html use File::Basename; use Data::Dumper; use File::Path; use POSIX qw(WNOHANG setsid :errno_h); use Term::ANSIColor qw(:constants); -$Term::ANSIColor::AUTORESET=1; +$Term::ANSIColor::AUTORESET = 1; -my $pro_name=basename($0); -my $pro_dir=dirname($0); -#my $plugin_dir="$pro_dir/subcmds"; -my $plugin_dir="/opt/xcat/probe/subcmds"; -my %cmds=(); +$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat'; -my $verbose=0; -my $nocolor=0; -my $help=0; -my $list=0; +my $pro_name = basename($0); + +my $pro_dir="$::XCATROOT/probe/"; +my $plugin_dir = "$pro_dir/subcmds"; +my %cmds = (); + +my $verbose = 0; +my $nocolor = 0; +my $help = 0; +my $list = 0; $::USAGE = "Usage: xcatprobe -h xcatprobe -l -xcatprobe -v -xcatprobe [-n] +xcatprobe [-n] [-V] Options: -h : get usage information of $pro_name -l : list all valid sub commands - -v : print verbose information of $pro_name + -V : print verbose information of $pro_name -n : print output without colors "; #----------------------------------- + =head3 Description: Load sub commands from ~/subcmds directory Using -t option of command to judge if it is valid. If command in ~/subcmds has syntax error, or doesn't follow interface specification, this command will be skipped =cut + #----------------------------------- -sub loadsubcmds{ - my @candidate=glob("$plugin_dir/*"); - my @subcmds=(); - my @validflag=("debug", "warning", "failed", "ok"); +sub loadsubcmds { + my @candidate = glob("$plugin_dir/*"); + my @subcmds = (); my $output; - - print "Starting to load sub command form ~/subcmds.............\n" if($verbose); + + print "Starting to load sub command form ~/subcmds.............\n" if ($verbose); foreach (@candidate) { my $cmdname = basename("$_"); $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 "\n-->$_\n[OUTPUT]:\n$output\n" if ($verbose); + if ($output !~ /\[(\w+)\]\s*:\s*(.+)/) { + print "skip $_ for doing '$_ -t' failed, bad format\n" if ($verbose); next; - }else{ - my $desc =$2; - unless(@validflag ~~ /$1/){ - print "skip $_ for doing '$_ -t' failed, invalid flag\n" if($verbose); + } else { + my $desc = $2; + unless ($1 ~~ /^ok$/) { + print "skip $_ for doing '$_ -t' failed, invalid flag\n" if ($verbose); next; } $cmds{$cmdname} = $desc; - print "load $_ \n" if($verbose); + print "load $_ \n" if ($verbose); } } - print "\nLoad sub command.........[done]\n" if($verbose); + print "\nLoad sub command.........[done]\n" if ($verbose); } #----------------------------------- + =head3 Description: Format the output of sub command, make them colorfully. =cut -#---------------------------------- -sub format_cmd_output{ - my $line=shift; - my $nocolor=shift; - if($line =~ /\[(\w+)\]\s*:\s*(.+)/){ +#---------------------------------- +sub format_cmd_output { + my $line = shift; + my $nocolor = shift; + + if ($line =~ /\[(\w+)\]\s*:\s*(.+)/) { my $flag = $1; - my $msg = $2; - if($flag =~ /failed/i){ - if($nocolor){ + my $msg = $2; + if ($flag =~ /failed/i) { + if ($nocolor) { print "[FAIL] "; - }else{ - print BOLD RED "[FAIL] "; + } else { + print BOLD RED "[FAIL] "; } - }elsif($flag =~ /warning/i){ - if($nocolor){ + } elsif ($flag =~ /warning/i) { + if ($nocolor) { print "[WARN] "; - }else{ - print BOLD BLUE "[WARN] "; + } else { + print BOLD BLUE "[WARN] "; } - }elsif($flag =~ /ok/i){ - if($nocolor){ + } elsif ($flag =~ /ok/i) { + if ($nocolor) { print "[ OK ] "; - }else{ + } else { print BOLD GREEN "[ OK ] "; } - }elsif($flag =~ /debug/i){ + } elsif ($flag =~ /debug/i) { print " "; } print "$msg\n"; - }else{ + } else { print "$line\n"; } return 0; } #----------------------------------- + =head3 Description: List all valid sub command in ~/subcmds directory =cut + #---------------------------------- -sub listvalidsubcmd{ - my $maxlen=0; +sub listvalidsubcmd { + my $maxlen = 0; foreach my $key (keys %cmds) { - $maxlen=length($key) if(length($key)>$maxlen); + $maxlen = length($key) if (length($key) > $maxlen); } - $maxlen+=4; + $maxlen += 4; 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){ - push @formatdesc, $str; - $str=""; + foreach my $key (keys %cmds) { + my @desc = split(" ", $cmds{$key}); + my $str = ""; + my @formatdesc = (); + foreach my $word (@desc) { + $str .= $word . " "; + if (length($str) > 100) { + push @formatdesc, $str; + $str = ""; } } push @formatdesc, $str; - if($nocolor){ + if ($nocolor) { print "$key"; - }else{ + } else { print BOLD GREEN "$key"; } - my $space=" " x ($maxlen-length($key)); + my $space = " " x ($maxlen - length($key)); print "$space $formatdesc[0]\n"; delete $formatdesc[0]; - $space=" " x $maxlen; - foreach my $line (@formatdesc){ - print "$space $line\n" if(length($line)); + $space = " " x $maxlen; + foreach my $line (@formatdesc) { + print "$space $line\n" if (length($line)); } } @@ -155,104 +162,72 @@ sub listvalidsubcmd{ ####################################### # main ####################################### -my @tmpargv=@ARGV; -my @supportopt=("-v","-h","-l","-n"); +my @tmpargv = @ARGV; +my @supportopt = ("-V", "-h", "-l", "-n"); my $pluginname; -my $optnum=0; -foreach my $attr (@tmpargv){ - if($attr =~ /^-/){ - unless(@supportopt ~~ /$attr/){ - print "Unsupported attribute: $attr\n"; - print $::USAGE; - exit 1; - } - $optnum++; - $help=1 if($attr eq "-h"); - $verbose=1 if($attr eq "-v"); - $list=1 if($attr eq "-l"); - $nocolor=1 if($attr eq "-n"); - }else{ - $pluginname=$attr; - last; - } +my $optnum = 0; +foreach my $attr (@tmpargv) { + if ($attr =~ /^-/) { + unless (@supportopt ~~ /^$attr$/) { + print "Unsupported attribute: $attr\n"; + print $::USAGE; + exit 1; + } + $optnum++; + $help = 1 if ($attr eq "-h"); + $verbose = 1 if ($attr eq "-V"); + $list = 1 if ($attr eq "-l"); + $nocolor = 1 if ($attr eq "-n"); + } else { + $pluginname = $attr; + last; + } } &loadsubcmds; -if(defined ($pluginname)){ - my $hit=0; - foreach my $key (keys %cmds){ - $hit=1 if($pluginname eq $key); +if (defined($pluginname)) { + my $hit = 0; + foreach my $key (keys %cmds) { + $hit = 1 if ($pluginname eq $key); } - unless($hit){ + unless ($hit) { print "Unsupported sub command: $pluginname\n"; &listvalidsubcmd; exit 1; } } -if($help){ +if ($help) { print $::USAGE; - exit 0; + exit 0; } -if($ARGV[0] eq "-l"){ +if ($ARGV[0] eq "-l") { &listvalidsubcmd; exit 0; } -if(!defined ($pluginname)){ +if (!defined($pluginname)) { print "There isn't sub command input from command line\n"; exit 0; } -for(my $i =0; $i<$optnum+1; $i++){ +for (my $i = 0 ; $i < $optnum + 1 ; $i++) { shift @tmpargv; } my $pluginattrs = join(" ", @tmpargv); +my $subcmd = "$plugin_dir/$pluginname $pluginattrs"; -my $date = `date +"%Y%m%d%H%M%S"`; -chomp($date); -my $cmmpath="/tmp/probe"; -my $cmmfile="$cmmpath/$pluginname.$date"; -mkpath("$cmmpath") unless(-d "$cmmpath"); -`touch $cmmfile`; -my $subcmd="$plugin_dir/$pluginname -f $cmmfile $pluginattrs"; +print "\nsubcmd = $subcmd\n" if ($verbose); -print "\n[main] subcmd = $subcmd\n" if($verbose); - -my $subproc = fork(); -my $subrst=0; -if(!defined($subproc)){ - print "Unable to fork new process to run subcommand\n"; - exit 1; -}elsif($subproc==0){ - $SIG{INT} = sub { - print "Sub proc $$ recrive INT signal to exit"; - exit 0; - }; - print "[sub $$] run $subcmd\n----------------------\n" if($verbose); - `$subcmd`; - exit $? ; +open(PIPE, "$subcmd |"); +while () { + chomp; + format_cmd_output($_, $nocolor); } +close(PIPE); # This will set the $? properly -if(!open (CMMFILE, "$cmmfile") ) { - print "[main] Failed to open $cmmfile\n"; - exit 1; -} -my $offset = 0; -my $prst=0; -if($subproc){ - do{ - seek(CMMFILE,$offset,0); - while(my $line = ){ - chomp($line); - format_cmd_output($line, $nocolor); - } - $offset=tell; - }while(waitpid($subproc, WNOHANG)==0) -} -close(CMMFILE); +my $ret = $? >> 8; -unlink($cmmfile); -exit 0; +exit $ret; From bb8e10cbac572c3cdaaf586bffb318f29a9881dc Mon Sep 17 00:00:00 2001 From: hu-weihua Date: Tue, 31 May 2016 03:24:53 -0400 Subject: [PATCH 3/7] change code logic and format depending on comments come from colleagues --- xCAT-probe/subcmds/xcatmn | 758 +++++++++++++++++++++----------------- 1 file changed, 417 insertions(+), 341 deletions(-) diff --git a/xCAT-probe/subcmds/xcatmn b/xCAT-probe/subcmds/xcatmn index 7e9c3c21c..125a4b881 100755 --- a/xCAT-probe/subcmds/xcatmn +++ b/xCAT-probe/subcmds/xcatmn @@ -1,41 +1,42 @@ #! /usr/bin/perl +# IBM(c) 2016 EPL license http://www.eclipse.org/legal/epl-v10.html -#use lib "/home/probe/lib/perl"; -use lib "/opt/xcat/probe/lib/perl"; -use ProbeUtils; +BEGIN { $::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : -d '/opt/xcat' ? '/opt/xcat' : '/usr'; } + +use lib "$::XCATROOT/probe/lib/perl"; +use probe_utils; use File::Basename; -use Getopt::Long; +use Getopt::Long qw(:config no_ignore_case); -my $proname=basename("$0"); +my $proname = basename("$0"); my $help; -my $mnip; +my $installnic; my $test; -my $output="stdout"; -my $verbose=0; -my $rst=0; +my $output = "stdout"; +my $verbose = 0; +my $rst = 0; $::USAGE = "Usage: $proname -h $proname -t - $proname [-m] [-f] [-v] + $proname [-n] [-V] Description: After xcat installation, use this command to check if xcat has been installed correctly and is ready for use. Options: - -h : get usage information of $proname - -t : to verify if $proname can work, reserve option for probe framework - -m : specify current MN's IP address, if not specified, use master ip in site table - -f : specify where should $proname pass its output to. if not specified, pass output to STDOUT by default. - -v : output more information for debug + -h : Get usage information of $proname + -t : To verify if $proname can work, reserve option for probe framework + -n : Required. Specify the network interface name of provision network + -V : Output more information for debug "; -sub returncmdoutput{ - my $rst=shift; +sub returncmdoutput { + my $rst = shift; chomp($rst); - my @lines=split("[\n\r]", $rst); - foreach my $line (@lines){ - ProbeUtils->send_msg("$output",0, "$line"); + my @lines = split("[\n\r]", $rst); + foreach my $line (@lines) { + probe_utils->send_msg("$output", "d", "$line"); } } @@ -44,432 +45,507 @@ sub returncmdoutput{ # main process #------------------------------------- if ( - !GetOptions("--help|h|?" => \$help, - "t"=> \$test, - "v"=> \$verbose, - "f=s" => \$output, - "m=s" => \$mnip)) + !GetOptions("--help|h" => \$help, + "t" => \$test, + "V" => \$verbose, + "f=s" => \$output, + "n=s" => \$installnic)) { - ProbeUtils->send_msg("$output",2, "Invalid parameter for $proname"); - ProbeUtils->send_msg("$output",0, "$::USAGE"); + probe_utils->send_msg("$output", "f", "Invalid parameter for $proname"); + probe_utils->send_msg("$output", "d", "$::USAGE"); exit 1; } -if($help){ - if($output ne "stdout"){ - ProbeUtils->send_msg("$output",0, "$::USAGE"); - }else{ - print "$::USAGE"; - } - exit 0; +if ($help) { + if ($output ne "stdout") { + probe_utils->send_msg("$output", "d", "$::USAGE"); + } else { + print "$::USAGE"; + } + exit 0; } -if($test){ - ProbeUtils->send_msg("$output",3,"After xcat installation, use this command to check if xcat has been installed correctly and is ready for use. Before using this command , please install tftp, nslookup and wget commands ahead. The platform supported are redhat, sles and ubuntu."); - exit 0; +if ($test) { + probe_utils->send_msg("$output", "o", "After xcat installation, use this command to check if xcat has been installed correctly and is ready for use. Before using this command , please install tftp, nslookup and wget commands ahead. The platform supported are redhat, sles and ubuntu."); + exit 0; } -my $hostname=`hostname -s`; -chomp($hostname); -my $hostipaddr=`hostname -i`; -chomp($hostipaddr); -my $othername=`hostname -a`; -chomp($othername); +if (!defined($installnic)) { + probe_utils->send_msg("$output", "f", "Option -n is required"); + probe_utils->send_msg("$output", "d", "$::USAGE"); + exit 1; +} -ProbeUtils->send_msg("$output",0,"Short hostname of this server is $hostname") if($verbose); -ProbeUtils->send_msg("$output",0,"The address of hostname $hostname is $hostipaddr") if($verbose); -ProbeUtils->send_msg("$output",0,"The other names of $hostname is $othername") if($verbose && ($othername ne "")); +my $msg = "NIC $installnic exists on current server"; +my $nics = `ip addr show 2>&1 | grep -E '^[1-9]' | cut -d: -f2 |grep $installnic`; +if ($?) { + probe_utils->send_msg("$output", "f", "$msg"); + probe_utils->send_msg("$output", "d", "Please use 'ip addr show' to check if there is NIC named $installnic on current server"); + exit 1; +} else { + probe_utils->send_msg("$output", "o", "$msg"); +} -my $msg="Sub process 'xcatd: SSL listener' is running"; +$msg = "Get ip address of NIC $installnic"; +my $mnip = `ip addr show |grep -A2 $installnic: | awk -F" " '/inet/ {print \$2}'|awk -F"/" '{print \$1}'`; +chomp($mnip); +if (!defined($mnip) || ($mnip eq "")) { + probe_utils->send_msg("$output", "f", "$msg"); + probe_utils->send_msg("$output", "d", "Please use 'ip addr show' to check if there is ip assigned to $installnic"); + exit 1; +} else { + probe_utils->send_msg("$output", "d", "The IP of NIC $installnic is $mnip") if ($verbose); + probe_utils->send_msg("$output", "o", "$msg"); +} + +$msg = "Sub process 'xcatd: SSL listener' is running"; my $xcatdproc = `ps aux|grep -v grep|grep xcatd`; chomp($xcatdproc); -if($xcatdproc =~ /xcatd: SSL listener/){ - ProbeUtils->send_msg("$output",3,"$msg"); -}else{ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; +if ($xcatdproc =~ /xcatd: SSL listener/) { + probe_utils->send_msg("$output", "o", "$msg"); +} else { + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; } -$msg="Sub process 'xcatd: DB Access' is running"; -if($xcatdproc =~ /xcatd: DB Access/){ - ProbeUtils->send_msg("$output",3,"$msg"); -}else{ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; +$msg = "Sub process 'xcatd: DB Access' is running"; +if ($xcatdproc =~ /xcatd: DB Access/) { + probe_utils->send_msg("$output", "o", "$msg"); +} else { + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; } -$msg="Sub process 'xcatd: UDP listener' is running"; -if($xcatdproc =~ /xcatd: UDP listener/){ - ProbeUtils->send_msg("$output",3,"$msg"); -}else{ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; +$msg = "Sub process 'xcatd: UDP listener' is running"; +if ($xcatdproc =~ /xcatd: UDP listener/) { + probe_utils->send_msg("$output", "o", "$msg"); +} else { + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; } -$msg="Sub process 'xcatd: install monitor' is running"; -if($xcatdproc =~ /xcatd: install monitor/){ - ProbeUtils->send_msg("$output",3,"$msg"); -}else{ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; +$msg = "Sub process 'xcatd: install monitor' is running"; +if ($xcatdproc =~ /xcatd: install monitor/) { + probe_utils->send_msg("$output", "o", "$msg"); +} else { + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; } -$msg="Sub process 'xcatd: Discovery worker' is running"; -if($xcatdproc =~ /xcatd: Discovery worker/){ - ProbeUtils->send_msg("$output",3,"$msg"); -}else{ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; +$msg = "Sub process 'xcatd: Discovery worker' is running"; +if ($xcatdproc =~ /xcatd: Discovery worker/) { + probe_utils->send_msg("$output", "o", "$msg"); +} else { + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; } -$msg="Sub process 'xcatd: Command log writer' is running"; -if($xcatdproc =~ /xcatd: Command log writer/){ - ProbeUtils->send_msg("$output",3,"$msg"); -}else{ - ProbeUtils->send_msg("$output",1,"Sub process 'xcatd: Command log writer' isn't running"); +$msg = "Sub process 'xcatd: Command log writer' is running"; +if ($xcatdproc =~ /xcatd: Command log writer/) { + probe_utils->send_msg("$output", "o", "$msg"); +} else { + probe_utils->send_msg("$output", "w", "Sub process 'xcatd: Command log writer' isn't running"); } -exit 1 if($rst); - +exit 1 if ($rst); -my $xcatdport=`tabdump site 2>&1 | awk -F',' '/xcatdport/ { gsub(/"/, "", \$2) ; print \$2 }'`; + +my $xcatdport = `tabdump site 2>&1 | awk -F',' '/xcatdport/ { gsub(/"/, "", \$2) ; print \$2 }'`; chomp($xcatdport); -ProbeUtils->send_msg("$output",0,"The port used by the xcatd daemon for client/server communication is $xcatdport") if($verbose); -$msg="xcatd is listening on port $xcatdport"; +probe_utils->send_msg("$output", "d", "The port used by the xcatd daemon for client/server communication is $xcatdport") if ($verbose); +$msg = "xcatd is listening on port $xcatdport"; my $cmdoutput = `netstat -ant|grep LISTEN|grep $xcatdport`; -if($?){ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; -}else{ - ProbeUtils->send_msg("$output",3,"$msg"); +if ($?) { + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; +} else { + probe_utils->send_msg("$output", "o", "$msg"); } -my $xcatiport=`tabdump site 2>&1| awk -F',' '/xcatiport/ { gsub(/"/, "", \$2) ; print \$2 }'`; +my $xcatiport = `tabdump site 2>&1| awk -F',' '/xcatiport/ { gsub(/"/, "", \$2) ; print \$2 }'`; chomp($xcatiport); -ProbeUtils->send_msg("$output",0,"The port used by xcatd to receive install status updates from nodes is $xcatiport") if($verbose); -$msg="xcatd is listening on port $xcatiport"; -$cmdoutput = `netstat -ant|grep LISTEN|grep $xcatiport`; -if($?){ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; -}else{ - ProbeUtils->send_msg("$output",3,"$msg"); +probe_utils->send_msg("$output", "d", "The port used by xcatd to receive install status updates from nodes is $xcatiport") if ($verbose); +$msg = "xcatd is listening on port $xcatiport"; +$cmdoutput = `netstat -antp | grep -i xcatd|grep LISTEN|grep $xcatiport`; +if ($?) { + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; +} else { + probe_utils->send_msg("$output", "o", "$msg"); } -exit 1 if($rst); - -$msg ="'lsxcatd -a' works"; -$cmdoutput=`lsxcatd -a 2>&1`; -$rst=$?; -returncmdoutput($cmdoutput) if($verbose); -if($rst){ - ProbeUtils->send_msg("$output",2,"$msg"); +exit 1 if ($rst); + +$msg = "'lsxcatd -a' works"; +$cmdoutput = `lsxcatd -a 2>&1`; +$rst = $?; +returncmdoutput($cmdoutput) if ($verbose); +if ($rst) { + probe_utils->send_msg("$output", "f", "$msg"); exit $rst; -}else{ - ProbeUtils->send_msg("$output",3,"$msg"); +} else { + probe_utils->send_msg("$output", "o", "$msg"); } -my $masteripinsite=`tabdump site | awk -F',' '/master/ { gsub(/"/, "", \$2) ; print \$2 }'`; +my $masteripinsite = `tabdump site | awk -F',' '/master/ { gsub(/"/, "", \$2) ; print \$2 }'`; chomp($masteripinsite); -ProbeUtils->send_msg("$output",0, "The value of 'master' in 'site' table is $masteripinsite") if($verbose); -ProbeUtils->send_msg("$output",2, "There isn't 'master' definition in 'site' talbe") if($masteripinsite eq ""); +probe_utils->send_msg("$output", "d", "The value of 'master' in 'site' table is $masteripinsite") if ($verbose); +probe_utils->send_msg("$output", "f", "There isn't 'master' definition in 'site' talbe") if ($masteripinsite eq ""); -$msg="The value of 'master' in 'site' table is a IP address"; -if(ProbeUtils->isIpAddr("$masteripinsite")){ - ProbeUtils->send_msg("$output",3,"$msg"); -}else{ - ProbeUtils->send_msg("$output",2,"$msg"); +$msg = "The value of 'master' in 'site' table is a IP address"; +if (probe_utils->is_ip_addr("$masteripinsite")) { + probe_utils->send_msg("$output", "o", "$msg"); +} else { + probe_utils->send_msg("$output", "f", "$msg"); exit 1; } -if($mnip){ - $msg="The IP passed by command line equals the value of 'master' in 'site' table"; - if($mnip eq $masteripinsite){ - ProbeUtils->send_msg("$output",3,"$msg"); - }else{ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; +if ($mnip) { + $msg = "The IP of $installnic equals the value of 'master' in 'site' table"; + if ($mnip eq $masteripinsite) { + probe_utils->send_msg("$output", "o", "$msg"); + } else { + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; } -}else{ - ProbeUtils->send_msg("$output",0,"Use the value of 'master' of 'site' table as MNIP") if($verbose); - $mnip=$masteripinsite; } -ProbeUtils->send_msg("$output",0,"MNIP used in below checking is $mnip") if($verbose); - -$msg="There is configuration in 'passwd' table for 'system' for node provision"; -my $passwd=`tabdump passwd |awk -F',' '/system/ { gsub(/"/, "", \$2); gsub(/"/, "", \$3); print \$2,\$3 }'`; -chomp($passwd); -my ($username, $pw)=split(" ", $passwd); -if($username eq "" || $pw eq ""){ - ProbeUtils->send_msg("$output",0,"There isn't username or password for 'system' in 'passwd' table"); - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; -}else{ - ProbeUtils->send_msg("$output",3,"$msg"); +$msg = "IP $mnip of NIC $installnic is a static IP on current server"; +if (probe_utils->is_static_ip("$mnip", "$installnic")) { + probe_utils->send_msg("$output", "o", "$msg"); +} else { + probe_utils->send_msg("$output", "w", "IP $mnip of $installnic is not a static ip on current server"); } -$msg="There is a network adapter on current server is configured by IP $mnip"; -my $nic=`ip addr show |grep -B2 $mnip|grep mtu|awk '{print \$2}'|sed -e 's/:\$//'`; -chomp($nic); -if("$nic" eq ""){ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; -}else{ - ProbeUtils->send_msg("$output",0,"IP $mnip belongs to network adapter $nic") if($verbose); - ProbeUtils->send_msg("$output",3,"$msg"); -} - -$msg="IP $mnip is a static IP on current server"; -if(ProbeUtils->isStaticIp("$mnip", "$nic")){ - ProbeUtils->send_msg("$output",3,"$msg"); -}else{ - ProbeUtils->send_msg("$output",1,"IP $mnip is not a static ip on current server"); -} - -$msg="$mnip belongs to one of networks defined in 'networks' table"; -my $networks=`tabdump networks|grep -v "^#"`; -$networks=~s/\"//g; +$msg = "$mnip belongs to one of networks defined in 'networks' table"; +my $networks = `tabdump networks|grep -v "^#"`; +$networks =~ s/\"//g; my $netcnt = `echo "$networks"|wc -l`; -my $hit=0; -for(my $i=1;$i<$netcnt+1;$i++){ - my $line=`echo "$networks" |sed -n ${i}p |awk -F"," '{print \$2,\$3,\$4}'`; +my $hit = 0; +for (my $i = 1 ; $i < $netcnt + 1 ; $i++) { + my $line = `echo "$networks" |sed -n ${i}p |awk -F"," '{print \$2,\$3,\$4}'`; chomp($line); - if($line =~ /(.+) (.+) (.+)/){ - $hit=1 if(ProbeUtils->isIpBelongToNet("$1", "$2", $mnip) && ("$3" eq "$nic")); + if ($line =~ /(.+) (.+) (.+)/) { + $hit = 1 if (probe_utils->is_ip_belong_to_net("$1", "$2", $mnip) && ("$3" eq "$installnic")); } } -if($hit){ - ProbeUtils->send_msg("$output",3,"$msg"); -}else{ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; +if ($hit) { + probe_utils->send_msg("$output", "o", "$msg"); +} else { + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; } -$msg="There is domain definition in 'site' table"; -my $domain=`tabdump site | awk -F',' '/domain/ { gsub(/"/, "", \$2) ; print \$2 }'`; +$msg = "There is domain definition in 'site' table"; +my $domain = `tabdump site | awk -F',' '/domain/ { gsub(/"/, "", \$2) ; print \$2 }'`; chomp($domain); -if($domain){ - ProbeUtils->send_msg("$output",0,"The value of 'domain' in 'site' table is $domain") if($verbose); - ProbeUtils->send_msg("$output",3,"$msg"); -}else{ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; +if ($domain) { + probe_utils->send_msg("$output", "d", "The value of 'domain' in 'site' table is $domain") if ($verbose); + probe_utils->send_msg("$output", "o", "$msg"); +} else { + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; } -$msg="There are /install and /install/postscripts directory on current server"; -if(-e "/install/postscripts/"){ - ProbeUtils->send_msg("$output",3,"$msg"); -}else{ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; +$msg = "There is configuration in 'passwd' table for 'system' for node provision"; +my $passwd = `tabdump passwd |awk -F',' '/system/ { gsub(/"/, "", \$2); gsub(/"/, "", \$3); print \$2,\$3 }'`; +chomp($passwd); +my ($username, $pw) = split(" ", $passwd); +if ($username eq "" || $pw eq "") { + probe_utils->send_msg("$output", "f", "$msg"); + probe_utils->send_msg("$output", "d", "Please define username and password for 'system' in 'passwd' table"); + $rst = 1; +} else { + probe_utils->send_msg("$output", "o", "$msg"); } -$msg="There is /tftpboot directory on current server"; -if(-e "/tftpboot/"){ - ProbeUtils->send_msg("$output",3,"$msg"); -}else{ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; +my $installdir = `tabdump site 2>&1 | awk -F',' '/installdir/ { gsub(/"/, "", \$2) ; print \$2 }'`; +chomp($installdir); +probe_utils->send_msg("$output", "d", "The 'install' directory is set to $installdir in 'site' table on current server") if ($verbose); +my $tftpdir = `tabdump site 2>&1 | awk -F',' '/tftpdir/ { gsub(/"/, "", \$2) ; print \$2 }'`; +chomp($tftpdir); +probe_utils->send_msg("$output", "d", "The 'tftp' directory is set to $tftpdir in 'site' talbe on current server") if ($verbose); + +$msg = "There is $installdir directory on current server"; +if (-e "$installdir/postscripts/") { + probe_utils->send_msg("$output", "o", "$msg"); +} else { + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; } -my $expected=10; -$msg="The free space of / directory is more than $expected G"; -my $diskspace=`df -h|awk '{print \$4,\$6}'|grep -E "/\$"`; -if($?){ - ProbeUtils->send_msg("$output",0,"There isn't any filesystem mount on / directory"); - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; -}else{ +$msg = "There is $tftpdir directory on current server"; +if (-e "$tftpdir") { + probe_utils->send_msg("$output", "o", "$msg"); +} else { + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; +} + +my $expected = 10; +$msg = "The free space of / directory is more than $expected G"; +my $diskspace = `df -h|awk '{print \$4,\$6}'|grep -E "/\$"`; +if ($?) { + probe_utils->send_msg("$output", "d", "There isn't any filesystem mount on / directory"); + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; +} else { chomp($diskspace); my ($size, $dir) = split(" ", $diskspace); - $size=~s/G//g; - ProbeUtils->send_msg("$output",0,"The free space of / is $size G") if($verbose); - if($size<$expected){ - ProbeUtils->send_msg("$output",1,"The free space of / is less than $expected G"); - }else{ - ProbeUtils->send_msg("$output",3,"$msg"); + $size =~ s/G//g; + probe_utils->send_msg("$output", "d", "The free space of / is $size G") if ($verbose); + if ($size < $expected) { + probe_utils->send_msg("$output", "w", "The free space of / is less than $expected G"); + } else { + probe_utils->send_msg("$output", "o", "$msg"); } } -$expected=1; -$msg="The free space of /var directory is more than $expected G"; -$diskspace=`df -h|awk '{print \$4,\$6}'|grep -E "/var\$"`; -if(!$?){ +$expected = 1; +$msg = "The free space of /var directory is more than $expected G"; +$diskspace = `df -h|awk '{print \$4,\$6}'|grep -E "/var\$"`; +if (!$?) { chomp($diskspace); my ($size, $dir) = split(" ", $diskspace); - $size=~s/G//g; - ProbeUtils->send_msg("$output",0,"The free space of /var is $size G") if($verbose); - if($size<$expected){ - ProbeUtils->send_msg("$output",1,"The free space of /var is less than $expected G"); - }else{ - ProbeUtils->send_msg("$output",3,"$msg"); + $size =~ s/G//g; + probe_utils->send_msg("$output", "d", "The free space of /var is $size G") if ($verbose); + if ($size < $expected) { + probe_utils->send_msg("$output", "w", "The free space of /var is less than $expected G"); + } else { + probe_utils->send_msg("$output", "o", "$msg"); } -} +} -$expected=1; -$msg="The free space of /tmp directory is more than $expected G"; -$diskspace=`df -h|awk '{print \$4,\$6}'|grep -E "/tmp\$"`; -if(!$?){ +$expected = 1; +$msg = "The free space of /tmp directory is more than $expected G"; +$diskspace = `df -h|awk '{print \$4,\$6}'|grep -E "/tmp\$"`; +if (!$?) { chomp($diskspace); my ($size, $dir) = split(" ", $diskspace); - $size=~s/G//g; - ProbeUtils->send_msg("$output",0,"The free space of /tmp is $size G") if($verbose); - if($size<$expected){ - ProbeUtils->send_msg("$output",1,"The free space of /tmp is less than $expected G"); - }else{ - ProbeUtils->send_msg("$output",3,"$msg"); + $size =~ s/G//g; + probe_utils->send_msg("$output", "d", "The free space of /tmp is $size G") if ($verbose); + if ($size < $expected) { + probe_utils->send_msg("$output", "w", "The free space of /tmp is less than $expected G"); + } else { + probe_utils->send_msg("$output", "o", "$msg"); } } -$expected=10; -$msg="The free space of /install directory is more than $expected G"; -$diskspace=`df -h|awk '{print \$4,\$6}'|grep -E "/install\$"`; -if(!$?){ +$expected = 10; +$msg = "The free space of $installdir directory is more than $expected G"; +$diskspace = `df -h|awk '{print \$4,\$6}'|grep -E "$installdir\$"`; +if (!$?) { chomp($diskspace); my ($size, $dir) = split(" ", $diskspace); - $size=~s/G//g; - ProbeUtils->send_msg("$output",0,"The free space of /install is $size G") if($verbose); - if($size<$expected){ - ProbeUtils->send_msg("$output",1,"The free space of /install is less than $expected G"); - }else{ - ProbeUtils->send_msg("$output",3,"$msg"); + $size =~ s/G//g; + probe_utils->send_msg("$output", "d", "The free space of /install is $size G") if ($verbose); + if ($size < $expected) { + probe_utils->send_msg("$output", "w", "The free space of /install is less than $expected G"); + } else { + probe_utils->send_msg("$output", "o", "$msg"); } } -$msg="SELinux is disabled on current server"; -if(ProbeUtils->isSelinuxEnable()){ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; -}else{ - ProbeUtils->send_msg("$output",3,"$msg"); +$msg = "SELinux is disabled on current server"; +if (probe_utils->is_selinux_enable()) { + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; +} else { + probe_utils->send_msg("$output", "o", "$msg"); } -$msg="Firewall is closed on current server"; -if(ProbeUtils->isFirewallOpen()){ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; -}else{ - ProbeUtils->send_msg("$output",3,"$msg"); +$msg = "Firewall is closed on current server"; +if (probe_utils->is_firewall_open()) { + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; +} else { + probe_utils->send_msg("$output", "o", "$msg"); } `which wget > /dev/null 2>&1`; -if($?){ - ProbeUtils->send_msg("$output",1,"wget command isn't installed on current server, skip checking HTTP service"); -}else{ - $msg="HTTP service is ready on $mnip"; - if(ProbeUtils->isHttpReady("$mnip")){ - ProbeUtils->send_msg("$output",3,"$msg"); - }else{ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; +if ($?) { + probe_utils->send_msg("$output", "w", "wget tool isn't installed on current server, skip checking HTTP service."); + probe_utils->send_msg("$output", "d", "Please do probe again after installing wget"); +} else { + $msg = "HTTP service is ready on $mnip"; + if (probe_utils->is_http_ready("$mnip")) { + probe_utils->send_msg("$output", "o", "$msg"); + } else { + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; } } `which tftp > /dev/null 2>&1`; -if($?){ - ProbeUtils->send_msg("$output",1,"tftp command isn't installed in current server, skip checking tftp service"); -}else{ - $msg="TFTP service is ready on $mnip"; - if(ProbeUtils->isTftpReady("$mnip")){ - ProbeUtils->send_msg("$output",3,"$msg"); - }else{ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst =1; +if ($?) { + probe_utils->send_msg("$output", "w", "tftp tool isn't installed on current server, skip checking tftp service."); + probe_utils->send_msg("$output", "d", "Please do probe again after installing tftp"); +} else { + $msg = "TFTP service is ready on $mnip"; + if (probe_utils->is_tftp_ready("$mnip")) { + probe_utils->send_msg("$output", "o", "$msg"); + } else { + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; } } `which nslookup > /dev/null 2>&1`; -if($?){ - ProbeUtils->send_msg("$output",1,"nslookup command isn't installed in current server, skip checking DNS service"); -}else{ - $msg="DNS server is ready on $mnip"; - ProbeUtils->send_msg("$output",0,"hostname used to check DNS is $hostname, domain used to check DNS is $domain") if($verbose); - if(ProbeUtils->isDnsReady("$mnip","$hostname", "$domain")){ - ProbeUtils->send_msg("$output",3,"$msg"); - }else{ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst =1; +if ($?) { + probe_utils->send_msg("$output", "w", "nslookup tool isn't installed in current server, skip checking DNS service."); + probe_utils->send_msg("$output", "d", "Please do probe again after installing nslookup"); +} else { + $msg = "DNS server is ready on $mnip"; + probe_utils->send_msg("$output", "d", "Domain used to check DNS is $domain") if ($verbose); + + my $rc = 0; + { #very important brace to create a block + my $tmp = `chdef xcatmntest groups=all ip=$mnip`; + if ($?) { + returncmdoutput($tmp) if ($verbose); + probe_utils->send_msg("$output", "d", "Simulate a node by chdef failed") if ($verbose); + $rc = 1; + last; + } else { + probe_utils->send_msg("$output", "d", "Simulate a node xcatmntest to do DNS test") if ($verbose); + } + + probe_utils->send_msg("$output", "d", "To do 'makehosts xcatmntest'") if ($verbose); + $tmp = `makehosts xcatmntest`; + if ($?) { + returncmdoutput($tmp) if ($verbose); + probe_utils->send_msg("$output", "d", "makehosts xcatmntest failed") if ($verbose); + $rc = 1; + `rmdef xcatmntest`; + last; + } + + $tmp = `cat /etc/hosts |grep xcatmntest |grep $mnip`; + if ($?) { + probe_utils->send_msg("$output", "d", "makehosts failed to add test node xcatmntest to /etc/hosts") if ($verbose); + $rc = 1; + `rmdef xcatmntest`; + last; + } + + probe_utils->send_msg("$output", "d", "To do 'makedns -n xcatmntest'") if ($verbose); + $tmp = `makedns -V -n xcatmntest 2>&1`; + if ($?) { + returncmdoutput($tmp) if ($verbose); + probe_utils->send_msg("$output", "d", "makedns -n xcatmntest failed") if ($verbose); + $rc = 1; + `makehosts -d xcatmntest && rmdef xcatmntest`; + last; + } + + if (!probe_utils->is_dns_ready("$mnip", "xcatmntest", "$domain")) { + probe_utils->send_msg("$output", "d", "nslookup xcatmntest $mnip failed"); + $rc = 1; + `makehosts -d xcatmntest && rmdef xcatmntest`; + last; + } + + probe_utils->send_msg("$output", "d", "Start to clear simulate information for DNS test") if ($verbose); + $tmp = `makedns -d xcatmntest && makehosts -d xcatmntest && rmdef xcatmntest`; + returncmdoutput($tmp) if ($verbose); + } + + if ($rc) { + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; + } else { + probe_utils->send_msg("$output", "o", "$msg"); } } -my $os=ProbeUtils->getOS(); -my $leasefile=""; -$leasefile="/var/lib/dhcpd/dhcpd.leases" if($os =~ /redhat/i); -$leasefile="/var/lib/dhcp/db/dhcpd.leases" if($os =~ /sles/i); -$leasefile="/var/lib/dhcp/dhcpd.leases" if($os =~ /ubuntu/i); -$msg="The size of $leasefile is less than 100M"; -my $filesizetmp=`du -sb $leasefile`; -if($?){ - returncmdoutput($filesizetmp) if($verbose); - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; -}else{ - chomp($filesizetmp); - my ($size, $file) = split(" ", $filesizetmp); - ProbeUtils->send_msg("$output",0,"The size of $leasefile is $size bytes") if($verbose); - if($size > 104857600){ - ProbeUtils->send_msg("$output",1,"The size of $leasefile is more than 100M"); - }else{ - ProbeUtils->send_msg("$output",3,"$msg"); - } +my $os = probe_utils->get_os(); +my $leasefile = ""; +$leasefile = "/var/lib/dhcpd/dhcpd.leases" if ($os =~ /redhat/i); +$leasefile = "/var/lib/dhcp/db/dhcpd.leases" if ($os =~ /sles/i); +$leasefile = "/var/lib/dhcp/dhcpd.leases" if ($os =~ /ubuntu/i); +$msg = "The size of $leasefile is less than 100M"; +my $filesizetmp = `du -sb $leasefile`; +if ($?) { + returncmdoutput($filesizetmp) if ($verbose); + probe_utils->send_msg("$output", "f", "$msg"); + $rst = 1; +} else { + chomp($filesizetmp); + my ($size, $file) = split(" ", $filesizetmp); + probe_utils->send_msg("$output", "d", "The size of $leasefile is $size bytes") if ($verbose); + if ($size > 104857600) { + probe_utils->send_msg("$output", "w", "The size of $leasefile is more than 100M"); + } else { + probe_utils->send_msg("$output", "o", "$msg"); + } } -my $msg="DHCP service is ready on $mnip"; -my $rc=0; +my $msg = "DHCP service is ready on $mnip"; +my $rc = 0; { #very important brace to create a block - my $tmp=`chdef xcatmntest groups=all ip=$mnip mac=aa:aa:aa:aa:aa:aa`; - if($?){ - returncmdoutput($tmp) if($verbose); - ProbeUtils->send_msg("$output",0,"Simulate a node by chdef failed") if($verbose); - $rc=1; - last; - }else{ - ProbeUtils->send_msg("$output",0,"Simulate a node xcatmntest to do dhcp test") if($verbose); - } - - $tmp=`makehosts xcatmntest`; - if($?){ - returncmdoutput($tmp) if($verbose); - ProbeUtils->send_msg("$output",0,"makehosts xcatmntest failed") if($verbose); - $rc=1; - `rmdef xcatmntest`; - last; + my $tmp = `chdef xcatmntest groups=all ip=$mnip mac=aa:aa:aa:aa:aa:aa`; if ($?) { + returncmdoutput($tmp) if ($verbose); + probe_utils->send_msg("$output", "d", "Simulate a node by chdef failed") if ($verbose); + $rc = 1; + last; + } else { + probe_utils->send_msg("$output", "d", "Simulate a node xcatmntest to do dhcp test") if ($verbose); } + probe_utils->send_msg("$output", "d", "To do 'makehosts xcatmntest'") if ($verbose); + $tmp = `makehosts xcatmntest`; + if ($?) { + returncmdoutput($tmp) if ($verbose); + probe_utils->send_msg("$output", "d", "makehosts xcatmntest failed") if ($verbose); + $rc = 1; + `rmdef xcatmntest`; + last; + } + + $tmp = `cat /etc/hosts |grep xcatmntest |grep $mnip`; + if ($?) { + probe_utils->send_msg("$output", "d", "makehosts failed to add test node xcatmntest to /etc/hosts") if ($verbose); + $rc = 1; + `rmdef xcatmntest`; + last; + } + + probe_utils->send_msg("$output", "d", "To do 'makedhcp xcatmntest'") if ($verbose); $tmp = `makedhcp xcatmntest 2>&1`; - if($?){ - returncmdoutput($tmp) if($verbose); - ProbeUtils->send_msg("$output",0,"makedhcp xcatmntest failed") if($verbose); - $rc=1; + if ($?) { + returncmdoutput($tmp) if ($verbose); + probe_utils->send_msg("$output", "d", "makedhcp xcatmntest failed") if ($verbose); + $rc = 1; `makehosts -d xcatmntest && rmdef xcatmntest`; last; - } + } + probe_utils->send_msg("$output", "d", "To do 'makedhcp -q xcatmntest'") if ($verbose); $tmp = `makedhcp -q xcatmntest`; - if($?){ - returncmdoutput($tmp) if($verbose); - ProbeUtils->send_msg("$output",0,"makedhcp -q xcatmntest failed") if($verbose); - $rc=1; - `makedhcp -d xcatmntest && makehosts -d xcatmntest && rmdef xcatmntest`; + if ($?) { + returncmdoutput($tmp) if ($verbose); + probe_utils->send_msg("$output", "d", "makedhcp -q xcatmntest failed") if ($verbose); + $rc = 1; +`makedhcp -d xcatmntest && makehosts -d xcatmntest && rmdef xcatmntest`; last; } chomp($tmp); - if($tmp !~ /xcatmntest: ip-address = $mnip, hardware-address = aa:aa:aa:aa:aa:aa/){ - returncmdoutput($tmp) if($verbose); - ProbeUtils->send_msg("$output",0,"DHCP server's reply is wrong") if($verbose); - $rc=1; - `makedhcp -d xcatmntest && makehosts -d xcatmntest && rmdef xcatmntest`; + if ($tmp !~ /xcatmntest: ip-address = $mnip, hardware-address = aa:aa:aa:aa:aa:aa/) { + returncmdoutput($tmp) if ($verbose); + probe_utils->send_msg("$output", "d", "DHCP server's reply is wrong") if ($verbose); + $rc = 1; +`makedhcp -d xcatmntest && makehosts -d xcatmntest && rmdef xcatmntest`; last; - } - - ProbeUtils->send_msg("$output",0,"Start to clear simulate information for dhcp test") if($verbose); - $tmp=`makedhcp -d xcatmntest && makehosts -d xcatmntest && rmdef xcatmntest`; - returncmdoutput($tmp) if($verbose); + } + + probe_utils->send_msg("$output", "d", "Start to clear simulate information for dhcp test") if ($verbose); + $tmp = `makedhcp -d xcatmntest && makehosts -d xcatmntest && rmdef xcatmntest`; + returncmdoutput($tmp) if ($verbose); } -if($rc){ - ProbeUtils->send_msg("$output",2,"$msg"); - $rst=1; -}else{ - ProbeUtils->send_msg("$output",3,"$msg"); +if ($rc) { + probe_utils->send_msg("$output", "f", "$msg"); + probe_utils->send_msg("$output", "d", "please run 'makedhcp -n' if never run it before."); + $rst = 1; +} else { + probe_utils->send_msg("$output", "o", "$msg"); } -exit $rst ; +exit $rst; From 8ae74152c12035b65af143442a2639e5e19c7d1f Mon Sep 17 00:00:00 2001 From: hu-weihua Date: Tue, 31 May 2016 03:26:04 -0400 Subject: [PATCH 4/7] change code logic, format and file name depending on comments come from colleagues --- xCAT-probe/lib/perl/probe_utils.pm | 341 +++++++++++++++++++++++++++++ 1 file changed, 341 insertions(+) create mode 100644 xCAT-probe/lib/perl/probe_utils.pm diff --git a/xCAT-probe/lib/perl/probe_utils.pm b/xCAT-probe/lib/perl/probe_utils.pm new file mode 100644 index 000000000..5043e9d09 --- /dev/null +++ b/xCAT-probe/lib/perl/probe_utils.pm @@ -0,0 +1,341 @@ +package probe_utils; + +# IBM(c) 2016 EPL license http://www.eclipse.org/legal/epl-v10.html + +use strict; +use File::Path; +use File::Copy; + +#----------------------------------------- + +=head3 + Description: + Format output message depending on probe framework requirement + Format is [] : + The valid are debug, warning, failed and ok + Arguments: + output: where should output the message + num: the number of + 0: debug + 1: warning + 2: failed + 3: ok + msg: the information need to output + Returns: + 1 : Failed + 0 : success +=cut + +#---------------------------------------- +sub send_msg { + my $output = shift; + $output = shift if (($output) && ($output =~ /probe_utils/)); + my $tag = shift; + my $msg = shift; + my $flag; + + if ($tag eq "d") { + $flag = "[debug] :"; + } elsif ($tag eq "w") { + $flag = "[warning]:"; + } elsif ($tag eq "f") { + $flag = "[failed] :"; + } elsif ($tag eq "o") { + $flag = "[ok] :"; + } + + if ($output eq "stdout") { + print "$flag $msg\n"; + } else { + if (!open(LOGFILE, ">> $output")) { + return 1; + } + print LOGFILE "$flag $msg\n"; + close LOGFILE; + } + return 0; +} + +#------------------------------------------ + +=head3 + Description: + Test if a string is a IP address + Arguments: + addr: the string want to be judged + Returns: + 1 : yes + 0 : no +=cut + +#------------------------------------------ +sub is_ip_addr { + my $addr = shift; + $addr = shift if (($addr) && ($addr =~ /probe_utils/)); + return 0 unless ($addr); + return 0 if ($addr !~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); + return 0 if ($1 > 255 || $1 == 0 || $2 > 255 || $3 > 255 || $4 > 255); + return 1; +} + +#------------------------------------------ + +=head3 + Description: + Test if a IP address belongs to a network + Arguments: + net : network address, such like 10.10.10.0 + mask: network mask. suck like 255.255.255.0 + ip: a ip address + Returns: + 1 : yes + 0 : no +=cut + +#------------------------------------------ +sub is_ip_belong_to_net { + my $net = shift; + $net = shift if (($net) && ($net =~ /probe_utils/)); + my $mask = shift; + my $targetip = shift; + + return 0 if ($net !~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); + return 0 if ($mask !~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); + return 0 if (!is_ip_addr($targetip)); + + my $bin_mask = 0; + $bin_mask = (($1 + 0) << 24) + (($2 + 0) << 16) + (($3 + 0) << 8) + ($4 + 0) if ($mask =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); + + my $bin_ip = 0; + $bin_ip = (($1 + 0) << 24) + (($2 + 0) << 16) + (($3 + 0) << 8) + ($4 + 0) if ($targetip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); + + my $tmp_net = $bin_mask & $bin_ip; + + my $bin_net = 0; + $bin_net = (($1 + 0) << 24) + (($2 + 0) << 16) + (($3 + 0) << 8) + ($4 + 0) if ($net =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); + + return 0 if ($tmp_net != $bin_net); + return 1; +} + +#------------------------------------------ + +=head3 + Description: + Get distro name of current operating system + Arguments: + None + Returns: + A string, include value are sles, redhat and ubuntu +=cut + +#------------------------------------------ +sub get_os { + my $os = "unknown"; + my $output = `cat /etc/*release* 2>&1`; + if ($output =~ /suse/i) { + $os = "sles"; + } elsif ($output =~ /Red Hat/i) { + $os = "redhat"; + } elsif ($output =~ /ubuntu/i) { + $os = "ubuntu"; + } + + return $os; +} + +#------------------------------------------ + +=head3 + Description: + Test if a IP address is a static IP address + Arguments: + ip: a ip address + nic: the network adapter which ip belongs to + Returns: + 1 : yes + 0 : no +=cut + +#------------------------------------------ +sub is_static_ip { + my $ip = shift; + $ip = shift if (($ip) && ($ip =~ /probe_utils/)); + my $nic = shift; + my $os = get_os(); + my $rst = 0; + + if ($os =~ /redhat/) { + my $output1 = `cat /etc/sysconfig/network-scripts/ifcfg-$nic 2>&1 |grep -i IPADDR`; + my $output2 = `cat /etc/sysconfig/network-scripts/ifcfg-$nic 2>&1 |grep -i BOOTPROTO`; + $rst = 1 if (($output1 =~ /$ip/) && ($output2 =~ /static/i)); + } elsif ($os =~ /sles/) { + my $output1 = `cat /etc/sysconfig/network/ifcfg-$nic 2>&1 |grep -i IPADDR`; + my $output2 = `cat /etc/sysconfig/network/ifcfg-$nic 2>&1 |grep -i BOOTPROTO`; + $rst = 1 if (($output1 =~ /$ip/) && ($output2 =~ /static/i)); + } elsif ($os =~ /ubuntu/) { + my $output = `cat /etc/network/interfaces 2>&1|grep -E "iface\s+$nic"`; + $rst = 1 if ($output =~ /static/i); + } + return $rst; +} + +#------------------------------------------ + +=head3 + Description: + Test if SELinux is opened in current operating system + Arguments: + None + Returns: + 1 : yes + 0 : no +=cut + +#------------------------------------------ +sub is_selinux_enable { + if (-e "/usr/sbin/selinuxenabled") { + `/usr/sbin/selinuxenabled`; + if ($? == 0) { + return 1; + } else { + return 0; + } + } else { + return 0; + } +} + +#------------------------------------------ + +=head3 + Description: + Test if firewall is opened in current operating system + Arguments: + None + Returns: + 1 : yes + 0 : no +=cut + +#------------------------------------------ +sub is_firewall_open { + my $os = get_os(); + my $output; + my $rst = 0; + + if ($os =~ /redhat/) { + $output = `service iptables status 2>&1`; + $rst = 1 if ($output =~ /running/i); + } elsif ($os =~ /sles/) { + $output = `service SuSEfirewall2_setup status`; + $rst = 1 if ($output =~ /running/i); + } elsif ($os =~ /ubuntu/) { + $output = `ufw status`; + $rst = 1 if ($output =~ /Status: active/i); + } + return $rst; +} + +#------------------------------------------ + +=head3 + Description: + Test if http service is ready to use in current operating system + Arguments: + ip: http server's ip + Returns: + 1 : yes + 0 : no +=cut + +#------------------------------------------ +sub is_http_ready { + my $mnip = shift; + $mnip = shift if (($mnip) && ($mnip =~ /probe_utils/)); + + my $http = "http://$mnip/install/postscripts/syslog"; + rename("./syslog", "./syslog.org") if (-e "./syslog"); + + my $outputtmp = `wget $http 2>&1`; + my $rst = $?; + if (($outputtmp =~ /200 OK/) && (!$rst) && (-e "./syslog")) { + unlink("./syslog"); + rename("./syslog.org", "./syslog") if (-e "./syslog.org"); + return 1; + } else { + rename("./syslog.org", "./syslog") if (-e "./syslog.org"); + return 0; + } +} + +#------------------------------------------ + +=head3 + Description: + Test if tftp service is ready to use in current operating system + Arguments: + ip: tftp server's ip + Returns: + 1 : yes + 0 : no +=cut + +#------------------------------------------ +sub is_tftp_ready { + my $mnip = shift; + $mnip = shift if (($mnip) && ($mnip =~ /probe_utils/)); + + rename("/tftpboot/tftptestt.tmp", "/tftpboot/tftptestt.tmp.old") if (-e "/tftpboot/tftptestt.tmp"); + rename("./tftptestt.tmp", "./tftptestt.tmp.old") if (-e "./tftptestt.tmp"); + + system("touch /tftpboot/tftptestt.tmp"); + my $output = `tftp -4 -v $mnip -c get tftptestt.tmp`; + if ((!$?) && (-e "./tftptestt.tmp")) { + unlink("./tftptestt.tmp"); + rename("./tftptestt.tmp.old", "./tftptestt.tmp") if (-e "./tftptestt.tmp.old"); + rename("/tftpboot/tftptestt.tmp.old", "/tftpboot/tftptestt.tmp") if (-e "/tftpboot/tftptestt.tmp.old"); + return 1; + } else { + rename("./tftptestt.tmp.old", "./tftptestt.tmp") if (-e "./tftptestt.tmp.old"); + rename("/tftpboot/tftptestt.tmp.old", "/tftpboot/tftptestt.tmp") if (-e "/tftpboot/tftptestt.tmp.old"); + return 0; + } +} + + +#------------------------------------------ + +=head3 + Description: + Test if DNS service is ready to use in current operating system + Arguments: + ip: DNS server's ip + Returns: + 1 : yes + 0 : no +=cut + +#------------------------------------------ +sub is_dns_ready { + my $mnip = shift; + $mnip = shift if (($mnip) && ($mnip =~ /probe_utils/)); + my $hostname = shift; + my $domain = shift; + + my $output = `nslookup $mnip $mnip 2>&1`; + + if ($?) { + return 0; + } else { + chomp($output); + my $tmp = `echo "$output" |grep "Server:[\t\s]*$mnip" >/dev/null 2>&1`; + print "$tmp"; + return 0 if ($?); + $tmp = `echo "$output"|grep "name ="|grep "$hostname\.$domain" >/dev/null 2>&1`; + return 0 if ($?); + return 1; + } +} + +1; From 22d14e483fad9c5266b9f2a0534c8dbbdbb7e1a0 Mon Sep 17 00:00:00 2001 From: hu-weihua Date: Tue, 31 May 2016 03:36:37 -0400 Subject: [PATCH 5/7] delete this file, replace by probe_utils.pm --- xCAT-probe/lib/perl/ProbeUtils.pm | 317 ------------------------------ 1 file changed, 317 deletions(-) delete mode 100644 xCAT-probe/lib/perl/ProbeUtils.pm diff --git a/xCAT-probe/lib/perl/ProbeUtils.pm b/xCAT-probe/lib/perl/ProbeUtils.pm deleted file mode 100644 index 8304fb76b..000000000 --- a/xCAT-probe/lib/perl/ProbeUtils.pm +++ /dev/null @@ -1,317 +0,0 @@ -package ProbeUtils; -use strict; -use File::Path; -use File::Copy; - -#----------------------------------------- -=head3 - Description: - Format output message depending on probe framework requirement - Format is [] : - The valid are debug, warning, failed and ok - Attribute: list by input sequence - output: where should output the message - num: the number of - 0: debug - 1: warning - 2: failed - 3: ok - msg: the information need to output - Return value: - 1 : Failed - 0 : success -=cut -#---------------------------------------- -sub send_msg { - my $output=shift; - $output=shift if(($output) && ($output =~ /ProbeUtils/)); - my $num = shift; - my $msg = shift; - my $flag; - - if ($num == 0) { - $flag = "[debug] :"; - }elsif($num == 1) { - $flag = "[warning]:"; - }elsif($num == 2) { - $flag = "[failed] :"; - }elsif($num == 3){ - $flag = "[ok] :"; - } - if($output eq "stdout"){ - print "$flag $msg\n"; - }else{ - if (!open (LOGFILE, ">> $output") ) { - return 1; - } - print LOGFILE "$flag $msg\n"; - close LOGFILE; - } - return 0; -} - -#------------------------------------------ -=head3 - Description: - Judge if a string is a IP address - Attribute: list by input sequence - addr: the string want to be judged - Return value: - 1 : yes - 0 : no -=cut -#------------------------------------------ -sub isIpAddr{ - my $addr = shift; - $addr=shift if(($addr) && ($addr =~ /ProbeUtils/)); - return 0 unless($addr); - return 0 if($addr !~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); - return 0 if ($1 > 255 || $1 == 0 || $2 > 255 || $3 > 255 || $4 > 255); - return 1; -} - -#------------------------------------------ -=head3 - Description: - Judge if a IP address belongs to a network - Attribute: list by input sequence - net : network address, such like 10.10.10.0 - mask: network mask. suck like 255.255.255.0 - ip: a ip address - Return value: - 1 : yes - 0 : no -=cut -#------------------------------------------ -sub isIpBelongToNet{ - my $net=shift; - $net=shift if(($net) && ($net =~ /ProbeUtils/)); - my $mask=shift; - my $targetip=shift; - - return 0 if($net !~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); - return 0 if($mask !~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); - return 0 if(! isIpAddr($targetip)); - - my $bin_mask=0; - $bin_mask=(($1+0)<<24)+(($2+0)<<16)+(($3+0)<<8)+($4+0) if($mask =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); - - my $bin_ip=0; - $bin_ip=(($1+0)<<24)+(($2+0)<<16)+(($3+0)<<8)+($4+0) if($targetip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); - - my $tmp_net = $bin_mask&$bin_ip; - - my $bin_net=0; - $bin_net=(($1+0)<<24)+(($2+0)<<16)+(($3+0)<<8)+($4+0) if($net =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/); - - return 0 if( $tmp_net != $bin_net); - return 1; -} - -#------------------------------------------ -=head3 - Description: - Get distro name of current operating system - Attribute: list by input sequence - None - Return value: - A string, include value are sles, redhat and ubuntu -=cut -#------------------------------------------ -sub getOS{ - my $os="unknown"; - my $output = `cat /etc/*release* 2>&1`; - if($output =~ /suse/i){ - $os="sles"; - }elsif($output =~ /Red Hat/i){ - $os="redhat"; - }elsif($output =~ /ubuntu/i){ - $os="ubuntu"; - } - - return $os; -} - -#------------------------------------------ -=head3 - Description: - Judge if a IP address is a static IP address - Attribute: list by input sequence - ip: a ip address - nic: the network adapter which ip belongs to - Return value: - 1 : yes - 0 : no -=cut -#------------------------------------------ -sub isStaticIp{ - my $ip=shift; - $ip=shift if(($ip) && ($ip =~ /ProbeUtils/)); - my $nic=shift; - my $os = getOS(); - my $rst=0; - - if($os =~ /redhat/){ - my $output1=`cat /etc/sysconfig/network-scripts/ifcfg-$nic 2>&1 |grep -i IPADDR`; - my $output2=`cat /etc/sysconfig/network-scripts/ifcfg-$nic 2>&1 |grep -i BOOTPROTO`; - $rst=1 if(($output1 =~ /$ip/) && ($output2 =~ /static/i)); - }elsif($os =~ /sles/){ - my $output1=`cat /etc/sysconfig/network/ifcfg-$nic 2>&1 |grep -i IPADDR`; - my $output2=`cat /etc/sysconfig/network/ifcfg-$nic 2>&1 |grep -i BOOTPROTO`; - $rst=1 if(($output1 =~ /$ip/) && ($output2 =~ /static/i)); - }elsif($os =~/ubuntu/){ - my $output=`cat /etc/network/interfaces 2>&1|grep -E "iface\s+$nic"`; - $rst=1 if($output =~ /static/i); - } - return $rst; -} - -#------------------------------------------ -=head3 - Description: - Judge if SELinux is opened in current operating system - Attribute: list by input sequence - None - Return value: - 1 : yes - 0 : no -=cut -#------------------------------------------ -sub isSelinuxEnable{ - if (-e "/usr/sbin/selinuxenabled") { - `/usr/sbin/selinuxenabled`; - if ($? == 0) { - return 1; - } else { - return 0; - } - } else { - return 0; - } -} - -#------------------------------------------ -=head3 - Description: - Judge if firewall is opened in current operating system - Attribute: list by input sequence - None - Return value: - 1 : yes - 0 : no -=cut -#------------------------------------------ -sub isFirewallOpen{ - my $os =getOS(); - my $output; - my $rst=0; - - if($os =~ /redhat/){ - $output=`service iptables status 2>&1`; - $rst=1 if($output =~ /running/i); - }elsif($os =~ /sles/){ - $output=`service SuSEfirewall2_setup status`; - $rst=1 if($output =~ /running/i); - }elsif($os =~/ubuntu/){ - $output=`ufw status`; - $rst=1 if($output =~ /Status: active/i); - } - return $rst; -} - -#------------------------------------------ -=head3 - Description: - Judge if http service is ready to use in current operating system - Attribute: list by input sequence - ip: http server's ip - Return value: - 1 : yes - 0 : no -=cut -#------------------------------------------ -sub isHttpReady{ - my $mnip=shift; - $mnip=shift if(($mnip) && ($mnip =~ /ProbeUtils/)); - - my $http = "http://$mnip/install/postscripts/efibootmgr"; - rename("./efibootmgr", "./efibootmgr.org") if(-e "./efibootmgr"); - - my $outputtmp = `wget $http 2>&1`; - my $rst =$?; - if(($outputtmp =~ /200 OK/) && (!$rst) && (-e "./efibootmgr")){ - unlink("./efibootmgr"); - rename("./efibootmgr.org", "./efibootmgr") if(-e "./efibootmgr.org"); - return 1; - }else{ - rename("./efibootmgr.org", "./efibootmgr") if(-e "./efibootmgr.org"); - return 0; - } -} - -#------------------------------------------ -=head3 - Description: - Judge if tftp service is ready to use in current operating system - Attribute: list by input sequence - ip: tftp server's ip - Return value: - 1 : yes - 0 : no -=cut -#------------------------------------------ -sub isTftpReady{ - my $mnip=shift; - $mnip=shift if(($mnip) && ($mnip =~ /ProbeUtils/)); - - rename("/tftpboot/tftptestt.tmp", "/tftpboot/tftptestt.tmp.old") if(-e "/tftpboot/tftptestt.tmp"); - rename("./tftptestt.tmp", "./tftptestt.tmp.old") if(-e "./tftptestt.tmp"); - - system("touch /tftpboot/tftptestt.tmp"); - my $output = `tftp -4 -v $mnip -c get tftptestt.tmp`; - if((!$?) && (-e "./tftptestt.tmp")){ - unlink("./tftptestt.tmp"); - rename("./tftptestt.tmp.old", "./tftptestt.tmp") if(-e "./tftptestt.tmp.old"); - rename("/tftpboot/tftptestt.tmp.old","/tftpboot/tftptestt.tmp") if(-e "/tftpboot/tftptestt.tmp.old"); - return 1; - }else{ - rename("./tftptestt.tmp.old", "./tftptestt.tmp") if(-e "./tftptestt.tmp.old"); - rename("/tftpboot/tftptestt.tmp.old","/tftpboot/tftptestt.tmp") if(-e "/tftpboot/tftptestt.tmp.old"); - return 0; - } -} - - -#------------------------------------------ -=head3 - Description: - Judge if DNS service is ready to use in current operating system - Attribute: list by input sequence - ip: DNS server's ip - Return value: - 1 : yes - 0 : no -=cut -#------------------------------------------ -sub isDnsReady{ - my $mnip=shift; - $mnip=shift if(($mnip) && ($mnip =~ /ProbeUtils/)); - my $hostname=shift; - my $domain=shift; - - my $output = `nslookup $mnip $mnip 2>&1`; - - if($?){ - return 0; - }else{ - chomp($output); - my $tmp=`echo "$output" |grep "Server:[\t\s]*$mnip" >/dev/null 2>&1`; - print "$tmp"; - return 0 if($?); - $tmp = `echo "$output"|grep "name ="|grep "$hostname\.$domain" >/dev/null 2>&1`; - return 0 if($?); - return 1; - } -} - -1; From 3bac56665b69bf0a4603d360c795d92a523e4623 Mon Sep 17 00:00:00 2001 From: hu-weihua Date: Tue, 31 May 2016 05:12:15 -0400 Subject: [PATCH 6/7] change the way of filtering NIC --- xCAT-probe/subcmds/xcatmn | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/xCAT-probe/subcmds/xcatmn b/xCAT-probe/subcmds/xcatmn index 125a4b881..aacf18b84 100755 --- a/xCAT-probe/subcmds/xcatmn +++ b/xCAT-probe/subcmds/xcatmn @@ -48,7 +48,6 @@ if ( !GetOptions("--help|h" => \$help, "t" => \$test, "V" => \$verbose, - "f=s" => \$output, "n=s" => \$installnic)) { probe_utils->send_msg("$output", "f", "Invalid parameter for $proname"); @@ -66,7 +65,7 @@ if ($help) { } if ($test) { - probe_utils->send_msg("$output", "o", "After xcat installation, use this command to check if xcat has been installed correctly and is ready for use. Before using this command , please install tftp, nslookup and wget commands ahead. The platform supported are redhat, sles and ubuntu."); + probe_utils->send_msg("$output", "o", "After xcat installation, use this command to check if xcat has been installed correctly and is ready for use. Before using this command, please install tftp, nslookup and wget commands ahead. The platform supported are redhat, sles and ubuntu."); exit 0; } @@ -77,7 +76,7 @@ if (!defined($installnic)) { } my $msg = "NIC $installnic exists on current server"; -my $nics = `ip addr show 2>&1 | grep -E '^[1-9]' | cut -d: -f2 |grep $installnic`; +my $nics = `ip addr show $installnic >/dev/null 2>&1`; if ($?) { probe_utils->send_msg("$output", "f", "$msg"); probe_utils->send_msg("$output", "d", "Please use 'ip addr show' to check if there is NIC named $installnic on current server"); @@ -87,7 +86,7 @@ if ($?) { } $msg = "Get ip address of NIC $installnic"; -my $mnip = `ip addr show |grep -A2 $installnic: | awk -F" " '/inet/ {print \$2}'|awk -F"/" '{print \$1}'`; +my $mnip = `ip addr show $installnic | awk -F" " '/inet / {print \$2}'|awk -F"/" '{print \$1}'`; chomp($mnip); if (!defined($mnip) || ($mnip eq "")) { probe_utils->send_msg("$output", "f", "$msg"); @@ -199,7 +198,7 @@ if (probe_utils->is_ip_addr("$masteripinsite")) { } if ($mnip) { - $msg = "The IP of $installnic equals the value of 'master' in 'site' table"; + $msg = "The IP $mnip of $installnic equals the value of 'master' in 'site' table"; if ($mnip eq $masteripinsite) { probe_utils->send_msg("$output", "o", "$msg"); } else { From 1665d4146d8c408de33cab11be153a8ac7c74b28 Mon Sep 17 00:00:00 2001 From: hu-weihua Date: Tue, 31 May 2016 05:14:24 -0400 Subject: [PATCH 7/7] change the description of send_msg function --- xCAT-probe/lib/perl/probe_utils.pm | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/xCAT-probe/lib/perl/probe_utils.pm b/xCAT-probe/lib/perl/probe_utils.pm index 5043e9d09..9a9b6f29e 100644 --- a/xCAT-probe/lib/perl/probe_utils.pm +++ b/xCAT-probe/lib/perl/probe_utils.pm @@ -16,10 +16,10 @@ use File::Copy; Arguments: output: where should output the message num: the number of - 0: debug - 1: warning - 2: failed - 3: ok + d: debug + w: warning + f: failed + o: ok msg: the information need to output Returns: 1 : Failed