读取系统运行状态的shell脚本

最近在学习shell,老大让写一个读取系统配置信息的脚本当作练习和工作验收,我就写了这么一个脚本,读取操作系统,内核,网卡,cpu,内存,磁盘等信息,目的是让看的人一眼就能看出这台机子的配置以及目前的运行状况:

#!/bin/bash
#Get system infomation
(
sys_time=$(date +"%Y-%m-%d %k:%M:%S")
#os_version=$(lsb_release -a | sed -n '/Description/p' | awk -F '[:]' '{print $2}' | sed 's/^[[:space:]]*//')
os_version=$(cat /etc/issue | grep Linux)
kernel_release=$(uname -r)
netcard_num=$(ifconfig -a | grep eth | wc -l)
echo "[public_info]"
echo -e "sys_time=$sys_time\t#系统时间"
echo -e "os_version=$os_version\t#操作系统版本"
echo -e "kernel-release=$kernel_release\t#内核版本"

#########NETCADE INFOMATION##########
echo 
echo "[netcard_info]"
echo "netcard_num=$netcard_num"
echo "#网卡名字|IP|MAC|网卡驱动|网卡速率|网卡发送流量(bytes)|网卡接收流量(bytes)|网卡总流量(bytes)"
for((n=0;n<$netcard_num;n++))
do
Receive_byte=$(cat /proc/net/dev | grep eth$n | awk '{print$2}')
Send_byte=$(cat /proc/net/dev | grep eth$n | awk '{print$10}')
echo "netcard_$((n+1))=eth$n|$(ifconfig eth$n | grep "inet addr" | awk '{print$2}' | awk -F'[:]' '{print$2}')|$(ifconfig -a | grep eth$n | awk '{print$5}')|$(ethtool eth$n | grep Speed | awk '{print$2}' | sed 's/^[[:space:]]*//')|${Receive_byte}|${Send_bytei}|$(($Receive_byte + $Send_byte))"
done

##########CPU INFOMATION##############
cpu_phical_count=$(cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l)
cpu_model=$(cat /proc/cpuinfo | grep "model name" | uniq | awk -F'[:]' '{print$2}')
cpu_core_num=$(cat /proc/cpuinfo | grep cores | uniq | awk -F'[:]' '{print $2}' | sed 's/^[[:space:]]*//')
cpu_process_num=$(cat /proc/cpuinfo | grep process | wc -l)
cpu_frequency=$(cat /proc/cpuinfo |grep MHz|uniq | awk -F'[:]' '{print $2}' | sed 's/^[[:space:]]*//')
cache_size=$(cat /proc/cpuinfo | grep "cache size" | uniq | awk -F'[:]' '{print$2}')
cpu_idle=$(mpstat | grep all | awk '{print$11}')
cpu_used=$(mpstat | grep all | awk '{print$3}')
echo
echo "[cpu_info]"
echo -e "cpu_model=$cpu_model\t#cpu型号"
echo -e "cpu_core_num=$cpu_core_num\t#cpu核数"
echo -e "cpu_phical_count=$cpu_phical_count\t#cpu个数"
echo -e "cpu_frequendy=$cpu_frequency\t#主频/单个"
echo -e "cache_size=${cache_size}*$cpu_process_num\t#缓存"
echo -e "cpu_idle=${cpu_idle}%\t#空闲率"
echo -e "cpu_used=${cpu_used}%\t#使用率"

###########memeber info###############
echo
echo "[mem_info]"
echo -e "mem_total=$(free -m | grep Mem | awk '{print$2}')\t#总内存"
echo -e "mem_used=$(free -m | grep buffers/cache | awk '{print$3}')\t#已使用"
echo -e "mem_free=$(free -m | grep buffers/cache | awk '{print$4}')\t#可使用"

###########hard info ##################
file_system_num=$(df -Ph | grep / | wc -l)
echo
echo "[hard_info]"
echo "file_system_num=$file_system_num"
echo "#磁盘总容量(单位M)|已用容量(单位M)|可用流量(单位M)|已用百分比(%)|挂载目录"
df -Pm | grep / | awk '{print$2"|"$3"|"$4"|"$5"|"$6}'
exit 0
) >system_infomation.txt

运行的结果是这个样子的:

[public_info]
sys_time=2014-12-18 11:42:43 #系统时间
os_version=Red Hat Enterprise Linux Server release 6.4 (Santiago) #操作系统版本
kernel-release=2.6.32-358.el6.i686 #内核版本


[netcard_info]
netcard_num=4
#网卡名字|IP|MAC|网卡驱动|网卡速率|网卡发送流量(bytes)|网卡接收流量(bytes)|网卡总流量(bytes)
netcard_1=eth0||28:51:32:04:68:31|1000Mb/s|6863054||13709858
netcard_2=eth1||28:51:32:04:68:32|Unknown!|0||0
netcard_3=eth2||28:51:32:04:68:33|1000Mb/s|6846296||13709858
netcard_4=eth3|192.168.6.193|28:51:32:04:68:34|1000Mb/s|209054928||209117419


[cpu_info]
cpu_model= Intel(R) Atom(TM) CPU D525   @ 1.80GHz #cpu型号
cpu_core_num=2 #cpu核数
cpu_phical_count=1 #cpu个数
cpu_frequendy=1800.000 #主频/单个
cache_size= 512 KB*4 #缓存
cpu_idle=99.60% #空闲率
cpu_used=0.06% #使用率


[mem_info]
mem_total=993 #总内存
mem_used=566 #已使用
mem_free=427 #可使用


[hard_info]
file_system_num=4
#磁盘总容量(单位M)|已用容量(单位M)|可用流量(单位M)|已用百分比(%)|挂载目录
162650|17438|136950|12%|/
497|0|497|0%|/dev/shm
291|31|245|12%|/boot
78745|815|73931|2%|/home

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。