#!/bin/sh
free | grep Mem | awk '{ printf("%.4f \n", $4/$2 * 100) }' |  while read -r output;
do

	if (( ${output%%.*} < 30 )); then
	free=$(free -m | awk 'NR==2{printf  $4}')
	total=$(free -m | awk 'NR==2{printf  $2}')
    aststatus=$(service asterisk status | awk 'NR==3{print $2,$3}')
	time=`date +”%I:%M %p”`
	date=`date +"%b %d, %Y"`
	result=$(curl -k GET --header "Accept: */*" "https://api-email.pulsework360.com/sendmail.php?subject=Asterisk%20Restart%20Alert!%20onboard-asterisk2.pulsework360.com&body=Warning!%20Onboard%20Asterisk%202%20is%20Restarting.%20Available%20Ram%20is%20${output%%.*}%%20%28$free%20mb%29%20out%20of%20Total%20Ram%20%28$total%20mb%29%20at%20%20")
	echo "Ram Memory:"
    echo 'Available memory' - $free 'MB'
    echo 'Total memory' - $total 'MB'
    echo 'Asterisk Status' - $aststatus 'MB'
	service asterisk restart
	fi

done