source: nutchez-0.2/src/shell/nutchez

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

Add: shell/rm_DB.sh

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