#!/bin/bash
#  System Health Monitor
#   sysmon.sh
#
#  Usage;  sysmon.sh interval  interface
#          interval:  time in seconds between samples
#          interface: the network interface name (e.g., eth0)
# 
#   Note: this script requires the bc tool (apt-get install bc)
#

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

declare -i tempOut=0
declare -i tempIn=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 (bps): "$'\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) / $interval"

  echo "$bpsOut" # >> bitsOutBash.txt
    
  echo -n "Inbound BW (bps): "$'\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) / $interval"
  echo "$bpsIn" # >> bitsInBash.txt
  
  sleep $interval
done


