#!/bin/bash
Color_Text()
{
echo -e " \e[0;$2m$1\e[0m"
}
Echo_Yellow()
{
echo $(Color_Text "$1" "33")
}
Echo_Yellown()
{
echo -n $(Color_Text "$1" "33")
}
Echo_Green()
{
echo $(Color_Text "$1" "32")
}
Echo_Red()
{
echo $(Color_Text "$1" "31")
}
Echo_Line()
{
echo ''
Echo_Yellow "-------> ${1}"
echo ''
}
[ $(id -u) -gt 0 ] && Echo_Red " 请用root用户执行此脚本!" && exit 1
centosversion=$(awk '{print $(NF-1)}' /etc/redhat-release)
Get_Service_State(){
if [[ $centosversion < 7 ]];then
if [ -e "/etc/init.d/$1" ];then
if [ `/etc/init.d/$1 status 2>/dev/null | grep -E "is running|正在运行" | wc -l` -ge 1 ];then
r="active"
else
r="inactive"
fi
else
r="unknown"
fi
else
r="$(systemctl is-active $1 2>&1)"
fi
echo "$1 服务状态:$r"
}
Get_Cpu_Status()
{
physical_cpus=$(cat /proc/cpuinfo |grep "physical id" | sort | uniq | wc -l)
processor_cpus=$(cat /proc/cpuinfo |grep "processor" | wc -l)
cpu_kernels=$(cat /proc/cpuinfo |grep "cpu cores" | uniq | awk -F' ' '{print $4}')
cpu_type=$(cat /proc/cpuinfo |grep "model name" | uniq | awk -F': ' '{print $2}')
cpu_arch=$(uname -m)
load_15=`uptime | awk '{print $NF}'`
average_load=`echo "scale=2;a=$load_15/$processor_cpus;if(length(a)==scale(a)) print 0;print a" | bc`
cpu_idle=`top -n1 |grep Cpu | awk '{print $8}'`
Echo_Line "CPU检查"
echo "物理CPU个数:$physical_cpus"
echo "逻辑CPU个数:$processor_cpus"
echo "每颗CPU核心数:$cpu_kernels"
echo "CPU型号:${cpu_type}"
echo "CPU架构:${cpu_arch}"
echo "CPU空闲率:$cpu_idle"
echo "CPU 15分钟内的平均负载:$load_15"
echo "每颗CPU 15分钟内的平均负载:$average_load"
}
Get_Mem_Status()
{
mem_total=$(free -m | grep Mem | awk '{print $2}')
mem_free=$(free -m | grep Mem | awk '{print $4}')
mem_available=$(free -m | grep Mem | awk '{print $7}')
let mem_used=($mem_total - $mem_free)
mem_used_percent=`echo "scale=2;$mem_used * 100 / $mem_total"|bc`
swap_total=$(free -m | grep Swap | awk '{print $2}')
swap_used=$(free -m | grep Swap | awk '{print $3}')
swap_free=$(free -m | grep Swap | awk '{print $4}')
Echo_Line "内存检查"
echo "内存总量:${mem_total} MB"
echo "内存余量:${mem_free} MB"
echo "内存可用量:${mem_available} MB"
echo "内存使用率:${mem_used_percent} %"
echo ''
echo "虚拟内存总量:${swap_total} MB"
echo "虚拟内存余量:${swap_free} MB"
echo "虚拟内存使用量:${swap_used} MB"
}
Get_Disk_Status()
{
ios=$(iostat -d 1 -c 4 | sed '1d')
df -hiP | sed 's/Mounted on/Mounted/'>> /tmp/inode
df -hTP | sed 's/Mounted on/Mounted/'>> /tmp/disk
Echo_Line "磁盘检查"
echo "`join /tmp/disk /tmp/inode | grep -v docker | awk '{print $1,$2,"|",$3,$4,$5,$6,"|",$8,$9,$10,$11,"|",$12}'`" | xargs -n14 | column -t
rm -rf /tmp/inode /tmp/disk
echo ''
df -hTP |grep -v 'docker\|tmpfs' | sed 's/Mounted on/Mounted/' >> /tmp/disk
echo "`cat /tmp/disk`" | xargs -n7 |column -t
rm -rf /tmp/disk
Echo_Line "IO检查"
echo "$ios"
Echo_Yellow "-------------------------------------------------------------"
iostat -d 1 -c 4 -k -x
}
Get_Sys_Status()
{
export LANG="en_US.UTF-8"
if [ -e /etc/sysconfig/i18n ];then
default_LANG="$(grep "LANG=" /etc/sysconfig/i18n | grep -v "^#" | awk -F '"' '{print $2}')"
else
default_LANG=$LANG
fi
Release=$(cat /etc/redhat-release 2>/dev/null)
Kernel=$(uname -r)
OS=$(uname -o)
Hostname=$(uname -n)
SELinux=$(getenforce)
LastReboot=$(who -b | awk '{print $3,$4}')
uptime=$(uptime | awk -F',' '{print $1,$2}')
Echo_Line "系统相关信息"
echo "系统: $OS"
echo "内核: $Kernel"
echo "编码: $default_LANG"
echo "主机名: $Hostname"
echo "SELinux: $SELinux"
echo "发行版本: $Release"
echo "当前时间: $(date +'%F %T')"
echo "最后启动: $LastReboot"
echo "运行时间: $uptime"
}
Get_Service_Status()
{
if [[ $centosversion > 7 ]];then
systemctl list-unit-files --type=service --state=enabled --no-pager | grep "enabled" >> /tmp/svc.enabled
systemctl list-units --type=service --state=running --no-pager | grep ".service" | awk '{print $1}' >> /tmp/svc.running
else
/sbin/chkconfig | grep -E ":on|:启用" >> /tmp/svc.enabled
/sbin/service --status-all 2>/dev/null | grep -E "is running|正在运行" >> /tmp/svc.running
fi
#Echo_Line "系统服务配置"
#cat /tmp/svc.enabled | column -t
Echo_Line "系统正在运行的服务"
cat /tmp/svc.running | column -t
rm -rf /tmp/svc.running /tmp/svc.enabled
}
Auto_Start_Status()
{
Echo_Line "自启动列表"
conf=$(grep -v "^#" /etc/rc.d/rc.local| sed '/^$/d')
echo "$conf"
}
Get_Login_Status(){
Echo_Line "最近登录记录"
last | head
}
Get_Listen_Status(){
Echo_Line "TCP监听端口"
echo "`ss -ntul | sed '1d' | awk '/tcp/ {print $5}' | awk -F: '{print $NF}' | sort |uniq`"
Echo_Line "系统建立的链接"
netstat -naptu |grep ESTABLISHED
}
Get_Cron_Status(){
Echo_Line "定时任务"
Get_Service_State crond
for shell in $(grep -v "/sbin/nologin" /etc/shells);do
for user in $(grep "$shell" /etc/passwd| awk -F: '{print $1}');do
crontab -l -u $user >/dev/null 2>&1
status=$?
if [ $status -eq 0 ];then
echo "$user"
Echo_Green "-----------"
crontab -l -u $user
echo ""
fi
done
done
}
Get_Sudoers_Status(){
Echo_Line "sudoer配置"
grep -v "^#" /etc/sudoers| grep -v "^Defaults" | sed '/^$/d'
}
Get_Installed_Status(){
Echo_Line "最近安装的10个应用包"
rpm -qa --last | head | column -t
}
Get_Process_Status(){
Echo_Line "僵尸进程"
if [ $(ps -ef | grep defunct | grep -v grep | wc -l) -ge 1 ];then
echo ""
echo "僵尸进程";
Echo_Green "--------"
ps -ef | head -n1
ps -ef | grep defunct | grep -v grep
fi
Echo_Line "CPU占用前十排序"
top b -n1 | head -17 | tail -11
Echo_Line "内存占用前十排序"
ps aux | awk '{print $2, $4, $6, $11}' | sort -k3rn | head -n 10 | column -t
}
Get_Dns_Status(){
Echo_Line "系统DNS配置"
cat /etc/resolv.conf
}
Get_Rsyslog_Status(){
Echo_Line "Syslog配置"
Get_Service_State "rsyslog"
cat /etc/rsyslog.conf 2>/dev/null | grep -v "^#" | grep -v "^\\$" | sed '/^$/d' | column -t
}
Get_User(){
Echo_Line "系统用户列表"
cat /etc/passwd | awk -F':' '{print $1,$NF}' | column -t
}
Get_Network_Status(){
Echo_Line "网卡配置"
ips=$(ip -f inet addr | grep -v 127.0.0.1 |grep inet | awk '{print $2,$NF}')
gateway=$(ip route | grep default | awk '{print $3}')
dns=$(grep nameserver /etc/resolv.conf| grep -v "#" | awk '{print $2}' | tr '\n' ',' | sed 's/,$//')
echo "IP:"
echo "$ips"
echo "网关:$gateway "
echo "DNS:$dns"
ping -c 2 www.baidu.com >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "公网连接:正常"
else
echo "公网连接:异常"
fi
}
Check(){
Get_Cpu_Status
Get_Mem_Status
Get_Disk_Status
Get_Sys_Status
Get_Network_Status
Get_Process_Status
Get_User
Auto_Start_Status
Get_Login_Status
Get_Cron_Status
Get_Sudoers_Status
Get_Installed_Status
Get_Dns_Status
Get_Rsyslog_Status
Get_Service_Status
Get_Listen_Status
echo ''
echo ''
}
Check | more