#!/bin/bash

chk_uart()
{
	ret=0
	if [ ! -L /dev/ttyM0 ]; then
		exit 3
	fi
	if [ ! -L /dev/ttyM1 ]; then
		exit 3
	fi
	exit 0
}

chk_lan1()
{
	ret=`ifconfig | grep eth0`
	if [ x"$ret" == x"" ]; then
		exit 1
	fi
	exit 0
}

chk_lan2()
{
	ret=`ifconfig | grep eth1`
	if [ x"$ret" == x"" ]; then
		exit 2
	fi
	exit 0
}

chk_btn()
{
	if [ ! -c /dev/input/event0 ]; then
		off_led ${GREEN} ${YELLOW} ${RED}
		on_led ${RED} 
		off_led ${GREEN} 
		blink_led ${YELLOW}
		exit
	fi
}

chk_led()
{
	ret=`ls -1 /sys/class/leds/ | wc -l`
	if [ x"$ret" != x"15" ]; then
		exit 4
	fi
	exit 0
}

chk_cpu_usage()
{
	local usage
	usage=`top -d 0.5 -b -n2 | grep "Cpu(s)"|tail -n 1 | awk '{print $2 + $4 + 0.5}' |cut -f1 -d"."`
	if [ "$usage" -gt "90" ]; then
		exit 5
	fi
	exit 0
}

chk_ram_usage()
{
	local usage
	usage=`free | grep Mem | awk '{print $3/$2 * 100 +0.5}' |cut -f1 -d"."`
	if [ "$usage" -gt "90" ]; then
		exit 6
	fi
	exit 0
}

chk_disk_usage()
{
	local usage
	usage=`df | grep 'dev/root' | awk '{print $5}' | tr -d "%"`
	if [ "$usage" -gt "90" ]; then
		exit 7
	fi
	exit 0
}

chk_gps_status()
{
	if [ ! -e /dev/ttyS1 ]; then
		exit 8
	fi
	exit 0
}

chk_fs_status()
{
	root_part=$(cat /proc/cmdline)
	root_part="${root_part##*root=}"
	root_part="${root_part%% *}"

	fs_state=`debugfs -R 'stats' ${root_part} 2> /dev/null | grep "Filesystem state" | grep "errors"`
	if [ x"$fs_state" != x"" ]; then
		exit 9
	fi
	exit 0
}

chk_lan1
chk_lan2
chk_uart
chk_led
chk_cpu_usage
chk_ram_usage
chk_disk_usage
chk_gps_status
chk_fs_status
