安装依赖
需要net-tools、namp!
CentOS:yum -y install net-tools nmap
Ubuntu:apt-get update && apt-get -y install net-tools nmap
脚本内容
#!/usr/bin/env bash
# @author: rby
# system info
host_name="$(cat /etc/hostname)"
product_name="$(dmidecode -s system-product-name)"
os_version="$(lsb_release -d|sed 's/Description:[[:space:]]//g')"
code_version="$(uname -r)"
currtime="$(date +%Y-%m-%d \%H:%M:%S)"
timezone="$(cat /etc/timezone) $(date +"%Z %z")"
os_start_time="$(uptime -s)"
os_running_day="$(uptime |grep -Eo 'up.*days'|tr -d '[a-z ]')"
# cpu
cpu_model="$(lscpu |awk -F ': ' 'NR==13{print $2}')"
cpu_number="$(lscpu |awk 'NR==4{print $2}')"
cpu_load="$(top -bn 1 |head -n 5)"
cpu_process_top10="$(ps aux --sort=-%cpu|head -n 10 |awk '{print substr($0,0,120)}')"
# memory
memory_total="$(cat /proc/meminfo |awk 'NR==1{print $2/1024/1024}')GB"
memory_usage="$(free -mh)"
# disk
disk_dev_name="$(lsblk -l |awk '{if($6=="disk"){print $1}}'|tr '
' ','|sed 's/,$//g')"
disk_usage="$(df -Th)"
disk_partition="$(lsblk)"
disk_usage_gt80="$(df -Th |grep -v '^Filesystem'|awk '{split($6,a,"%");if(a[1]>80){print $0}}')"
# net
net_name="$(cat /proc/net/dev |grep -Ev '^Inter|^ face'|awk -F ':' '{print $1}'|sed 's/[[:space:]]//g')"
net_route="$(route -n)"
echo -e " 33[36m--------------------- System Info --------------------- 33[0m"
echo -e "主机名称: $host_name"
echo -e "主机厂商: $product_name"
echo -e "系统版本: $os_version"
echo -e "内核版本: $code_version"
echo -e "当前时间: $currtime"
echo -e "当前时区: $timezone"
echo -e "当前语言: $LANG"
echo -e "启动时间: $os_start_time"
echo -e "运行天数: $os_running_day"
echo -e " 33[36m--------------------- CPU ----------------------------- 33[0m"
echo -e "处理器型号: $cpu_model"
echo -e "处理器数量: $cpu_number"
echo -e "系统负载情况:
$cpu_load"
echo -e "使用高(top10)的进程:
$cpu_process_top10"
echo -e " 33[36m--------------------- Memory -------------------------- 33[0m"
echo -e "内存总量: $memory_total"
echo -e "内存使用情况:
$memory_usage"
echo -e " 33[36m--------------------- Disk ---------------------------- 33[0m"
echo -e "磁盘设备: $disk_dev_name"
echo -e "分区信息:
$disk_partition"
echo -e "磁盘使用情况:
$disk_usage"
echo -e "使用高(>80%)的卷:"
if [ ! -z "$disk_usage_gt80" ];then
echo -e " 33[33m$disk_usage_gt80 33[0m"
else
echo "None"
fi
echo -e " 33[36m--------------------- Network ------------------------- 33[0m"
echo -e "网卡信息:"
printf "%-20s %-20s %-20s %-20s %-20s %-20s
" "NAME" "IP" "MASK" "MAC" "RX" "TX"
for i in $net_name;do
name="$i"
ip="$(ifconfig $i |awk 'NR==2{split($2,a,":");{print a[2]}}')"
[ -z "$ip" ] && ip="-"
mask="$(ifconfig $i |awk 'NR==2{split($3,a,":");{print a[2]}}')"
[ -z "$mask" ] && mask="-"
mac="$(ifconfig $i |awk 'NR==1{print $5}')"
[ -z "$mac" ] && mac="-"
rx_rate="$(ifconfig $i |awk 'NR==7{print $3}'|tr -d '(')GB"
[ -z "$rx_tate" ] && rx_tate="-"
tx_rate="$(ifconfig $i |awk 'NR==7{print $7}'|tr -d '(')GB"
[ -z "$tx_tate" ] && tx_tate="-"
printf "%-20s %-20s %-20s %-20s %-20s %-20s
" $name $ip $mask $mac $rx_rate $tx_rate
done
echo -e "路由信息:
$net_route"
echo -e "扫描开放端口:"
for i in $net_name;do
ip="$(ifconfig $i |awk 'NR==2{split($2,a,":");{print a[2]}}')"
nmap -sS $ip
done
### system check ###
# system loadavg
if [ $(cat /proc/loadavg |awk -F '.' '{print $1}') -gt $(lscpu |awk 'NR==4{print $2}') ];then
echo -e " 33[33m警告: 当前系统负载严重,请快速处理! 33[0m"
exit 1
fi
# memory
if [ $(free -m |grep 'Mem'|awk '{if($6<1024){print 1}else{print 0}}') -eq 1 ];then
echo -e " 33[33m警告: 当前系统内存可用空间小于1G! 33[0m"
exit 1
fi
# network
ping www.baidu.com -c 1 >/dev/null
if [ $? -ne 0 ];then
echo -e " 33[33m警告: 当前系统网络不可用! 33[0m"
exit 1
fi
# io
if [ $(top -bn 1 |grep '%Cpu(s):' |awk '{if($10>30){print 1}else{print 0}}') -eq 1 ];then
echo -e " 33[33m警告: 当前系统IO负载过高! 33[0m"
exit 1
fi
echo ""
echo -e " 33[36m当前系统CPU、MEM、DISK、NETWORK工作正常! 33[0m"