脚本任务
监测主机是否联通正常
检查安装操作系统版本是否和需求一致
检查CPU是否满足规格要求
检查内存是否满足规格要求
检查数据磁盘是否满足规格要求
检查操作系统分区目录大小是否满足需求
检查集群主机时间是否一致
0.配置文件准备及脚本变量初始化
编写config.ini存放主机配置文件
[hosts]
10.0.1.10 node01
10.0.1.20 node02
10.0.1.30 node03
[root_password]
000000
[os_version]
7.9.2009
[cpu_cores]
10
[memory_kb]
1048576
[data_disk_number]
1
[root_partition_size]
100
[time_sync_diff]
30
编写 env-check.sh 取出ip和 密码
#!/bin/bash
#
if [ ! -e ./config.ini ];then
echo "config.ini is not exist...please check.."
EXIT 1
fi
HOSTS_IP=`cat config.ini | sed -n '/\[hosts\]/,/\[.*\]/p' | grep -v "\[.*\]" |grep -v ^$ |grep -v ^# |awk '{print $1}'`
ROOT_PATH=`cat config.ini | sed -n '/\[root_password\]/,/\[.*\]/p' | grep -v "\[.*\]" |grep -v ^$ |grep -v ^#`
1.监测主机是否联通正常
function check_host_online
{
echo "++++++++++++++++监测主机是否联通正常++++++++++++++++"
for host in $HOSTS_IP;do
ping -w 3 $host &> /dev/null
if [ $? -eq 0 ];then
echo "检测主机 $host 连通性通过"
else
echo "检测主机 $host 无法连通"
ping_faild_hosts="$ping_faild_hosts $host"
fi
done
if [[ "$ping_faild_hosts" == "" ]];then
echo "1.使用ping对主机连通性检查,全部通过"
else
echo "1.使用ping对主机连通性检查,未全部通过:$ping_faild_hosts"
exit 1
fi
}
循环遍历主机列表:通过for
循环遍历HOSTS_IP
变量中包含的所有主机IP地址。
执行ping命令:对每个主机执行ping
命令,-w 3
参数指定ping操作持续3秒,&> /dev/null
将标准输出和标准错误都重定向到/dev/null
,即不显示ping命令的输出。
检查ping命令的返回值:使用$?
获取上一个命令(即ping命令)的退出状态。如果返回值为0(-eq 0
),表示ping命令成功,即主机连通性通过。
打印连通性结果:如果主机连通性通过,则打印"检测主机 $host 连通性通过";如果不通,则打印"检测主机 $host 无法连通",并将该主机IP添加到ping_faild_hosts
变量中,用于记录失败的主机。
检查是否有主机未通过连通性检查:循环结束后,使用if
语句检查ping_faild_hosts
变量是否为空。如果为空,表示所有主机的连通性检查都通过了,打印"1.使用ping对主机连通性检查,全部通过"。如果不为空,表示有主机未通过连通性检查,打印"1.使用ping对主机连通性检查,未全部通过:$ping_faild_hosts",并退出脚本,返回状态为1。
退出脚本:如果检测到有主机连通性失败,脚本将通过exit 1
命令退出,并返回状态码1,表示脚本执行失败。
2.检查安装操作系统版本是否和需求一致
先编写检查主机是否能登录
function verify_password
{
if [ $# -lt 2 ];then
echo "Usage: verify_password IP root_password"
exit 1
fi
sshpass -p$2 ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 root@$1 "df -h" &> /dev/null
if [ $? -ne 0 ];then
echo "尝试登录ssh主机$1 失败,检查后重试"
return 255
else
return 0
fi
}
编写检查操作系统版本是否一致
grep $OS_VERSION /etc/redhat-release
OS_VERSION=`cat config.ini | sed -n '/\[os_version\]/,/\[.*\]/p' | grep -v "\[.*\]" |grep -v ^$ |grep -v ^#`
function check_os_version
{
for host in $HOSTS_IP;do
verify_password $host $ROOT_PASS
if [ $? -eq 0 ];then
sshpass -p$ROOT_PASS ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 root@$host "grep $OS_VERSION /etc/redhat-release" &> /dev/null
if [ $? -ne 0 ];then
echo "检查主机$host 版本,与目标不一致,检查不通过"
os_failed_hosts="$os_failed_hosts $host"
else
echo "检查主机$host 版本,与目标一致,检查通过"
fi
else
os_failed_hosts="$os_failed_hosts $host"
fi
done
if [[ "$os_failed_hosts" == "" ]];then
echo "2.对主机版本检查,全部通过"
else
echo "2.对主机版本检查,未全部通过: $os_failed_hosts"
exit 1
fi
}
循环遍历主机列表:通过for
循环遍历HOSTS_IP
变量中包含的所有主机IP地址。
验证密码:对每个主机调用verify_password
函数,传入主机IP和root用户的密码$ROOT_PASS
,以验证SSH连接的密码是否正确。
检查SSH连接:如果verify_password
函数返回状态码0(表示密码验证成功),则继续执行;否则,跳过当前主机。
SSH连接并检查操作系统版本:使用sshpass
工具和提供的root用户密码通过SSH连接到主机,并执行grep
命令检查/etc/redhat-release
文件中是否包含特定的操作系统版本字符串$OS_VERSION
。这里使用了-o StrictHostKeyChecking=no
选项来禁用严格的主机密钥检查,这在自动化脚本中常用于避免每次连接时的手动确认。
记录检查结果:如果操作系统版本检查未通过(即grep
命令返回非0状态码),则打印该主机的检查未通过信息,并将主机IP添加到os_failed_hosts
变量中;如果检查通过,则打印该主机的检查通过信息。
汇总检查结果:循环结束后,使用if
语句检查os_failed_hosts
变量是否为空。如果为空,表示所有主机的操作系统版本检查都通过了,打印"2.对主机版本检查,全部通过"。如果不为空,表示有主机的操作系统版本检查未通过,打印"2.对主机版本检查,未全部通过:$os_faild_hosts",并退出脚本,返回状态码1。
退出脚本:如果检测到有主机操作系统版本不一致,脚本将通过exit 1
命令退出,并返回状态码1,表示脚本执行失败。
3.检查CPU是否满足规格要求
cat /proc/cpuinfo |grep ^processor |sort |uniq |wc -l
CPU_CORES=`cat config.ini | sed -n '/\[cpu_cores\]/,/\[.*\]/p' | grep -v "\[.*\]" |grep -v ^$ |grep -v ^#`
function check_cpu_cores
{
for host in $HOSTS_IP;do
verify_password $host $ROOT_PASS
if [ $? -eq 0 ];then
DST_CPU_CORES=`sshpass -p$ROOT_PASS ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 root@$host "cat /proc/cpuinfo |grep ^processor |sort |uniq |wc -l"` &> /dev/null
if [ $DST_CPU_CORES -lt $CPU_CORES ];then
echo "检查主机CPU逻辑核心数量,检查不通过"
cpu_failed_hosts="$cpu_failed_hosts $host"
else
echo "检查主机CPU逻辑核心数量,检查通过"
fi
else
cpu_failed_hosts="$cpu_failed_hosts $host"
fi
done
if [[ "$cpu_failed_hosts" == "" ]];then
echo "3.对主机cpu检查,全部通过"
else
echo "3.对主机cpu检查,未全部通过: $cpu_failed_hosts"
exit 1
fi
4.检查内存是否满足规格要求
cat /proc/meminfo |grep MemTotal| awk '{print $2}'
TOTAL_MEMORY=`cat config.ini | sed -n '/\[memory_kb\]/,/\[.*\]/p' | grep -v "\[.*\]" |grep -v ^$ |grep -v ^#`
function check_memory
{
for host in $HOSTS_IP;do
verify_password $host $ROOT_PASS
if [ $? -eq 0 ];then
DST_TOTAL_MEMORY=`sshpass -p$ROOT_PASS ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 root@$host "cat /proc/meminfo |grep MemTotal| awk '{print \\$2}'"` &> /dev/null
if [ $DST_TOTAL_MEMORY -lt $TOTAL_MEMORY ];then
echo "检查主机 $host 内存大小,检查不通过"
men_failed_hosts="$men_failed_hosts $host"
else
echo "检查主机 $host 内存大小,检查通过"
fi
else
men_failed_hosts="$men_failed_hosts $host"
fi
done
if [[ "$men_failed_hosts" == "" ]];then
echo "4.对主机内存检查,全部通过"
else
echo "4.对主机内存检查,未全部通过,未通过的主机: $men_failed_hosts"
fi
}
5.检查数据磁盘是否满足规格要求
function check_disk_number
{
for host in $HOSTS_IP;do
verify_password $host $ROOT_PASS
ALL_DISK_SYMBOLS=`sshpass -p$ROOT_PASS ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 root@$host "lsscsi |awk '\\$2~/disk/{print \\$8}'"` &> /dev/null
for d in $ALL_DISK_SYMBOLS;do
df -h | grep "$d" &> /dev/null
if [ $? -eq 0 ];then
DATA_DISK_SYMBOLS=`echo $ALL_DISK_SYMBOLS | sed "s#$d##g"`
fi
done
DATA_DISK_NUMBER=`echo $DATA_DISK_SYMBOLS | awk '{print NF}'`
if [ $DATA_DISK_NUMBER -ge $NUM_OF_DISK ]; then
echo "检查主机 $host 数据盘个数,检查通过"
else
echo "检查主机 $host 数据盘个数,检查不通过"
disk_failed_hosts="$disk_failed_hosts $host"
fi
done
if [[ "$disk_failed_hosts" == "" ]];then
echo "5.对主机内存检查,全部通过"
else
echo "5.对主机内存检查,未全部通过,未通过的主机: $disk_failed_hosts"
fi
}
6.检查操作系统分区目录大小是否满足需求
PARTITION_SIZE=`cat config.ini | sed -n '/\[root_partition_size\]/,/\[.*\]/p' | grep -v "\[.*\]" |grep -v ^$ |grep -v ^#`
function check_root_partition_size
{
for host in $HOSTS_IP;do
verify_password $host $ROOT_PASS
if [ $? -eq 0 ];then
ROOT_PARTITION_SIZE=`sshpass -p$ROOT_PASS ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 root@$host "df -h |awk '\\$6==\"/\"{print \\$2}' |sed 's/[^0-9]//g'"` &> /dev/null
if [ $ROOT_PARTITION_SIZE -ge $PARTITION_SIZE ]; then
echo "检查主机 $host 分区大小,检查通过"
else
echo "检查主机 $host 分区大小,检查不通过"
part_failed_hosts="$part_failed_hosts $host"
fi
else
part_failed_hosts="$part_failed_hosts $host"
fi
done
if [[ "$part_failed_hosts" == "" ]];then
echo "6.对主机分区大小,全部通过"
else
echo "6.对主机分区大小,未全部通过,未通过的主机: $part_failed_hosts"
fi
}
7.检查集群主机时间是否一致
TIME_VALUE=`cat config.ini | sed -n '/\[time_sync_diff\]/,/\[.*\]/p' | grep -v "\[.*\]" |grep -v ^$ |grep -v ^#`
function check_time_sync
{
for host in $HOSTS_IP;do
LOCAL_TIME=`date "+%Y%m%d%H%M%S"`
verify_password $host $ROOT_PASS
if [ $? -eq 0 ];then
DST_HOST_TIME=`sshpass -p$ROOT_PASS ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 root@$host 'date "+%Y%m%d%H%M%S"'` &> /dev/null
TIME_DIFF=`expr $LOCAL_TIME - $DST_HOST_TIME |sed 's/[^0-9]//g'`
if [ $TIME_DIFF -lt $TIME_VALUE ];then
echo "检查主机 $host 时间同步通过"
else
echo "检查主机 $host 时间同步不通过,时间误差在 $TIME_DIFF"
time_failed_hosts="$time_failed_hosts $host"
fi
else
time_failed_hosts="$time_failed_hosts $host"
fi
done
if [[ "$time_failed_hosts" == "" ]];then
echo "7.对主机时间检查,全部通过"
else
echo "7.对主机时间检查,未全部通过,未通过的主机: $time_failed_hosts"
fi
}
全部代码在这
#!/bin/bash
#
if [ ! -e ./config.ini ];then
echo "config.ini is not exist...please check.."
exit 1
fi
HOSTS_IP=`cat config.ini | sed -n '/\[hosts\]/,/\[.*\]/p' | grep -v "\[.*\]" |grep -v ^$ |grep -v ^# |awk '{print $1}'`
ROOT_PASS=`cat config.ini | sed -n '/\[root_password\]/,/\[.*\]/p' | grep -v "\[.*\]" |grep -v ^$ |grep -v ^#`
OS_VERSION=`cat config.ini | sed -n '/\[os_version\]/,/\[.*\]/p' | grep -v "\[.*\]" |grep -v ^$ |grep -v ^#`
CPU_CORES=`cat config.ini | sed -n '/\[cpu_cores\]/,/\[.*\]/p' | grep -v "\[.*\]" |grep -v ^$ |grep -v ^#`
TOTAL_MEMORY=`cat config.ini | sed -n '/\[memory_kb\]/,/\[.*\]/p' | grep -v "\[.*\]" |grep -v ^$ |grep -v ^#`
NUM_OF_DISK=`cat config.ini | sed -n '/\[data_disk_number\]/,/\[.*\]/p' | grep -v "\[.*\]" |grep -v ^$ |grep -v ^#`
PARTITION_SIZE=`cat config.ini | sed -n '/\[root_partition_size\]/,/\[.*\]/p' | grep -v "\[.*\]" |grep -v ^$ |grep -v ^#`
TIME_VALUE=`cat config.ini | sed -n '/\[time_sync_diff\]/,/\[.*\]/p' | grep -v "\[.*\]" |grep -v ^$ |grep -v ^#`
if [[ "$HOSTS_IP" == "" ]];then
echo "NO HOST IP ADDRESS is configured in config.please check config.ini"
exit 1
else
echo "the cluster includes the following hosts:"
for host in $HOSTS_IP; do
echo $host
done
read -p "Please confirm,input yes/no:" choice
if [[ "$choice" == "yes" || "$choice" == "YES" || "$choice" == "Y" || "$choice" == "y" ]];then
continue
else
exit
fi
fi
function format_print
{
if [ $# -lt 1 ]; then
echo "Usage: format_print 'args1 args2...'"
exit 1
fi
for str in $@;do
echo $str
done
}
function verify_password
{
if [ $# -lt 2 ];then
echo "Usage: verify_password IP root_password"
exit 1
fi
sshpass -p$2 ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 root@$1 "df -h" &> /dev/null
if [ $? -ne 0 ];then
echo "尝试登录ssh主机$1 失败,检查后重试"
return 255
else
return 0
fi
}
function check_host_online
{
echo "++++++++++++++++1、检查主机是否联通正常++++++++++++++++"
for host in $HOSTS_IP;do
ping -w 3 $host &> /dev/null
if [ $? -eq 0 ];then
echo "检测主机 $host 连通性通过"
else
echo "检测主机 $host 无法连通"
ping_faild_hosts="$ping_faild_hosts $host"
fi
done
if [[ "$ping_faild_hosts" == "" ]];then
echo "1.使用ping对主机连通性检查,全部通过"
else
echo "1.使用ping对主机连通性检查,未全部通过:"
format_print $ping_faild_hosts
fi
}
function check_os_version
{
echo "++++++++++++++++2、检查安装操作系统版本是否和需求一致++++++++++++++++"
for host in $HOSTS_IP;do
verify_password $host $ROOT_PASS
if [ $? -eq 0 ];then
sshpass -p$ROOT_PASS ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 root@$host "grep $OS_VERSION /etc/redhat-release" &> /dev/null
if [ $? -ne 0 ];then
echo "检查主机$host 版本,与目标不一致,检查不通过"
os_failed_hosts="$os_failed_hosts $host"
else
echo "检查主机$host 版本,与目标一致,检查通过"
fi
else
os_failed_hosts="$os_failed_hosts $host"
fi
done
if [[ "$os_failed_hosts" == "" ]];then
echo "2.对主机版本检查,全部通过"
else
echo "2.对主机版本检查,未全部通过,未通过的主机: "
format_print $os_failed_hosts
fi
}
function check_cpu_cores
{
echo "++++++++++++++++3、检查CPU是否满足规格要求++++++++++++++++"
for host in $HOSTS_IP;do
verify_password $host $ROOT_PASS
if [ $? -eq 0 ];then
DST_CPU_CORES=`sshpass -p$ROOT_PASS ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 root@$host "cat /proc/cpuinfo |grep ^processor |sort |uniq |wc -l"` &> /dev/null
if [ $DST_CPU_CORES -lt $CPU_CORES ];then
echo "检查主机CPU逻辑核心数量,检查不通过"
cpu_failed_hosts="$cpu_failed_hosts $host"
echo $cpu_failed_hosts
else
echo "检查主机CPU逻辑核心数量,检查通过"
fi
else
cpu_failed_hosts="$cpu_failed_hosts $host"
fi
done
if [[ "$cpu_failed_hosts" == "" ]];then
echo "3.对主机cpu检查,全部通过"
else
echo "3.对主机cpu检查,未全部通过,未通过的主机: "
format_print $cpu_failed_hosts
fi
}
function check_memory
{
echo "++++++++++++++++4、检查内存是否满足规格要求++++++++++++++++"
for host in $HOSTS_IP;do
verify_password $host $ROOT_PASS
if [ $? -eq 0 ];then
DST_TOTAL_MEMORY=`sshpass -p$ROOT_PASS ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 root@$host "cat /proc/meminfo |grep MemTotal| awk '{print \\$2}'"` &> /dev/null
if [ $DST_TOTAL_MEMORY -lt $TOTAL_MEMORY ];then
echo "检查主机 $host 内存大小,检查不通过"
men_failed_hosts="$men_failed_hosts $host"
else
echo "检查主机 $host 内存大小,检查通过"
fi
else
men_failed_hosts="$men_failed_hosts $host"
fi
done
if [[ "$men_failed_hosts" == "" ]];then
echo "4.对主机内存检查,全部通过"
else
echo "4.对主机内存检查,未全部通过,未通过的主机:"
format_print $men_failed_hosts
fi
}
function check_disk_number
{
echo "++++++++++++++++5、检查数据磁盘是否满足规格要求++++++++++++++++"
for host in $HOSTS_IP;do
verify_password $host $ROOT_PASS
ALL_DISK_SYMBOLS=`sshpass -p$ROOT_PASS ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 root@$host "lsscsi |awk '\\$2~/disk/{print \\$8}'"` &> /dev/null
for d in $ALL_DISK_SYMBOLS;do
df -h | grep "$d" &> /dev/null
if [ $? -eq 0 ];then
DATA_DISK_SYMBOLS=`echo $ALL_DISK_SYMBOLS | sed "s#$d##g"`
fi
done
DATA_DISK_NUMBER=`echo $DATA_DISK_SYMBOLS | awk '{print NF}'`
if [ $DATA_DISK_NUMBER -ge $NUM_OF_DISK ]; then
echo "检查主机 $host 数据盘个数,检查通过"
else
echo "检查主机 $host 数据盘个数,检查不通过"
disk_failed_hosts="$disk_failed_hosts $host"
fi
done
if [[ "$disk_failed_hosts" == "" ]];then
echo "5.对主机内存检查,全部通过"
else
echo "5.对主机内存检查,未全部通过,未通过的主机:"
format_print $disk_failed_hosts
fi
}
function check_root_partition_size
{
echo "++++++++++++++++6、检查操作系统分区目录大小是否满足需求++++++++++++++++"
for host in $HOSTS_IP;do
verify_password $host $ROOT_PASS
if [ $? -eq 0 ];then
ROOT_PARTITION_SIZE=`sshpass -p$ROOT_PASS ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 root@$host "df -h |awk '\\$6==\"/\"{print \\$2}' |sed 's/[^0-9]//g'"` &> /dev/null
if [ $ROOT_PARTITION_SIZE -ge $PARTITION_SIZE ]; then
echo "检查主机 $host 分区大小,检查通过"
else
echo "检查主机 $host 分区大小,检查不通过"
part_failed_hosts="$part_failed_hosts $host"
fi
else
part_failed_hosts="$part_failed_hosts $host"
fi
done
if [[ "$part_failed_hosts" == "" ]];then
echo "6.对主机分区大小,全部通过"
else
echo "6.对主机分区大小,未全部通过,未通过的主机:"
format_print $part_failed_hosts
fi
}
function check_time_sync
{
echo "++++++++++++++++7、检查集群主机时间是否一致++++++++++++++++"
for host in $HOSTS_IP;do
LOCAL_TIME=`date "+%Y%m%d%H%M%S"`
verify_password $host $ROOT_PASS
if [ $? -eq 0 ];then
DST_HOST_TIME=`sshpass -p$ROOT_PASS ssh -o StrictHostKeyChecking=no -o ConnectTimeout=2 root@$host 'date "+%Y%m%d%H%M%S"'` &> /dev/null
TIME_DIFF=`expr $LOCAL_TIME - $DST_HOST_TIME |sed 's/[^0-9]//g'`
if [ $TIME_DIFF -lt $TIME_VALUE ];then
echo "检查主机 $host 时间同步通过"
else
echo "检查主机 $host 时间同步不通过,时间误差在 $TIME_DIFF"
time_failed_hosts="$time_failed_hosts $host"
fi
else
time_failed_hosts="$time_failed_hosts $host"
fi
done
if [[ "$time_failed_hosts" == "" ]];then
echo "7.对主机时间检查,全部通过"
else
echo "7.对主机时间检查,未全部通过,未通过的主机: "
format_print $time_failed_hosts
fi
}
check_host_online
check_os_version
check_cpu_cores
check_memory
check_disk_number
check_root_partition_size
check_time_sync