需要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 '\n' ','|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 "\033[36m--------------------- System Info ---------------------\033[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 "\033[36m--------------------- CPU -----------------------------\033[0m"
echo -e "处理器型号: $cpu_model"
echo -e "处理器数量: $cpu_number"
echo -e "系统负载情况: \n$cpu_load"
echo -e "使用高(top10)的进程: \n$cpu_process_top10"
echo -e "\033[36m--------------------- Memory --------------------------\033[0m"
echo -e "内存总量: $memory_total"
echo -e "内存使用情况: \n$memory_usage"
echo -e "\033[36m--------------------- Disk ----------------------------\033[0m"
echo -e "磁盘设备: $disk_dev_name"
echo -e "分区信息: \n$disk_partition"
echo -e "磁盘使用情况: \n$disk_usage"
echo -e "使用高(>80%)的卷:"
if [ ! -z "$disk_usage_gt80" ];then
echo -e "\033[33m$disk_usage_gt80 \033[0m"
else
echo "None"
fi
echo -e "\033[36m--------------------- Network -------------------------\033[0m"
echo -e "网卡信息:"
printf "%-20s %-20s %-20s %-20s %-20s %-20s\n" "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\n" $name $ip $mask $mac $rx_rate $tx_rate
done
echo -e "路由信息:\n$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 "\033[33m警告: 当前系统负载严重,请快速处理!\033[0m"
exit 1
fi
# memory
if [ $(free -m |grep 'Mem'|awk '{if($6<1024){print 1}else{print 0}}') -eq 1 ];then
echo -e "\033[33m警告: 当前系统内存可用空间小于1G!\033[0m"
exit 1
fi
# network
ping www.baidu.com -c 1 >/dev/null
if [ $? -ne 0 ];then
echo -e "\033[33m警告: 当前系统网络不可用!\033[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 "\033[33m警告: 当前系统IO负载过高!\033[0m"
exit 1
fi
echo ""
echo -e "\033[36m当前系统CPU、MEM、DISK、NETWORK工作正常!\033[0m"
手机扫一扫
移动阅读更方便
你可能感兴趣的文章