Bash脚本显示当前系统运行报告

简介:

下面这个脚本是我之前学习bash脚本时,从一本书上看到了,觉得很不错,所以拿来跟大家分享一下哈。呵呵。别的就不多说了,看代码吧:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
 
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 $white$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: $white$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"
 
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
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: $white$tot_mem$normal $tot_mem_unit Free: $white$free_mem$normal \
$fre_mem_unit $Used: $mem_color$perc_mem_used$normal"
 
echo  -e "Swap:   $white$tot_swap$normal $tot_swap_unit Free: $white$free_swap$normal \
$fre_swap_unit $Used: $swap_color$perc_swap_used$normal"
 
echo
 
set  ` cat  /proc/loadavg `
one_min=$1
five_min=$2
fifteen_min=$3
echo  -n  "Load Avderage: "
 
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
 
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  -n  "Process Count:"
echo  -e "$white$process$normal total $white$running$normal running \
$white$ sleep $normal sleeping $white$stopped$normal stopped \
$white$zombie$normal zombie"
echo


至此,脚本完毕,来运行一下,看有什么结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
System Report  for  xxxxxx on Wed Jun  4 17:09:54 CST 2014
 
Hostname: xxxxxx NIS Domain: 
Processor: 
Running at 6077.22
6077.22
...此处省略很多行
6077.22 bogomips with 2048 KB
2048 KB
...此处省略很多行
2048 KB cache
 
OS Type: Linux
Kernel:  3.10.13-0.96-xen
Kernel Compile  #1 on SMP Wed Feb 15 13:33:49 UTC 2012 (d73692b)
Uptime: 26.09 days
Memory: 4155904 kB Free:   : 100
Swap:   16779260 kB Free:   : 100
 
Load Avderage: 0.00 0.01 0.05 
Process Count: total 0 running  sleeping 0 stopped 4 zombie


本文转自   bigstone2012  51CTO博客,原文链接:http://blog.51cto.com/lavenliu/1422169

相关文章
|
8天前
|
监控 安全 Shell
防止员工泄密的措施:在Linux环境下使用Bash脚本实现日志监控
在Linux环境下,为防止员工泄密,本文提出使用Bash脚本进行日志监控。脚本会定期检查系统日志文件,搜索敏感关键词(如"password"、"confidential"、"secret"),并将匹配项记录到临时日志文件。当检测到可疑活动时,脚本通过curl自动将数据POST到公司内部网站进行分析处理,增强信息安全防护。
121 0
|
9月前
|
Ubuntu 安全 Linux
不用安装虚拟机,直接在Windows上面运行Linux Bash Shell,嗯!真香!!!
不用安装虚拟机,直接在Windows上面运行Linux Bash Shell,嗯!真香!!!
162 0
|
8天前
|
存储 Shell Linux
Linux Bash 脚本中的 IFS 是什么?
【4月更文挑战第25天】
30 0
Linux Bash 脚本中的 IFS 是什么?
|
6天前
|
Shell Linux 应用服务中间件
centos系统内容器部署服务访问不到是因为run后面加了bash
centos系统内容器部署服务访问不到是因为run后面加了bash
|
8天前
|
存储 弹性计算 运维
用bash脚本创建目录
【4月更文挑战第29天】
16 3
|
8天前
|
存储 Unix Shell
【简化Cmake编译过程 】编写通用的bash脚本:简化和构建cmake高效自动化任务
【简化Cmake编译过程 】编写通用的bash脚本:简化和构建cmake高效自动化任务
55 0
|
7月前
|
监控 Shell Linux
使用Python和Bash编写内网监控工具:自动巡检脚本示例
为了确保内网的顺畅运行,自动化监控工具变得不可或缺。本文将介绍如何使用Python和Bash编写一个简单而强大的内网监控工具,它可以帮助您自动巡检网络和系统状态,及时发现问题并采取措施。
255 0
|
9月前
|
Shell
使用Bash备份脚本
使用Bash备份脚本
57 1
|
9月前
|
Shell 测试技术 Python
如何在Bash Shell脚本中使用`exec`命令?
如何在Bash Shell脚本中使用`exec`命令?
153 0
|
11月前
|
运维 Shell
善用chatGPT学习 | bash脚本如何判断字符串在数组中
善用chatGPT学习 | bash脚本如何判断字符串在数组中
151 0