#!/bin/bash
# System Health Monitor
# monitorScript.sh

interval=$1
interface=$2
if [ "$#" -ne 2 ]
  then
    echo
    echo "Usage: $0 <interval> <interface>"
    echo
    exit 1
fi

#declare -i outbound0=0
#declare -i inbound0=0
declare -i tempOut=0
declare -i tempIn=0
#declare -i outbound1=0
#declare -i inbound1=0

while true; do
  clear
  echo "Interval (sec): "$'\t'"$interval"
  echo "Inteface: "$'\t'"$interface"
  echo -n "Number of Cores: "$'\t'
  cores=$(grep pro /proc/cpuinfo -c)
  echo $cores
  
# - CPU Utilization  
  echo -n "5 Minute Avg CPU Utilization (%): "$'\t'
  util=$(cat /proc/loadavg | awk '{print $1}')
  echo "scale=1; (($util*100)/$cores)/1" | bc # >>  cpuUtilBash.txt

# - Bandwidth Consumption
  echo -n "Outbound BW (bits): "$'\t'
  outbound1=$(awk -v interface="$interface" -F'[: \t]+' \
   '{ sub(/^ */,"");
       if ($1 == interface) print $10;
    }' /proc/net/dev)
   
  let "tempOut = outbound1 + 0"
  let "outbound1 -= outbound0"
  let "outbound0 = tempOut + 0"
  let "bpsOut = outbound1 * 8"

  echo "$bpsOut" # >> bitsOutBash.txt
    
  echo -n "Inbound BW (bits): "$'\t'
  inbound1=$(awk -v interface="$interface" -F'[: \t]+' \
   '{ sub(/^ */,"");
       if ($1 == interface) print $2;       
    }' /proc/net/dev)
 
  let "tempIn = inbound1 + 0"
  let "inbound1 -= inbound0"
  let "inbound0 = tempIn + 0"
  let "bpsIn = inbound1 * 8"
  echo "$bpsIn" # >> bitsInBash.txt
  
  sleep $interval
done


