server_info.sh 5.84 KB
#!/bin/bash
# irul@20200907

if [[ `id -u` -ne 0 ]] ; then printf "\nPlease run as root.\n\n" ; exit 1 ; fi

clear

# https://www.2daygeek.com/bash-shell-script-view-linux-system-information/
echo -e "== System Information =="
echo -e "Manufacturer: "`cat /sys/class/dmi/id/chassis_vendor`
echo -e "Product Name: "`cat /sys/class/dmi/id/product_name`
echo -e "Version: "`cat /sys/class/dmi/id/product_version`
echo -e "Serial Number: "`cat /sys/class/dmi/id/product_serial`
echo -e "Machine Type: "`vserver=$(lscpu | grep Hypervisor | wc -l); if [ $vserver -gt 0 ]; then echo "VM"; else echo "Physical"; fi`
echo -e "Kernel: "`uname -r`
echo -e "Architecture: "`arch`
echo -e "Processor Name: "`awk -F':' '/^model name/ {print $2}' /proc/cpuinfo | uniq | sed -e 's/^[ \t]*//'`
echo ""

echo -e "Hostname: "`hostname`
# echo -e "Operating System: "`hostnamectl | grep "Operating System" | cut -d ' ' -f5-`;
echo -e "Operating System: "`cat /etc/*release | grep PRETTY_NAME | cut -d "=" -f 2 | tr -d '"'`
echo -e "Using Systemd: "`pidof systemd > /dev/null && echo "True" || echo "False"`
echo -e "Uptime: "`uptime | awk '{print $3,$4}' | sed 's/,//'`
echo ""

echo -e "Default Gateway: "`ip route show | sed 's/\(\S\+\s\+\)\?default via \(\S\+\).*/\2/p; d'`
# echo -e "Server IP: "`hostname -I`
echo -e "Server IP: "`/sbin/ip -4 a | grep "inet " | tail -n +2 | awk '{print "["NR"] "$7 "=" $2}'`
echo -e "WAN/Public IP: "`dig +short myip.opendns.com @resolver1.opendns.com`
echo ""

echo -e "HDD: "`lsblk -dio SIZE,MODEL,KNAME,TYPE | grep disk|rev|cut -d" " -f2-|rev|awk {'print "["NR"] "$0'}`
echo -e "Memory(RAM): "`free -mht| awk '/Mem/{print $2 " |Used: "$3 " |Free: "$4 " |Available: "$7}'`
echo -e "Swap Memory: "`free -mht| awk '/Swap/{print $2 " |Used: "$3 " |Free: "$4}'`
echo ""

# MTYPE=$(echo `vserver=$(lscpu | grep Hypervisor | wc -l); echo $vserver`)
# if [ $MTYPE -eq 0 ]; then
    #echo -e "Disk Usage: "`df -h --total | grep "/dev/" | grep -v "loop" | grep -v "tmpfs" | sort`
    echo -e "== Disk Usage == "
    df -h --total | grep -v "loop\|tmpfs\|udev\|none\|shm" | while read -r line ; do
        if grep -q "Sistem" <<< "$line" || grep -q "Filesystem" <<< "$line" || grep -q "total" <<< "$line"; then
            str="[_] $line"
            echo "${str/total/Total}"
        else
            echo "[+] $line"
        fi
    done
    echo ""
# fi

echo -e "== Base Application =="

# virtualmin
if dpkg --get-selections | grep -E '(^|\s)virtualmin-core($|\s)' &> /dev/null
then
    echo "[x] virtualmin-core-"$(dpkg -l virtualmin-core | grep ^ii | awk '{print $3}' | cut -f1 -d-)
elif dpkg --get-selections | grep -E '(^|\s)virtualmin-custom($|\s)' &> /dev/null
then
    echo "[x] virtualmin-custom-"$(dpkg -l virtualmin-custom | grep ^ii | awk '{print $3}' | cut -f1 -d-)
else
    echo "[_] virtualmin"
fi
# webmin
if dpkg --get-selections | grep webmin &> /dev/null
then
    echo "[x] webmin-"$(dpkg -l webmin | grep ^ii | awk '{print $3}' | cut -f1 -d-)
