shell脚本查看系统基本信息

1、一个可以查看和监控Linux一些基本信息的脚本:

初步代码

reset_terminal=$(tput sgr0) #定义一个高亮输出的变量
	
	#OS type
	os_type=$(uname -o)
	echo -e '\E[32m' "OS type:" $reset_terminal $os_type

	#OS release version
	os_version=$(cat /etc/issue | grep "CentOS")
	#os_version=$(cat /etc/redhat-release)
	echo -e '\E[32m' "OS release version:" $reset_terminal $os_version

	#architecture cpu指令集
	architecture=$(uname -m)
	echo -e '\E[32m' "architecture:" $reset_terminal $architecture
	
	#Kernel release
	kernel_release=$(uname -r)
	echo -e '\E[32m' "Kernel release:" $reset_terminal $kernel_release
	
	#hostname
	hostname=$(uname -n)
	#hostname=$(set | grep HOSTNAME)  $HOSTNAME
	echo -e '\E[32m' "hostname:" $reset_terminal $hostname
	
	#internal ip
	internal_ip=$(hostname -I)
	echo -e '\E[32m' "internal_ip:" $reset_terminal $internal_ip

	#external ip
	#external_ip=$(curl -s http://ipecho.net/plain)

	#DNS
	name_server=$(cat /etc/resolv.conf | grep "nameserver" | awk '{print $NF}')
	echo -e '\E[32m' "DNS:" $reset_terminal $name_server

	#if connected to internet or not
	ping -c 2 www.shanproxy.com &> /etc/null && echo "Internet:connected" || echo "Internet:Disconnected"

	#Logged in users
	who > /tmp/who
	echo -e '\E[32m' "Logged users" $reset_terminal && cat /tmp/who
	rm -f /tmp/who  #删除临时文件,避免再次执行时的影响

######################################################################################################################
	
	#操作系统真实占用内存
	sys_mem_used=$(awk '/MemTotal/{total=$2}/MemFree/{free=$2}/^Cached/{cache=$2}/Buffers/{buffers=$2}END{print (tota
l-free-cache-buffers)/1024}' /proc/meminfo)
	#sys_mem_used=$(free -m | grep "buffers/cache" | awk '{print $3}')
	echo -e '\E[32m' "sys_mem_used"  $reset_terminal $sys_mem_used

	
	#操作系统真实可用内存
	sys_men_free=$(awk '/MemFree/{free1=$2}/^Cached/{cache1=$2}/Buffers/{buffers1=$2}END{print(free1+cache1+buffers1)
/1024}' /proc/meminfo)
	#sys_mem_free=$(free -m | grep "buffers/cache" | awk '{print $4}')
	echo -e '\E[32m' "sys_men_free"  $reset_terminal $sys_men_free

	#CPU loadaverge
	loadaverge=$(top -n 1 -b | grep "load average" | awk '{print $12 $13 $14}')
	#loadaverge=$(uptime | awk '{print $10 $11 $12}')
	echo -e '\E[32m' "CPU loadaverge" $reset_terminal $loadaverge

	#Disk used
	disk_used=$(df -h | grep -v "Filesystem" | awk '{print $1 " "  $5}')
	echo -e '\E[32m' "Disk used" $reset_terminal $disk_used



2、输出结果

shell脚本查看系统基本信息_第1张图片

3、代码优化:将echo编写成一个函数:

echoFun(){
echo -e '\E[32m' "$1" $reset_terminal $2
}
#OS type
os_type=$(uname -o)
echoFun "OS type" $os_type

4、客户端nginx服务检查脚本:

#!/bin/bash


reset_terminal=$(tput sgr0) #定义一个高亮输出的变量
nginxserver_url='http://10.199.134.158'


echoFun(){
echo -e '\E[32m' "$1" $reset_terminal $2
}


Check_nginx_server(){
#curl  -m 设置最大传输时间 -s 静音模式 -w 返回状态码
status_code=$(curl -m 5 -s -w %{http_code} $nginxserver_url -o /dev/null)
#判断nginx返回的状态码
if [ $status_code -eq 000 -o $status_code -ge 500 ];then
echoFun "Nginx server error! http_code is" "$status_code"
else
nginx_content=$(curl -s $nginxserver_url)
echoFun "Nginx server ok!" "$nginx_content"
fi 
}


Check_nginx_server

5、nginx检查脚本输出结果:

shell脚本查看系统基本信息_第2张图片



你可能感兴趣的:(shell脚本查看系统基本信息)