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

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

新增 NameNodeJobTracker 功能

File size: 12.7 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=
14
15# [PATH Declaration] #
16NutchEZ_Install_PATH=/opt/nutchez
17Tomcat_HOME=/opt/nutchez/tomcat
18NutchEZ_HOME=/home/nutchuser/nutchez
19
20# [Functions Declaration] #
21## [Prepare Check]  ##
22function prepare_check(){ 
23# 若無 nutchez_nodes 則跳出
24if [ ! -e "${NutchEZ_HOME}/system/nutch_nodes" ]; then
25    $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[nutch_nodes Files]" \
26    --msgbox "Fail: Don't find $NutchEZ_HOME/system/nutch_nodes !!!" 10 40
27    exit
28fi
29
30# 判斷 nutch_nodes 和 nutch_nodes.back
31# 第一次執行則要求 user 更新 /etc/hosts
32if [ ! -e "${NutchEZ_HOME}/system/nutch_nodes.bak" ]; then
33    $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[/etc/host Update]" \
34    --yesno "First execut nutchez management interface.\nPlz update /etc/hosts !!!" 10 55
35
36    echo -e "Plz input root password to update /etc/hosts !!!"
37    ./duplicate_del "${NutchEZ_HOME}/system/nutch_nodes"
38    su root -c "./duplicate_del /etc/hosts && ./add_hosts ${NutchEZ_HOME}/system/nutch_nodes /etc/hosts"
39    # 若密碼輸入失敗則跳出
40    if [ $? == "1" ]; then
41        echo "Root password is error ! Plz check root password !!!"
42        exit
43    fi 
44# 若 nutch_nodes 被更新過,則要求更新 /etc/hosts
45elif [ ${NutchEZ_HOME}/system/nutch_nodes -nt ${NutchEZ_HOME}/system/nutch_nodes.bak ]; then
46    $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[/etc/host Update]" \
47    --yesno "nutch_nodes already be modified.\nPlz update /etc/hosts !!!" 10 55
48
49    echo -e "Plz input root password to update /etc/hosts !!!"
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 "Root password is error ! Plz check root password !!!"
55        exit                                 
56    fi
57fi
58}
59
60## [Main Menu] ##
61function main_menu(){
62main_choice="/tmp/main_choice"
63
64$DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" \
65    --title "[Management Options]" \
66        --menu "Plz choose: " 15 60 7 \
67        "cluster_status" "Check cluster status" \
68        "server_setup" "Setup namenode & jobtracker" \
69        "cluster_setup" "Setup datanode & tasktracker" \
70        "tomcat_switch" "Start/Stop/Restart Tomcat" \
71        "tomcat_port" "Change Tomcat port" \
72        "lang_switch" "Change language" \
73        "exit" "Exit" 2>$main_choice
74}
75
76## [Cluster Status (datanode & tasktracker)] ##
77function cluster_status(){
78IP_list=$(cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#'  | awk '{print $1}')
79clusterStatus="/tmp/clusterStatus"
80rm $clusterStatus 2> /dev/null
81#printf '%16s\t %11s\t %10s\t %25s\n' "[IP] [Hostanme] [Network] [Dtatnode & Tasktracker]" >>$clusterStatus
82echo -e "[IP] \t\t [Hostname] \t [Network] \t [Dtatnode & Tasktracker]" >>$clusterStatus
83echo -e "------------------------------------------------------------------------" >>$clusterStatus
84echo -e "\nStart to check cluster..."
85for ip in $IP_list
86do
87    # Check Network status
88    ip_status="online"
89    ping -c1 -w1 $ip 2>&1 > /dev/null || ip_status="offline"
90    # Check Hadoop/Nutch service through ssh
91    Task_Data_status="stop"
92    if [ $ip_status == "online" ]; then
93    Task_Data=$(ssh -o StrictHostKeyChecking=no $ip "jps |grep TaskTracker ; jps | grep  DataNode")
94    fi
95
96#   if [ $? == "255" ]; then
97#   echo -e "$ip Network is down"
98#   elif [ $? == "1" ]; then
99#   echo -e "Can't ssh to $ip"
100#   fi
101
102    [ -z "$Task_Data" ] || Task_Data_status="running"
103    host_name=$(cat $NutchEZ_HOME/system/nutch_nodes | grep $ip | awk '{print $2}')
104    echo  -e "$ip  $host_name \t\t $ip_status \t $Task_Data_status" >>$clusterStatus
105#    printf '%16s\t %11s\t %10s\t %25s\n' "$ip $host_name $ip_status $Task_Data_status" >>$clusterStatus
106done
107
108read -p "Plz input anykey to continue..."
109$DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" \
110     --title "[Cluster Status]" --textbox $clusterStatus 20 90
111}
112
113
114## [Cluster Setup (namenode & jobtracker)] ##
115function server_setup(){
116serverSetup=/tmp/serverSetup
117pid_name=$(jps | grep NameNode)
118pid_job=$(jps | grep JobTracker)
119
120if [ -z "$pid_name" -a -z "$pid_job" ]; then                                                                                               
121    $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Server Status]" \
122    --msgbox "Namenode & Jobtracker are not running" 7 50
123elif [ -z "$pid_name" -a -n "$pid_job" ]; then
124    $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Server Status]" \
125    --msgbox "Jobtracker is running, Namenode is not running" 7 50
126elif [ -n "$pid_name" -a -z "$pid_job" ]; then
127    $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Server Status]" \
128    --msgbox "Namenode is running, Jobtracker is not running" 7 50
129else
130    $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Server Status]" \
131    --msgbox "Namenode & Jobtracker are running" 7 50
132fi
133
134$DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Namenode & Jobtracker Setup]" \
135    --menu "Plz choose: " 15 65 4 \
136    "start" "Start namenode & jobtracker" \
137    "stop" "Stop namenode & jobtracker" \
138    "restart" "restart namenode & jobtracker" \
139    "exit" "Exit" 2>$serverSetup
140
141if [ "$(cat $serverSetup)" == "start" ]; then
142    $NutchEZ_Install_PATH/nutch/bin/start-dfs.sh
143    $NutchEZ_Install_PATH/nutch/bin/start-mapred.sh
144elif [ "$(cat $serverSetup)" == "stop" ]; then
145    $NutchEZ_Install_PATH/nutch/bin/stop-dfs.sh
146    $NutchEZ_Install_PATH/nutch/bin/stop-mapred.sh
147elif [ "$(cat $serverSetup)" == "restart" ]; then
148    $NutchEZ_Install_PATH/nutch/bin/stop-dfs.sh
149    $NutchEZ_Install_PATH/nutch/bin/stop-mapred.sh
150    $NutchEZ_Install_PATH/nutch/bin/start-dfs.sh
151    $NutchEZ_Install_PATH/nutch/bin/start-mapred.sh
152else
153    exit
154fi
155
156
157}
158
159
160## [Cluster Setup (datanode & tasktracker)] ##
161function cluster_setup(){
162# 從 nutch_nodes 讀出 ip 和 hostname
163#IP_list=$(cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#' | awk '{print $1}')
164#HOST_list=$(cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#' | awk '{print $2}')
165LIST_status="off"
166allpart="/tmp/allpart"
167runNodes="/tmp/runNodes"
168clusterSetup="/tmp/clusterSetup"
169
170$DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" \
171    --title "[Management Options]" \
172    --menu "Plz choose: " 15 55 3 \
173    "All" "All nodes" \
174    "Part" "Choose part nodess" \
175    "Exit" "Exit" 2>$allpart
176
177# 判斷是否選 all 和 part,若是 all 直接到服務選單,若是 part 則先到機器選單
178if [ "$(cat $allpart)" == "All" ]; then
179     cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#'  | awk '{print $1}' >$runNodes
180elif [ "$(cat $allpart)" == "Part" ]; then
181    $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC  = " --title "[Datanode & Tasktracker Nodes Choose]" \
182    --checklist "datanode & tasktracker choose: " 15 55 7 $(cat $NutchEZ_HOME/system/nutch_nodes | \
183    grep -v '^$' | grep -v '#'  | awk '{print $1 "\t" $2 "\t" "$LIST_status" }') 2>$runNodes
184else
185    exit
186fi
187
188if [ -z "$(cat $runNodes)" ]; then
189    exit
190else
191    $(cat $runNodes) | sed -i 's/"//g' $runNodes 
192fi
193
194$DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Datanode & Tasktracker Setup]" \
195    --menu "Plz choose: " 15 65 4 \
196    "start" "Start datanodes & tasktrackers" \
197    "stop" "Stop datanodes & tasktrackers" \
198    "restart" "restart datanodes & tasktrackers" \
199    "exit" "Exit" 2>$clusterSetup
200
201if [ "$(cat $clusterSetup)" == "start" ]; then
202    echo -e "\n[Start datanode & tasktracker]"
203    for node in $(cat $runNodes)
204    do
205        echo -e "start $node datanode & tasktracker !"
206        ssh -o StrictHostKeyChecking=no $node "$NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start datanode \
207        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start tasktracker"
208        [ $? == "0" ] || echo "Can't ssh to $node !!!"
209    done
210elif [ "$(cat $clusterSetup)" == "stop" ]; then
211    echo -e "\n[Stop datanode & tasktracker]"
212    for node in $(cat $runNodes)
213    do
214        echo -e "stop $node datanode & tasktracker !"
215        ssh -o StrictHostKeyChecking=no $node "$NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop datanode \
216        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop tasktracker"
217        [ $? == "0" ] || echo "Can't ssh to $node !!!"
218    done
219elif [ "$(cat $clusterSetup)" == "restart"  ]; then
220    echo -e "\n[Restart datanode & tasktracker]"
221    for node in $(cat $runNodes)
222    do 
223        echo -e "restart $node datanode & tasktracker !"
224        ssh -o StrictHostKeyChecking=no $node "$NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop datanode \
225        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop tasktracker \
226        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start datanode \
227        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start tasktracker"
228        [ $? == "0" ] || echo "Can't ssh to $node !!!"
229    done
230else
231    exit
232fi
233}
234
235## [Tomcat Severice start/stop/restart] ##
236function tomcat_switch(){
237pid_tc=$(ps axw -eo pid,command | grep "catalina" | grep "java" | grep "start" | awk '{print $1}')
238#echo $pid_tc
239#read
240if [ -n "$pid_tc" ]; then
241    $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Status]" \
242        --msgbox "Tomcat is runing in this system !!!" 7 50
243else
244    $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Status]" \
245        --msgbox "Tomcat is not running in this system !!!" 7 50
246fi
247tomcatSwitch="/tmp/tomcatSwitch"
248$DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Service Options]" \
249    --menu "Plz choose: " 15 55 4 \
250        "start" "Start tomcat" \
251        "stop" "Stop tomcat" \
252        "restart" "Restart tomcat" \
253        "exit" "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 "\nDon't fine $Tomcat_HOME/bin/startup.sh"
262    exit
263fi
264
265if [ ! -e $Tomcat_HOME/bin/shudown.sh ]; then
266    echo -e "\nDon't fine $Tomcat_HOME/bin/shutdown.sh"
267    exit
268fi
269
270if [ "$(cat $tomcatSwitch)" == "start" ]; then
271    echo "$Tomcat_HOME/bin/startup.sh"
272    $Tomcat_HOME/bin/startup.sh
273elif [ "$(cat $tomcatSwitch)" == "stop" ]; then
274    echo "$Tomcat_HOME/bin/shutdown.sh"
275    $Tomcat_HOME/bin/shutdown.sh
276elif [ "$(cat $tomcatSwitch)" == "restart" ]; then
277    echo "$Tomcat_HOME/bin/restart.sh"
278    $Tomcat_HOME/bin/shutdown.sh
279    $Tomcat_HOME/bin/startup.sh
280else
281    exit 0
282fi
283}
284
285## [Tomcat Port Change] ##
286function tomcat_port(){
287
288if [ ! -e $Tomcat_HOME/conf/server.xml ]; then
289    $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Servel.xml]" \
290        --msgbox "Fial: Don't find $Tomcat_HOME/conf/server.xml !!!" 10 50
291    exit
292fi
293tomcatPort="/tmp/tomcatPort"
294$DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC  = " --title "[Tomcat Port Change]" \
295    --inputbox "Plz input port number for tomcat (default is 8080)" 10 55 2>$tomcatPort
296
297if [ -z "$(cat $tomcatPort)" ]; then
298    exit
299else
300    modify_line_nu=$(cat -n $Tomcat_HOME/conf/server.xml | grep -v SSL | grep 'HTTP/1.1' | grep '<Connector' | awk '{print $1}')
301    sed -i "${modify_line_nu}c <Connector port=\"$(cat $tomcatPort)\" protocol=\"HTTP/1.1\"" $Tomcat_HOME/conf/server.xml
302    $Tomcat_HOME/bin/shutdown.sh
303    $Tomcat_HOME/bin/startup.sh
304fi
305
306
307}
308
309
310## [Language Change] ##
311function lang_switch(){
312echo -e "lang_switch"
313langSwitch="/tmp/langSwitch"
314$DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC  =" --title "[Language Swaitch]" \
315    --menu "Plz choose: " 15 55 3 \
316    "English" "English" \
317    "Chinese" "Chinese" \
318    "Exit" "Exit" 2>$langSwitch
319#
320if [ "$(cat $langSwitch)" == "Exit" ]; then
321    exit
322else
323    echo "source $(cat $langSwitch)"
324fi
325}
326
327
328# [Main Code] #
329if [ $USER != "nutchuser" ]; then
330    echo -e "\nYour identify is not nutchuser, plz change to \"nutchuser\" !!!"
331    exit
332fi
333
334prepare_check
335main_menu
336case $(cat $main_choice) in
337    "cluster_status")
338        cluster_status;;
339    "server_setup")   
340        server_setup;;
341    "cluster_setup")
342        cluster_setup;;
343    "tomcat_switch")
344        tomcat_switch;;
345    "tomcat_port")
346        tomcat_port;;
347    "lang_switch")
348        lang_switch;;
349esac
Note: See TracBrowser for help on using the repository browser.