else
    echo "[_] webmin"
fi
# openvpn
if command -v openvpn &> /dev/null
then
    echo "[x] openvpn-"$(openvpn --version | head -n 1 | cut -d " " -f 2)
else
    echo "[_] openvpn"
fi
# git
if command -v git &> /dev/null
then
    echo "[x] git-"$(git --version | head -n 1 | cut -d " " -f 3)
else
    echo "[_] git"
fi
# apache2
if dpkg --get-selections | grep -E '(^|\s)apache2($|\s)' &> /dev/null
then
    echo "[x] apache2-"$(dpkg -l apache2 | grep ^ii | awk '{print $3}' | cut -f1 -d-)
else
    echo "[_] apache2"
fi
# nginx
if dpkg --get-selections | grep nginx &> /dev/null
then
    echo "[x] nginx-"$(dpkg -l nginx | grep ^ii | awk '{print $3}' | cut -f1 -d-)
else
    echo "[_] nginx"
fi
# postgresql
if command -v psql &> /dev/null
then
    echo "[x] postgresql-"$(psql -V | head -n 1 | cut -d " " -f 3)
else
    echo "[_] postgresql"
fi
# mysql
if command -v mysql &> /dev/null
then
    echo "[x] mysql-"$(dpkg -l mysql-server | grep ^ii | awk '{print $3}' | cut -f1 -d-)
else
    echo "[_] mysql"
fi
# python3
if command -v python3 &> /dev/null
then
    echo "[x] python3-"$(python3 --version | head -n 1 | cut -d " " -f 2)
else
    echo "[_] python3"
fi
# php
if command -v php &> /dev/null
then
    echo "[x] php-"$(php -v | head -n 1 | cut -d " " -f 2)
else
    echo "[_] php"
fi
echo ""

if dpkg --get-selections | grep -E '(^|\s)apache2($|\s)' &> /dev/null ; then
    #echo "== Sites Available ==";
    #echo -e `apache2ctl -S 2>&1 | grep "namevhost" | awk '{print "(" NR ") " $4}'`

    SITES=$(echo `apache2ctl -S 2>&1 |grep "sites-en"|cut -d"/" -f5|cut -d ":" -f1|rev|cut -d"." -f2-|rev|grep -v "default\|le-ssl"|sort|uniq`)
    if [ ! -z "$SITES" ] ; then
        echo "== Sites Available (Apache) ==";

        SITES_CNT=1
        for SITE in $SITES
        do
            if curl -I -m 10 "$SITE" 2>&1 | grep -w "200\|301\|302" > /dev/null ; then
                echo "[$SITES_CNT] $SITE (`host $SITE|head -n1|awk {'print $4'}`) is up"
            else
                echo "[$SITES_CNT] $SITE is down"
            fi
            SITES_CNT=$[$SITES_CNT +1]
        done
        echo ""
    fi
fi

if dpkg --get-selections | grep -E '(^|\s)nginx($|\s)' &> /dev/null ; then
    SITES=$(echo `grep server_name /etc/nginx/sites-enabled/* -RiI|cut -d"/" -f5-|cut -d":" -f1|rev|cut -d"." -f2-|rev|grep -v "default\|le-ssl\|#\|_;\|server_name\|example"|sort|uniq`)
    if [ ! -z "$SITES" ] ; then
        echo "== Sites Available (Nginx) ==";

        SITES_CNT=1
        for SITE in $SITES
        do
            if curl -I -m 10 "$SITE" 2>&1 | grep -w "200\|301\|302" > /dev/null ; then
                echo "[$SITES_CNT] $SITE (`host $SITE|head -n1|awk {'print $4'}`) is up."
            else
                echo "[$SITES_CNT] $SITE is down"
            fi
            SITES_CNT=$[$SITES_CNT +1]
        done
        echo ""
    fi
fi

if ! command -v curl &> /dev/null ; then
    echo "Curl command not found!"
    echo ""
fi

echo "Last Check: "$(date '+%Y-%m-%d %H:%M')
echo ""