yuhuohu 发表于 2009-12-04 08:34

[分享]一个检测操作系统基础配置的shell脚本for solaris&redhat

系统装多了难免会出现各种人为失误或遗漏,比如

[*]主机名和/etc/hosts文件中的配置不一致
[*]忘记在/etc/hosts给主机加别名
[*]漏配了缺省网关
[*]把网卡地址敲成网关地址
[*]ntp-sever或client没配置



特别是一次在调整18台rhel 5.1 x64的地址段时上直接把某台网卡地址配成网关,更不幸的是折腾了2天才找出来。。。

含恨之下花了2天写了个脚本专门检测这种低级失误,水平有限,欢迎各位继续建议优化挑错。

两个文件,check_configure.conf是配置文件,check_configure.sh是可执行脚本

检测结果有四种:

[*]OK         通过
[*]FALSE      失败,表示没有该项配置
[*]WARNNING   警告,表示该项配置不太规范,可能存在潜在的问题,但通常情况下是可以正常工作的,可视情况忽视或改正
[*]ERROR      错误,表示该配置需要进行修正


redhat 5.1,solaris 10上运行通过,执行演示:

暂时只检测上述5项,大家可帮忙补充还有哪些经常出现的低级失误,在后续版本中可一并加上

bash-3.00# ./check_configure.sh
Usage: ./check_configure.sh {redhat|solaris}
      interval-time's default value is 1,means 1 second.
      for example:./check_configure.sh solaris 0 ,0 means no stop.

bash-3.00# ./check_configure.sh solaris

1.Checking hostname <--> /etc/hosts                                             OK

2.Checking hostname_alias                                                       OK
      34.29.79.132db01   db01.com loghost

3.Checking default_router configuration
      34.29.79.129                                                            OK

4.Checking default_router <--> NIC's ip_address                                 OK
4.Checking default_router <--> NIC's ip_address                                 OK
4.Checking default_router <--> NIC's ip_address                                 OK
4.Checking default_router <--> NIC's ip_address                                 OK

5.Checking ntp_server configuration                                             OK
      /*--- running 'svcs ntp' ---*/
STATE          STIME    FMRI
online         Aug_03   svc:/network/ntp:default
      /*--- running 'ntpq -p ---*/
   remote         refid      st t when poll reach   delay   offset    disp
==============================================================================
*34.128.34.7    32.129.24.6   5 u368 1024377   1.56   -0.097    0.15
+34.129.79.14134.128.34.7   6 u239 1024377   0.93   -0.127    0.12
bash-3.00#




脚本代码下载:

脚本代码展示,欢迎各路板砖
# cat check_configure.conf
#========================================================
#定义各种配置文件
#========================================================
if [ "$1" = "redhat" ];then

        #主机名配置文件
        #NAME_FILE=/etc/sysconfig/network

        #主机名-ip地址映射文件
        HOST_FILE=/etc/hosts

        #缺省网关配置文件
        GW_FILE=/etc/sysconfig/network

        #网卡的配置文件
        NIC_FILE_HEAD=/etc/sysconfig/network-scripts/ifcfg-

elif [ "$1" = "solaris" ];then

        #主机名配置文件
        #NAME_FILE=/etc/nodename

        #主机名-ip地址映射文件
        HOST_FILE=/etc/hosts

        #缺省网关配置文件
        GW_FILE=/etc/defaultrouter

        #网卡的配置文件
        NIC_FILE_HEAD=/etc/hostname.

fi
#========================================================
#初始化变量

#检测项的间隔时间,这个可以改
interval_time=1

#以下没事就不要改
gateway_configure=0
gateway_num=0
check_item_counts=0




# cat check_configure.sh
#!/bin/bash


###############################################################################
#initial some variable
###############################################################################

. ./check_configure.conf $1

###############################################################################
#usage
###############################################################################

if [ $# = 0 ];then
      echo $"Usage: $0 {redhat|solaris} "
      echo "interval-time's default value is $interval_time,means $interval_time second."
      exit 1
elif [ -n "$2" ];then
      interval_time=$2
fi
###############################################################################
#01.    Checking hostname <--> $HOST_FILE 查主机名和/etc/hosts中是否一致
###############################################################################

echo
((check_item_counts++))

Check_hostname_result=`grep $HOSTNAME $HOST_FILE|grep -v ^#|wc -l`
if [ $Check_hostname_result = 0 ];then
      echo $check_item_counts".Checking hostname <--> $HOST_FILE                                              ERROR"
      echo "-->Please Check $GW_FILE & $HOST_FILE file."
else
      echo $check_item_counts".Checking hostname <--> $HOST_FILE                                              OK"
fi

###############################################################################
#02.    Checking hostname's alias name
###############################################################################
echo
sleep $interval_time
((check_item_counts++))

hostname_alias=`grep $HOSTNAME $HOST_FILE|awk '{print $3}'`
if [ "$hostname_alias" = "" ]; then
      echo $check_item_counts".Checking hostname_alias                                                      WARNNING"
      echo "Advice:'vi $HOST_FILE' and change the follow line"
      echo "          `grep -v ^# $HOST_FILE|grep $HOSTNAME $HOST_FILE`"
      echo "          to follow line:"
      echo "          'grep -v ^# $HOST_FILE|grep $HOSTNAME $HOST_FILE'       $HOSTNAME.com"
else
      echo $check_item_counts".Checking hostname_alias                                                      OK"
      echo "`grep -v ^# $HOST_FILE|grep $HOSTNAME $HOST_FILE`"
fi

###############################################################################
#03.    Check default_router,查缺省网关是否配置
###############################################################################
echo
sleep $interval_time
((check_item_counts++))

echo $check_item_counts".Checking default_router configuration"

case "$1" in

"redhat")

