1 |
#!/bin/bash |
#!/bin/bash |
2 |
|
# $Id$ |
3 |
|
|
4 |
#startup seq: K01 Level {0,6} | S99 Level {3,4,5} |
#%rlevels: 3:s 0:k |
|
|
|
|
#%rlevels: 3:s 4:s 5:s 0:k 6:k |
|
5 |
#%start: 99 |
#%start: 99 |
6 |
#%stop: 01 |
#%stop: 01 |
7 |
|
|
10 |
#%before: |
#%before: |
11 |
#%after: |
#%after: |
12 |
|
|
13 |
source /etc/sysconfig/rc |
source /etc/conf.d/rc |
14 |
source $rc_functions |
source $rc_functions |
15 |
|
|
16 |
#mysql settings |
# mysql settings |
17 |
source /etc/alx-config/config.rc |
source /etc/alxconfig-ng/config.rc |
18 |
source /opt/alx-config/functions/mysqlfunctions |
# helper functions |
19 |
source /opt/alx-config/functions/alx-mysql-functions |
source ${ALX_FUNCTIONS}/mysqlfunctions |
20 |
|
source ${ALX_FUNCTIONS}/serial_functions |
21 |
#check if mysql is available |
source ${ALX_FUNCTIONS}/common |
|
[ -x /usr/bin/mysql ] && MYSQL_ALX=true |
|
|
|
|
|
#other needed vars |
|
|
ALX_HW_DETECT=false |
|
22 |
|
|
23 |
#unset vars which may kill us |
# unset vars which may kill us |
24 |
unset ALX_SERIAL ALX_STATE |
unset ALX_SERIAL ALX_STATE ALX_IFACE |
25 |
|
|
26 |
#get current system state |
# get current system state |
27 |
if [ -f /etc/alx-config/state/state ] |
if [ -f /etc/alxconfig-ng/state/state ] |
28 |
then |
then |
29 |
source /etc/alx-config/state/state |
source /etc/alxconfig-ng/state/state |
30 |
else |
else |
31 |
ALX_STATE=error |
ALX_STATE=error |
32 |
fi |
fi |
33 |
|
|
34 |
|
if [ -f ${SETTINGSPATH}/confd-networking ] |
35 |
|
then |
36 |
|
ALX_IFACE="$(< ${SETTINGSPATH}/confd-networking)" |
37 |
|
fi |
38 |
|
[[ -z ${ALX_IFACE} ]] && export ALX_IFACE=eth0 |
39 |
|
|
40 |
# need to put this to an extra init script which will be |
# need to put this to an extra init script which will be |
41 |
# executed when the real network is up and not the preliminary. |
# executed when the real network is up and not the preliminary. |
42 |
# nice name is alx_connected_state or sth like this |
# nice name is alx_connected_state or sth like this |
43 |
set_current_network_state(){ |
set_current_network_state() |
44 |
local CUR_IP CUR_MAC ID |
{ |
45 |
|
local cur_ip cur_mac cur_mtime id |
46 |
echo -e ${COLMAGENTA}"Register system to database"${COLDEFAULT} |
|
47 |
|
rc_mecho "Register system to database" |
48 |
CUR_IP=$(/sbin/ifconfig eth0 | sed -n '/addr:/s/ [^r]*..//gp') |
|
49 |
CUR_MAC=$(/sbin/ifconfig eth0 | grep HWaddr | cut -d ' ' -f11) |
cur_ip=$(/sbin/ifconfig ${ALX_IFACE} | sed -n '/addr:/s/ [^r]*..//gp') |
50 |
CUR_MTIME=$(date +%s) |
cur_mac=$(/sbin/ifconfig ${ALX_IFACE} | grep HWaddr | cut -d ' ' -f11) |
51 |
|
cur_mtime=$(date +%s) |
52 |
|
|
53 |
# validate current serial |
# validate current serial |
54 |
if ! validate_serial "${ALX_SERIAL}" "${ALX_REG_DATE}" "${CUR_MAC}" |
if ! validate_serial "${ALX_SERIAL}" "${ALX_REG_DATE}" "${cur_mac}" |
55 |
then |
then |
56 |
|
# abort on non valid serial |
57 |
ALX_STATE="invalid serial" |
ALX_STATE="invalid serial" |
58 |
|
local CONFIG="/etc/alxconfig-ng/state/state" |
59 |
|
clearconfig |
60 |
|
addconfig "ALX_STATE=\"${ALX_STATE}\"" |
61 |
|
show_invalid_serial_msg |
62 |
|
exit 1 |
63 |
fi |
fi |
64 |
|
|
65 |
#first check if an entry exist with my serial |
# first check if an entry exist with my serial |
66 |
# if it exist update this entry else insert a new one |
# if it exist update this entry else insert a new one |
67 |
ID=$(mysql_command ${SQL_USER} ${SQL_PASS} ${SQL_HOST} ${SQL_DB} \ |
id=$(mysqldo "select serial from state_connected where serial=${ALX_SERIAL};") |
|
"select serial from state_connected where serial=${ALX_SERIAL};") |
|
68 |
|
|
69 |
if [ -n "${ID}" ] |
if [[ -n ${id} ]] |
70 |
then |
then |
71 |
#run an update |
# run an update |
72 |
|
|
73 |
#nice status |
# nice status |
74 |
$CURS_UP |
$CURS_UP |
75 |
$SET_WCOL |
$SET_WCOL |
76 |
echo "[ U, State: ${ALX_STATE} ]" |
rc_echo "[ SN: ${ALX_SERIAL}, U, ${ALX_STATE} ]" |
77 |
|
|
78 |
mysql_command ${SQL_USER} ${SQL_PASS} ${SQL_HOST} ${SQL_DB} \ |
mysqldo "update state_connected set |
|
"update state_connected set |
|
79 |
hostname='${HOSTNAME}', |
hostname='${HOSTNAME}', |
80 |
ip='${CUR_IP}', |
ip='${cur_ip}', |
81 |
mac='${CUR_MAC}', |
mac='${cur_mac}', |
82 |
state='${ALX_STATE}', |
state='${ALX_STATE}', |
83 |
mtime='${CUR_MTIME}' |
mtime='${cur_mtime}' |
84 |
where serial=${ALX_SERIAL};" |
where serial=${ALX_SERIAL};" |
|
|
|
|
evaluate_retval |
|
85 |
else |
else |
86 |
#run an insert |
# run an insert |
87 |
|
|
88 |
#nice status |
# nice status |
89 |
$CURS_UP |
$CURS_UP |
90 |
$SET_WCOL |
$SET_WCOL |
91 |
echo "[ N, State: ${ALX_STATE} ]" |
rc_echo "[ SN: ${ALX_SERIAL}, N, ${ALX_STATE} ]" |
92 |
|
|
93 |
mysql_command ${SQL_USER} ${SQL_PASS} ${SQL_HOST} ${SQL_DB} \ |
mysqldo "insert into state_connected( |
|
"insert into state_connected( |
|
94 |
serial, |
serial, |
95 |
hostname, |
hostname, |
96 |
ip, |
ip, |
101 |
values( |
values( |
102 |
'${ALX_SERIAL}', |
'${ALX_SERIAL}', |
103 |
'${HOSTNAME}', |
'${HOSTNAME}', |
104 |
'${CUR_IP}', |
'${cur_ip}', |
105 |
'${CUR_MAC}', |
'${cur_mac}', |
106 |
'${ALX_STATE}', |
'${ALX_STATE}', |
107 |
'${CUR_MTIME}' |
'${cur_mtime}' |
108 |
);" |
);" |
|
evaluate_retval |
|
109 |
fi |
fi |
110 |
} |
} |
111 |
|
|
|
|
|
112 |
# need to put this to an extra init script which will |
# need to put this to an extra init script which will |
113 |
# be executed first when the system is going down. |
# be executed first when the system is going down. |
114 |
# nice name is alx_connected_state or sth like this |
# nice name is alx_connected_state or sth like this |
115 |
unset_alx_connected(){ |
unset_alx_connected() |
116 |
local SQL_OPTS |
{ |
117 |
|
local cur_mac |
118 |
echo -e ${COLMAGENTA}"Unregister system from database"${COLDEFAULT} |
cur_mac=$(/sbin/ifconfig ${ALX_IFACE} | grep HWaddr | cut -d ' ' -f11) |
119 |
|
|
120 |
|
rc_mecho "Unregister system from database" |
121 |
|
|
122 |
|
# nice status |
123 |
|
$CURS_UP |
124 |
|
$SET_WCOL |
125 |
|
rc_echo "[ SN: ${ALX_SERIAL} ]" |
126 |
|
|
127 |
mysql_command ${SQL_USER} ${SQL_PASS} ${SQL_HOST} ${SQL_DB} \ |
mysqldo "delete from state_connected where serial='${ALX_SERIAL}' and mac='${cur_mac}';" |
|
"delete from state_connected where serial='${ALX_SERIAL}' and mac='${CUR_MAC}';" |
|
128 |
evaluate_retval |
evaluate_retval |
129 |
} |
} |
130 |
|
|
131 |
########### starts here ################ |
########### starts here ################ |
132 |
|
|
133 |
#first of all get current system serial |
# first of all get current system serial |
134 |
if [ -f /etc/alx-config/serial ] |
if [ -f /etc/alxconfig-ng/serial ] |
135 |
then |
then |
136 |
source /etc/alx-config/serial |
source /etc/alxconfig-ng/serial |
137 |
else |
else |
138 |
ALX_SERIAL=0 |
ALX_SERIAL=0 |
139 |
fi |
fi |
140 |
|
|
|
|
|
141 |
case $1 in |
case $1 in |
142 |
start) |
start) |
143 |
|
# check if mysql server is reachable |
144 |
|
# if not abort this script |
145 |
|
reach_mysql_server || exit 1 |
146 |
|
|
147 |
set_current_network_state |
set_current_network_state |
148 |
|
evaluate_retval |
149 |
;; |
;; |
150 |
|
|
151 |
stop) |
stop) |
152 |
|
# check if mysql server is reachable |
153 |
|
# if not abort this script |
154 |
|
reach_mysql_server || exit 1 |
155 |
|
|
156 |
unset_alx_connected |
unset_alx_connected |
157 |
|
evaluate_retval |
158 |
;; |
;; |
159 |
|
|
160 |
*) |
*) |
161 |
echo "Usage: $0 {start|stop} ..." |
rc_echo "Usage: $0 {start|stop} ..." |
162 |
;; |
;; |
|
esac |
|
163 |
|
esac |