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