1#!/bin/sh
2
3# Created on September 20, 2012
4#
5# Copyright (c) 2012, NETGEAR, Inc.
6# 350 East Plumeria, San Jose California, 95134, U.S.A.
7# All rights reserved.
8#
9# This software is the confidential and proprietary information of
10# NETGEAR, Inc. ("Confidential Information").  You shall not
11# disclose such Confidential Information and shall use it only in
12# accordance with the terms of the license agreement you entered into
13# with NETGEAR.
14
15# cp_checkbox.sh - script to check if a device is Ecosystem ready.
16# 
17#   usage:
18#       cp_checkbox.sh <path-for-eco.env> <path-for-currentsetting.htm>
19#   e.g.
20#       cp_checkbox.sh /tmp /www
21#
22
23#################################################################
24# System Resource Requirements
25#################################################################
26MIN_NAND_SIZE=90000	# Minimum non-volatile storage size in Kb
27WARN_NAND_SIZE=96000	# Warning size for non-volatile storage
28MIN_RAM_SIZE=70000	# Minimum free RAM size in Kb
29WARN_RAM_SIZE=80000	# Warning size for RAM size
30BROWSER_FILE="currentsetting.htm"
31SETTING_ENTRY="SmartNetworkSupported=1"
32WGET_URL=http://updates.netgear.com/ecosystem/scripts
33WGET_FILE=pkg.tar.gz
34
35#################################################################
36# Command parameters
37#################################################################
38PATH_ECO_ENV=${1}
39PATH_CURRENT_SETTING=${2}
40if [ -z "${PATH_ECO_ENV}" ] || [ -z "${PATH_CURRENT_SETTING}" ]; then
41    echo "Usage:"
42    echo "    ${0} <path-for-eco.env> <path-for-currentsetting.htm>"
43    echo " e.g."
44    echo "    ${0} /tmp /www"
45    exit 1
46fi
47
48# Source the eco.env file
49. ${PATH_ECO_ENV}/eco.env
50
51SAVE_DIR=$PWD
52THIS_SCRIPT="cp_checkbox.sh"
53LOGFILE="/tmp/cp_checkbox.log"
54TESTFILE="test.txt"
55TESTMSG="Hello World"
56if [ -f ${LOGFILE} ]; then
57    mv -f ${LOGFILE} ${LOGFILE}.old
58fi
59echo "# Log file of test results of command:" > ${LOGFILE}
60echo "${0} ${1} ${2}" >> ${LOGFILE}
61
62COUNT_PASS=0
63COUNT_FAIL=0
64COUNT_WARN=0
65
66####################################################################
67#  Functions
68####################################################################
69# cp_log_header()
70#   input:  ${1} = header to be logged
71cp_log_header() {
72    echo -n "${1} " >> ${LOGFILE}
73    return 0
74}
75
76####################################################################
77# cp_log_newline()
78cp_log_newline() {
79    echo "" >> ${LOGFILE}
80}
81
82####################################################################
83# cp_log_uptime()
84cp_log_uptime() {
85    echo "" >> ${LOGFILE}
86    echo " `uptime`" >> ${LOGFILE}
87}
88
89################################################
90# cp_log_result()
91#   input:  ${1} = result:
92#	       0 = passed
93#	     > 0 = failed
94#	     < 0 = warning
95cp_log_result() {
96    if [ ${1} -eq 0 ]; then
97	echo "passed" >> ${LOGFILE}
98	COUNT_PASS=`expr $COUNT_PASS + 1`
99    elif [ ${1} -gt 0 ]; then
100	echo "${1} *** failed ***" >> ${LOGFILE}
101	COUNT_FAIL=`expr $COUNT_FAIL + 1`
102    else
103	echo "${1} * warning *" >> ${LOGFILE}
104	COUNT_WARN=`expr $COUNT_WARN + 1`
105    fi
106    return 0
107}
108
109################################################
110# cp_check_command()
111#
112#   function for shell command verification
113#   input:  ${1} = shell command
114#	    ${2}..${9} = command parameters
115#   output: result (passed/failed) - in $LOGFILE
116cp_check_command() {
117    cp_log_header "Checking command $1 $2 $3 $4 $5 $6 $7 $8 $9..."
118    ${1} ${2} ${3} ${4} ${5} ${6} ${7} ${8} ${9}
119    CMD_RESULT=$?
120    cp_log_result ${CMD_RESULT}
121    return ${CMD_RESULT}
122}
123
124################################################
125# cp_check_size()
126#
127#   function for checking a size:
128#   input: $1 = size to check
129#	   $2 = minimum size
130#	   $3 = warning size
131#	   $4 = Header string to print
132#   output 0 = passed
133#	   1 = failed
134#	  -1 = warning
135cp_check_size() {
136    cp_log_header "Checking $4 size $1"
137    if [ $1 -ge $2 ]; then
138	cp_log_header ">= $2"
139	cp_log_result 0
140    elif [ $1 -lt $3 ]; then
141	cp_log_header "< $3"
142	cp_log_result 1
143    else
144	cp_log_header "falls between $2 and $3"
145	cp_log_result -1
146    fi
147}
148
149####################################################################
150# Execute the platform dependent script, if ODM provides one. 
151####################################################################
152SCRIPT_PATH=`echo "${0}" | sed -e"s/${THIS_SCRIPT}//"`
153cp_log_uptime
154if [ -x ${SCRIPT_PATH}cp_platform.sh ]; then
155    echo "Executing ${SCRIPT_PATH}cp_platform.sh ..." >> ${LOGFILE}
156    . ${SCRIPT_PATH}cp_platform.sh
157else
158    echo "No ${SCRIPT_PATH}cp_platform.sh" >> ${LOGFILE}
159fi
160
161####################################################################
162# System resource check:
163####################################################################
164# - check if /bin is in $PATH
165cp_log_uptime
166cp_log_header "Checking /bin in PATH ..."
167echo ${PATH} | grep ":/bin"
168BIN_IN_PATH=$?
169if [ $BIN_IN_PATH -eq 1 ]; then
170    echo ${PATH} | grep "^/bin"
171    BIN_IN_PATH=$?
172fi
173cp_log_result $BIN_IN_PATH
174
175# - check non-volatile storage file create/delete
176cp_log_header "Non-volatile storage file create/delete ..."
177if [ -z $APPS_MOUNT_POINT ]; then
178    cp_log_result 1
179else
180    rm -f $APPS_MOUNT_POINT/$TESTFILE
181    if [ -f $APPS_MOUNT_POINT/$TESTFILE ]; then
182	cp_log_header "rm"
183	cp_log_result 2	# Failed to remove file
184    else
185    # - create a file
186	echo "$TESTMSG" > $APPS_MOUNT_POINT/$TESTFILE
187	if [ ! -f $APPS_MOUNT_POINT/$TESTFILE ]; then
188	    cp_log_header "echo"
189	    cp_log_result 3		# Failed to create file
190	else
191	    # - check file content
192	    if [ "`cat $APPS_MOUNT_POINT/$TESTFILE`" != "$TESTMSG" ]; then
193		cp_log_header "$TESTMSG"
194		cp_log_result 4	# The file content is different
195	    else
196	        # -delete file
197		cp_log_newline
198		cp_check_command rm $APPS_MOUNT_POINT/$TESTFILE
199	    fi
200	fi
201    fi
202fi
203
204# - check non-volatile storage size
205NAND_SIZE=`df -k $APPS_MOUNT_POINT | grep "[0-9]" | grep -v 1k | awk '{ print $2 }'`
206cp_check_size $NAND_SIZE $MIN_NAND_SIZE $WARN_NAND_SIZE "non-volatile storage"
207
208# - check free RAM size
209RAM_SIZE=`free | grep "Mem:" | awk '{ print $4 }'`
210cp_check_size $RAM_SIZE $MIN_RAM_SIZE $WARN_RAM_SIZE "free RAM"
211
212# - check web browser file
213cp_check_command grep "$SETTING_ENTRY" $PATH_CURRENT_SETTING/$BROWSER_FILE
214
215####################################################################
216# Check environment variables defined in eco.env 
217####################################################################
218cp_log_uptime
219echo "Checking eco.env ..." >> ${LOGFILE}
220cat ${PATH_ECO_ENV}/eco.env | grep "^export" | sed -e's/=/ /' \
221		| awk '{ print $2 " " $3 }' > /tmp/eco.env.test
222while read line; do
223    ENV_VAR_NAME=`echo $line | awk '{ print $1 }'`
224    ENV_VAR_VALUE=`echo $line | awk '{ print $2 }'`
225    cp_log_header "  $ENV_VAR_NAME=$ENV_VAR_VALUE ... "
226    RESULT=0
227    if [ -z $ENV_VAR_VALUE ]; then
228	RESULT=1
229	cp_log_header "(empty) "
230    elif [ "$ENV_VAR_NAME" = "DEVICE_MODEL_NAME" ]; then
231	DEV_MODEL_UPCASE=`echo $ENV_VAR_VALUE | tr '[:lower:]' '[:upper:]'`
232	if [ "$DEV_MODEL_UPCASE" = "$ENV_VAR_VALUE" ]; then
233	    RESULT=1
234	    cp_log_header "(uppercase) "
235	fi
236    fi
237    cp_log_result $RESULT
238done < /tmp/eco.env.test
239
240
241####################################################################
242# Check shell commands
243#   Some shell commands are already used in this script so we do
244# not need to check them.  Some cannot be checked by the function
245# cp_check_command(), e.g, for loop, case statement, etc.
246####################################################################
247cp_log_uptime
248echo "Checking shell commands ..." >> ${LOGFILE} 
249cp_check_command uptime
250cp_check_command let x=1+2
251cp_check_command cp ${PATH_ECO_ENV}/eco.env /tmp/eco.env.test
252cp_check_command mkdir $APPS_MOUNT_POINT/testdir
253cd $APPS_MOUNT_POINT
254cp_check_command find -name testdir -print
255cd $SAVE_DIR
256cp_check_command rmdir $APPS_MOUNT_POINT/testdir
257cp_check_command md5sum ${PATH_ECO_ENV}/eco.env 
258cp_check_command cut -d'=' -f1 /tmp/eco.env.test
259cp_check_command sed -e's/=/@/' /tmp/eco.env.test
260cp_check_command tail /tmp/eco.env.test
261cp_check_command wc -l /tmp/eco.env.test
262cp_check_command sleep 1
263cp_check_command ping -c 1 127.0.0.1
264# - check expr
265cp_log_header "Checking command expr ..."
266X=0
267X=`expr $X + 1`
268if [ $X -eq 1 ]; then
269    cp_log_result 0
270else
271    cp_log_result 1
272fi
273# - check whether 'ps' or 'ps -ef' is supported
274PS_CMD="ps"
275PS_LIST=`${PS_CMD}`
276if [ $? -ne 0 ]; then
277    PS_CMD="ps -ef"
278fi
279cp_check_command ${PS_CMD}
280# - check crond
281cp_log_header "Checking command crond ..."
282CROND=`${PS_CMD} | grep " crond " | grep -v grep | wc -l`
283if [ ${CROND} -eq 0 ]; then
284    crond -b
285    CROND=`${PS_CMD} | grep " crond " | grep -v grep | wc -l`
286fi
287RESULT=`expr 1 - ${CROND}`
288cp_log_result $RESULT
289# - check crondtab
290crontab -l > /tmp/cp_crontab
291cp_check_command crontab /tmp/cp_crontab
292# - check case statement and for loop 
293cp_log_header "Checking case statement in for loop ..."
294X=1         
295for X in 1 2 3 ; do
296    case $X in
297	1 | 2 )
298	    echo "X=$X le 2";;
299	3 )
300	    echo "X=$X eq 3";;
301    esac
302done
303cp_log_result $?
304
305####################################################################
306# - check wget and tar commands
307WGET_OUTPUT=/tmp/$WGET_FILE
308cp_check_command rm -f $WGET_OUTPUT
309cp_check_command wget $WGET_URL/$DEVICE_MODEL_NAME/$WGET_FILE \
310		-P/tmp --delete-after
311cp_check_command wget -4 $WGET_URL/$DEVICE_MODEL_NAME/$WGET_FILE \
312		-T 10 --tries=1 -O $WGET_OUTPUT
313cd /tmp
314cp_log_header "Checking tar ..."
315cp_check_command tar -zxf $WGET_FILE
316
317####################################################################
318cp_log_uptime
319cd $SAVE_DIR
320echo "All checking done." >> ${LOGFILE}
321echo " $COUNT_PASS passed" >> ${LOGFILE}
322echo " $COUNT_FAIL failed" >> ${LOGFILE}
323echo " $COUNT_WARN warnings" >> ${LOGFILE}
324echo "" >> ${LOGFILE}
325echo "Shell commands not checked but used in this script:" >> ${LOGFILE}
326echo "  cd, tr, while, awk, echo, mv, read" >> ${LOGFILE}
327echo "Shell commands not checked or used in this script:" >> ${LOGFILE}
328echo "  vi" >> ${LOGFILE}
329
330echo "${0} - All checking done - Results in ${LOGFILE}"
331
332exit 0
333
334