zoukankan      html  css  js  c++  java
  • 读取系统执行状态的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	#系统时间"
    echo -e "os_version=$os_version	#操作系统版本号"
    echo -e "kernel-release=$kernel_release	#内核版本号"
    
    #########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	#cpu型号"
    echo -e "cpu_core_num=$cpu_core_num	#cpu核数"
    echo -e "cpu_phical_count=$cpu_phical_count	#cpu个数"
    echo -e "cpu_frequendy=$cpu_frequency	#主频/单个"
    echo -e "cache_size=${cache_size}*$cpu_process_num	#缓存"
    echo -e "cpu_idle=${cpu_idle}%	#空暇率"
    echo -e "cpu_used=${cpu_used}%	#使用率"
    
    ###########memeber info###############
    echo
    echo "[mem_info]"
    echo -e "mem_total=$(free -m | grep Mem | awk '{print$2}')	#总内存"
    echo -e "mem_used=$(free -m | grep buffers/cache | awk '{print$3}')	#已使用"
    echo -e "mem_free=$(free -m | grep buffers/cache | awk '{print$4}')	#可使用"
    
    ###########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

  • 相关阅读:
    struts2的核心和工作原理
    JAVA NIO学习笔记1
    [深入JUnit] 测试运行的入口
    阿里巴巴最新开源项目
    数据库进阶之路(五)
    flutter 常用plugins
    小程序 自定义组件 并实现组件间通讯
    小程序 背景叠阴影
    小程序 navigateTo传对象参数
    小程序 解决同行cell中 多点击事件冲突
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5207664.html
Copyright © 2011-2022 走看看