daisycloud-core/code/pxe_server_install/pxe_server_check

381 lines
14 KiB
Bash
Executable File

#!/bin/bash
function display_install_status
{
echo "===================display install status==================="
begin_time="${1}"
discovered_macaddr="${2}"
installing_macaddr="${3}"
installed_macaddr="${4}"
interface="${5}"
dhcpd_log=`journalctl --unit=dhcpd --since="$begin_time"`
request_macaddr=`echo -e "$dhcpd_log" | grep "DHCPDISCOVER" | grep "via $interface" | awk -F'via' '{print $1}' | awk '{print $NF}' | sort -u`
offer_macaddr=`echo -e "$dhcpd_log" | grep "DHCPOFFER" | grep "via $interface" | awk '{print $10}' | sort -u`
echo "request for dhcp hosts:"
for each in $request_macaddr
do
echo " $each"
done
echo
echo "discovered hosts:"
for each in $discovered_macaddr
do
echo " $each"
done
echo
echo "installing hosts:"
for each in $installing_macaddr
do
echo " $each"
done
echo
echo "installed hosts:"
for each in $installed_macaddr
do
echo " $each"
done
echo
}
function check_server_rpms
{
echo "checking server rpms..."
[ `rpm -qa xinetd` ] || { echo -e "\e[31;1m[Warning]\e[0mrpm xinetd not installed"; exit 1; }
[ `rpm -qa tftp-server` ] || { echo -e "\e[31;1m[Warning]\e[0mrpm tftp-server not installed"; exit 1; }
[ `rpm -qa tftp` ] || { echo -e "\e[31;1m[Warning]\e[0mrpm tftp not installed"; exit 1; }
[ `rpm -qa ipxe-roms-qemu` ] || { echo -e "\e[31;1m[Warning]\e[0mrpm ipxe-roms-qemu not installed"; exit 1; }
[ `rpm -qa syslinux` ] || { echo -e "\e[31;1m[Warning]\e[0mrpm syslinux not installed"; exit 1; }
[ `rpm -qa dhcp-common` ] || { echo -e "\e[31;1m[Warning]\e[0mrpm dhcp-common not installed"; exit 1; }
[ `rpm -qa dhcp` ] || { echo -e "\e[31;1m[Warning]\e[0mrpm dhcp not installed"; exit 1; }
[ `rpm -qa ntpdate` ] || { echo -e "\e[31;1m[Warning]\e[0mrpm ntpdate not installed"; exit 1; }
}
function check_service_status
{
echo "checking service status..."
local status=""
status=`systemctl is-active iptables`
if [[ "$status" == "active" || "$status" == "reloading" || "$status" == "activating" ]];then
echo -e "\e[31;1m[Warning]\e[0miptables.service is active, use 'systemctl status iptables' for details"
exit 1
fi
status=""
status=`systemctl is-active firewalld`
if [[ "$status" == "active" || "$status" == "reloading" || "$status" == "activating" ]];then
echo -e "\e[31;1m[Warning]\e[0mfirewalld.service is active, use 'systemctl status firewalld' for details"
exit 1
fi
status=""
status=`systemctl is-active xinetd`
if [[ "$status" != "active" && "$status" != "reloading" && "$status" != "activating" ]];then
echo -e "\e[31;1m[Warning]\e[0mxinetd.service is inactive, use 'systemctl status xinetd' for details"
exit 1
fi
status=""
status=`systemctl is-active nfs-server`
if [[ "$status" != "active" && "$status" != "reloading" && "$status" != "activating" ]];then
echo -e "\e[31;1m[Warning]\e[0mnfs-server.service is inactive, use 'systemctl status nfs-server' for details"
exit 1
fi
status=""
status=`systemctl is-active dhcpd`
if [[ "$status" != "active" && "$status" != "reloading" && "$status" != "activating" ]];then
echo -e "\e[31;1m[Warning]\e[0mdhcpd.service is inactive, use 'systemctl status dhcpd' for details"
exit 1
fi
}
function check_multi_dhcp_server
{
echo "checking other dhcp server..."
local interface=$1
if [[ `systemctl list-unit-files |grep -w dhcpd.service` != "" ]]; then
[[ `systemctl is-enabled dhcpd.service` != "enabled" ]] && dhcpd_is_enable="disabled" || dhcpd_is_enable="enabled"
[[ `systemctl is-active dhcpd.service` != "active" ]] && dhcpd_is_active="inactive" || dhcpd_is_active="active"
fi
systemctl disable dhcpd.service >/dev/null
systemctl stop dhcpd.service >/dev/null
tcpdump -i $interface -e udp port 67 -n -vv 1>./dhcplog 2>/dev/null &
nmap -e $interface --script broadcast-dhcp-discover &> /dev/null
sleep 17
pid=`ps -def | grep "tcpdump -i $interface -e udp port 67 -n -vv" | grep -v grep |grep tcpdump | awk '{print $2}'`
[[ -n "$pid" ]] && kill $pid > /dev/null
sleep 3
while :
do
pid=`ps -def | grep "tcpdump -i $interface -e udp port 67 -n -vv" | grep -v grep |grep tcpdump | awk '{print $2}'`
[[ -n "$pid" ]] && kill $pid > /dev/null && sleep 2 || break
done
server_ips=`cat ./dhcplog | grep "Server-ID Option 54" | awk -F ': ' '{print $2}'`
if [[ -n "$server_ips" ]];then
local i=1
echo -e "\e[31;1m[Warning]\e[0mthere are other dhcp servers in the network"
for one in $server_ips
do
server_ip=`echo $server_ips | awk -v N=$i '{printf "%-15s\n", $N}'`
echo "$i. $server_ip"
let i++
done
exit 1
fi
rm -f ./dhcplog
[[ $dhcpd_is_enable == "enabled" ]] && systemctl enable dhcpd.service >/dev/null
[[ $dhcpd_is_active == "active" ]] && systemctl restart dhcpd.service >/dev/null
}
#检查网口是不是有ip并且为RUNNING状态
function check_interface
{
echo "checking interface ${1}..."
local nic4dhcp="${1}"
local ipaddr="${2}"
status=`ifconfig "${nic4dhcp}:100" 2>/dev/null | grep "RUNNING" -A 2 | grep "$ipaddr"`
if [ -z "$status" ];then
echo -e "\e[31;1m[Warning]\e[0minterface "${nic4dhcp}:100" is not up!"
exit 1
fi
}
function check_ip_conflict
{
echo "checking ip conflict..."
local interface=$1
local ip_addr=$2
local result=`arping -w 2 -D -I ${interface} ${ip_addr}`
if [ -n "`echo "${result}" | grep "Unicast reply from ${ip_addr}"`" ];then
echo -e "\e[31;1m[Warning]\e[0mip-address conflicts, there is other interface with ip ${ip_addr}"
echo -e "${result}"
exit 1
fi
}
function check_file_and_directory
{
echo "checking file and directory..."
local MACADDR=`echo -e "$1" | sed s/:/-/g`
local TFTPDIR=/tftpboot
local INSTALLSHARE=/home/install_share
local LINUXINSTALL=/linuxinstall
local flag=0
[ -f $TFTPDIR/initrd.img ] || echo -e "\e[31;1m[Warning]\e[0mfile $TFTPDIR/initrd.img missing"
[ -f $TFTPDIR/vmlinuz ] || echo -e "\e[31;1m[Warning]\e[0mfile $TFTPDIR/vmlinuz missing"
[ -f $TFTPDIR/pxelinux.0 ] || echo -e "\e[31;1m[Warning]\e[0mfile $TFTPDIR/pxelinux.0 missing"
[ -f $TFTPDIR/pxelinux.cfg/default ] || echo -e "\e[31;1m[Warning]\e[0mfile $TFTPDIR/pxelinux.cfg/default missing"
if [ -n "${MACADDR}" ];then
for each in $MACADDR
do
#if [ -n "`cat $INSTALL_LOG | grep "MACADDR=${each}"`" ];then
[ -f $TFTPDIR/$each/initrd.img ] || echo -e "\e[31;1m[Warning]\e[0mfile $TFTPDIR/$each/initrd.img missing"
[ -f $TFTPDIR/$each/vmlinuz ] || echo -e "\e[31;1m[Warning]\e[0mfile $TFTPDIR/$each/vmlinuz missing"
[ -f $TFTPDIR/pxelinux.cfg/01-${each} ] || echo -e "\e[31;1m[Warning]\e[0mfile $TFTPDIR/pxelinux.cfg/01-$each missing"
[ -f $INSTALLSHARE/$each/os.json ] || echo -e "\e[31;1m[Warning]\e[0mfile $INSTALLSHARE/$each/os.json missing"
[ -f $INSTALLSHARE/$each/pxe_kickstart.cfg ] || echo -e "\e[31;1m[Warning]\e[0mfile $INSTALLSHARE/$each/pxe_kickstart.cfg missing"
[ -d $INSTALLSHARE/$each/custom ] || echo -e "\e[31;1m[Warning]\e[0mdirectory $INSTALLSHARE/$each/custom missing"
[ -d $INSTALLSHARE/$each/usrdata ] || echo -e "\e[31;1m[Warning]\e[0mdirectory $INSTALLSHARE/$each/usrdata missing"
#fi
done
fi
}
function check_nfs_server
{
echo "checking nfs server..."
local result=`showmount -e`
local flag=0
[ "`echo -e "$result" | grep "/home/install_share"`" ] || flag=1
[ "`echo -e "$result" | grep "/tftpboot"`" ] || flag=1
[ "`echo -e "$result" | grep "/linuxinstall"`" ] || flag=1
[ $flag -eq 1 ] && { echo -e "\e[31;1m[Warning]\e[0mnfs server failed, directory is not shared, use 'showmount -e' for details"; exit 1; }
}
function check_os_json_file
{
echo "checking os json file..."
local MACADDR=`echo -e "$1" | sed s/:/-/g`
if [ -n "${MACADDR}" ];then
for each in $MACADDR
do
#if [ -n "`cat $INSTALL_LOG | grep "MACADDR=${each}"`" ];then
local JSON_FILE=/home/install_share/${each}/os.json
[ -e $JSON_FILE ] || return 1
local flag=0
local result=`cat $JSON_FILE | grep disk_list`
[ -n "`echo "$result" | grep "multi"`" ] && { echo -e "\e[31;1m[Warning]\e[0mdisk list in file $JSON_FILE error, ${result}"; exit 1; }
#fi
done
fi
}
#获取daisy服务器的网口和ip地址
function get_server_interface_and_ip
{
local Log_File="$INSTALL_LOG"
local begin_flag="begin pxe server install"
local end_flag="end pxe server install"
log_fragment=""
begin_line=`cat $Log_File -n | grep "$begin_flag" | tail -n 1 | awk '{print $1}'`
end_line=`cat $Log_File -n | grep "$end_flag" | tail -n 1 | awk '{print $1}'`
[ -z "$begin_line" ] && echo "[error]no pxe server install log" && exit 0
[ -z "$end_line" ] && echo "[error]no pxe server install log" && exit 0
if [ "$begin_line" -gt "$end_line" ];then
echo "[error]no pxe server install log"
exit 0
fi
log_fragment=`sed -n "$begin_line,$end_line p" ${Log_File}`
ethname_l=`echo -e "${log_fragment}" | grep "ethname_l" | tail -n 1 | awk -F '=' '{print $2}'`
ip_addr_l=`echo -e "${log_fragment}" | grep "ip_addr_l" | tail -n 1 | awk -F '=' '{print $2}'`
}
#获取目标机的mac地址
function get_macaddr
{
local result=`ironic physical-node-list`
local mac_addr=`echo -e "$result" | grep -E [0-9,A-F,a-f]{2}: | awk '{print $6}'`
#local mac_addr=`sed -n "$end_line,$ p" ${Log_File} | grep -E MACADDR=.*[0-9,A-F,a-f]{2}$ |awk -F 'MACADDR=' '{print $2}'`
#macaddr=""
#echo -e "$mac_addr"
discovered_macaddr=`echo -e "${mac_addr}" | sort`
installing_macaddr=`echo -e "${mac_addr}" | sort`
if [ -n "${mac_addr}" ];then
for each in $mac_addr
do
#if [ -z "`sed -n "$end_line,$ p" ${Log_File} | grep -A1 "${each}" | tail -n 1 | grep "install complete, will clean files"`" ];then
temp_mac=`echo ${each} | sed s/:/-/g`
if [[ "`cat $OS_TABLE_LOG | grep ${temp_mac} | awk '{print $2}'`" == "100" ]];then
if [ -z "$installed_macaddr" ];then
installed_macaddr=${each}
else
installed_macaddr="$installed_macaddr\n${each}"
fi
installing_macaddr=`echo -e "${installing_macaddr}" | sed /${each}/d`
fi
#temp_mac=`echo ${each} | sed s/:/-/g`
#echo installing_macaddr=$installing_macaddr
if [ -z "`cat $INSTALL_LOG | grep "MACADDR=${temp_mac}"`" ];then
installing_macaddr=`echo -e "${installing_macaddr}" | sed /${each}/d`
#echo installing_macaddr=$installing_macaddr
fi
done
#installing_macaddr="${mac_addr}"
#echo $macaddr
#echo discovered_macaddr=$discovered_macaddr
#echo installed_macaddr=$installed_macaddr
#echo installing_macaddr=$installing_macaddr
fi
}
#获取daisy服务器安装的时间
function get_server_install_time
{
local begin_time=`cat $INSTALL_LOG | grep "begin pxe server install" | tail -n 1 | cut -c1-15`
server_install_time=`date -d "${begin_time}" "+%Y-%m-%d %H:%M:%S"`
}
#从环境中获取配置文件,供之后的检查函数使用
function get_config
{
get_server_interface_and_ip
get_macaddr
get_server_install_time
}
function check_daisy_server
{
display_install_status "$server_install_time" "$discovered_macaddr" "$installing_macaddr" "$installed_macaddr" "${ethname_l}"
echo "===================checking daisy server==================="
echo
check_server_rpms
check_service_status
check_interface "${ethname_l}" "${ip_addr_l}"
check_multi_dhcp_server "${ethname_l}"
check_ip_conflict "${ethname_l}" "${ip_addr_l}"
check_nfs_server
check_file_and_directory "${installing_macaddr}"
check_os_json_file "${installing_macaddr}"
echo
echo -e "\e[31;1mif the check passed but still with errors, please check the network\e[0m"
}
function collect_log
{
begin_time="$server_install_time"
local LOGDIR=/var/log/tfg/pxechecktools
[ -d ${LOGDIR} ] && rm -rf ${LOGDIR}
mkdir -p ${LOGDIR}
cp /var/lib/dhcpd/dhcpd.leases ${LOGDIR}
journalctl --unit=dhcpd --since="$begin_time" > ${LOGDIR}/journalctl.dhcpd.log
}
#检查运行脚本需要的rpm包
function prepare_for_tools
{
rpm -qi nmap >/dev/null
if [ $? -ne 0 ];then
yum -y install nmap >/dev/null 2>&1 || { echo "[Error] Please install rpm of nmap and run the script again"; exit 1; }
fi
rpm -qi tcpdump >/dev/null
if [ $? -ne 0 ];then
yum -y install tcpdump >/dev/null 2>&1 || { echo "[Error] Please install rpm of tcpdump and run the script again"; exit 1; }
fi
rpm -qi nfs-utils >/dev/null
if [ $? -ne 0 ];then
yum -y install nfs-utils >/dev/null 2>&1 || { echo "[Error] Please install rpm of nfs-utils and run the script again"; exit 1; }
fi
}
#===============================================================================
#=====================================start=====================================
#===============================================================================
INSTALL_LOG=/var/log/pxe_install.log
OS_TABLE_LOG=/var/log/pxe_os_table.log
[ -e /root/daisyrc_admin ] && source /root/daisyrc_admin || { echo "[error]file /root/daisyrc_admin missing"; exit 1; }
prepare_for_tools
get_config
collect_log
check_daisy_server