Function One: the ability to extract basic information about the operating system (kernel, system version, network address, etc.)
#############################################################################
#File Name:System_monitor.sh
#Author: Ivan
#############################################################################
#!/bin/bash
#把之前屏幕上的内容清除掉
Clear
if [[$#-eq 0]]
Then
Echo-e ' \e[33m ' "########## #系统分析 ##########"
#定义一个高亮输出的变量
output=$ (Tput sgr0)
#检查系统类型 using the Uname-o command
os=$ (Uname-o)
Echo-e ' \e[32m ' "System type:" $output $os
#获取操作系统版本类型
os_name=$ (cat/etc/issue |grep-ie "Server")
Echo-e ' \e[33m ' "System version:" $output $os _name
#获取CPU的指令集
os_bit=$ (UNAME-M)
Echo-e ' \e[34m ' "system bits:" $output $os _bit
#获取内核版本
os_kernel=$ (UNAME-R)
Echo-e ' \e[35m ' "kernel version:" $output $os _kernel
#获取主机名 $HOSTNAME and Uname-n can also be implemented
hostname=$ (hostname)
#获取内网ip
inner=$ (Hostname-i)
Echo-e ' \e[36m ' "Intranet IP:" $output $inner
#获取外网ip we simply use the information returned after requesting this HTTP address
outer=$ (Curl-s http://ipecho.net/plain)
Echo-e ' \e[29m ' "Extranet IP:" $output $outer
#获取DNS extract matches from/etc/resolv.conf file
nameservers=$ (cat/etc/resolv.conf |grep-e "\<nameserver[]" +|awk ' {print $NF} ')
Echo-e ' \e[31m ' "System DNS:" $output $nameservers
#判断网络连通性 test directly with the ping command
Ping-c 2 www.toutiao.com &>/dev/null && echo "Network connectivity: Yes" | | echo "Network connectivity: No"
#检查当前登录的用户 output to a temporary file using the WHO command
Who >/tmp/who
Echo-e ' \e[32m ' "currently logged in User:" $output && cat/tmp/who
#记得把临时文件给删掉
Rm-rf/tmp/who
echo "#############################"
# function Two: ability to analyze the operating state of the system (CPU load, memory and disk usage, etc.)
#使用free-M to view memory usage
#我们要取的是系统使用内存和应用使用内存
#系统使用内存 =total-free while applying memory =total-(free+buffers+cached)
#在/proc/meminfo file is a directory of memory conditions
#获取系统已经使用的内存通过awk命令文本进行提取然后计算出结果换算成Mb
system_men=$ (awk '/memtotal/{total=$2}/memfree/{free=$2}end{print (total-free)/1024} '/proc/meminfo)
#获取应用使用内存通过awk命令文本进行提取然后计算出结果换算成Mb
app_men=$ (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[36m ' system uses memory: "$output $system _men" M "
Echo-e ' \e[36m ' "Application uses memory:" $output $app _men "M"
# The next step is to operate the system CPU load this piece, we can use the top command to view
#top-N 1-b|grep "load average:"
#再使用awk进行空格划分提取我们想要的load Average
#top-N 1-b|grep "load average:" |awk ' {print $ $13 $14} '
load_average=$ (top-n 1-b|grep "load average:" |awk ' {print $ $13 $14} ')
Echo-e ' \e[33m ' "Load Condition:" $output $load _average
# Finally the disk analysis, the usual, first we use the df-h command to see
# Before we can format it and output it
#df-hp|grep-ve ' Filesystem|tmpfs ' |awk ' {print $ ' Size: ' $ $ ' used: ' $ $ ' use%: ' $ $ '
disk=$ (df-hp|grep-ve ' Filesystem|tmpfs ' |awk ' {print $ "total:" $ "used:" $ $ "Usage:" $ ")
Echo-e ' \e[34m ' "Disk condition:" $output $disk
Else
echo "Please do not pass parameters!" "
Fi
Note: The red font can not be added to the script
Post-run Effects:
########## #系统分析 ##########
System Type: Gnu/linux
System version: CentOS release 6.6 (Final)
Number of system bits: x86_64
Kernel version: 2.6.32-504.el6.x86_64
Intranet ip:172.16.1.237
External network ip:x.x.x.x
System dns:8.8.8.8 211.136.192.6
Network connectivity: Yes
Currently logged in User:
Root PTS/1 2017-01-12 20:14 (x.x.x.x)
#############################
System use Memory: 3387.73M
Applications use Memory: 1084.01M
Load Condition: 0.00,0.00,0.00
Disk Condition:/dev/mapper/vg_itcacti-logvol00 total: 287G used: 3.9G usage: 2%/dev/sda1 total: 477M used: 32M usage: 8%
This article is from the IT Operations Information blog, please be sure to keep this source http://aqiang.blog.51cto.com/6086626/1891584
Extract basic information about the operating system (kernel, system version, network address, etc.)