#!/bin/bash

SSIP_API_URI="http://10.4.4.40:8444/api/system-status"
SSIP_API_TIMEOUT=3
HEADER_CONTEXT_TYPE="Content-Type: application/json"

# Check hostname
name=$HOSTNAME
echo -e "Hostname:" $name

# Check OS Type
otype=$(uname -o)
echo -e "Operating System Type:" $otype

# Check OS Release Version and Name
oname=`cat /etc/os-release | grep 'NAME' | grep -v 'PRETTY_NAME' | grep -v 'CODENAME' | cut -f2 -d\"`
over=`cat /etc/os-release | grep 'VERSION' | grep -v '_' | cut -f2 -d\"`
echo "OS Name:" $oname
echo "OS Version:" $over

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

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

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

# Check Internal IP
eip=$(dig +short myip.opendns.com @resolver1.opendns.com)
echo "External IP:" $eip

run_monitoring() {
    totalmem=`free -m | grep ^Mem | awk '{print $2}'`
    usedmem=`free -m | grep ^Mem | awk '{print $3}'`
    freemem=`free -m | grep ^Mem | awk '{print $4}'`
    cachemem=`free -m | grep ^Mem | awk '{print $6}'`
    availablemem=`free -m | grep ^Mem | awk '{print $7}'`

    swapmem=`free -m | grep ^Swap | awk '{print $2}'`
    usedswap=`free -m | grep ^Swap | awk '{print $3}'`

    cpucnt=`cat /proc/cpuinfo | grep 'processor' | wc -l`
    uptimes=`uptime -s`

    echo "cpuCnt:" ${cpucnt}

    #usecpurate=`top -b -n 1 | grep Cpu | awk '{print $8}' | tr -d "%id," | awk '{print 100-$1}'`
    usecpurate=`mpstat 1 1 | tail -1 | awk '{print 100-$NF}'`
    echo "useCpuRate:" ${usecpurate}
    tasks=`ps aux | grep -v ps | wc -l`
    threads=200

    #loadavg=`uptime | awk '{print $10}' | cut -f 1 -d ','`
    loadavg=`cat /proc/loadavg | awk '{print $1}'`
    echo "loadAvg:" ${loadavg}
    
    #DISK 사용율
    totaldisk=`df -P | grep -v ^Filesystem | awk '{sum += $2} END { print sum; }'`
    useddisk=`df -P | grep -v ^Filesystem | awk '{sum += $3} END { print sum; }'`
    useddiskrate=$((100*$useddisk/$totaldisk))
    echo DISK: $(($totaldisk/1024/1024)) GB, Used DISK: $(($useddisk/1024/1024)) GB, Rate: $useddiskrate %

    # Call System Status Save REST API
    PAYLOAD='{"hostName"      : "'${name}'", 
              "osType"        : "'${otype}'", 
              "osName"        : "'${oname}'", 
              "osVersion"     : "'${over}'", 
              "osArch"        : "'${oarch}'", 
              "kernelRelease" : "'${kernel}'", 
              "internalIp"    : "'${iip}'", 
              "externalIp"    : "'${eip}'",
              "totalMem"      : "'${totalmem}'",
              "swapMem"       : "'${swapmem}'",
              "totalDisk"     : "'${totaldisk}'",
              "cpuCnt"        : "'${cpucnt}'",
              "upTimes"       : "'${uptimes}'",
              "usedDisk"      : "'${useddisk}'",
              "usedMem"       : "'${usedmem}'",
              "freeMem"       : "'${freemem}'",
              "cacheMem"      : "'${cachemem}'",
              "availableMem"  : "'${availablemem}'",
              "usedSwap"      : "'${usedswap}'",
              "tasks"         : "'${tasks}'",
              "threads"       : "'${threads}'",
              "useCpuRate"    : "'${usecpurate}'",
              "loadAvg"       : "'${loadavg}'"
	    }'

    api_result=$(curl -d "${PAYLOAD}" -X POST $SSIP_API_URI -H "$HEADER_CONTEXT_TYPE" --connect-timeout $SSIP_API_TIMEOUT)

    # Result
    echo ""
    echo ${api_result}
    echo ""
}

run_monitoring

