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

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

修改tomcat關閉時輸入筆誤

File size: 10.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 6 \
67        "cluster_status" "Check cluster status" \
68        "cluster_setup" "Setup your datanode & tasktracker" \
69        "tomcat_switch" "Start/Stop/Restart Tomcat" \
70        "tomcat_port" "Change Tomcat port" \
71        "lang_switch" "Change language" \
72        "exit" "Exit" 2>$main_choice
73}
74
75## [Cluster Status (datanode & tasktracker)] ##
76function cluster_status(){
77IP_list=$(cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#'  | awk '{print $1}')
78clusterStatus="/tmp/clusterStatus"
79rm $clusterStatus 2> /dev/null
80#printf '%16s\t %11s\t %10s\t %25s\n' "[IP] [Hostanme] [Network] [Dtatnode & Tasktracker]" >>$clusterStatus
81echo -e "[IP] \t\t [Hostname] \t [Network] \t [Dtatnode & Tasktracker]" >>$clusterStatus
82echo -e "------------------------------------------------------------------------" >>$clusterStatus
83echo -e "\nStart to check cluster..."
84for ip in $IP_list
85do
86    # Check Network status
87    ip_status="online"
88    ping -c1 -w1 $ip 2>&1 > /dev/null || ip_status="offline"
89    # Check Hadoop/Nutch service through ssh
90    Task_Data_status="stop"
91    if [ $ip_status == "online" ]; then
92    Task_Data=$(ssh -o StrictHostKeyChecking=no $ip "jps |grep TaskTracker ; jps | grep  DataNode")
93    fi
94
95#   if [ $? == "255" ]; then
96#   echo -e "$ip Network is down"
97#   elif [ $? == "1" ]; then
98#   echo -e "Can't ssh to $ip"
99#   fi
100
101    [ -z "$Task_Data" ] || Task_Data_status="running"
102    host_name=$(cat $NutchEZ_HOME/system/nutch_nodes | grep $ip | awk '{print $2}')
103    echo  -e "$ip  $host_name \t\t $ip_status \t $Task_Data_status" >>$clusterStatus
104#    printf '%16s\t %11s\t %10s\t %25s\n' "$ip $host_name $ip_status $Task_Data_status" >>$clusterStatus
105done
106
107read -p "Plz input anykey to continue..."
108$DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" \
109     --title "[Cluster Status]" --textbox $clusterStatus 20 90
110}
111
112## [Cluster Setup (datanode & tasktracker)] ##
113function cluster_setup(){
114# 從 nutch_nodes 讀出 ip 和 hostname
115#IP_list=$(cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#' | awk '{print $1}')
116#HOST_list=$(cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#' | awk '{print $2}')
117LIST_status="off"
118allpart="/tmp/allpart"
119runNodes="/tmp/runNodes"
120clusterSetup="/tmp/clusterSetup"
121
122$DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" \
123    --title "[Management Options]" \
124    --menu "Plz choose: " 15 55 3 \
125    "All" "All nodes" \
126    "Part" "Choose part nodess" \
127    "Exit" "Exit" 2>$allpart
128
129# 判斷是否選 all 和 part,若是 all 直接到服務選單,若是 part 則先到機器選單
130if [ "$(cat $allpart)" == "All" ]; then
131     cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#'  | awk '{print $1}' >$runNodes
132elif [ "$(cat $allpart)" == "Part" ]; then
133    $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC  = " --title "[Datanode & Tasktracker Nodes Choose]" \
134    --checklist "datanode & tasktracker choose: " 15 55 7 $(cat $NutchEZ_HOME/system/nutch_nodes | \
135    grep -v '^$' | grep -v '#'  | awk '{print $1 "\t" $2 "\t" "$LIST_status" }') 2>$runNodes
136else
137    exit
138fi
139
140if [ -z "$(cat $runNodes)" ]; then
141exit
142fi
143
144$DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Datanode & Tasktracker Setup]" \
145    --menu "Plz choose: " 15 65 4 \
146    "start" "Start datanodes & tasktrackers" \
147    "stop" "Stop datanodes & tasktrackers" \
148    "restart" "restart datanodes & tasktrackers" \
149    "exit" "Exit" 2>$clusterSetup
150
151if [ "$(cat $clusterSetup)" == "start" ]; then
152    echo -e "\n[Start datanode & tasktracker]"
153    for node in $(cat $runNodes)
154    do
155        echo -e "start $node datanode & tasktracker !"
156        ssh -o StrictHostKeyChecking=no $node "$NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start datanode \
157        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start tasktracker"
158        [ $? == "0" ] || echo "Can't ssh to $node !!!"
159    done
160elif [ "$(cat $clusterSetup)" == "stop" ]; then
161    echo -e "\n[Stop datanode & tasktracker]"
162    for node in $(cat $runNodes)
163    do
164        echo -e "stop $node datanode & tasktracker !"
165        ssh -o StrictHostKeyChecking=no $node "$NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop datanode \
166        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop tasktracker"
167        [ $? == "0" ] || echo "Can't ssh to $node !!!"
168    done
169elif [ "$(cat $clusterSetup)" == "restart"  ]; then
170    echo -e "\n[Restart datanode & tasktracker]"
171    for node in $(cat $runNodes)
172    do 
173        echo -e "restart $node datanode & tasktracker !"
174        ssh -o StrictHostKeyChecking=no $node "$NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop datanode \
175        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop tasktracker \
176        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start datanode \
177        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start tasktracker"
178        [ $? == "0" ] || echo "Can't ssh to $node !!!"
179    done
180else
181    exit
182fi
183}
184
185## [Tomcat Severice start/stop/restart] ##
186function tomcat_switch(){
187pid_tc=$(ps axw -eo pid,command | grep "catalina" | grep "java" | grep "start" | awk '{print $1}')
188#echo $pid_tc
189#read
190if [ -n "$pid_tc" ]; then
191    $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Status]" \
192        --msgbox "Tomcat is runing in this system !!!" 7 50
193else
194    $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Status]" \
195        --msgbox "Tomcat is not running in this system !!!" 7 50
196fi
197tomcatSwitch="/tmp/tomcatSwitch"
198$DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Service Options]" \
199    --menu "Plz choose: " 15 55 4 \
200        "start" "Start tomcat" \
201        "stop" "Stop tomcat" \
202        "restart" "Restart tomcat" \
203        "exit" "Exit" 2>$tomcatSwitch
204
205if [ "$(cat $tomcatSwitch)" == "exit" ]; then
206    exit 0
207fi
208
209# jude $Tomcat_HOME/bin/startup.sh
210if [ ! -e $Tomcat_HOME/bin/startup.sh ]; then
211    echo -e "\nDon't fine $Tomcat_HOME/bin/startup.sh"
212    exit
213fi
214
215if [ ! -e $Tomcat_HOME/bin/shudown.sh ]; then
216    echo -e "\nDon't fine $Tomcat_HOME/bin/shutdown.sh"
217    exit
218fi
219
220if [ "$(cat $tomcatSwitch)" == "start" ]; then
221    echo "$Tomcat_HOME/bin/startup.sh"
222    $Tomcat_HOME/bin/startup.sh
223elif [ "$(cat $tomcatSwitch)" == "stop" ]; then
224    echo "$Tomcat_HOME/bin/shutdown.sh"
225    $Tomcat_HOME/bin/shutdown.sh
226elif [ "$(cat $tomcatSwitch)" == "restart" ]; then
227    echo "$Tomcat_HOME/bin/restart.sh"
228    $Tomcat_HOME/bin/shutdown.sh
229    $Tomcat_HOME/bin/startup.sh
230else
231    exit 0
232fi
233}
234
235## [Tomcat Port Change] ##
236function tomcat_port(){
237
238if [ ! -e $Tomcat_HOME/conf/server.xml ]; then
239    $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Servel.xml]" \
240        --msgbox "Fial: Don't find $Tomcat_HOME/conf/server.xml !!!" 10 50
241    exit
242fi
243tomcatPort="/tmp/tomcatPort"
244$DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC  = " --title "[Tomcat Port Change]" \
245    --inputbox "Plz input port number for tomcat (default is 8080)" 10 55 2>$tomcatPort
246
247if [ -z "$(cat $tomcatPort)" ]; then
248    exit
249else
250    modify_line_nu=$(cat -n $Tomcat_HOME/conf/server.xml | grep -v SSL | grep 'HTTP/1.1' | grep '<Connector' | awk '{print $1}')
251    sed -i "${modify_line_nu}c <Connector port="$tomcatPort" protocol="HTTP/1.1"" $Tomcat_HOME/conf/server.xml
252    $Tomcat_HOME/bin/shutdown.sh
253    $Tomcat_HOME/bin/startup.sh
254fi
255
256
257}
258
259
260## [Language Change] ##
261function lang_switch(){
262echo -e "lang_switch"
263langSwitch="/tmp/langSwitch"
264$DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC  =" --title "[Language Swaitch]" \
265    --menu "Plz choose: " 15 55 3 \
266    "English" "English" \
267    "Chinese" "Chinese" \
268    "Exit" "Exit" 2>$langSwitch
269#
270if [ "$(cat $langSwitch)" == "Exit" ]; then
271    exit
272else
273    echo "source $(cat $langSwitch)"
274fi
275}
276
277
278# [Main Code] #
279if [ $USER != "nutchuser" ]; then
280    echo -e "\nYour identify is not nutchuser, plz change to \"nutchuser\" !!!"
281    exit
282fi
283
284prepare_check
285main_menu
286case $(cat $main_choice) in
287    "cluster_status")
288        cluster_status;;
289    "cluster_setup")
290        cluster_setup;;
291    "tomcat_switch")
292        tomcat_switch;;
293    "tomcat_port")
294        tomcat_port;;
295    "lang_switch")
296        lang_switch;;
297esac
Note: See TracBrowser for help on using the repository browser.