shell监控脚本

shell监控脚本,主要包括获取当前系统版本、内核版本、主机名、登录用户、磁盘统计、内存统计

#!/bin/bash - 
#===============================================================================
#
#          FILE: monitor.sh
# 
#         USAGE: ./monitor.sh 
# 
#   DESCRIPTION: 
# 
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: YOUR NAME (), 
#  ORGANIZATION: 
#       CREATED: 12/16/2018 15:16
#      REVISION:  ---
#===============================================================================


function get_system_version()
{
    echo "----------------------------------------"
    OS=`uname -s`
    REV=`uname -r`
    MACH=`uname -m`

    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 "\n" ' '| sed s/VERSION.*//`
            REV=`cat /etc/SuSE-release | tr "\n" ' ' | 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/os-release ]; then
            DIST=`awk -F "PRETTY_NAME=" '{print $2}' /etc/os-release | tr -d '\n"'`
        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 "\n" ' ' | sed s/VERSION.*//`]"
        fi

        OSSTR="${OS} ${DIST} ${REV}(${PSUEDONAME} ${KERNEL} ${MACH})"
    fi
    #echo "OS:"${OS}
    #echo "dist:"${DIST}
    #echo "rev:"${PSUEDONAME}
    #echo "psuedname:${PSUEDONAME}"
    #echo "kernel:${KERNEL}"
    #echo "MACH:$MACH"
    echo "OS version:${OSSTR}"

}


function get_kernel_version()
{
    echo "----------------------------------------"
    kernel_version=`uname -r`
    echo "Kernel Release:${kernel_version}"
}

function get_hostname()
{
    echo "----------------------------------------"
    echo "hostname:$HOSTNAME"
}
function get_login_users()
{
    echo "----------------------------------------"
    echo "login uses statistics information"
    who -s
}

function get_mem_info()
{
    echo "----------------------------------------"
    echo "memory statistics information:"
    free -h| grep Mem
    free -h | grep Swap
}
function get_disk_info()
{
    echo "----------------------------------------"
    echo "disk statistics information:"
    df -h
}

function show_menu()
{
    echo -e "Menu:"
    echo -e "-i:install software"
    echo -e "-u:uninstall software"
    echo -e "-v:print version information"
    echo -e "-h:show help menu"
}
function show_software_version_information()
{
    echo -e "monitor version 0.1"
}
function install_software()
{
    absolute_path=$(readlink -f "$0")
    basename "$(test -L "$0" && readlink "$0" || echo "$0")" > /tmp/scriptname
    scriptname=$(echo -e -n $wd/ && cat /tmp/scriptname)
    cp ${absolute_path} /usr/bin/monitor && echo "Congratulations! Script Installed, now run monitor Command" || echo "Installation failed"

}
function uninstall_software()
{
    rm /usr/bin/monitor -rf
    echo "uninstall software successfully"
}
function main()
{

    while getopts iuvh name;do
        case $name in
            i)
                install_software ;;
            u)
                uninstall_software;;
            v)
                show_software_version_information;;
            h)
                show_menu;;
            *) echo "Invalid args";;
        esac
        exit

    done



    get_kernel_version

    get_system_version

    get_hostname

    get_login_users

    get_mem_info
     
    get_disk_info

}


main $@

你可能感兴趣的:(shell脚本学习)