1 | #!/bin/bash |
---|
2 | # Program: |
---|
3 | # NutchEz management interface |
---|
4 | # Author: |
---|
5 | # Waue, Shunfa, Rock {waue, shunfa, rock}@nchc.org.tw |
---|
6 | # Version: |
---|
7 | # 1.0 |
---|
8 | # History: |
---|
9 | # 2010/06/07 Rock First release (1.0) |
---|
10 | |
---|
11 | # [Variables Declaration] # |
---|
12 | DIALOG="dialog" |
---|
13 | main_choice= |
---|
14 | lang= |
---|
15 | |
---|
16 | # [PATH Declaration] # |
---|
17 | NutchEZ_Install_PATH="/opt/nutchez" |
---|
18 | Tomcat_HOME="/opt/nutchez/tomcat" |
---|
19 | NutchEZ_HOME="/home/nutchuser/nutchez" |
---|
20 | Work_Path=$(echo $0 | sed 's/client_install//') |
---|
21 | |
---|
22 | # [Env Declaration] # |
---|
23 | . $Work_Path/lang_en_US_nutchez |
---|
24 | |
---|
25 | # [Functions Declaration] # |
---|
26 | ## [Pre-check for language] ## |
---|
27 | function prepare_lang(){ |
---|
28 | lang=$(locale | grep 'LANG=' | cut -d "=" -f2) |
---|
29 | |
---|
30 | # if lang=zh then source lang_zh_TW_nutchez |
---|
31 | echo $lang | grep 'zh' && source $Work_Path/lang_zh_TW_nutchez |
---|
32 | } |
---|
33 | |
---|
34 | |
---|
35 | ## [Pre-Check for nutch_nodes file] ## |
---|
36 | function prepare_check(){ |
---|
37 | # 若無 nutchez_nodes 則跳出 |
---|
38 | if [ ! -e "${NutchEZ_HOME}/system/nutch_nodes" ]; then |
---|
39 | $DIALOG --clear --backtitle "$dia_back" --title "$dia_pre_check_title_1" \ |
---|
40 | --msgbox "$dia_pre_check_msg_1 $NutchEZ_HOME/system/nutch_nodes !!!" 10 40 |
---|
41 | exit |
---|
42 | fi |
---|
43 | |
---|
44 | # 判斷 nutch_nodes 和 nutch_nodes.back |
---|
45 | # 第一次執行則要求 user 更新 /etc/hosts |
---|
46 | if [ ! -e "${NutchEZ_HOME}/system/nutch_nodes.bak" ]; then |
---|
47 | $DIALOG --clear --backtitle "$dia_back" --title "$dia_pre_check_title_2" \ |
---|
48 | --yesno "$dia_pre_check_yesno_1\n$dia_pre_check_yesno_2" 10 55 |
---|
49 | |
---|
50 | echo -e "$pre_check_echo_1" |
---|
51 | ./duplicate_del "${NutchEZ_HOME}/system/nutch_nodes" |
---|
52 | su root -c "./duplicate_del /etc/hosts && ./add_hosts ${NutchEZ_HOME}/system/nutch_nodes /etc/hosts" |
---|
53 | # 若密碼輸入失敗則跳出 |
---|
54 | if [ $? == "1" ]; then |
---|
55 | echo "$pre_check_echo_2" |
---|
56 | exit |
---|
57 | fi |
---|
58 | # 若 nutch_nodes 被更新過,則要求更新 /etc/hosts |
---|
59 | elif [ ${NutchEZ_HOME}/system/nutch_nodes -nt ${NutchEZ_HOME}/system/nutch_nodes.bak ]; then |
---|
60 | $DIALOG --clear --backtitle "$dia_back" --title "$dia_pre_check_title_2" \ |
---|
61 | --yesno "$dia_pre_check_yesno_3\n$dia_pre_check_yesno_2" 10 55 |
---|
62 | |
---|
63 | echo -e "$pre_check_echo_1" |
---|
64 | ./duplicate_del "${NutchEZ_HOME}/system/nutch_nodes" |
---|
65 | su root -c "./duplicate_del /etc/hosts && ./add_hosts ${NutchEZ_HOME}/system/nutch_nodes /etc/hosts" |
---|
66 | # 若密碼輸入失敗則跳出 |
---|
67 | if [ $? == "1" ]; then |
---|
68 | echo "$pre_check_echo_2=" |
---|
69 | exit |
---|
70 | fi |
---|
71 | fi |
---|
72 | } |
---|
73 | |
---|
74 | ## [Main Menu] ## |
---|
75 | function main_menu(){ |
---|
76 | main_choice="/tmp/main_choice" |
---|
77 | |
---|
78 | $DIALOG --clear --backtitle "$dia_back" \ |
---|
79 | --title "$dia_main_title_1" \ |
---|
80 | --menu "$dia_choose" 15 60 7 \ |
---|
81 | "cluster_status" "$dia_main_menu_1_1" \ |
---|
82 | "cluster_setup" "$dia_main_menu_1_2" \ |
---|
83 | "server_setup" "$dia_main_menu_1_3" \ |
---|
84 | "tomcat_switch" "$dia_main_menu_1_4" \ |
---|
85 | "tomcat_port" "$dia_main_menu_1_5" \ |
---|
86 | "lang_switch" "$dia_main_menu_1_6" \ |
---|
87 | "exit" "$dia_exit" 2>$main_choice |
---|
88 | } |
---|
89 | |
---|
90 | ## [Cluster Status (datanode & tasktracker)] ## |
---|
91 | function cluster_status(){ |
---|
92 | IP_list=$(cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#' | awk '{print $1}') |
---|
93 | clusterStatus="/tmp/clusterStatus" |
---|
94 | rm $clusterStatus 2> /dev/null |
---|
95 | #printf '%16s\t %11s\t %10s\t %25s\n' "[IP] [Hostanme] [Network] [Dtatnode & Tasktracker]" >>$clusterStatus |
---|
96 | echo -e "$cluster_status_echo_1" >>$clusterStatus |
---|
97 | echo -e "------------------------------------------------------------------------" >>$clusterStatus |
---|
98 | echo -e "\n$cluster_status_echo_2" |
---|
99 | for ip in $IP_list |
---|
100 | do |
---|
101 | # Check Network status |
---|
102 | ip_status="online" |
---|
103 | ping -c1 -w1 $ip 2>&1 > /dev/null || ip_status="offline" |
---|
104 | # Check Hadoop/Nutch service through ssh |
---|
105 | Task_Data_status="stop" |
---|
106 | if [ $ip_status == "online" ]; then |
---|
107 | Task_Data=$(ssh -o StrictHostKeyChecking=no $ip "jps |grep TaskTracker ; jps | grep DataNode") |
---|
108 | fi |
---|
109 | |
---|
110 | [ -z "$Task_Data" ] || Task_Data_status="running" |
---|
111 | host_name=$(cat $NutchEZ_HOME/system/nutch_nodes | grep $ip | awk '{print $2}') |
---|
112 | echo -e "$ip $host_name \t\t $ip_status \t $Task_Data_status" >>$clusterStatus |
---|
113 | # printf '%16s\t %11s\t %10s\t %25s\n' "$ip $host_name $ip_status $Task_Data_status" >>$clusterStatus |
---|
114 | done |
---|
115 | |
---|
116 | read -p "$cluster_status_read_1" |
---|
117 | $DIALOG --clear --backtitle "$dia_back" \ |
---|
118 | --title "$dia_cluster_status_title_1" --textbox $clusterStatus 20 90 |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | ## [Server Setup (namenode & jobtracker)] ## |
---|
123 | function server_setup(){ |
---|
124 | serverSetup=/tmp/serverSetup |
---|
125 | pid_name=$(jps | grep NameNode) |
---|
126 | pid_job=$(jps | grep JobTracker) |
---|
127 | |
---|
128 | if [ -z "$pid_name" -a -z "$pid_job" ]; then |
---|
129 | $DIALOG --clear --backtitle "$dia_back" --title "$dia_server_title_1" \ |
---|
130 | --msgbox "$dia_server_msg_1" 7 50 |
---|
131 | elif [ -z "$pid_name" -a -n "$pid_job" ]; then |
---|
132 | $DIALOG --clear --backtitle "$dia_back" --title "$dia_server_title_1" \ |
---|
133 | --msgbox "$dia_server_msg_2" 7 50 |
---|
134 | elif [ -n "$pid_name" -a -z "$pid_job" ]; then |
---|
135 | $DIALOG --clear --backtitle "$dia_back" --title "$dia_server_title_1" \ |
---|
136 | --msgbox "$dia_server_msg_3" 7 50 |
---|
137 | else |
---|
138 | $DIALOG --clear --backtitle "$dia_back" --title "$dia_server_title_1" \ |
---|
139 | --msgbox "$dia_server_msg_4" 7 50 |
---|
140 | fi |
---|
141 | |
---|
142 | $DIALOG --clear --backtitle "$dia_back" --title "$dia_server_title_2" \ |
---|
143 | --menu "$dia_choose" 15 65 4 \ |
---|
144 | "start" "$dia_server_menu_1_1" \ |
---|
145 | "stop" "$dia_server_menu_1_2" \ |
---|
146 | "restart" "$dia_server_menu_1_3" \ |
---|
147 | "exit" "$dia_exit" 2>$serverSetup |
---|
148 | |
---|
149 | if [ "$(cat $serverSetup)" == "start" ]; then |
---|
150 | $NutchEZ_Install_PATH/nutch/bin/start-dfs.sh |
---|
151 | $NutchEZ_Install_PATH/nutch/bin/start-mapred.sh |
---|
152 | elif [ "$(cat $serverSetup)" == "stop" ]; then |
---|
153 | $NutchEZ_Install_PATH/nutch/bin/stop-dfs.sh |
---|
154 | $NutchEZ_Install_PATH/nutch/bin/stop-mapred.sh |
---|
155 | elif [ "$(cat $serverSetup)" == "restart" ]; then |
---|
156 | $NutchEZ_Install_PATH/nutch/bin/stop-dfs.sh |
---|
157 | $NutchEZ_Install_PATH/nutch/bin/stop-mapred.sh |
---|
158 | $NutchEZ_Install_PATH/nutch/bin/start-dfs.sh |
---|
159 | $NutchEZ_Install_PATH/nutch/bin/start-mapred.sh |
---|
160 | else |
---|
161 | exit |
---|
162 | fi |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | ## [Cluster Setup (datanode & tasktracker)] ## |
---|
167 | function cluster_setup(){ |
---|
168 | # 從 nutch_nodes 讀出 ip 和 hostname |
---|
169 | #IP_list=$(cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#' | awk '{print $1}') |
---|
170 | #HOST_list=$(cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#' | awk '{print $2}') |
---|
171 | LIST_status="off" |
---|
172 | allpart="/tmp/allpart" |
---|
173 | runNodes="/tmp/runNodes" |
---|
174 | clusterSetup="/tmp/clusterSetup" |
---|
175 | |
---|
176 | $DIALOG --clear --backtitle "$dia_back" \ |
---|
177 | --title "$dia_cluster_setup_title_1" \ |
---|
178 | --menu "$dia_choose" 15 55 3 \ |
---|
179 | "All" "$dia_cluster_setup_menu_1_1" \ |
---|
180 | "Part" "$dia_cluster_setup_menu_1_2" \ |
---|
181 | "Exit" "$dia_exit" 2>$allpart |
---|
182 | |
---|
183 | # 判斷是否選 all 和 part,若是 all 直接到服務選單,若是 part 則先到機器選單 |
---|
184 | if [ "$(cat $allpart)" == "All" ]; then |
---|
185 | cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#' | awk '{print $1}' >$runNodes |
---|
186 | elif [ "$(cat $allpart)" == "Part" ]; then |
---|
187 | $DIALOG --clear --backtitle "$dia_back" --title "$dia_cluster_setup_title_2" \ |
---|
188 | --checklist "$dia_cluster_setup_check_1" 15 55 7 $(cat $NutchEZ_HOME/system/nutch_nodes | \ |
---|
189 | grep -v '^$' | grep -v '#' | awk '{print $1 "\t" $2 "\t" "$LIST_status" }') 2>$runNodes |
---|
190 | else |
---|
191 | exit |
---|
192 | fi |
---|
193 | |
---|
194 | if [ -z "$(cat $runNodes)" ]; then |
---|
195 | exit |
---|
196 | else |
---|
197 | $(cat $runNodes) | sed -i 's/"//g' $runNodes |
---|
198 | fi |
---|
199 | |
---|
200 | $DIALOG --clear --backtitle "$dia_back" --title "$dia_cluster_setup_title_3" \ |
---|
201 | --menu "$dia_choose" 15 65 4 \ |
---|
202 | "start" "$dia_cluster_setup_menu_2_1" \ |
---|
203 | "stop" "$dia_cluster_setup_menu_2_2" \ |
---|
204 | "restart" "$dia_cluster_setup_menu_2_3" \ |
---|
205 | "exit" "$dia_exit" 2>$clusterSetup |
---|
206 | |
---|
207 | if [ "$(cat $clusterSetup)" == "start" ]; then |
---|
208 | echo -e "\n$cluster_setup_echo_1" |
---|
209 | for node in $(cat $runNodes) |
---|
210 | do |
---|
211 | ssh -o StrictHostKeyChecking=no $node "$NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start datanode \ |
---|
212 | ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start tasktracker" 2>/dev/null |
---|
213 | [ $? == "0" ] || echo "$cluster_setup_echo_error $node !!!" |
---|
214 | done |
---|
215 | elif [ "$(cat $clusterSetup)" == "stop" ]; then |
---|
216 | echo -e "\n$cluster_setup_echo_3" |
---|
217 | for node in $(cat $runNodes) |
---|
218 | do |
---|
219 | ssh -o StrictHostKeyChecking=no $node "$NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop datanode \ |
---|
220 | ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop tasktracker" 2>/dev/null |
---|
221 | [ $? == "0" ] || echo "$cluster_setup_echo_error $node !!!" |
---|
222 | done |
---|
223 | elif [ "$(cat $clusterSetup)" == "restart" ]; then |
---|
224 | echo -e "\n$cluster_setup_echo_5" |
---|
225 | for node in $(cat $runNodes) |
---|
226 | do |
---|
227 | ssh -o StrictHostKeyChecking=no $node "$NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop datanode \ |
---|
228 | ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop tasktracker \ |
---|
229 | ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start datanode \ |
---|
230 | ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start tasktracker" 2>/dev/null |
---|
231 | [ $? == "0" ] || echo "$cluster_setup_echo_error $node !!!" |
---|
232 | done |
---|
233 | else |
---|
234 | exit |
---|
235 | fi |
---|
236 | } |
---|
237 | |
---|
238 | ## [Tomcat Severice start/stop/restart] ## |
---|
239 | function tomcat_switch(){ |
---|
240 | tom_pids=$(ps x | grep -v 'grep' | grep "tomcat" | awk '{print $1}') |
---|
241 | if [ -n "$tom_pids" ]; then |
---|
242 | $DIALOG --clear --backtitle "$dia_back" --title "$dia_tomcat_switch_title_1" \ |
---|
243 | --msgbox "$dia_tomcat_switch_msg_1" 7 50 |
---|
244 | else |
---|
245 | $DIALOG --clear --backtitle "$dia_back" --title "$dia_tomcat_switch_title_1" \ |
---|
246 | --msgbox "$dia_tomcat_switch_msg_2" 7 50 |
---|
247 | fi |
---|
248 | tomcatSwitch="/tmp/tomcatSwitch" |
---|
249 | $DIALOG --clear --backtitle "$dia_back" --title "$dia_tomcat_switch_title_2" \ |
---|
250 | --menu "$dia_choose" 15 55 4 \ |
---|
251 | "start" "$dia_tomcat_switch_menu_1_1" \ |
---|
252 | "stop" "$dia_tomcat_switch_menu_1_2" \ |
---|
253 | "restart" "$dia_tomcat_switch_menu_1_3" \ |
---|
254 | "exit" "$dia_exit" 2>$tomcatSwitch |
---|
255 | |
---|
256 | if [ "$(cat $tomcatSwitch)" == "exit" ]; then |
---|
257 | exit 0 |
---|
258 | fi |
---|
259 | |
---|
260 | # jude $Tomcat_HOME/bin/startup.sh |
---|
261 | if [ ! -e $Tomcat_HOME/bin/startup.sh ]; then |
---|
262 | echo -e "\n$tomcat_switch_echo_1 $Tomcat_HOME/bin/startup.sh" |
---|
263 | exit |
---|
264 | fi |
---|
265 | |
---|
266 | if [ ! -e $Tomcat_HOME/bin/shutdown.sh ]; then |
---|
267 | echo -e "\n$tomcat_switch_echo_1 $Tomcat_HOME/bin/shutdown.sh" |
---|
268 | exit |
---|
269 | fi |
---|
270 | |
---|
271 | if [ "$(cat $tomcatSwitch)" == "start" ]; then |
---|
272 | echo "$tomcat_switch_echo_3" |
---|
273 | $Tomcat_HOME/bin/startup.sh |
---|
274 | elif [ "$(cat $tomcatSwitch)" == "stop" ]; then |
---|
275 | echo "$tomcat_switch_echo_4" |
---|
276 | $Tomcat_HOME/bin/shutdown.sh |
---|
277 | for tom_pid in $tom_pids |
---|
278 | do |
---|
279 | kill -9 $tom_pid 2>/dev/null |
---|
280 | done |
---|
281 | elif [ "$(cat $tomcatSwitch)" == "restart" ]; then |
---|
282 | echo "$tomcat_switch_echo_5" |
---|
283 | $Tomcat_HOME/bin/shutdown.sh |
---|
284 | for tom_pid in $tom_pids |
---|
285 | do |
---|
286 | kill -9 $tom_pid 2>/dev/null |
---|
287 | done |
---|
288 | $Tomcat_HOME/bin/startup.sh |
---|
289 | else |
---|
290 | exit 0 |
---|
291 | fi |
---|
292 | } |
---|
293 | |
---|
294 | ## [Tomcat Port Change] ## |
---|
295 | function tomcat_port(){ |
---|
296 | |
---|
297 | if [ ! -e $Tomcat_HOME/conf/server.xml ]; then |
---|
298 | $DIALOG --clear --backtitle "$dia_back" --title "$dia_tomcat_port_title_1" \ |
---|
299 | --msgbox "$dia_tomcat_port_msg_1 $Tomcat_HOME/conf/server.xml !!!" 10 50 |
---|
300 | exit |
---|
301 | fi |
---|
302 | tomcatPort="/tmp/tomcatPort" |
---|
303 | $DIALOG --clear --backtitle "$dia_back" --title "$dia_tomcat_port_title_2" \ |
---|
304 | --inputbox "$dia_tomcat_port_input_1" 10 55 2>$tomcatPort |
---|
305 | |
---|
306 | if [ -z "$(cat $tomcatPort)" ]; then |
---|
307 | exit |
---|
308 | else |
---|
309 | modify_line_nu=$(cat -n $Tomcat_HOME/conf/server.xml | grep -v SSL | grep 'HTTP/1.1' | grep '<Connector' | awk '{print $1}') |
---|
310 | sed -i "${modify_line_nu}c <Connector port=\"$(cat $tomcatPort)\" protocol=\"HTTP/1.1\"" $Tomcat_HOME/conf/server.xml |
---|
311 | |
---|
312 | tom_pids=$(ps x | grep -v 'grep' | grep "tomcat" | awk '{print $1}') |
---|
313 | $Tomcat_HOME/bin/shutdown.sh |
---|
314 | for tom_pid in $tom_pids |
---|
315 | do |
---|
316 | kill -9 $tom_pid 2>/dev/null |
---|
317 | done |
---|
318 | $Tomcat_HOME/bin/startup.sh |
---|
319 | fi |
---|
320 | } |
---|
321 | |
---|
322 | |
---|
323 | ## [Language Change] ## |
---|
324 | function lang_switch(){ |
---|
325 | langSwitch="/tmp/langSwitch" |
---|
326 | $DIALOG --clear --backtitle "$dia_back" --title "$dia_lang_title_1" \ |
---|
327 | --menu "$dia_choose" 15 55 3 \ |
---|
328 | "en_US" "$dia_lang_menu_1_1" \ |
---|
329 | "zh_TW" "$dia_lang_menu_1_2" \ |
---|
330 | "Exit" "$dia_exit" 2>$langSwitch |
---|
331 | # |
---|
332 | if [ "$(cat $langSwitch)" == "Exit" ]; then |
---|
333 | exit |
---|
334 | elif [ "$(cat $langSwitch)" == "en_US" ]; then |
---|
335 | source $Work_Path/lang_en_US_nutchez |
---|
336 | main_menu |
---|
337 | menu_choose |
---|
338 | elif [ "$(cat $langSwitch)" == "zh_TW" ]; then |
---|
339 | source $Work_Path/lang_zh_TW_nutchez |
---|
340 | main_menu |
---|
341 | menu_choose |
---|
342 | fi |
---|
343 | } |
---|
344 | |
---|
345 | ## [Menu_choose] ## |
---|
346 | function menu_choose(){ |
---|
347 | case $(cat $main_choice) in |
---|
348 | "cluster_status") |
---|
349 | cluster_status;; |
---|
350 | "cluster_setup") |
---|
351 | cluster_setup;; |
---|
352 | "server_setup") |
---|
353 | server_setup;; |
---|
354 | "tomcat_switch") |
---|
355 | tomcat_switch;; |
---|
356 | "tomcat_port") |
---|
357 | tomcat_port;; |
---|
358 | "lang_switch") |
---|
359 | lang_switch;; |
---|
360 | esac |
---|
361 | } |
---|
362 | |
---|
363 | # [Main Code] # |
---|
364 | if [ $USER != "nutchuser" ]; then |
---|
365 | echo -e "\n$user_error" |
---|
366 | exit |
---|
367 | fi |
---|
368 | |
---|
369 | prepare_lang |
---|
370 | prepare_check |
---|
371 | main_menu |
---|
372 | menu_choose |
---|