讀取系統運行狀態的shell指令碼

來源:互聯網
上載者:User

標籤:系統配置   系統運行狀態   shell   linux   

最近在學習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++))doReceive_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}')echoecho "[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###############echoecho "[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)echoecho "[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

讀取系統運行狀態的shell指令碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.