#!/bin/bash #%rlevels: 3:s 4:s 5:s 0:k 6:k #%start: 99 #%stop: 01 #deps #%needs: #%before: #%after: # $Header: /home/cvsd/alx-cvs/alx-src/alxconfig-ng/init.d/alxsetstate,v 1.3 2005-03-09 00:04:18 niro Exp $ source /etc/sysconfig/rc source $rc_functions #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 #unset vars which may kill us unset ALX_SERIAL ALX_STATE #get current system state if [ -f /etc/alx-config/state/state ] then source /etc/alx-config/state/state else ALX_STATE=error fi # 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(){ local CUR_IP CUR_MAC ID echo -e ${COLMAGENTA}"Register system to database"${COLDEFAULT} CUR_IP=$(/sbin/ifconfig eth0 | sed -n '/addr:/s/ [^r]*..//gp') CUR_MAC=$(/sbin/ifconfig eth0 | grep HWaddr | cut -d ' ' -f11) CUR_MTIME=$(date +%s) # validate current serial if ! validate_serial "${ALX_SERIAL}" "${ALX_REG_DATE}" "${CUR_MAC}" then ALX_STATE="invalid serial" fi #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};") if [ -n "${ID}" ] then #run an update #nice status $CURS_UP $SET_WCOL echo "[ U, State: ${ALX_STATE} ]" mysql_command ${SQL_USER} ${SQL_PASS} ${SQL_HOST} ${SQL_DB} \ "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 #nice status $CURS_UP $SET_WCOL echo "[ N, State: ${ALX_STATE} ]" mysql_command ${SQL_USER} ${SQL_PASS} ${SQL_HOST} ${SQL_DB} \ "insert into state_connected( serial, hostname, ip, mac, state, mtime ) values( '${ALX_SERIAL}', '${HOSTNAME}', '${CUR_IP}', '${CUR_MAC}', '${ALX_STATE}', '${CUR_MTIME}' );" evaluate_retval fi } # 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(){ local SQL_OPTS echo -e ${COLMAGENTA}"Unregister system from database"${COLDEFAULT} mysql_command ${SQL_USER} ${SQL_PASS} ${SQL_HOST} ${SQL_DB} \ "delete from state_connected where serial='${ALX_SERIAL}' and mac='${CUR_MAC}';" evaluate_retval } ########### starts here ################ #first of all get current system serial if [ -f /etc/alx-config/serial ] then source /etc/alx-config/serial else ALX_SERIAL=0 fi case $1 in start) set_current_network_state ;; stop) unset_alx_connected ;; *) echo "Usage: $0 {start|stop} ..." ;; esac