--- alx-src/trunk/alxconfig-ng/init.d/alxsetstate 2005/05/11 12:25:39 274 +++ alx-src/branches/alxconf-060/init.d/alxsetstate 2011/08/26 11:49:43 2778 @@ -1,6 +1,7 @@ #!/bin/bash +# $Id$ -#%rlevels: 3:s 4:s 5:s 0:k 6:k +#%rlevels: 3:s 0:k #%start: 99 #%stop: 01 @@ -9,26 +10,20 @@ #%before: #%after: -# $Header: /home/cvsd/alx-cvs/alx-src/alxconfig-ng/init.d/alxsetstate,v 1.6 2005-05-11 12:25:39 niro Exp $ - -source /etc/sysconfig/rc +source /etc/conf.d/rc source $rc_functions -#mysql settings +# mysql settings source /etc/alxconfig-ng/config.rc -source /usr/lib/alxconfig-ng/functions/mysqlfunctions -source /usr/lib/alxconfig-ng/functions/serial_functions - -#check if mysql is available -[ -x /usr/bin/mysql ] && MYSQL_ALX=true - -#other needed vars -ALX_HW_DETECT=false +# helper functions +source ${ALX_FUNCTIONS}/mysqlfunctions +source ${ALX_FUNCTIONS}/serial_functions +source ${ALX_FUNCTIONS}/common -#unset vars which may kill us -unset ALX_SERIAL ALX_STATE +# unset vars which may kill us +unset ALX_SERIAL ALX_STATE ALX_IFACE -#get current system state +# get current system state if [ -f /etc/alxconfig-ng/state/state ] then source /etc/alxconfig-ng/state/state @@ -36,58 +31,64 @@ ALX_STATE=error fi +if [ -f ${SETTINGSPATH}/confd-networking ] +then + ALX_IFACE="$(< ${SETTINGSPATH}/confd-networking)" +fi +[[ -z ${ALX_IFACE} ]] && export ALX_IFACE=eth0 + # need to put this to an extra init script which will be # executed when the real network is up and not the preliminary. # nice name is alx_connected_state or sth like this -set_current_network_state(){ +set_current_network_state() +{ local CUR_IP CUR_MAC ID - echo -e ${COLMAGENTA}"Register system to database"${COLDEFAULT} + rc_mecho "Register system to database" - CUR_IP=$(/sbin/ifconfig eth0 | sed -n '/addr:/s/ [^r]*..//gp') - CUR_MAC=$(/sbin/ifconfig eth0 | grep HWaddr | cut -d ' ' -f11) + CUR_IP=$(/sbin/ifconfig ${ALX_IFACE} | sed -n '/addr:/s/ [^r]*..//gp') + CUR_MAC=$(/sbin/ifconfig ${ALX_IFACE} | grep HWaddr | cut -d ' ' -f11) CUR_MTIME=$(date +%s) # validate current serial if ! validate_serial "${ALX_SERIAL}" "${ALX_REG_DATE}" "${CUR_MAC}" then + # abort on non valid serial ALX_STATE="invalid serial" + echo "ALX_STATE=\"${ALX_STATE}\"" > /etc/alxconfig-ng/state/state + show_invalid_serial_msg + exit 1 fi - #first check if an entry exist with my serial + # first check if an entry exist with my serial # if it exist update this entry else insert a new one - ID=$(mysql_command ${SQL_USER} ${SQL_PASS} ${SQL_HOST} ${SQL_DB} \ - "select serial from state_connected where serial=${ALX_SERIAL};") + ID=$(mysqldo "select serial from state_connected where serial=${ALX_SERIAL};") if [ -n "${ID}" ] then - #run an update + # run an update - #nice status + # nice status $CURS_UP $SET_WCOL - echo "[ U, State: ${ALX_STATE} ]" + rc_echo "[ SN: ${ALX_SERIAL}, U, ${ALX_STATE} ]" - mysql_command ${SQL_USER} ${SQL_PASS} ${SQL_HOST} ${SQL_DB} \ - "update state_connected set + mysqldo "update state_connected set hostname='${HOSTNAME}', ip='${CUR_IP}', mac='${CUR_MAC}', state='${ALX_STATE}', mtime='${CUR_MTIME}' where serial=${ALX_SERIAL};" - - evaluate_retval else - #run an insert + # run an insert - #nice status + # nice status $CURS_UP $SET_WCOL - echo "[ N, State: ${ALX_STATE} ]" + rc_echo "[ SN: ${ALX_SERIAL}, N, ${ALX_STATE} ]" - mysql_command ${SQL_USER} ${SQL_PASS} ${SQL_HOST} ${SQL_DB} \ - "insert into state_connected( + mysqldo "insert into state_connected( serial, hostname, ip, @@ -103,7 +104,6 @@ '${ALX_STATE}', '${CUR_MTIME}' );" - evaluate_retval fi } @@ -111,22 +111,28 @@ # need to put this to an extra init script which will # be executed first when the system is going down. # nice name is alx_connected_state or sth like this -unset_alx_connected(){ +unset_alx_connected() +{ local SQL_OPTS local CUR_MAC - CUR_MAC=$(/sbin/ifconfig eth0 | grep HWaddr | cut -d ' ' -f11) + CUR_MAC=$(/sbin/ifconfig ${ALX_IFACE} | grep HWaddr | cut -d ' ' -f11) - echo -e ${COLMAGENTA}"Unregister system from database"${COLDEFAULT} + rc_mecho "Unregister system from database" - mysql_command ${SQL_USER} ${SQL_PASS} ${SQL_HOST} ${SQL_DB} \ - "delete from state_connected where serial='${ALX_SERIAL}' and mac='${CUR_MAC}';" + # nice status + $CURS_UP + $SET_WCOL + rc_echo "[ SN: ${ALX_SERIAL} ]" + + mysqldo "delete from state_connected where serial='${ALX_SERIAL}' and mac='${CUR_MAC}';" evaluate_retval } + ########### starts here ################ -#first of all get current system serial +# first of all get current system serial if [ -f /etc/alxconfig-ng/serial ] then source /etc/alxconfig-ng/serial @@ -134,17 +140,26 @@ ALX_SERIAL=0 fi - case $1 in start) - + # check if mysql server is reachable + # if not abort this script + reach_mysql_server || exit 1 + set_current_network_state + evaluate_retval ;; stop) + # check if mysql server is reachable + # if not abort this script + reach_mysql_server || exit 1 + unset_alx_connected + evaluate_retval ;; *) echo "Usage: $0 {start|stop} ..." ;; esac +