码迷,mamicode.com
首页 > 其他好文 > 详细

一个bash脚本

时间:2015-10-15 18:53:05      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:bash

#!/bin/bash

# system info report

def_colors () {
    # Attributes
    normal=‘\033[0m‘; bold=‘\033[1m‘; dim=‘\033[2m‘; under=‘\033[4m‘
    italic=‘033[3m‘;  notalic=‘\033[23m‘; blink=‘\033[5m‘;
    reverse=‘\033[7m‘; conceal=‘\033[8m‘; nobold=‘\033[22m‘;
    nounder=‘\033[24m‘; noblink=‘\033[25m‘

    # Foreground
    black=‘\033[30m‘; red=‘\033[31m‘; green=‘\033[32m‘; yellow=‘\033[33m‘
    blue=‘\033[34m‘; magenta=‘\033[35m‘; cyan=‘\033[36m‘; white=‘\033[37m‘

    # Background
    bblack=‘\033[40m‘; bred=‘\033[41m‘
    bgreen=‘\033[42m‘; byellow=‘\033[43m‘
    bblue=‘\033[44m‘; bmagenta=‘\033[45m‘
    bcyan=‘\033[46m‘; bwhite=‘\033[47m‘
}

def_colors

clear

hostname=`cat /proc/sys/kernel/hostname`
echo
echo -e "System Report for $yellow$hostname$normal on `date`"
echo

prcessor=`grep ‘model name‘ /proc/cpuinfo | cut -d: -f2 | cut -c2-`
nisdomain=`cat /proc/sys/kernel/domainname`

cache=`grep ‘cache size‘ /proc/cpuinfo | awk ‘{print $4, $5}‘`
bogomips=`grep ‘bogomips‘ /proc/cpuinfo | awk ‘{print $3}‘`
vendor=`grep ‘vendor_id‘ /proc/cpuinfo`

echo -e "Hostname: $yellow$hostname$normal NIS Domain: $white$nisdomain$normal"

if [ "`echo $vedner | grep -i intel`" ]
then
    cpu_color=$blue
elif [ "`echo $vender | grep -i amd`" ]
then
    cpu_color=$green
fi

echo -e "Processor: $cpu_color$processor$normal"
echo -e "Running at $white$bogomips$normal bogomips with $white$cache$normal cache"
echo

ostype=`cat /proc/sys/kernel/ostype`
osrelease=`cat /proc/sys/kernel/osrelease`
rev=`cat /proc/sys/kernel/version | awk ‘{print $1}‘`
da_date=`cat /proc/sys/kernel/version | cut -d\  -f2-`
upsec=`awk ‘{print $1}‘ /proc/uptime`
uptime=`echo "scale=2;$upsec/86400" | bc`

echo -e "OS Type: $white${ostype}$normal"
echo -e "Kernel:  $white${osrelease}$normal"
echo -e "Kernel Compile $white${rev}$normal on $white${da_date}$normal"
echo -e "Uptime: $magenta${uptime}$normal days"

echo 
echo -e "${red}Network Statistics:${normal}"
echo "-------------------"
iface_list=`ip addr |grep -A1 -B1 "link/ether"`

echo ${iface_list} |awk ‘{
if (NF == 0) {
  print "No Network Info";
} else {
  num_of_iface = int(NF / 22);
  for (i = 1; i <= num_of_iface; i++) {
    print $(23*i-21), $(23*i-10), $(23*i-6);
  }
  }
}‘

echo
echo -e "${red}Memory Statistics:${normal}"
echo "------------------"

# statistics the memory info
set `grep MemTotal /proc/meminfo`
tot_mem=$2; 
tot_mem_unit=$3

set `grep MemFree /proc/meminfo`
free_mem=$2; 
fre_mem_unit=$3
perc_mem_used=$((100-(100*free_mem/tot_mem)))

set `grep SwapTotal /proc/meminfo`
tot_swap=$2; 
tot_swap_unit=$3

set `grep SwapFree /proc/meminfo`
free_swap=$2
free_swap_unit=$3
perc_swap_used=$((100-(100*free_swap/tot_swap)))

if [ $perc_mem_used -lt 80 ]
then
    mem_color=$green
elif [ $perc_mem_used -ge 80 -a $perc_mem_used -lt 90 ]
then
    mem_color=$yellow
else
    mem_color=$red
fi

if [ $perc_swap_used -lt 80 ]
then
    swap_color=$green
elif [ $perc_swap_used -ge 80 -a $perc_swap_used -lt 90 ]
then
    swap_color=$yellow
else
    swap_color=$red
fi

echo -e "Memory:\n-------\nTotal:\t$white${tot_mem}$normal ${tot_mem_unit}\nFree:\t$white${free_mem}$normal ${fre_mem_unit}\nUsed:\t${mem_color}${perc_mem_used}%$normal"
echo
echo -e "Swap:\n-----\nTotal:\t$white${tot_swap}$normal ${tot_swap_unit}\nFree:\t$white${free_swap}$normal ${free_swap_unit}${fre_swap_unit}\nUsed:\t${swap_color}${perc_swap_used}%$normal"

echo

# statistics the load average info
set `cat /proc/loadavg`
one_min=$1
five_min=$2
fifteen_min=$3

echo -e "${red}Load Adverage:${normal}"
echo "---------------"
echo "1min 5min 15min"
echo "---- ---- -----"

for ave in $one_min $five_min $fifteen_min
do
    int_ave=`echo $ave | cut -d. -f1`
    if [ $int_ave -lt 1 ]
    then
        echo -en "$green$ave$normal "
    elif [ $int_ave -ge 1 -a $int_ave -lt 5 ]
    then
        echo -en "$yellow$ave$normal "
    else
        echo -en "$red$ave$normal"
    fi
done

echo

# statistics the process info
running=0;
sleeping=0;
stopped=0;
zombie=0
for pid in /proc/[1-9]*
do
    procs=$((procs+1))
    stat=`awk ‘{print $3}‘ ${pid}/stat`
    case $stat in
        R) running=$((running+1))
           ;;
	    S) sleeping=$((sleeping+1))
           ;;
        T) stopped=$((stopped+1))
           ;;
        Z) zombie=$((zombie+1))
           ;;
    esac
done

echo 
echo -e "${red}Process Counting:${normal}"
echo "-----------------"
echo -e "$white${procs}$normal \t total\n$white$running$normal \t running\n$white${sleeping}$normal \t sleeping\n$white${stopped}$normal \t stopped\n$white$zombie$normal \t zombie"
echo


运行结果为:

# bash sys_info.sh 

System Report for master on Thu Oct 15 08:33:02 CST 2015

Hostname: master NIS Domain: (none)
Processor: 
Running at 4389.86 bogomips with 3072 KB cache

OS Type: Linux
Kernel:  2.6.32-573.3.1.el6.x86_64
Kernel Compile #1 on SMP Thu Aug 13 22:55:16 UTC 2015
Uptime: .06 days

Network Statistics:
-------------------
eth0: 08:00:27:00:00:00 10.11.1.51/24
eth1: 08:00:27:00:00:01 192.168.56.108/24

Memory Statistics:
------------------
Memory:
-------
Total:	1020176 kB
Free:	499448 kB
Used:	52%

Swap:
-----
Total:	1015804 kB
Free:	1015804 kB
Used:	0%

Load Adverage:
---------------
1min 5min 15min
---- ---- -----
0.06 0.02 0.00 

Process Counting:
-----------------
104 	 total
0 	 running
104 	 sleeping
0 	 stopped
0 	 zombie


本文出自 “天道酬勤” 博客,请务必保留此出处http://lavenliu.blog.51cto.com/5060944/1703252

一个bash脚本

标签:bash

原文地址:http://lavenliu.blog.51cto.com/5060944/1703252

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!