shell系统巡检脚本
Posted 哭泣的馒头
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell系统巡检脚本相关的知识,希望对你有一定的参考价值。
1、Linux 基础信息巡检
sys_check()
echo "主机名称:`hostname`"
#echo "操作系统:`cat /etc/*-release|awk ENDprint`"
echo "操作系统:`cat /etc/*-release|awk ENDprint|cut -d \\= -f 2|sed s/\\"//g`"
echo "系统内核:`uname -r`"
#echo "SELinux:`/usr/sbin/sestatus | grep SELinux status: | awk print $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 ENDprintf $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 ENDprint $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占比 `echo "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_DiskRate:分区" $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
4、服务状态检查
service_Check()
#返回服务状态
service_name=$1
#local service_name="sshd"
echo `systemctl status $service_name |grep Active |awk print "ResCheck_Service:" service_name " " $2 service_name=$service_name`
service_Check $1
5、网站可用性检查
url_Check()
#通过端口的返回值判断状态
url_path=$1
#local url_path="https://asts.cxmt.com:8080"
#echo `curl -k -s --head $url_path |awk NR==1 print "ResCheck_URL:" url_path " "$2$3 url_path=$url_path`
url_checkRes=`curl --connect-timeout 3 -k -s --head $url_path`
if [ "$url_checkRes" ]; then
echo `curl -k -s --head $url_path |awk NR==1 print "ResCheck_URL:" url_path " "$2$3 url_path=$url_path`
else
echo "ResCheck_URL:$url_path Unreachable "
fi
url_Check $1
以上是关于shell系统巡检脚本的主要内容,如果未能解决你的问题,请参考以下文章