linux 巡检脚本

#!/bin/sh
[email protected]
#Modified according to the actual situation mysql server IP and username password

export black='\033[0m'
export boldblack='\033[1;0m'
export red='\033[31m'
export boldred='\033[1;31m'
export green='\033[32m'
export boldgreen='\033[1;32m'
export yellow='\033[33m'
export boldyellow='\033[1;33m'
export blue='\033[34m'
export boldblue='\033[1;34m'
export magenta='\033[35m'
export boldmagenta='\033[1;35m'
export cyan='\033[36m'
export boldcyan='\033[1;36m'
export white='\033[37m'
export boldwhite='\033[1;37m'


cecho ()

## -- Function to easliy print colored text -- ##
	
	# Color-echo.
	# 参数 $1 = message
	# 参数 $2 = color
{
local default_msg="No message passed."

message=${1:-$default_msg}	# 如果$1没有输入则为默认值default_msg.
color=${2:-black}		# 如果$1没有输入则为默认值black.

case $color in
	black)
		 printf "$black" ;;
	boldblack)
		 printf "$boldblack" ;;
	red)
		 printf "$red" ;;
	boldred)
		 printf "$boldred" ;;
	green)
		 printf "$green" ;;
	boldgreen)
		 printf "$boldgreen" ;;
	yellow)
		 printf "$yellow" ;;
	boldyellow)
		 printf "$boldyellow" ;;
	blue)
		 printf "$blue" ;;
	boldblue)
		 printf "$boldblue" ;;
	magenta)
		 printf "$magenta" ;;
	boldmagenta)
		 printf "$boldmagenta" ;;
	cyan)
		 printf "$cyan" ;;
	boldcyan)
		 printf "$boldcyan" ;;
	white)
		 printf "$white" ;;
	boldwhite)
		 printf "$boldwhite" ;;
esac
  printf "%s\n"  "$message"
  tput sgr0			# tput sgr0即恢复默认值
  printf "$black"

return
}


cechon ()		

	# Color-echo.
	# 参数1 $1 = message
	# 参数2 $2 = color
{
local default_msg="No message passed."
				# Doesn't really need to be a local variable.

message=${1:-$default_msg}	# 如果$1没有输入则为默认值default_msg.
color=${2:-black}		# 如果$1没有输入则为默认值black.

case $color in
	black)
		printf "$black" ;;
	boldblack)
		printf "$boldblack" ;;
	red)
		printf "$red" ;;
	boldred)
		printf "$boldred" ;;
	green)
		printf "$green" ;;
	boldgreen)
		printf "$boldgreen" ;;
	yellow)
		printf "$yellow" ;;
	boldyellow)
		printf "$boldyellow" ;;
	blue)
		printf "$blue" ;;
	boldblue)
		printf "$boldblue" ;;
	magenta)
		printf "$magenta" ;;
	boldmagenta)
		printf "$boldmagenta" ;;
	cyan)
		printf "$cyan" ;;
	boldcyan)
		printf "$boldcyan" ;;
	white)
		printf "$white" ;;
	boldwhite)
		printf "$boldwhite" ;;
esac
  printf "%s"  "$message"
  tput sgr0			# tput sgr0即恢复默认值
  printf "$black"

return
}





#1.the server infomation
echo "the system basic infomation:"
echo "***********************************************************************"
echo 
hostname=`hostname`  #主机名

ipaddress=`ifconfig |grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'` #IP地址

gtway01=`cat  /etc/sysconfig/network|grep GATEWAY|awk -F "=" '{print $2}'` #网关

gtway02=`netstat -rn | awk '/^0.0.0.0/ {print $2}'`

cpuinfo=`cat /proc/cpuinfo|grep "name"|cut -d: -f2 |awk '{print "*"$1,$2,$3,$4}'|uniq -c` #cpu

phmem=`dmidecode | grep -A 16 "Memory Device$" |grep Size:|grep -v "No Module Installed"|awk '{print "*" $2,$3}'|uniq -c` #物理内存数量

sysver=`cat /etc/issue | head -1` #--系统版本


kerver=`uname -a  |awk '{print $3}'`  #内核版本



#mem usage
mem_total=$(free -m |grep Mem|awk '{print $2}')
mem_used=$(free -m |grep Mem|awk '{print $3}')
mem_rate=`expr $mem_used/$mem_total*100|bc -l`

#mem_summary
mem_sum=`free -m | xargs | awk '{print "Free/total memory: " $17 " / " $8 " MB"}' | awk -F":" 'BEGIN{print " FREE / TOTAL " }  {print $2 }'`

#disk space
dk_usage=`df -h | xargs | awk '{print "Free/total disk: " $11 " / " $9}'`



cechon "1.1 server hostname is:" red
echo ${hostname}

cechon "1.2 server ipaddree is: " red
echo  ${ipaddress}


if [ "${gtway01}" = "" ];then
	cechon "1.3 server gateway is:" red
	echo ${gtway02}
else
	cechon "1.3 server gateway is:" red
	echo ${gtway01}
fi



cechon "1.4 server cpuinfo is: " red
echo ${cpuinfo}


cechon "1.5 server Physical memory number is: " red
echo ${phmem}


cechon "1.6 server version  is: " red
echo ${sysver}


cechon "1.7 server system kernel version is: " red
echo ${kerver}

cechon "1.8 server  memory Summary is: " red
echo ${mem_sum}


cechon "1.9 server  memory usage rate is: " red
echo ${mem_rate:0:5}%



cechon "1.10 server  disk usage  is: " red
echo
cechon "detail: " green
echo
#disk usage
df -H |awk -F '\t' '{ print $1,$2,$3,$4,$5,$6}'
echo
cechon "Summary: " green
echo 
echo ${dk_usage}



echo
cechon "1.11 server  CPU load average  is: " red
echo
uptime | awk 'BEGIN{print "1min, 5min, 15min"}   {print $10,$11,$12}'

echo
cechon "1.12 server  started services   is: " red
echo
chkconfig --list | grep on 
echo

echo
cechon "1.13 server  CPU free   is: " red
top -b -n 1 | grep Cpu | awk '{print $5}' | cut -f 1 -d "."
echo


cechon "1.14 mysql ESTABLISHED connect   is: " red
echo
netstat -an -t | grep ":3306" | grep ESTABLISHED | awk '{printf "%s %s\n",$5,$6}' | sort |sed  's/^::ffff://'
echo

cechon "1.15 server  ESTABLISHED  TCP connect number   is: " red
echo
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
echo





echo
echo "***********************************************************************"
echo 













 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。