适用于linux主机-进程-数据库等巡检
#!/bin/bash
#date:2023-03-06
#write by Xu
#巡检
echo -ne "\033[0;33m"
cat<<EOT
_oo0oo_
088888880
88" . "88
(| -_- |)
0\ = /0
___/'---'\___
.' \\\\| |// '.
/ \\\\||| : |||// \\
/_ ||||| -:- |||||- \\
| | \\\\\\ - /// | |
| \_| ''\---/'' |_/ |
\ .-\__ '-' __/-. /
___'. .' /--.--\ '. .'___
."" '< '.___\_<|>_/___.' >' "".
| | : '- \'.;'\ _ /';.'/ - ' : | |
\ \ '_. \_ __\ /__ _/ .-' / /
====='-.____'.___ \_____/___.-'____.-'=====
'=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
佛祖保佑 iii 永不宕机
EOT
echo -ne "\033[m"
#全局变量
# 系统IP
OS_IP=$(hostname -I)
# 主机名
OS_HOSTNAME=$(hostname)
# 系统版本
OS_VERSION=$(cat /etc/redhat-release)
# 内核版本
OS_KERNEL=$(uname -r)
# 主机负载
OS_LOAD=$(top -bn1 | grep load | awk '{printf "%.2f\n", $(NF-2)}')
# 系统运行时间
OS_UPTIME=$(uptime | sed 's/.*up \([^,]*\), .*/\1/')
# 系统时间
OS_DATE=`date +"%Y-%m-%d %H:%M:%S"`
# 当前目录
DIR=$(cd $(dirname $0); pwd)
# 输出文件
REPORT="report.txt"
###系统信息
echo -e "\n--------------------${OS_DATE} 开始检查--------------------\n" 2>&1|tee -a ${REPORT}
echo -e "###############主机系统信息###############" 2>&1|tee -a ${REPORT}
echo -e "系统IP:${OS_IP}" 2>&1|tee -a ${REPORT}
echo -e "主机名:${OS_HOSTNAME}" 2>&1|tee -a ${REPORT}
echo -e "系统版本:${OS_VERSION}" 2>&1|tee -a ${REPORT}
echo -e "系统内核版本:${OS_KERNEL}" 2>&1|tee -a ${REPORT}
echo -e "主机运行时间:${OS_UPTIME}" 2>&1|tee -a ${REPORT}
###CPU信息
CPU_NUM=$(grep "physical id" /proc/cpuinfo| sort | uniq | wc -l)
CPU_KERNEL=$(grep "cores" /proc/cpuinfo|uniq| awk -F ': ' '{print $2}')
CPU_TYPE=$(grep "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq)
#CPU时间计算公式:CPU_TIME=user+system+nice+idle+iowait+irq+softirq
#CPU使用率计算公式:cpu_usage=(idle2-idle1)/(cpu2-cpu1)*100
#默认时间间隔
TIME_INTERVAL=5
LAST_CPU_INFO=$(cat /proc/stat | grep -w cpu | awk '{print $2,$3,$4,$5,$6,$7,$8}')
LAST_SYS_IDLE=$(echo $LAST_CPU_INFO | awk '{print $4}')
LAST_TOTAL_CPU_T=$(echo $LAST_CPU_INFO | awk '{print $1+$2+$3+$4+$5+$6+$7}')
sleep ${TIME_INTERVAL}
NEXT_CPU_INFO=$(cat /proc/stat | grep -w cpu | awk '{print $2,$3,$4,$5,$6,$7,$8}')
NEXT_SYS_IDLE=$(echo $NEXT_CPU_INFO | awk '{print $4}')
NEXT_TOTAL_CPU_T=$(echo $NEXT_CPU_INFO | awk '{print $1+$2+$3+$4+$5+$6+$7}')
#系统空闲时间
SYSTEM_IDLE=`echo ${NEXT_SYS_IDLE} ${LAST_SYS_IDLE} | awk '{print $1-$2}'`
#CPU总时间
TOTAL_TIME=`echo ${NEXT_TOTAL_CPU_T} ${LAST_TOTAL_CPU_T} | awk '{print $1-$2}'`
#CPU使用率
CPU_USAGE=`echo ${SYSTEM_IDLE} ${TOTAL_TIME} | awk '{printf "%.2f", 100-$1/$2*100}'`
echo -e "###############主机CPU信息###############" 2>&1|tee -a ${REPORT}
echo -e "当前主机负载:${OS_LOAD}" 2>&1|tee -a ${REPORT}
echo -e "物理CPU个数:${CPU_NUM}" 2>&1|tee -a ${REPORT}
echo -e "单个CPU核数:${CPU_KERNEL}" 2>&1|tee -a ${REPORT}
echo -e "CPU型号:${CPU_TYPE}" 2>&1|tee -a ${REPORT}
echo -e "当前CPU使用率:${CPU_USAGE}%" 2>&1|tee -a ${REPORT}
###内存信息
MemTotal=$(grep MemTotal /proc/meminfo| awk '{print $2}') #KB
MemFree=$(grep MemFree /proc/meminfo| awk '{print $2}') #KB
let MemUsed=MemTotal-MemFree
MemPercent=$(awk "BEGIN {if($MemTotal==0){printf 100}else{printf \"%.2f\",$MemUsed*100/$MemTotal}}")
echo -e "###############主机内存信息###############" 2>&1|tee -a ${REPORT}
echo -e "内存总量:$((MemTotal/1024))MB" 2>&1|tee -a ${REPORT}
echo -e "内存剩余量:$((MemFree/1024))MB" 2>&1|tee -a ${REPORT}
echo -e "当前内存使用率:$(awk "BEGIN {if($MemTotal==0){printf 100}else{printf \"%.2f\",$MemUsed*100/$MemTotal}}")""%" 2>&1|tee -a ${REPORT}
#磁盘信息
DISK_all=$(df -h |awk '$6=="/"{sub("",$5);print $2}')
DISK_all_used=$(df -h |awk '$6=="/"{sub("",$5);print $5}')
echo -e "###############主机磁盘信息###############" 2>&1|tee -a ${REPORT}
echo -e "根目录大小:${DISK_all}" 2>&1|tee -a ${REPORT}
echo -e "根目录使用率:${DISK_all_used}" 2>&1|tee -a ${REPORT}
df -h|awk 'NR>2{print $6}'|grep -w / -v>tmp.txt
for i in `grep -v -P "sys|dev|run" tmp.txt`
do
DISK=$(df -h|grep ${i}|awk '{print $2}')
DISK_U=$(df -h|grep ${i}|awk '{print $5}')
echo -e "${i} 目录大小:${DISK}" 2>&1|tee -a ${REPORT}
echo -e "${i} 目录使用率:${DISK_U}" 2>&1|tee -a ${REPORT}
done
#Inode信息
INODE_all=$(df -i |awk '$6=="/"{sub("",$5);print $2}')
INODE_all_used=$(df -i |awk '$6=="/"{sub("",$5);print $5}')
echo -e "###############主机Inode信息###############" 2>&1|tee -a ${REPORT}
echo -e "根目录Inode总量:${INODE_all}" 2>&1|tee -a ${REPORT}
echo -e "根目录Inode使用率:${INODE_all_used}" 2>&1|tee -a ${REPORT}
df -h|awk 'NR>2{print $6}'|grep -w / -v>tmp.txt
for i in `grep -v -P "sys|dev|run" tmp.txt`
do
INODE=$(df -i|grep ${i}|awk '{print $2}')
INODE_U=$(df -i|grep ${i}|awk '{print $5}')
echo -e "${i} 目录Inode总量:${INODE}" 2>&1|tee -a ${REPORT}
echo -e "${i} 目录Inode使用率:${INODE_U}" 2>&1|tee -a ${REPORT}
done
#中间件-进程
echo -e "###############进程信息###############" 2>&1 | tee -a ${REPORT}
echo -e "开始检查中间件信息,此环节需要手动传参交互" 2>&1 | tee -a ${REPORT}
echo -e "\033[31m 请输入进程关键词,多个请使用空格隔开 \033[0m"
echo -e "\033[31m 例如:tomcat sdk(切记一个关键词对应一个进程,如果该主机存在一个关键词对应多个进程,请打标签) \033[0m"
read process
echo -e "您此次输入进程关键词为 {$process}" 2>&1|tee -a ${REPORT}
for var in $process
do
echo -e "当前检查 {$var} 进程 ------------------------" 2>&1 | tee -a ${REPORT}
pid=$(ps -aux|grep $var|grep -v grep|awk '{print $2}')
ps -aux|grep $var|grep -v grep $1>/dev/null 2>&1
if [ $? -ne 0 ];then
echo -e "未找到关于 {$var} 相关的进程信息,请手动确认" 2>&1|tee -a ${REPORT}
continue
else
num=$(ps -ef|grep $var|grep -v grep|wc -l)
if [ $num -gt 1 ];then
echo -e "{$var}关键词存在多个进程,请尝试使用标签,确保一个关键词对应一个进程" 2>&1|tee -a ${REPORT}
echo -e "\033[31m 跳过该关键词检索进程信息 \033[0m"
continue;
else
cpu=$(ps -aux|grep $var|grep -v grep|awk '{print $3}')
free=$(ps -aux|grep $var|grep -v grep|awk '{print $4}')
port=$(netstat -tanp|grep $pid|grep LISTEN|grep -v grep|awk '{print $4}'|awk -F":" '{print $NF}')
netstat -tanp|grep $pid|grep LISTEN|grep -v grep|awk '{print $4}'|awk -F":" '{print $NF}' >tmp.txt
if [ $? -ne 0 ];then
echo -e "{$var} 进程不存在端口号:" 2>&1|tee -a ${REPORT}
else
echo -e "{$var} 进程号为:$pid " 2>&1|tee -a ${REPORT}
echo -e "{$var} cpu占用百分比:$cpu% " 2>&1|tee -a ${REPORT}
echo -e "{$var} 内存占用百分比:$free% " 2>&1|tee -a ${REPORT}
hang=$(awk '{print NR}' tmp.txt | tail -1)
if [ $hang -gt 1 ];then
echo -e "{$var} 存在多个端口号:$(a=`cat tmp.txt`;echo $a)" 2>&1|tee -a ${REPORT}
else
echo -e "{$var} 端口号为:$port" 2>&1|tee -a ${REPORT}
fi
fi
fi
fi
done
#MySQL数据库
mysql_user=root
mysql_pass=Hdlh@2605
mysql_host=127.0.0.1
con="mysql -h ${mysql_host} -u ${mysql_user} -p${mysql_pass} -e"
echo -e "###############数据库信息###############" 2>&1 | tee -a ${REPORT}
netstat -tanp|grep mysqld|awk '{print $7}'|awk -F"/" '{print $1}'>tmp.txt
mysql_pid=$(awk 'END {print $0}' tmp.txt)
echo -e "MySQL数据库进程pid号:${mysql_pid} " 2>&1|tee -a ${REPORT}
mysql_port=$(netstat -tanp|grep $mysql_pid|grep LISTEN|grep -v grep|awk '{print $4}'|awk -F":" '{print $NF}')
netstat -tanp|grep $mysql_pid|grep LISTEN|grep -v grep|awk '{print $4}'|awk -F":" '{print $NF}' >tmp.txt
mysql_hang=$(awk '{print NR}' tmp.txt | tail -1)
if [ $mysql_hang -gt 1 ];then
echo -e "MySQL数据库存在多个端口号:$(a=`cat tmp.txt`;echo $a)" 2>&1|tee -a ${REPORT}
else
echo -e "MySQL数据库端口号为:$mysql_port" 2>&1|tee -a ${REPORT}
fi
mysql_cpu=$(ps -aux|grep $mysql_pid|grep -v grep|awk '{print $3}')
echo -e "MySQL数据库进程CPU使用占比:${mysql_cpu}% " 2>&1|tee -a ${REPORT}
mysql_free=$(ps -aux|grep $mysql_pid|grep -v grep|awk '{print $4}')
echo -e "MySQL数据库进程内存使用占比:${mysql_free}% " 2>&1|tee -a ${REPORT}
#版本
$con "select version();" >tmp.txt 2>&1
mysql_version=$(awk 'END {print $0}' tmp.txt)
echo -e "MySQL数据库版本号:${mysql_version} " 2>&1|tee -a ${REPORT}
#安装目录
$con "select @@basedir as basePath from dual;" >tmp.txt 2>&1
mysql_base=$(awk 'END {print $0}' tmp.txt)
echo -e "MySQL数据库安装目录:${mysql_base} " 2>&1|tee -a ${REPORT}
#数据目录
$con "select @@datadir as dataPath from dual;" >tmp.txt 2>&1
mysql_data=$(awk 'END {print $0}' tmp.txt)
echo -e "MySQL数据库数据目录:${mysql_data} " 2>&1|tee -a ${REPORT}
#错误日志
$con "select @@global.log_error;" >tmp.txt 2>&1
mysql_log=$(awk 'END {print $0}' tmp.txt)
echo -e "MySQL数据库日志文件:${mysql_log} " 2>&1|tee -a ${REPORT}
#慢查询
$con "show status like 'slow_queries';" >tmp.txt 2>&1
mysql_slow=$(awk 'END {print $2}' tmp.txt)
echo -e "MySQL数据库慢查询:${mysql_slow} " 2>&1|tee -a ${REPORT}
#连接数
$con "show status like 'Threads_connected';" >tmp.txt 2>&1
mysql_connect=$(awk 'END {print $2}' tmp.txt)
echo -e "MySQL数据库连接数:${mysql_connect} " 2>&1|tee -a ${REPORT}
#并发线程,此值越小越好
$con "show global status like '%threads_running%';" >tmp.txt 2>&1
mysql_threads=$(awk 'END {print $2}' tmp.txt)
echo -e "MySQL数据库并发线程:${mysql_threads} " 2>&1|tee -a ${REPORT}
#wait事件
$con "show status like 'Innodb_buffer_pool_wait_free';" >tmp.txt 2>&1
mysql_wait=$(awk 'END {print $2}' tmp.txt)
echo -e "MySQL数据库wait事件:${mysql_wait} " 2>&1|tee -a ${REPORT}
#主从
$con "show slave status\G';" >tmp.txt 2>&1
mysql_master_ip=$(egrep "Master_Host" tmp.txt|awk '{print $2}')
echo -e "MySQL数据库主库IP:${mysql_master_ip} " 2>&1|tee -a ${REPORT}
mysql_master_user=$(egrep "Master_User" tmp.txt|awk '{print $2}')
echo -e "MySQL数据库同步账号:${mysql_master_user} " 2>&1|tee -a ${REPORT}
mysql_master_DB=$(egrep "Replicate_Do_DB" tmp.txt|awk '{print $2}')
echo -e "MySQL数据库同步库名:${mysql_master_DB} " 2>&1|tee -a ${REPORT}
mysql_master_io=$(egrep -w "Slave_IO_Running" tmp.txt|awk '{print $2}')
if [ $mysql_master_io == Yes ];then
echo -e "MySQL数据库主从I/O线程:${mysql_master_io}-正常" 2>&1|tee -a ${REPORT}
else
echo -e "MySQL数据库主从I/O线程:${mysql_master_io}-异常" 2>&1|tee -a ${REPORT}
fi
mysql_master_sql=$(egrep -w "Slave_SQL_Running" tmp.txt|awk '{print $2}')
if [ $mysql_master_sql == Yes ];then
echo -e "MySQL数据库主从sql线程:${mysql_master_sql}-正常" 2>&1|tee -a ${REPORT}
else
echo -e "MySQL数据库主从sql线程:${mysql_master_sql}-异常" 2>&1|tee -a ${REPORT}
fi
mysql_master_wait=$(egrep "Seconds_Behind_Master" tmp.txt|awk '{print $2}')
echo -e "MySQL数据库主从延迟状态:$mysql_master_wait 秒" 2>&1|tee -a ${REPORT}
echo -e "\n--------------------${OS_DATE} 结束检查--------------------\n" 2>&1|tee -a ${REPORT}