规划中平台是具备一定的通用性和可扩展性,所以在写脚本时引入了参数,以便实现代码的复用
**1、Linux 基础信息巡检**点击查看代码
sys_check(){
echo "主机名称:`hostname`"
#echo "操作系统:`cat /etc/*-release|awk 'END{print}'`"
echo "操作系统:`cat /etc/*-release|awk 'END{print}'|cut -d \= -f 2|sed 's/\"//g'`"
echo "系统内核:`uname -r`"
#echo "SELinux:`/usr/sbin/sestatus | grep 'SELinux status:' | awk '{pri nt $3}'`"
echo "系统语言:`echo $LANG |awk -F "." '{print $1}'`"
echo "系统编码:`echo $LANG |awk -F "." '{print $2}'`"
echo "当前时间:`date +%F_%T`"
echo "启动时间:`who -b | awk '{print $3,$4}'`"
echo "运行时间:`uptime | awk '{print $3 " " $4}' | sed 's/,//g'`"
}
cpu_Info(){
echo "CPU架构:`uname -m`"
echo "CPU型号:`cat /proc/cpuinfo | grep "model name" | uniq|awk -F":" ' {print $2}'`"
# echo "CPU数量:`cat /proc/cpuinfo | grep "physical id"|sort|uniq|wc -l` 颗"
# echo "CPU核心:`cat /proc/cpuinfo | grep "cpu cores"|sort|uniq|awk -F " :" '{print $2}'` 核"
echo "CPU线程:`cat /proc/cpuinfo | grep "processor" | awk '{print $3}' | sort | uniq | wc -l` 线程"
}
cpu_Check(){
Check_Res=`sar -u 1 5 |grep Average`
echo "CPU用户占比:`echo $Check_Res|awk '{printf $3}'`%"
echo "CPU内核占比:`echo $Check_Res|awk '{printf $5}'`%"
echo "ResCheck_CPURate:`echo $Check_Res|awk '{printf $3+$5}'`%"
echo "CPU可用占比:`echo $Check_Res|awk '{printf $8}'`%"
}
mem_check(){
free_total=`free -m | grep Mem|awk '{printf $2}'`
free_used=`free -m | grep -v Swap|awk 'END{printf $3}'`
#free_available=`free -m | grep Mem|awk '{printf $4}'`
#used_baifen=`echo "scale=2;$free_used/$free_total*100"|bc`
echo "内存合计:`free -g | awk "NR==2"| awk '{print $2}'` GB "
echo "内存used:`free -g | grep -v Swap | awk 'END{print $3}'` GB"
#echo "内存buff/cache:`free -g | awk "NR==2"| awk '{print $6}'` GB"
#echo "内存使用:`free -m | awk "NR==2"| awk '{printf ("%.2f\n", ($3+$6) /1024)}'` GB 占比 `echo "scale=2;$free_used/$free_total*100"|bc`%"
echo "ResCheck_MemRate: `echo "scale=2;($free_used/$free_total)*100"|bc `%"
# echo "内存使用:`free -mh | awk "NR==2"| awk '{print $3+$6}'` G占比 `e cho "scale=2;$free_used/$free_total*100"|bc`%"
#echo "内存可用:`free -g | awk "NR==2"| awk '{print $4}'` GB 占比 `echo "scale=2;$free_available/$free_total*100"|bc`% "
#echo "内存可用:`free -g | awk "NR==2"| awk '{print $4}'` GB 占比 `echo "scale=2;$free_available/$free_total*100"|bc`% "
}
disk_Check(){
echo "`df -h | sort |grep -E "/sd|/mapper" |awk '{print "ResCheck_DiskRa te:分区" $1 ," 合 计"$2" 已用" $3 " 剩余"$4 " 使用占比 " $5}'`"
# echo "`df -h | sort |grep /sd |awk '{print "ResCheck_DiskRate:分区" $1 ," 合计"$2" 已用" $3 " 剩余"$4 " 使用占比 " $5}'`"
}
ip_Addr(){
echo "`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6 |awk -F " " 'BEGIN {count=0} {count=count+1; print "IP地址" count ":" $2}'`"
}
sys_check
cpu_Info
cpu_Check
mem_check
disk_Check
ip_Addr
执行效果如图:
**2、服务器端口检查** 点击查看代码
port_Check(){
# local service_port=50456
service_port=$1
Temp_S=`netstat -naut| grep -v tcp6 |grep "0.0.0.0:$service_port "`
if [ -z "$Temp_S" ]; then
echo "ResCheck_Port:$service_port Unknown"
fi
if [ -n "$Temp_S" ]; then
#echo `echo $Temp_S|awk '{ print "ResCheck_Port:" service_port " " $6}' service_port=$service_port `
echo "ResCheck_Port:$service_port LISTEN"
fi
}
port_Check $1
效果图:
**3、检查远程主机端口状态**点击查看代码
[root@hfeboasmg01 script]# cat remotePort_check.sh
remotePort_Check(){
# local service_port=50456
Host=$1
Port=$2
#Temp_S=`netstat -naut| grep -v tcp6 |grep "0.0.0.0:$service_port "`
#nc -z -v -w 5 $Host $Port &> /dev/null
nc -z -v -w 5 $Host $Port
res=$?
#Do whatever you want
if [ $res -eq 0 ]; then
echo ResCheck_remotePort: port $Port on $Host is open
else
echo ResCheck_Port:port $Port on $Host is closed
fi
}
remotePort_Check $1 $2