Linux 的shell脚本的分享,运用了多个不同的方法,实现使用脚本批量巡检服务器,非常有借鉴作用
1、今天分享一个以前坐项目使用过的巡检脚本,是使用bash shell 去编写的,对于经常使用shell脚本去完成日常巡检的同行,非常有借鉴作用;
2、脚本使用的一些变量以及模块都很有用,如果是初学习shell的脚本使用,参考这个脚本,绝对能让你快速的提升,具体代码如下:
#!/bin/bash ############################################################## #脚本用于对服务器各资源,服务进程等信息采集。 ############################################################# TIME=`date +"%Y-%m-%d-%H-%M"` #30:黑,31:红,32:绿,33:黄,34:蓝色,35:紫色,36:深绿 #red:31 green:32 yellow:33 blue:34 purple:35 darkgreen:36 #颜色设置 RED(){ val=$1 echo -e "\033[31m ${val} \033[0m" } GREEN(){ val=$1 echo -e "\033[32m ${val} \033[0m" } YELLOW(){ val=$1 echo -e "\033[33m ${val} \033[0m" } BLUE(){ val=$1 echo -e "\033[34m ${val} \033[0m" } PURPLE(){ val=$1 echo -e "\033[35m ${val} \033[0m" } DARKGREEN(){ val=$1 echo -e "\033[36m ${val} \033[0m" } ########################################################### #登录交互操作 commd(){ ssh -o "StrictHostKeyChecking no" $IP "$cmd" < /dev/null } ########################################################### #服务资源参数加载 eval $(/bin/grep disksize gather.conf) eval $(/bin/grep cpusize gather.conf) eval $(/bin/grep memsize gather.conf) eval $(/bin/grep swapsize gather.conf) eval $(/bin/grep dropsize gather.conf) eval $(/bin/grep ntpsize gather.conf) eval $(/bin/grep vss gather.conf) eval $(/bin/grep vss_ip gather.conf) eval $(/bin/grep ntp1_server_ip gather.conf) eval $(/bin/grep ntp1_client gather.conf) eval $(/bin/grep ntp2_server_ip gather.conf) eval $(/bin/grep ntp2_client gather.conf) eval $(/bin/grep sarip gather.conf) eval $(/bin/grep mass gather.conf) eval $(/bin/grep ipvsadm gather.conf) eval $(/bin/grep losssize gather.conf) eval $(/bin/grep bo gather.conf) eval $(/bin/grep bo_ip gather.conf) eval $(/bin/grep cdn gather.conf) eval $(/bin/grep cdn_ip gather.conf) eval $(/bin/grep VSS gather.conf) eval $(/bin/grep VSS_IP gather.conf) eval $(/bin/grep lvs gather.conf) eval $(/bin/grep lvs_ip gather.conf) eval $(/bin/grep portal gather.conf) eval $(/bin/grep portal_ip gather.conf) ########################################################## DISK(){ cmd='df -h' disk=`commd $IP $cmd |awk '{if (NF==6){print $5","$6}else if (NF==5){print $4","$5}}'|grep -v -E "已用%|挂载点|shm|boot"` for data in $disk do valnum=`echo $data|awk -F[,%] '{print $1}'` diskname=`echo $data|awk -F[,%] '{print $3}'` if [ $valnum -gt $disksize ] ;then RED "($diskname),$valnum%" else echo "($diskname),$valnum%" fi done } CPU(){ cmd='top -b n 1' CPUVAL=`commd $IP $cmd|grep "Cpu(s)"|awk -F, '{print $1,$2,$4}'` cpuval=`echo $CPUVAL|awk -F[:%] -v cpusizes=$cpusize '{if($2>cpusizes){print 0}else {print 1}}'` if [ -n "$CPUVAL" ];then if [ $cpuval -eq 0 ];then RED "$CPUVAL" else echo "$CPUVAL" fi fi } MEM(){ cmd='free -m' mem=`commd $IP $cmd|grep "Mem"|awk '{sum=$2-$4-$6-$7}END{printf "%d\n", (sum/$2*100)}'` if [ -n "$mem" ];then if [ $mem -gt $memsize ];then RED "MEM: $mem%" else echo "MEM: $mem%" fi fi } SWAP(){ cmd='free -m' swap=`commd $IP $cmd|grep "Swap"|awk '{printf "%d\n",($3/$2)}'` if [ -n "$swap" ];then if [ $swap -gt $swapsize ];then RED "SWAP: $swap%" else echo "SWAP: $swap%" fi fi } NET(){ cmd="ifconfig|grep -E 'eth|bond'" network=`commd $IP $cmd|grep -E '^eth|^bond'|awk '{print $1}'` for net in $network do net=`echo $net|grep -v ":"` if [ "$net" != "" ];then cmd="ifconfig $net" DROPVAL=`commd $IP $PASSWD $cmd|grep "RX packets"|awk /dropped/'{print $4}'` dropval=`echo $DROPVAL|awk -F: '{print $2}'` cmd="ethtool $net|grep 'Link'" UPDOWN=`commd $IP $PASSWD $cmd|grep 'detected'|awk '{print $NF}'` statval=`echo $UPDOWN|sed 's/\r//g'` if [ "$statval" == "no" ] || [ $dropval -gt $dropsize ] ;then RED "$NAME,$IP,$net,$DROPVAL,$UPDOWN" else echo "$NAME,$IP,$net,$DROPVAL,$UPDOWN" fi fi done } GATH(){ for IP in $1 do NAME=`cat iplist.txt|grep "\<$IP\>"|awk '{print $3}'` sip=`echo $IP|awk -F. '{print $1"."$2"."$3}'` cmd="ip add|grep $sip|awk '{print \$2}'|uniq|wc -l" num=`commd $IP $cmd` NAMES=`echo $NAME|grep 'CDN'` if [ "${NAMES}" != "" ];then if [ $num -ge 4 ]; then for role in $2 do cmd="ps uax|grep ngod|grep $role|grep -v 'grep'" val=`commd $IP $cmd|grep "$role"|grep -v "grep"` if [ "$val" == "" ];then RED "$NAME,$IP,$role,进程不存在!" else echo "$NAME,$IP,$role,进程存在!" fi done else for role in $2 do roles=`echo $role|grep -E "rti|csi|cls"` if [ "$roles" == "" ] ;then cmd="ps uax|grep ngod|grep $role|grep -v 'grep'" val=`commd $IP $cmd|grep "$role"|grep -v "grep"` if [ "$val" == "" ];then RED "$NAME,$IP,$role,进程不存在!" else echo "$NAME,$IP,$role,进程存在!" fi fi done fi else if [ $num -ge 2 ]; then for role in $2 do cmd="ps uax|grep $role|grep -v 'grep'" val=`commd $IP $cmd|grep "?"` if [ "$val" == "" ];then RED "$NAME,$IP,$role,进程不存在!" else echo "$NAME,$IP,$role,进程存在!" fi done else echo "$NAME,$IP,备机器" fi fi done } GATHS(){ for IP in $1 do NAME=`cat iplist.txt|grep "\<$IP\>"|awk '{print $3}'` for role in $2 do cmd="ps aux|grep ngod|grep $role|grep -v 'grep'" val=`commd $IP $cmd|grep "$role"|grep -v "grep"` if [ "$val" == "" ];then echo "$NAME,$IP,$role,进程不存在!" else echo "$NAME,$IP,$role,进程存在!" fi done done } NTP(){ ntp1(){ if [ "$ntp1_server_ip" != "" ] && [ "$ntp1_client" != "" ] ;then for IP in ${ntp1_client[@]} do NAME=`cat iplist.txt|grep "\<$IP\>"|awk '{print $3}'` cmd="ntpdate -u ${ntp1_server_ip}" ntpval=`commd $IP $cmd|awk /offset/'{print $10}'` ntpnum=`echo $ntpval|awk -v ntpsizes=$ntpsize '{if ($1>ntpsizes){print 0}else {print 1}}'` if [ $ntpnum -eq 0 ] ;then RED "$NAME,$IP,$ntpval" else echo "$NAME,$IP,$ntpval" fi done fi } ntp2(){ if [ "$ntp2_server_ip" != "" ] && [ "$ntp2_client" != "" ] ;then for IP in ${ntp2_client[@]} do NAME=`cat iplist.txt|grep "\<$IP\>"|awk '{print $3}'` cmd="ntpdate -u ${ntp2_server_ip}" ntpval=`commd $IP $cmd|awk /offset/'{print $10}'` ntpnum=`echo $ntpva2|awk -v ntpsizes=$ntpsize '{if ($1>ntpsizes){print 0}else {print 1}}'` if [ $ntpnum -eq 0 ] ;then RED "$NAME,$IP,$ntpval" else echo "$NAME,$IP,$ntpval" fi done fi } ntp2 ntp1 } PING(){ network(){ cmd="ping -I $1 -c 5 $2" loss=`commd $IP $cmd|grep " packets"|awk '{print $6}'` lossval=`echo $loss|awk -F% '{print $1}'` if [ $lossval -gt $losssize ] ;then RED "$NAME,$IP,$1,$loss" else echo "$NAME,$IP,$1,$loss" fi } cat iplist.txt|grep -v "\#" |while read file do IP=`echo $file|awk '{print $1}'` NAME=`echo $file|awk '{print $3}'` cmd='route -n|grep -E "bond1|bond0"' val=`commd $IP $cmd|grep "\<UG\>"|awk '{print $2,$NF}'|sort|uniq` net_num=`echo $val|awk '{print NF}'` if [ $net_num -eq 2 ];then gw=`echo $val|awk '{print $1}'` netname=`echo $val|awk '{print $2}'` network $netname $gw elif [ $net_num -eq 4 ] ; then gw1=`echo $val|awk '{print $1}'` gw2=`echo $val|awk '{print $3}'` netname1=`echo $val|awk '{print $2}'` netname2=`echo $val|awk '{print $4}'` network $netname1 $gw1 network $netname2 $gw2 fi done } SAR(){ if [ "$sarip" != "" ];then sar(){ NAME=`cat iplist.txt|grep "\<$IP\>"|awk '{print $3}'` cmd="export LANG=zh_CN.UTF-8;sar -n DEV 2 2" GREEN "*********************************${NAME},${IP}*******************************************" commd $IP $cmd|grep -E "平均时间:|Average" } for IP in ${sarip[@]} do sar done fi } MASS(){ if [ "$mass" != "" ];then Date=`date +%Y-%m-%d_%H-%M-%S` ipfile='disk_mass.csv' WGET() { wget -O diskstatus.xml "http://${ip}:${port}/cmd?cmdname=GetSysInfo" >/dev/null 2>&1 } echo "AREA,IPADDR,MASSSTATUS" for serverinfo in `cat ${ipfile}|grep "$mass"` do area=`echo ${serverinfo}|cut -d, -f1` ip=`echo ${serverinfo}|cut -d, -f2` port=`echo ${serverinfo}|cut -d, -f3` if ( ping -c 1 $ip >/dev/null );then WGET if [ `grep -c ok diskstatus.xml` -eq 1 ];then allsize=`awk -F "<SysAllSize>" '{print $2}' diskstatus.xml |awk -F "</" '{print $1}'` usesize=`awk -F "<SysUsedSize>" '{print $2}' diskstatus.xml |awk -F "</" '{print $1}'` stat='ok' let per=usesize*100/allsize else stat='bad' fi else stat='bad' fi if [ "$stat" = "ok" ];then echo "${area},${ip},${per}%" elif [ "$stat" = "bad" ] ;then RED "${area},${ip},error" fi done rm -rf diskstatus.xml fi } IPVSADM(){ if [ "$ipvsadm" != "" ];then for IP in ${ipvsadm[@]} do NAME=`cat iplist.txt|grep "\<$IP\>"|awk '{print $3}'` sip=`echo $IP|awk -F. '{print $1"."$2"."$3}'` cmd="ip add|grep -c $sip" num=`commd $IP $cmd` if [ $num -gt 1 ];then cmd='ipvsadm -ln' GREEN "*********************************${NAME},${IP}************************************" commd $IP $cmd|grep "Route" else cmd='/etc/init.d/keepalived status' BLUE "**********************************${NAME},${IP}************************************" commd $IP $cmd|grep "keepalived" fi done fi } main(){ RED "####IP#########|################CPU#############|##MEM#####|####SWAP###|########################DISK###############################" cat iplist.txt|grep -v "\#"|while read file do IP=`echo $file|awk '{print $1}'` NAME=`echo $file|awk '{print $3}'` disk=`DISK|xargs` cpu=`CPU` mem=`MEM` swa=`SWAP` echo "$NAME,$IP:| $cpu | $mem | $swa | DISK:$disk" done } WNETS(){ cat iplist.txt|grep -v "#"|while read file do IP=`echo $file|awk '{print $1}'` NAME=`echo $file|awk '{print $3}'` NET done } ips=${vss_ip[@]};gath=${vss[@]} bos=${bo[@]};bo_ips=${bo_ip[@]} cdns=${cdn[@]};cdn_ips=${cdn_ip[@]} VSSS=${VSS[@]};VSS_IPS=${VSS_IP[@]} lvss=${lvs[@]};lvs_ips=${lvs_ip[@]} portals=${portal[@]};portal_ips=${portal_ip[@]} #RED GREEN YELLOW BLUE PURPLE DARKGREEN Usage(){ echo """$0 -h help. -z cpu,mem,swap... -n ntp -p ping -s sar -n DEV 2 2 -m MASS -i IPVSADM -w NETWORK -j 程序进程 -a 以上所有的。""" } if [ $# -eq 0 ];then Usage fi while getopts ":h",":znpsmiwja" opt do case $opt in "h") Usage exit -1 ;; "z") main #TIME ;; "n") NTP ;; "p") PING ;; "s") SAR ;; "m") MASS ;; "i") IPVSADM ;; "w") WNETS ;; "j") GATH "$ips" "$gath" GATHS "$bo_ips" "$bos" GATH "$cdn_ips" "$cdns" GATHS "$VSS_IPS" "$VSSS" GATH "$lvs_ips" "$lvss" GATHS "$portal_ips" "$portals" ;; "a") main #TIME GREEN "NTP******************************************************************************" NTP YELLOW "PINT******************************************************************************" PING SAR PURPLE "MASS******************************************************************************" MASS IPVSADM DARKGREEN "NETWORK******************************************************************************" WNETS YELLOW "程序进程******************************************************************************" GATH "$ips" "$gath" GATHS "$bo_ips" "$bos" GATH "$cdn_ips" "$cdns" GATHS "$VSS_IPS" "$VSSS" GATH "$lvs_ips" "$lvss" GATHS "$portal_ips" "$portals" ;; *) echo "请输入参数" exit -1 ;; esac done