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

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

新增 Work_Path

File size: 12.4 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"
20Work_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] ##
27function prepare_lang(){
28lang=$(locale | grep 'LANG=' | cut -d "=" -f2)
29
30# if lang=zh then source lang_zh_TW_nutchez
31echo $lang | grep 'zh' && source $Work_Path/lang_zh_TW_nutchez
32}
33
34
35## [Pre-Check for nutch_nodes file] ##
36function prepare_check(){ 
37# 若無 nutchez_nodes 則跳出
38if [ ! -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
42fi
43
44# 判斷 nutch_nodes 和 nutch_nodes.back
45# 第一次執行則要求 user 更新 /etc/hosts
46if [ ! -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
59elif [ ${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
71fi
72}
73
74## [Main Menu] ##
75function main_menu(){
76main_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)] ##
91function cluster_status(){
92IP_list=$(cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#'  | awk '{print $1}')
93clusterStatus="/tmp/clusterStatus"
94rm $clusterStatus 2> /dev/null
95#printf '%16s\t %11s\t %10s\t %25s\n' "[IP] [Hostanme] [Network] [Dtatnode & Tasktracker]" >>$clusterStatus
96echo -e "$cluster_status_echo_1" >>$clusterStatus
97echo -e "------------------------------------------------------------------------" >>$clusterStatus
98echo -e "\n$cluster_status_echo_2"
99for ip in $IP_list
100do
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
114done
115
116read -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)] ##
123function server_setup(){
124serverSetup=/tmp/serverSetup
125pid_name=$(jps | grep NameNode)
126pid_job=$(jps | grep JobTracker)
127
128if [ -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
131elif [ -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
134elif [ -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
137else
138    $DIALOG --clear --backtitle "$dia_back" --title "$dia_server_title_1" \
139    --msgbox "$dia_server_msg_4" 7 50
140fi
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
149if [ "$(cat $serverSetup)" == "start" ]; then
150    $NutchEZ_Install_PATH/nutch/bin/start-dfs.sh
151    $NutchEZ_Install_PATH/nutch/bin/start-mapred.sh
152elif [ "$(cat $serverSetup)" == "stop" ]; then
153    $NutchEZ_Install_PATH/nutch/bin/stop-dfs.sh
154    $NutchEZ_Install_PATH/nutch/bin/stop-mapred.sh
155elif [ "$(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
160else
161    exit
162fi
163}
164
165
166## [Cluster Setup (datanode & tasktracker)] ##
167function 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}')
171LIST_status="off"
172allpart="/tmp/allpart"
173runNodes="/tmp/runNodes"
174clusterSetup="/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 則先到機器選單
184if [ "$(cat $allpart)" == "All" ]; then
185     cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#'  | awk '{print $1}' >$runNodes
186elif [ "$(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
190else
191    exit
192fi
193
194if [ -z "$(cat $runNodes)" ]; then
195    exit
196else
197    $(cat $runNodes) | sed -i 's/"//g' $runNodes 
198fi
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
207if [ "$(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
215elif [ "$(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
223elif [ "$(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
233else
234    exit
235fi
236}
237
238## [Tomcat Severice start/stop/restart] ##
239function tomcat_switch(){
240tom_pids=$(ps x | grep -v 'grep' | grep "tomcat" | awk '{print $1}')
241if [ -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
244else
245    $DIALOG --clear --backtitle "$dia_back" --title "$dia_tomcat_switch_title_1" \
246        --msgbox "$dia_tomcat_switch_msg_2" 7 50
247fi
248tomcatSwitch="/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
256if [ "$(cat $tomcatSwitch)" == "exit" ]; then
257    exit 0
258fi
259
260# jude $Tomcat_HOME/bin/startup.sh
261if [ ! -e $Tomcat_HOME/bin/startup.sh ]; then
262    echo -e "\n$tomcat_switch_echo_1 $Tomcat_HOME/bin/startup.sh"
263    exit
264fi
265
266if [ ! -e $Tomcat_HOME/bin/shutdown.sh ]; then
267    echo -e "\n$tomcat_switch_echo_1 $Tomcat_HOME/bin/shutdown.sh"
268    exit
269fi
270
271if [ "$(cat $tomcatSwitch)" == "start" ]; then
272    echo "$tomcat_switch_echo_3"
273    $Tomcat_HOME/bin/startup.sh
274elif [ "$(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
281elif [ "$(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
289else
290    exit 0
291fi
292}
293
294## [Tomcat Port Change] ##
295function tomcat_port(){
296
297if [ ! -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
301fi
302tomcatPort="/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
306if [ -z "$(cat $tomcatPort)" ]; then
307    exit
308else
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
319fi
320}
321
322
323## [Language Change] ##
324function lang_switch(){
325langSwitch="/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#
332if [ "$(cat $langSwitch)" == "Exit" ]; then
333    exit
334elif [ "$(cat $langSwitch)" == "en_US" ]; then
335    source $Work_Path/lang_en_US_nutchez
336    main_menu
337    menu_choose
338elif [ "$(cat $langSwitch)" == "zh_TW" ]; then
339    source $Work_Path/lang_zh_TW_nutchez
340    main_menu
341    menu_choose
342fi
343}
344
345## [Menu_choose] ##
346function menu_choose(){
347case $(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;;
360esac
361}
362
363# [Main Code] #
364if [ $USER != "nutchuser" ]; then
365    echo -e "\n$user_error"
366    exit
367fi
368
369prepare_lang
370prepare_check
371main_menu
372menu_choose
Note: See TracBrowser for help on using the repository browser.