主机监控脚本
主机监控的信息
- 系统信息
uname -a - 网络信息
- 系统基本信息
top
w
who
uptime - 进程
ps123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145#! /bin/bash# unset any variable which system may be using# clear the screen## 处理参数while getopts ivh namedocase $name ini)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.1\nDesigned by Tecmint.com\nReleased Under Apache 2.0 License"}fi#判断是否请求帮助if [[ ! -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"}fi#无参数时,直接运行脚本if [[ $# -eq 0 ]]; thenclearunset 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 "\n" ' ' | sed s/.*VERSION.*=\ // `}if [ "${OS}" = "SunOS" ] ; thenOS=SolarisARCH=`uname -p`OSSTR="${OS} ${REV}(${ARCH} `uname -v`)"elif [ "${OS}" = "AIX" ] ; thenOSSTR="${OS} `oslevel` (`oslevel -r`)"elif [ "${OS}" = "Linux" ] ; thenKERNEL=`uname -r`if [ -f /etc/redhat-release ] ; thenDIST='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 ] ; thenDIST=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//`REV=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //`elif [ -f /etc/mandrake-release ] ; thenDIST='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 ] ; thenDIST="Debian `cat /etc/debian_version`"REV=""fiif ${OSSTR} [ -f /etc/UnitedLinux-release ] ; thenDIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]"fiOSSTR="${OS} ${DIST} ${REV}(${PSUEDONAME} ${KERNEL} ${MACH})"fi##################################echo -e '\E[32m'"OS Version :" $tecreset $OSSTRarchitecture=$(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 $nameserverswho>/tmp/whoecho -e '\E[32m'"Logged In users :" $tecreset && cat /tmp/whofree -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 > /tmp/diskusageecho -e '\E[32m'"Disk Usages :" $tecresetcat /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 $tecuptime##Process systemecho -e '\E[32m'"Process system :" $tecresetecho -e '\E[32m'"CPU share of the top 10:" $tecresetps -axo user,pid,%cpu,%mem,comm --sort -%cpu,-%mem| head -11unset tecreset os architecture kernelrelease internalip externalip nameserver loadaveragerm /tmp/who /tmp/ramcache /tmp/diskusagefishift $(($OPTIND -1))#