#!/bin/bash

# Function to check free memory
check_free_memory() {
	# Read memory information from /proc/meminfo

	meminfo=$(cat /proc/meminfo)

	# Extract fields: MemTotal, MemFree, Buffers, Cached
	mem_total=$(echo "$meminfo" | awk '/MemTotal:/ {print $2}')
	mem_free=$(echo "$meminfo" | awk '/MemFree:/ {print $2}')
	buffers=$(echo "$meminfo" | awk '/Buffers:/ {print $2}')
	cached=$(echo "$meminfo" | awk '/^Cached:/ {print $2}')

	# Calculate total free memory (MemFree + Buffers + Cached)
	total_free=$((mem_free + buffers + cached))

	# Calculate free memory percentage
	free_mem_percentage=$(( (total_free * 100) / mem_total ))

	# Free Memory percentage should not be lower than threshold_free_mem
	threshold_free_mem=$1
	# Check if free memory is higher than threshold
	if [ "$free_mem_percentage" -gt "$threshold_free_mem" ]; then
		return 0
	else
		return 1 # Memory leak,
	fi
}

# Function to get CPU load
get_cpu_load() {
	stats1=$(grep '^cpu ' /proc/stat)
	# Read CPU statistics from /proc/stat
	sleep 2
	# Read CPU statistics from /proc/stat in one second
	stats2=$(grep '^cpu ' /proc/stat)

	# Extract individual CPU usage values
	cpu_user1=$(echo "$stats1" | awk '{ print $2 }')
	cpu_nice1=$(echo "$stats1" | awk '{ print $3 }')
	cpu_system1=$(echo "$stats1" | awk '{ print $4 }')
	cpu_idle1=$(echo "$stats1" | awk '{ print $5 }')
	cpu_iowait1=$(echo "$stats1" | awk '{ print $6 }')
	cpu_irq1=$(echo "$stats1" | awk '{ print $7 }')
	cpu_softirq1=$(echo "$stats1" | awk '{ print $8 }')
	cpu_steal1=$(echo "$stats1" | awk '{ print $9 }')
	cpu_guest1=$(echo "$stats1" | awk '{ print $10 }')
	cpu_guest_nice1=$(echo "$stats1" | awk '{ print $11 }')

	# Extract individual CPU usage values in one second
	cpu_user2=$(echo "$stats2" | awk '{ print $2 }')
	cpu_nice2=$(echo "$stats2" | awk '{ print $3 }')
	cpu_system2=$(echo "$stats2" | awk '{ print $4 }')
	cpu_idle2=$(echo "$stats2" | awk '{ print $5 }')
	cpu_iowait2=$(echo "$stats2" | awk '{ print $6 }')
	cpu_irq2=$(echo "$stats2" | awk '{ print $7 }')
	cpu_softirq2=$(echo "$stats2" | awk '{ print $8 }')
	cpu_steal2=$(echo "$stats2" | awk '{ print $9 }')
	cpu_guest2=$(echo "$stats2" | awk '{ print $10 }')
	cpu_guest_nice2=$(echo "$stats2" | awk '{ print $11 }')

	cpu_non_idle1=$((cpu_user1 + cpu_nice1 + cpu_system1 + cpu_irq1 + cpu_softirq1 + cpu_steal1 + cpu_guest1 + cpu_guest_nice1))
	cpu_total1=$((cpu_non_idle1 + cpu_idle1))

	cpu_non_idle2=$((cpu_user2 + cpu_nice2 + cpu_system2 + cpu_irq2 + cpu_softirq2 + cpu_steal2 + cpu_guest2 + cpu_guest_nice2))
	cpu_total2=$((cpu_non_idle2 + cpu_idle2))

	cpu_load=$((100 * (cpu_non_idle2 - cpu_non_idle1) / (cpu_total2 - cpu_total1)))

	echo "$cpu_load"
}

restarted=0
threshold_free_mem=30
threshold_cpu_load=18
num_samples=16
# get threshold_free_mem, threshold_cpu_load and num_samples from threshold.conf
if [ -f /etc/fiad-conf/threshold.conf ] ; then
	. /etc/fiad-conf/threshold.conf
fi
# Call function to check free memory
check_free_memory $threshold_free_mem
result=$?
if [ "$result" -ne 0 ]; then
	restarted=1
	/etc/init.d/httpd restart > /dev/null 2>&1
	/etc/init.d/ecmonclient restart
	sleep 5
# Call function to check free memory again
	check_free_memory $threshold_free_mem
	result=$?
	if [ "$result" -ne 0 ]; then
#		echo "Low Free memory"
		exit 1
	fi
fi

# Take multiple samples and accumulate the CPU loads
for (( i=1; i<=$num_samples; i++ )); do
	load=$(get_cpu_load)
# Check if CPU load is higher than threshold
	if [ "$load" -gt "$threshold_cpu_load" ]; then
		if [ $restarted = "0" ]; then
			restarted=1
			/etc/init.d/httpd restart > /dev/null 2>&1
		fi
		sleep 180  # Sleep for 180 seconds between samples
	else
		# low CPU load
		if [ $restarted = "0" ]; then
			#check close_wait in netstat
			CLOSEWAIT=$(netstat -an | grep CLOSE_WAIT)
			if [ "$CLOSEWAIT" != "" ]; then
				/etc/init.d/httpd restart > /dev/null 2>&1
			fi
		fi
		exit 0
	fi
done
#need to reboot the device
exit 1
