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

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

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

File size: 12.3 KB
RevLine 
[162]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
[172]11# [Variables Declaration] #
[186]12DIALOG="dialog"
[162]13main_choice=
[186]14lang=
[162]15
[172]16# [PATH Declaration] #
[186]17NutchEZ_Install_PATH="/opt/nutchez"
18Tomcat_HOME="/opt/nutchez/tomcat"
19NutchEZ_HOME="/home/nutchuser/nutchez"
[171]20
[186]21# [Env Declaration] #
22. lang_en_US_nutchez
23
[172]24# [Functions Declaration] #
[186]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] ##
[162]35function prepare_check(){ 
[172]36# 若無 nutchez_nodes 則跳出
37if [ ! -e "${NutchEZ_HOME}/system/nutch_nodes" ]; then
[186]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
[162]40    exit
41fi
42
[172]43# 判斷 nutch_nodes 和 nutch_nodes.back
44# 第一次執行則要求 user 更新 /etc/hosts
45if [ ! -e "${NutchEZ_HOME}/system/nutch_nodes.bak" ]; then
[186]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
[162]48
[186]49    echo -e "$pre_check_echo_1"
[172]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
[186]54        echo "$pre_check_echo_2"
[172]55        exit
56    fi 
57# 若 nutch_nodes 被更新過,則要求更新 /etc/hosts
58elif [ ${NutchEZ_HOME}/system/nutch_nodes -nt ${NutchEZ_HOME}/system/nutch_nodes.bak ]; then
[186]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
[165]61
[186]62    echo -e "$pre_check_echo_1"
[172]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                   
[186]67        echo "$pre_check_echo_2="
[172]68        exit                                 
69    fi
70fi
[162]71}
72
[172]73## [Main Menu] ##
[162]74function main_menu(){
75main_choice="/tmp/main_choice"
76
[186]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
[162]87}
88
[174]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
[186]95echo -e "$cluster_status_echo_1" >>$clusterStatus
[174]96echo -e "------------------------------------------------------------------------" >>$clusterStatus
[186]97echo -e "\n$cluster_status_echo_2"
[174]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
[175]109    [ -z "$Task_Data" ] || Task_Data_status="running"
[174]110    host_name=$(cat $NutchEZ_HOME/system/nutch_nodes | grep $ip | awk '{print $2}')
[186]111    echo -e "$ip  $host_name \t\t $ip_status \t $Task_Data_status" >>$clusterStatus
[174]112#    printf '%16s\t %11s\t %10s\t %25s\n' "$ip $host_name $ip_status $Task_Data_status" >>$clusterStatus
113done
114
[186]115read -p "$cluster_status_read_1"
116$DIALOG --clear --backtitle "$dia_back" \
117     --title "$dia_cluster_status_title_1" --textbox $clusterStatus 20 90
[174]118}
119
[183]120
[186]121## [Server Setup (namenode & jobtracker)] ##
[183]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                                                                                               
[186]128    $DIALOG --clear --backtitle "$dia_back" --title "$dia_server_title_1" \
129    --msgbox "$dia_server_msg_1" 7 50
[183]130elif [ -z "$pid_name" -a -n "$pid_job" ]; then
[186]131    $DIALOG --clear --backtitle "$dia_back" --title "$dia_server_title_1" \
132    --msgbox "$dia_server_msg_2" 7 50
[183]133elif [ -n "$pid_name" -a -z "$pid_job" ]; then
[186]134    $DIALOG --clear --backtitle "$dia_back" --title "$dia_server_title_1" \
135    --msgbox "$dia_server_msg_3" 7 50
[183]136else
[186]137    $DIALOG --clear --backtitle "$dia_back" --title "$dia_server_title_1" \
138    --msgbox "$dia_server_msg_4" 7 50
[183]139fi
140
[186]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
[183]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
[172]165## [Cluster Setup (datanode & tasktracker)] ##
[162]166function cluster_setup(){
[172]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"
[162]174
[186]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
[162]181
[172]182# 判斷是否選 all 和 part,若是 all 直接到服務選單,若是 part 則先到機器選單
[177]183if [ "$(cat $allpart)" == "All" ]; then
[172]184     cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#'  | awk '{print $1}' >$runNodes
[177]185elif [ "$(cat $allpart)" == "Part" ]; then
[186]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 | \
[172]188    grep -v '^$' | grep -v '#'  | awk '{print $1 "\t" $2 "\t" "$LIST_status" }') 2>$runNodes
[177]189else
190    exit
[172]191fi
192
[177]193if [ -z "$(cat $runNodes)" ]; then
[180]194    exit
195else
196    $(cat $runNodes) | sed -i 's/"//g' $runNodes 
[177]197fi
198
[186]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
[162]205
206if [ "$(cat $clusterSetup)" == "start" ]; then
[186]207    echo -e "\n$cluster_setup_echo_1"
208    for node in $(cat $runNodes) 
[162]209    do
[176]210        ssh -o StrictHostKeyChecking=no $node "$NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start datanode \
[186]211        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start tasktracker" 2>/dev/null
212        [ $? == "0" ] || echo "$cluster_setup_echo_error $node !!!"
[162]213    done
214elif [ "$(cat $clusterSetup)" == "stop" ]; then
[186]215    echo -e "\n$cluster_setup_echo_3"
[172]216    for node in $(cat $runNodes)
[162]217    do
[176]218        ssh -o StrictHostKeyChecking=no $node "$NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop datanode \
[186]219        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop tasktracker" 2>/dev/null
220        [ $? == "0" ] || echo "$cluster_setup_echo_error $node !!!"
[162]221    done
[177]222elif [ "$(cat $clusterSetup)" == "restart"  ]; then
[186]223    echo -e "\n$cluster_setup_echo_5"
[172]224    for node in $(cat $runNodes)
225    do 
[176]226        ssh -o StrictHostKeyChecking=no $node "$NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop datanode \
[177]227        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop tasktracker \
228        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start datanode \
[186]229        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start tasktracker" 2>/dev/null
230        [ $? == "0" ] || echo "$cluster_setup_echo_error $node !!!"
[172]231    done
[177]232else
233    exit
[162]234fi
235}
236
[172]237## [Tomcat Severice start/stop/restart] ##
[162]238function tomcat_switch(){
[186]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
[162]243else
[186]244    $DIALOG --clear --backtitle "$dia_back" --title "$dia_tomcat_switch_title_1" \
245        --msgbox "$dia_tomcat_switch_msg_2" 7 50
[162]246fi
247tomcatSwitch="/tmp/tomcatSwitch"
[186]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
[162]254
[177]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
[186]261    echo -e "\n$tomcat_switch_echo_1 $Tomcat_HOME/bin/startup.sh"
[177]262    exit
263fi
264
[184]265if [ ! -e $Tomcat_HOME/bin/shutdown.sh ]; then
[186]266    echo -e "\n$tomcat_switch_echo_1 $Tomcat_HOME/bin/shutdown.sh"
[177]267    exit
268fi
269
[162]270if [ "$(cat $tomcatSwitch)" == "start" ]; then
[186]271    echo "$tomcat_switch_echo_3"
[176]272    $Tomcat_HOME/bin/startup.sh
[162]273elif [ "$(cat $tomcatSwitch)" == "stop" ]; then
[186]274    echo "$tomcat_switch_echo_4"
[176]275    $Tomcat_HOME/bin/shutdown.sh
[186]276    for tom_pid in $tom_pids
277    do
278        kill -9 $tom_pid 2>/dev/null
279    done
[162]280elif [ "$(cat $tomcatSwitch)" == "restart" ]; then
[186]281    echo "$tomcat_switch_echo_5"
[176]282    $Tomcat_HOME/bin/shutdown.sh
[186]283    for tom_pid in $tom_pids
284    do
285        kill -9 $tom_pid 2>/dev/null
286    done
[176]287    $Tomcat_HOME/bin/startup.sh
[162]288else
289    exit 0
290fi
291}
292
[172]293## [Tomcat Port Change] ##
[162]294function tomcat_port(){
295
[176]296if [ ! -e $Tomcat_HOME/conf/server.xml ]; then
[186]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
[162]299    exit
300fi
301tomcatPort="/tmp/tomcatPort"
[186]302$DIALOG --clear --backtitle "$dia_back" --title "$dia_tomcat_port_title_2" \
303    --inputbox "$dia_tomcat_port_input_1" 10 55 2>$tomcatPort
[162]304
[177]305if [ -z "$(cat $tomcatPort)" ]; then
[162]306    exit
307else
[176]308    modify_line_nu=$(cat -n $Tomcat_HOME/conf/server.xml | grep -v SSL | grep 'HTTP/1.1' | grep '<Connector' | awk '{print $1}')
[182]309    sed -i "${modify_line_nu}c <Connector port=\"$(cat $tomcatPort)\" protocol=\"HTTP/1.1\"" $Tomcat_HOME/conf/server.xml
[186]310
311    tom_pids=$(ps x | grep -v 'grep' | grep "tomcat" | awk '{print $1}')
[176]312    $Tomcat_HOME/bin/shutdown.sh
[186]313    for tom_pid in $tom_pids
314       do
315           kill -9 $tom_pid 2>/dev/null
316       done
[176]317    $Tomcat_HOME/bin/startup.sh
[162]318fi
319}
320
321
[172]322## [Language Change] ##
[162]323function lang_switch(){
324langSwitch="/tmp/langSwitch"
[186]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
[162]330#
[177]331if [ "$(cat $langSwitch)" == "Exit" ]; then
332    exit
[186]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
[177]341fi
[162]342}
343
[186]344## [Menu_choose] ##
345function menu_choose(){
[162]346case $(cat $main_choice) in
[174]347    "cluster_status")
348        cluster_status;;
[186]349    "cluster_setup")
350        cluster_setup;;
[183]351    "server_setup")   
352        server_setup;;
[162]353    "tomcat_switch")
354        tomcat_switch;;
355    "tomcat_port")
356        tomcat_port;;
357    "lang_switch")
358        lang_switch;;
359esac
[186]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.