博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
读取系统执行状态的shell脚本
阅读量:6943 次
发布时间:2019-06-27

本文共 3660 字,大约阅读时间需要 12 分钟。

近期在学习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

转载地址:http://ftanl.baihongyu.com/

你可能感兴趣的文章
杭电1702--ACboy needs your help again!
查看>>
web前端开发分享-css,js进阶篇
查看>>
安大校赛/异或运算一题。
查看>>
强制回收和IDisposable.Dispose方法
查看>>
mybatis plus条件构造器
查看>>
quick sort(重复数版)
查看>>
乌班图 root权限获取
查看>>
Java内部类
查看>>
趣说Java:我是一个线程
查看>>
HDU 1498 50 years, 50 colors
查看>>
杭电 1874 畅通工程续 (求某节点到某节点的最短路径)
查看>>
PHP添加mongodb驱动的问题
查看>>
JS将秒转换为 天-时-分-秒
查看>>
CRUD
查看>>
Unity3D性能优化--- 收集整理的一堆
查看>>
数据库基础
查看>>
基础概要
查看>>
网络传输中的三张表,MAC地址表、ARP缓存表以及路由表
查看>>
FOR ALL ENTRIES IN 与 INNER JOIN 写在一个SQL上影响效率
查看>>
【转载】aspx,ascx和ashx使用小结
查看>>