#!/bin/bash

echo "==============================================================================="
if ! wget -q --spider http://baidu.com
then
        echo  "Internet:  Disconnected"
else
        echo  "Internet:  Connected"
fi

# Check OS Release Version and Name
echo "==============================================================================="
name=$(</etc/os-release grep 'PRETTY_NAME=' | cut -f2 -d\")
echo "OS :" "$name"

# Check Architecture
echo "==============================================================================="
architecture=$(uname -m)
echo "Architecture :"  "$architecture"

# Check Kernel Release
echo "==============================================================================="
kernelrelease=$(uname -r)
echo "Kernel Release :"  "$kernelrelease"

# Check Hostname
echo "==============================================================================="
hostname=$(hostname)
echo "Hostname :"  "$hostname"

# Check Internal IP
echo "==============================================================================="
internalip=$(hostname -I)
echo "Internal IP :"  "$internalip"

# Check External IP
echo "==============================================================================="
externalip=$(curl -s https://api.myip.la;echo)
echo "External IP :" "$externalip"

# Check DNS
echo "==============================================================================="
nameservers=$(</etc/resolv.conf grep "nameserver" | awk '{print $2}')
echo "Name Servers :"
echo "$nameservers"

# Check Logged In Users
echo "==============================================================================="
echo "Logged In users :"
who

# Check RAM and SWAP Usages
echo "==============================================================================="
echo "Memory usage :"
free -h 

# Check Disk Usage
echo "==============================================================================="
df -h| grep 'Filesystem\|/dev/sda*\|/dev/mapper*\|/dev/md*\|/dev/disk*\|/dev/vda*\|/dev/dm*' >> "$tdir/diskusage.tmp"
echo "Disk Usage :"
cat "$tdir/diskusage.tmp"

# Check Load Average
echo "==============================================================================="
loadaverage=$(</proc/loadavg awk '{print $1,$2,$3}')
echo "Load Average :"  "$loadaverage"

# Check iowait
echo "==============================================================================="
iowait=$(iostat -c|awk '/^ /{print $4}')
echo "I/O in %:" "$iowait"

# Check System Uptime
echo "==============================================================================="
uptime=$(uptime | awk '{print $3,$4}' | cut -f1 -d,)
echo "System Uptime Days/(HH:MM) :"  "$uptime"
echo "==============================================================================="

# check cpu count
echo "==============================================================================="
echo "物理内核数"
cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l
echo "逻辑内核数"
cat /proc/cpuinfo | grep "core id" | sort | uniq | wc -l
echo "==============================================================================="

# check mitliprocee
echo "==============================================================================="
echo "查看线程数"
cat /proc/cpuinfo | grep "processor" | sort | uniq | wc -l
echo "==============================================================================="

# check idel memory
echo "==============================================================================="
#内存
total=$(free -m | sed -n '2p' | awk '{print $2}')
used=$(free -m | sed -n '2p' | awk '{print $3}')
free=$(free -m | sed -n '2p' | awk '{print $4}')
shared=$(free -m | sed -n '2p' | awk '{print $5}')
buff=$(free -m | sed -n '2p' | awk '{print $6}')
cached=$(free -m | sed -n '2p' | awk '{print $7}')
rate=`echo "scale=2;$used/$total" | bc | awk -F. '{print $2}'`
echo -e "total\tused\tfree\tshared\tbuffer\tavailable"
echo -e "${total}M\t${used}M\t${free}M\t${shared}M\t${buff}M\t${cached}M\n内存使用
率:${rate}%"
if    [ $rate -ge 30 ]
then    echo "Memory Warn"
    ps aux | grep -v USER | sort -rn -k4 | head
fi
echo "==============================================================================="

# check idel diskspace
echo "==============================================================================="
DEV=`df -hP | grep '^/dev/*' | cut -d' ' -f1 | sort`
for I in $DEV
do dev=`df -Ph | grep $I | awk '{print $1}'`
size=`df -Ph | grep $I | awk '{print $2}'`
used=`df -Ph | grep $I | awk '{print $3}'`
free=`df -Ph | grep $I | awk '{print $4}'`
rate=`df -Ph | grep $I | awk '{print $5}'`
mount=`df -Ph | grep $I | awk '{print $6}'`
echo -e "$I:\tsize:$size\tused:$used\tfree:$free\trate:$rate\tmount:$mount"
F=`echo $rate | awk -F% '{print $1}'`
if [ $F -ge 50 ]
then
    echo "$mount Warn"
    else

echo "It's OK"
fi
done
echo "==============================================================================="
