Shell应用 之 系统信息及运行状态获取

系统信息监控脚本:

  脚本:system_monitor.sh

  功能一:提取操作系统信息(内核、系统版本、网络地址等) 

  功能二:分析系统的运行状态(CPU负载、内存及磁盘使用率等)

    - 系统使用的内存和应用使用内存区别

      系统使用内存 = Total - Free

      应用使用内存 = Total - ( Free + Cached + Buffers )

      - 内存中 cache 和 buffer 的区别

        

    - 操作系统负载

      - CPU负载概念

    - 系统磁盘使用量

  完整 system_monitor.sh 脚本:

#!/bin/bash

clear
if [[ $# -eq 0 ]]
then
# Define Variable rest_terminal
reset_terminal=$(tput sgr0)

# Check OS Type
	os=$(uname -o)
	echo -e '\e[32m'"Check OS Type : " $reset_terminal $os
# Check OS Released Version and Name
	os_name=$(cat /etc/issue | grep -e "release")
	echo -e '\e[32m'"Check OS Released Version and Name : " $reset_terminal $os_name
# Check Architecture
	architecture=$(uname -m)
	echo -e '\e[32m'"Check Architecture : " $reset_terminal $architecture
# Check Kernel Release
	kernelrelease=$(uname -r)
	echo -e '\e[32m'"Check Kernel Release : " $reset_terminal $kernelrelease
# Check hostname
	#hostname=$(uname -n)
	hostname=$HOSTNAME
	echo -e '\e[32m'"Check OS Released Version and Name : " $reset_terminal $hostname
# Check Internal IP
	internalip=$(hostname -I)
	echo -e '\e[32m'"Check Internal IP : " $reset_terminal $internalip
# Check External IP
	externalip=$(curl -s http://ipecho.net/plain)
	echo -e '\e[32m'"Check External IP : " $reset_terminal $externalip
# Check DNS
	nameservers=$(cat /etc/resolv.conf |grep -E "\<nameserver[ ]+"|awk '{print $NF}')
	echo -e '\e[32m'"Check DNS : " $reset_terminal $nameservers
# Check if connected to Internet or not
	ping -c 2 baidu.com &>/dev/null && echo -e '\e[32m'"Check Internet:"'\033[0m'"Connected"|| echo -e '\e[32m'"Check Internet:"'\033[0m'"Disconnected"
# Check Logged-in Users
	who>/tmp/who
	echo -e '\e[32m'"Check Logged-in Users : " $reset_terminal && cat /tmp/who
	rm -rf /tmp/who
####################################################
# Check System Memory Usage and APPs Memory Usage
	system_mem_usage=$(awk '/MemTotal/{total=$2}/MemFree/{free=$2}END{print (total-free)/1024}' /proc/meminfo)
	echo -e '\e[32m'"System Memory Usage :" $reset_terminal $system_mem_usage M

	apps_mem_usage=$(awk '/MemTotal/{total=$2}/MemFree/{free=$2}/^Cached/{cached=$2}/Buffers/{buffers=$2}END{print (total-free-cached-buffers)/1024}' /proc/meminfo)
	echo -e '\e[32m'"APPs Memory Usage :" $reset_terminal $apps_mem_usage M 
# Check Load Averages
	loadaverage=$(top -n 1 -b|grep "load average:"|awk '{print $10 $11 $12}')
	echo -e '\e[32m'"Check Load Averages :" $reset_terminal $loadaverage
# Check Disk Averages
	diskaverage=$(df -h|grep -vE 'Filesystem|tmpfs'|awk '{print $1 "  " $5}')
	echo -e '\e[32m'"Check Disk Averages :" $reset_terminal $diskaverage

fi

  

 运行效果:

 

posted on 2017-02-12 23:22  你的踏板车要滑向哪里  阅读(332)  评论(0编辑  收藏  举报

导航