source: nutchez-0.2/src/shell/nutchez @ 230

Last change on this file since 230 was 228, checked in by shunfa, 14 years ago

變更檔案屬性

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