生产实践:
查看系统的基础信息及负载,用于系统排错等
学习技巧:
cut、dmidecode、egrep等使用
脚本内容:
#!/bin/bash ############################################################ # $Name: system_info.sh # $Version: v1.0 # $Function: For view the system basic information # $Author: Zhihe An # $Copyright (c) https://chegva.com # $Create Date: 2018-07-25 ############################################################ sys(){ echo -e '\n=========system:=========' dmidecode -t system |egrep "Manufacturer|Product Name" |xargs echo; uname -a|cut -d ' ' -f 2|sed s/^/HostName:' '/;hostname -I|sed s/^/IP:' '/;cat /etc/redhat-release |sed s/^/System:' '/;uname -a|cut -d ' ' -f 3|sed s/^/Kernel:' '/;uptime |sed s/^/Uptime:' '/; echo -e '\n=========CPU:=========' cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c lscpu |egrep -v 'Order|Vendor|Model|Stepping|BogoMIPS|family|On-line|op-mode|CPU\ socket|NUMA\ node' | perl -pe 's/(CPU\(s\))/\e[1;31m$1\e[0m/ig' echo -e '\n=========内存:=========' dmidecode |grep 'Range Size' | awk '{sub(/^[ \t]+/,"");print $0}' MEM_TOTAL=`free -m |awk '{if(NR==2)printf "%.1f",$2/1024}END{print "G"}'` USE=`free -m |awk '{if(NR==3) printf "%.1f",$3/1024}END{print "G"}'` FREE=`free -m |awk '{if(NR==3) printf "%.1f",$4/1024}END{print "G"}'` CACHE=`free -m |awk '{if(NR==2) printf "%.1f",($6+$7)/1024}END{print "G"}'` echo -e "Total: \033[31m$MEM_TOTAL\033[0m" echo -e "Use: \033[31m$USE\033[0m" echo -e "Free: $FREE" echo -e "Cache: $CACHE" free -m echo -e '\n=========磁盘:=========' cat /etc/fstab |egrep -v "^(#|$|[ ]*#)";echo;lsblk;echo;df -TH echo -e '\n=========网卡:=========' lspci -vvv |grep Ethernet echo ifconfig -a |awk -F'[: ]+' '{print $1}' |xargs echo echo route -n;echo;route |grep default echo arp -a echo -e '\n=========连接:=========' netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}';echo -e '\n=======top10K======='; netstat -na|grep ESTABLISHED|grep -v 127.0.0.1|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -rn |head |xargs -n1 -r -i bash -c "echo -n {};echo {}|awk '{ print \$2 }'|xargs host |awk -v OFS=' ' '{ print \$10,\$NF }'"; echo -e '\n=========top5-cpu:=========' ps auxw|head -1|perl -pe 's/(%cpu)/\e[1;31m$1\e[0m/ig';ps auxw|sort -rn -k3|head -5 echo -e '\n=========top5-mem:=========' ps auxw|head -1|perl -pe 's/(%mem)/\e[1;31m$1\e[0m/ig';ps auxf|sort -rn -k4|head -5 echo -e '\n=========服务:=========' pstree -aph;echo;service --status-all;echo echo -e '\n=========iptables:=========' iptables -nvL;echo echo -e '\n=========crontab:=========' crontab -l;echo; [[ -f "/etc/crontab" ]] && cat /etc/crontab; echo -e '\n=========rc.local:=========' cat /etc/rc.local /etc/rc.d/rc.local | egrep -v "^(#|$|[ ]*#)" echo -e '\n=========负载:=========' dstat -lcpdmsn 1 4 };clear;echo;sys
◎查看效果