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

Last change on this file since 182 was 182, checked in by rock, 14 years ago
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
141    exit
142else
143    $(cat $runNodes) | sed -i 's/"//g' $runNodes 
144fi
145
146$DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Datanode & Tasktracker Setup]" \
147    --menu "Plz choose: " 15 65 4 \
148    "start" "Start datanodes & tasktrackers" \
149    "stop" "Stop datanodes & tasktrackers" \
150    "restart" "restart datanodes & tasktrackers" \
151    "exit" "Exit" 2>$clusterSetup
152
153if [ "$(cat $clusterSetup)" == "start" ]; then
154    echo -e "\n[Start datanode & tasktracker]"
155    for node in $(cat $runNodes)
156    do
157        echo -e "start $node datanode & tasktracker !"
158        ssh -o StrictHostKeyChecking=no $node "$NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start datanode \
159        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start tasktracker"
160        [ $? == "0" ] || echo "Can't ssh to $node !!!"
161    done
162elif [ "$(cat $clusterSetup)" == "stop" ]; then
163    echo -e "\n[Stop datanode & tasktracker]"
164    for node in $(cat $runNodes)
165    do
166        echo -e "stop $node datanode & tasktracker !"
167        ssh -o StrictHostKeyChecking=no $node "$NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop datanode \
168        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop tasktracker"
169        [ $? == "0" ] || echo "Can't ssh to $node !!!"
170    done
171elif [ "$(cat $clusterSetup)" == "restart"  ]; then
172    echo -e "\n[Restart datanode & tasktracker]"
173    for node in $(cat $runNodes)
174    do 
175        echo -e "restart $node datanode & tasktracker !"
176        ssh -o StrictHostKeyChecking=no $node "$NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop datanode \
177        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop tasktracker \
178        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start datanode \
179        ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start tasktracker"
180        [ $? == "0" ] || echo "Can't ssh to $node !!!"
181    done
182else
183    exit
184fi
185}
186
187## [Tomcat Severice start/stop/restart] ##
188function tomcat_switch(){
189pid_tc=$(ps axw -eo pid,command | grep "catalina" | grep "java" | grep "start" | awk '{print $1}')
190#echo $pid_tc
191#read
192if [ -n "$pid_tc" ]; then
193    $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Status]" \
194        --msgbox "Tomcat is runing in this system !!!" 7 50
195else
196    $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Status]" \
197        --msgbox "Tomcat is not running in this system !!!" 7 50
198fi
199tomcatSwitch="/tmp/tomcatSwitch"
200$DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Service Options]" \
201    --menu "Plz choose: " 15 55 4 \
202        "start" "Start tomcat" \
203        "stop" "Stop tomcat" \
204        "restart" "Restart tomcat" \
205        "exit" "Exit" 2>$tomcatSwitch
206
207if [ "$(cat $tomcatSwitch)" == "exit" ]; then
208    exit 0
209fi
210
211# jude $Tomcat_HOME/bin/startup.sh
212if [ ! -e $Tomcat_HOME/bin/startup.sh ]; then
213    echo -e "\nDon't fine $Tomcat_HOME/bin/startup.sh"
214    exit
215fi
216
217if [ ! -e $Tomcat_HOME/bin/shudown.sh ]; then
218    echo -e "\nDon't fine $Tomcat_HOME/bin/shutdown.sh"
219    exit
220fi
221
222if [ "$(cat $tomcatSwitch)" == "start" ]; then
223    echo "$Tomcat_HOME/bin/startup.sh"
224    $Tomcat_HOME/bin/startup.sh
225elif [ "$(cat $tomcatSwitch)" == "stop" ]; then
226    echo "$Tomcat_HOME/bin/shutdown.sh"
227    $Tomcat_HOME/bin/shutdown.sh
228elif [ "$(cat $tomcatSwitch)" == "restart" ]; then
229    echo "$Tomcat_HOME/bin/restart.sh"
230    $Tomcat_HOME/bin/shutdown.sh
231    $Tomcat_HOME/bin/startup.sh
232else
233    exit 0
234fi
235}
236
237## [Tomcat Port Change] ##
238function tomcat_port(){
239
240if [ ! -e $Tomcat_HOME/conf/server.xml ]; then
241    $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Servel.xml]" \
242        --msgbox "Fial: Don't find $Tomcat_HOME/conf/server.xml !!!" 10 50
243    exit
244fi
245tomcatPort="/tmp/tomcatPort"
246$DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC  = " --title "[Tomcat Port Change]" \
247    --inputbox "Plz input port number for tomcat (default is 8080)" 10 55 2>$tomcatPort
248
249if [ -z "$(cat $tomcatPort)" ]; then
250    exit
251else
252    modify_line_nu=$(cat -n $Tomcat_HOME/conf/server.xml | grep -v SSL | grep 'HTTP/1.1' | grep '<Connector' | awk '{print $1}')
253    sed -i "${modify_line_nu}c <Connector port=\"$(cat $tomcatPort)\" protocol=\"HTTP/1.1\"" $Tomcat_HOME/conf/server.xml
254    $Tomcat_HOME/bin/shutdown.sh
255    $Tomcat_HOME/bin/startup.sh
256fi
257
258
259}
260
261
262## [Language Change] ##
263function lang_switch(){
264echo -e "lang_switch"
265langSwitch="/tmp/langSwitch"
266$DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC  =" --title "[Language Swaitch]" \
267    --menu "Plz choose: " 15 55 3 \
268    "English" "English" \
269    "Chinese" "Chinese" \
270    "Exit" "Exit" 2>$langSwitch
271#
272if [ "$(cat $langSwitch)" == "Exit" ]; then
273    exit
274else
275    echo "source $(cat $langSwitch)"
276fi
277}
278
279
280# [Main Code] #
281if [ $USER != "nutchuser" ]; then
282    echo -e "\nYour identify is not nutchuser, plz change to \"nutchuser\" !!!"
283    exit
284fi
285
286prepare_check
287main_menu
288case $(cat $main_choice) in
289    "cluster_status")
290        cluster_status;;
291    "cluster_setup")
292        cluster_setup;;
293    "tomcat_switch")
294        tomcat_switch;;
295    "tomcat_port")
296        tomcat_port;;
297    "lang_switch")
298        lang_switch;;
299esac
Note: See TracBrowser for help on using the repository browser.