天天看点

linux filesystem_分享一个实用的shell脚本--获取和监控Linux系统信息和资源使用率...概述脚本执行结果:

概述

今天主要分享包含各种不同参数的 Shell 脚本,用来获取和监控 Linux 系统信息和资源使用率。

脚本

#! /bin/bash# unset any variable which system may be using# clear the screenwhile getopts ivh namedo case $name in i)iopt=1;; v)vopt=1;; h)hopt=1;; *)echo "Invalid arg";; esacdone# if [[ ! -z $iopt ]]then{wd=$(pwd)basename "$(test -L "$0" && readlink "$0" || echo "$0")" > /tmp/scriptnamescriptname=$(echo -e -n $wd/ && cat /tmp/scriptname)su -c "cp $scriptname /usr/bin/monitor" root && echo "Congratulations! Script Installed, now run monitor Command" || echo "Installation failed"}fiif [[ ! -z $vopt ]]then{echo -e "tecmint_monitor version 0.1Designed by Tecmint.comReleased Under Apache 2.0 License"}fiif [[ ! -z $hopt ]]then{echo -e " -i Install script"echo -e " -v Print version information and exit"echo -e " -h Print help (this information) and exit"}fiif [[ $# -eq 0 ]]then{clearunset tecreset os architecture kernelrelease internalip externalip nameserver loadaverage# Define Variable tecresettecreset=$(tput sgr0)# Check if connected to Internet or notping -c 1 www.baidu.com &> /dev/null && echo -e 'E[32m'"Internet: $tecreset Connected" || echo -e 'E[32m'"Internet: $tecreset Disconnected"# Check OS Typeos=$(uname -o)echo -e 'E[32m'"Operating System Type :" $tecreset $os# Check OS Release Version and Name###################################OS=`uname -s`REV=`uname -r`MACH=`uname -m`GetVersionFromFile(){ VERSION=`cat $1 | tr "" ' ' | sed s/.*VERSION.*= // `}if [ "${OS}" = "SunOS" ] ; then OS=Solaris ARCH=`uname -p` OSSTR="${OS} ${REV}(${ARCH} `uname -v`)"elif [ "${OS}" = "AIX" ] ; then OSSTR="${OS} `oslevel` (`oslevel -r`)"elif [ "${OS}" = "Linux" ] ; then KERNEL=`uname -r` if [ -f /etc/redhat-release ] ; then DIST='RedHat' PSUEDONAME=`cat /etc/redhat-release | sed s/.*(// | sed s/)//` REV=`cat /etc/redhat-release | sed s/.*release // | sed s/ .*//` elif [ -f /etc/SuSE-release ] ; then DIST=`cat /etc/SuSE-release | tr "" ' '| sed s/VERSION.*//` REV=`cat /etc/SuSE-release | tr "" ' ' | sed s/.*= //` elif [ -f /etc/mandrake-release ] ; then DIST='Mandrake' PSUEDONAME=`cat /etc/mandrake-release | sed s/.*(// | sed s/)//` REV=`cat /etc/mandrake-release | sed s/.*release // | sed s/ .*//` elif [ -f /etc/debian_version ] ; then DIST="Debian `cat /etc/debian_version`" REV="" fi if ${OSSTR} [ -f /etc/UnitedLinux-release ] ; then DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "" ' ' | sed s/VERSION.*//`]" fi OSSTR="${OS} ${DIST} ${REV}(${PSUEDONAME} ${KERNEL} ${MACH})"fi###################################cat /etc/os-release | grep 'NAME|VERSION' | grep -v 'VERSION_ID' | grep -v 'PRETTY_NAME' > /tmp/osrelease#echo -n -e 'E[32m'"OS Name :" $tecreset && cat /tmp/osrelease | grep -v "VERSION" | grep -v CPE_NAME | cut -f2 -d"#echo -n -e 'E[32m'"OS Version :" $tecreset && cat /tmp/osrelease | grep -v "NAME" | grep -v CT_VERSION | cut -f2 -d"echo -e 'E[32m'"OS Version :" $tecreset $OSSTR architecture=$(uname -m)echo -e 'E[32m'"Architecture :" $tecreset $architecturekernelrelease=$(uname -r)echo -e 'E[32m'"Kernel Release :" $tecreset $kernelreleaseecho -e 'E[32m'"Hostname :" $tecreset $HOSTNAMEinternalip=$(hostname -I)echo -e 'E[32m'"Internal IP :" $tecreset $internalipexternalip=$(curl -s ipecho.net/plain;echo)echo -e 'E[32m'"External IP : $tecreset "$externalipnameservers=$(cat /etc/resolv.conf | sed '1 d' | awk '{print $2}')echo -e 'E[32m'"Name Servers :" $tecreset $nameservers who>/tmp/whoecho -e 'E[32m'"Logged In users :" $tecreset && cat /tmp/who free -h | grep -v + > /tmp/ramcacheecho -e 'E[32m'"Ram Usages :" $tecresetcat /tmp/ramcache | grep -v "Swap"echo -e 'E[32m'"Swap Usages :" $tecresetcat /tmp/ramcache | grep -v "Mem"df -h| grep 'Filesystem|/dev/sda*' > /tmp/diskusageecho -e 'E[32m'"Disk Usages :" $tecreset cat /tmp/diskusageloadaverage=$(top -n 1 -b | grep "load average:" | awk '{print $10 $11 $12}')echo -e 'E[32m'"Load Average :" $tecreset $loadaveragetecuptime=$(uptime | awk '{print $3,$4}' | cut -f1 -d,)echo -e 'E[32m'"System Uptime Days/(HH:MM) :" $tecreset $tecuptimeunset tecreset os architecture kernelrelease internalip externalip nameserver loadaveragerm /tmp/who /tmp/ramcache /tmp/diskusage}fi# shift命令用于对参数的移动(左移)shift $(($OPTIND -1))
           
linux filesystem_分享一个实用的shell脚本--获取和监控Linux系统信息和资源使用率...概述脚本执行结果:
linux filesystem_分享一个实用的shell脚本--获取和监控Linux系统信息和资源使用率...概述脚本执行结果:

执行结果:

linux filesystem_分享一个实用的shell脚本--获取和监控Linux系统信息和资源使用率...概述脚本执行结果:

后面会分享更多devops和DBA方面内容,感兴趣的朋友可以关注下!

linux filesystem_分享一个实用的shell脚本--获取和监控Linux系统信息和资源使用率...概述脚本执行结果: