#!/bin/bash # $Id$ #%rlevels: 3:s 0:k #%start: 99 #%stop: 01 #deps #%needs: #%before: #%after: source /etc/conf.d/rc source $rc_functions # mysql settings source /etc/alxconfig-ng/config.rc # 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 ALX_IFACE # get current system state if [ -f /etc/alxconfig-ng/state/state ] then source /etc/alxconfig-ng/state/state else 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() { local cur_ip cur_mac cur_mtime id rc_mecho "Register system to database" 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 # if it exist update this entry else insert a new one id=$(mysqldo "select serial from state_connected where serial=${ALX_SERIAL};") if [ -n "${id}" ] then # run an update # nice status $CURS_UP $SET_WCOL rc_echo "[ SN: ${ALX_SERIAL}, U, ${ALX_STATE} ]" mysqldo "update state_connected set hostname='${HOSTNAME}', ip='${cur_ip}', mac='${cur_mac}', state='${ALX_STATE}', mtime='${cur_mtime}' where serial=${ALX_SERIAL};" else # run an insert # nice status $CURS_UP $SET_WCOL rc_echo "[ SN: ${ALX_SERIAL}, N, ${ALX_STATE} ]" mysqldo "insert into state_connected( serial, hostname, ip, mac, state, mtime ) values( '${ALX_SERIAL}', '${HOSTNAME}', '${cur_ip}', '${cur_mac}', '${ALX_STATE}', '${cur_mtime}' );" 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 cur_mac cur_mac=$(/sbin/ifconfig ${ALX_IFACE} | grep HWaddr | cut -d ' ' -f11) rc_mecho "Unregister system from database" # 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 if [ -f /etc/alxconfig-ng/serial ] then source /etc/alxconfig-ng/serial else 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 ;; *) rc_echo "Usage: $0 {start|stop} ..." ;; esac