linux 巡检脚本

来源:互联网 发布:伴奏制作的软件 编辑:程序博客网 时间:2024/05/17 04:17
#!/bin/sh#ocpyang@126.com#Modified according to the actual situation mysql server IP and username passwordexport 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 inblack) 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 inblack)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 infomationecho "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` #cpuphmem=`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 usagemem_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_summarymem_sum=`free -m | xargs | awk '{print "Free/total memory: " $17 " / " $8 " MB"}' \| awk -F":" 'BEGIN{print " FREE / TOTAL " }  {print $2 }'`#disk spacedk_usage=`df -h | xargs | awk '{print "Free/total disk: " $11 " / " $9}'`cechon "1.1 server hostname is:" redecho ${hostname}cechon "1.2 server ipaddree is: " redecho  ${ipaddress}if [ "${gtway01}" = "" ];thencechon "1.3 server gateway is:" redecho ${gtway02}elsecechon "1.3 server gateway is:" redecho ${gtway01}ficechon "1.4 server cpuinfo is: " redecho ${cpuinfo}cechon "1.5 server Physical memory number is: " redecho ${phmem}cechon "1.6 server version  is: " redecho ${sysver}cechon "1.7 server system kernel version is: " redecho ${kerver}cechon "1.8 server  memory Summary is: " redecho ${mem_sum}cechon "1.9 server  memory usage rate is: " redecho ${mem_rate:0:5}%cechon "1.10 server  disk usage  is: " redechocechon "detail: " greenecho#disk usagedf -H |awk -F '\t' '{ print $1,$2,$3,$4,$5,$6}'echocechon "Summary: " greenecho echo ${dk_usage}echocechon "1.11 server  CPU load average  is: " redechouptime | awk 'BEGIN{print "1min, 5min, 15min"}   {print $10,$11,$12}'echocechon "1.12 server  started services   is: " redechochkconfig --list | grep on echoechocechon "1.13 server  CPU free   is: " redtop -b -n 1 | grep Cpu | awk '{print $5}' | cut -f 1 -d "."echocechon "1.14 mysql ESTABLISHED connect   is: " redechonetstat -an -t | grep ":3306" | grep ESTABLISHED | awk '{printf "%s %s\n",$5,$6}' | sort |sed  's/^::ffff://'echocechon "1.15 server  ESTABLISHED  TCP connect number   is: " redechonetstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'echoechoecho "***********************************************************************"echo 


 

0 0