GATEWAY=`grep ^GATEWAY $GW_FILE 2>/dev/null|awk -F "=" '{print $2}'`

Check_nic_list=`ifconfig |grep -1 "inet addr"|grep HWaddr|awk '{print $1}'`

for i in $Check_nic_list
do
      nic_configure_file=$NIC_FILE_HEAD$i
      Check_nic_result=`grep ^GATEWAY $nic_configure_file|wc -l`
      if [ $Check_nic_result = 1 ];then
                echo "Check '$nic_configure_file'                                                   WARNNING"
                echo "          -->Please remove the entry '`grep ^GATEWAY $nic_configure_file`' from"
                echo "          -->'$nic_configure_file' to '$GW_FILE'"
                gateway_configure=1
                ((gateway_num++))
      fi
done

if [ $gateway_num -gt 1 ];then
      echo "This OS has $gateway_num GATEWAYs,plz using 'route add' to add static route"
fi

if [ $gateway_configure = 0 ];then
      Check_gateway_result=`grep ^GATEWAY $GW_FILE|wc -l`
      if [ $Check_gateway_result = 0 ];then
                echo "Check configuration of$GW_FILE                        FALSE"
                echo "          -->Please add 'GATEWAY=a.c.b.c' to '$GW_FILE'"
      else
                echo "Check configuration of $GW_FILE                         OK"
      fi
fi
;;

"solaris")
      if [ -f $GW_FILE ];then
                GATEWAY=`cat $GW_FILE`
                echo "$GATEWAY                                                                OK"
      else
                echo "$GW_FILE has not existed                                        FALSE"
      fi
      ;;
esac
###############################################################################
#04.    Check default_router <--> NIC's ip_address 查网关和网卡地址是否冲突
###############################################################################
echo
sleep $interval_time
((check_item_counts++))

Check_ip_list=`ifconfig -a|grep"inet"|awk '{print $2}'|grep -v '127.0.0.1'|sed 's/addr://g'`

for i in $Check_ip_list
do
      if [ "$GATEWAY" = "$i" ];then
                echo $check_item_counts".Checking default_router <--> NIC's ip_address                                  ERROR"
                echo "-->GATEWAY's ip $GATEWAY can not been same as NIC's ip address"
      else
                echo $check_item_counts".Checking default_router <--> NIC's ip_address                                  OK"
      fi
done

###############################################################################
#05.    Check ntp
###############################################################################
echo
sleep $interval_time
((check_item_counts++))

Check_ntp_client=`crontab -l|grep -v ^#|grep ntpdate|wc -l`
Check_ntp_server=`ntpq -p 2>/dev/null|wc -l`
if [[ $Check_ntp_client = 0 && $Check_ntp_server = 0 ]];then
      echo $check_item_counts".Checking ntp configuration                                                   FALSE"
      echo "-->Two selection:"
      echo "-->a) 'crontab -e' and insert '0 12 * * * /usr/sbin/ntpdate 134.128.34.7'"
      echo "-->b) 'vi /etc/ntp.conf' and 'chkconfig ntpd on' and 'service ntpd start'"
else
      if [ $Check_ntp_client = 1 ];then
                echo $check_item_counts".Checking ntp_client configuration                                              OK"
                echo "   `crontab -l|grep -v ^#|grep ntpdate`"
      else
                echo $check_item_counts".Checking ntp_server configuration                                              OK"
                case "$1" in
                redhat)
                        echo "/*--- running 'service ntpd status' ---*/"
                        service ntpd status
                        echo "running 'ntpq -p ---*/"
                        ntpq -p
                        ;;
                solaris)
                        echo "/*--- running 'svcs ntp' ---*/"
                        svcs ntp
                        echo "/*--- running 'ntpq -p ---*/"
                        ntpq -p
                        ;;
                esac
      fi
fi


[ 本帖最后由 yuhuohu 于 2010-1-6 11:43 编辑 ]

welcome008 发表于 2009-12-04 08:36

我先顶一下

zhoujm1976 发表于 2009-12-04 09:30

不错啊,楼主写shell可以的。

hhak 发表于 2009-12-04 10:29

很好,很强大,学习了。

东方蜘蛛 发表于 2009-12-04 11:59

收藏:em09:

zhmzhouming 发表于 2009-12-04 12:44

solaris论坛最近可是越来越热闹了:luya:

doging 发表于 2009-12-04 13:03

好东东,收藏

yqx1986 发表于 2009-12-04 16:41

收藏

[ 本帖最后由 yqx1986 于 2009-12-4 16:48 编辑 ]

easybegin 发表于 2009-12-04 16:51

收藏

michael1983 发表于 2009-12-04 16:59

必须要收藏啊
页: [1] 2 3
查看完整版本: [分享]一个检测操作系统基础配置的shell脚本for solaris&redhat