source: nutchez-0.2/src/test/nutchez @ 186

Last change on this file since 186 was 186, checked in by rock, 14 years ago

(1)新增語言(英文和繁中)
(2)BugFix?: tomcat 無法 shutdown (多 kill 步驟)

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