Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *