Magellan Linux

Annotation of /alx-src/branches/alxconf-060/init.d/alxsetstate

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *