Shell Scripting Advanced 2


 1. Query system basic situation script

# !/bin/bash #System 
status query script 
#Output basic system information
#
#system version 
SYSTEM_VERSION=`cat /etc/ redhat-release` #hostname
 
HOSTNAME= `hostname`
 #network card name 
DEV= " ens32 " # IP address IP_ADDR 
=`ifconfig $DEV | grep -v inet6 | grep inet | awk ' {print $2} ' `
 # MAC address 
MAC_ADDR=`ifconfig $DEV | awk ' /ether/{print $2} ' `
 # Kernel version 
KERNEL_VERSION=`uname -a | awk ' {print $3} ' `
 # CPU version 
CPU_VERSION= `cat /proc/cpuinfo | grep -n '
^model name ' | awk -F " : "  ' {print $3 } ' |sed ' s/^ //g ' `
 #Current time 
DATE= `date` #Start
 time DATE_START 
=`date -d " $(awk - F. '{print $1}' /proc/uptime) second ago " + " %Y-%m-%d %H:%M:%S " `
 #
 echo -e " \n+------- -- $DATE --------+\n " 
echo -e " hostname:\n$HOSTNAME\n " 
echo -e " system version:\n$SYSTEM_VERSION\n"
echo -e " kernel version:\n$KERNEL_VERSION\n " 
echo -e " CPU version:\n$CPU_VERSION\n " 
echo -e " system IP:\n$DEV $IP_ADDR\n " 
echo -e " system MAC :\n$DEV $MAC_ADDR\n " 
echo -e " Startup time:\n$DATE_START\n " 
# ------------------------- -- #system load 
# uptime 
# --------------------------- 
CPU_NUM 
=`cat /proc/cpuinfo | grep processor | awk -F " : " ' {print $2} ' | wc - l`
Load =`uptime | awk -F " average: "  ' {print $2} ' | awk -F " , "  ' {print "<__system load__> --> ""CPU count: '" $CPU_NUM "' ""\t1min:"$1"\t5min:"$2"\t15min:"$3} ' `
echo -e "\n$Load\n"
#---------------------------
# CPU状态
# vmstat
#---------------------------
CPU_CHECK=`vmstat | awk '{print NR $0}' | awk '/^3/{print "<__  CPU状态   __>  -->  ""CPU使用:"$14"%\t""CPU空闲:"$16"%"}'`
echo -e " $CPU_CHECK\n " 
# ----------------------------- 
#Memory status 
# free 
# ------- -------------------- 
MEMORY=`free -mh | awk ' /^Mem/{print "<__ memory status__> --> ""The total memory Amount:"$2"\tUsed memory:"$3"\tRemaining memory:"$4"\tAvailable memory:"$7} ' `
echo -e " $MEMORY\n " 
# -------------------------- 
#disk usage 
# df 
# -------- ------------------ 
DISK=`df -h | grep -n ' /$ ' | awk ' {print "<__disk_use__> --> "" Disk capacity: "$2"\tUsed capacity:"$3"\tAvailable capacity:"$4} ' `
echo -e " $DISK\n " 
# ----------------------------- 
#Process statistics 
# top 
# ------- -------------------- 
PROCESS=`top -bn1 | awk ' /Tasks:/{print $0} ' | awk ' {print "<__process stats__ > --> ""Total number of processes:"$2"\tRunning process:"$4"\tSleeping process:"$6"\tStopping process:"$8"\tZombie process:"$10} ' `
echo -e " $PROCESS\n " 
# --------------------------- 
#Disk IO usage 
# sar 
# ----- ---------------------- 
IO=`sar -b | grep -n ' ^[^0-9] ' | awk ' {print NR $0} ' | awk ' /^2/{print"<__disk read/write__> --> ""read/sec:"$5" bytes""\twrite/sec:"$6" bytes"} ' `
echo -e " $IO\n " 
# --------------------------- #Network 
card traffic usage 
# sar 
# ----- ---------------------- 
NETWORK=`sar -n DEV | grep $DEV | grep -n ' ^[^0-9] ' | awk ' { print "<__ NIC traffic__> --> ""NIC name:"$2"\tpacket receipt/sec:"$3"\tpacket/sec:"$4"\tdownload/sec:"$5" Bytes" "Uploads/sec: "$6" Bytes"} ' `
echo -e "$NETWORK\n"
system_check.sh
[root@localhost shell]# sh system_check.sh 

+--------- Friday April 27th, 2018 11:00:55 CST --------+

CPU name:
localhost.localdomain

system version:
CentOS Linux release 7.4.1708 (Core) 

Kernel version:
3.10.0-693 .el7.x86_64

CPU version:
Intel(R) Xeon(R) CPU E5-2603 v4 @ 1.70GHz
Intel(R) Xeon(R) CPU E5-2603 v4 @ 1.70GHz

System IP:
ens32 192.168.1.159

System MAC:
ens32 00:0c:29:b0:e8:12

Start Time:
2018-04-19 09:12:48


<__ system load__> --> CPU count: 2 1 minute: 0.12 5 minutes: 0.04 15 minutes: 0.05

<__ CPU status__> --> CPU usage: 0% CPU idle: 100%

<__ Memory status__> --> Total memory: 3.7G Used memory: 181M Remaining memory: 2.8G Available memory: 3.2G

<__ Disk usage__> --> Disk capacity: 50G Used capacity: 1.3G Available capacity: 49G

<__ Process statistics__> --> Total number of processes: 116 Running processes: 1 Sleeping processes: 115     Stopping processes: 0 Zombie processes: 0

<__ Disk read/write__> --> Read/sec: 0.00 bytes Write/sec: 0.93 bytes

<__ NIC traffic__> --> NIC name: ens32 Receive packets/sec: 10.74 Send packets/sec: 1.60 Download/sec: 0.65 Bytes Upload/sec: 0.10 Bytes
output case

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324819008&siteId=291194637