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] # |
---|
12 | DIALOG=dialog |
---|
13 | main_choice= |
---|
14 | |
---|
15 | # [PATH Declaration] # |
---|
16 | NutchEZ_Install_PATH=/opt/nutchez |
---|
17 | Tomcat_HOME=/opt/nutchez/tomcat |
---|
18 | NutchEZ_HOME=/home/nutchuser/nutchez |
---|
19 | |
---|
20 | # [Functions Declaration] # |
---|
21 | ## [Prepare Check] ## |
---|
22 | function prepare_check(){ |
---|
23 | # 若無 nutchez_nodes 則跳出 |
---|
24 | if [ ! -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 |
---|
28 | fi |
---|
29 | |
---|
30 | # 判斷 nutch_nodes 和 nutch_nodes.back |
---|
31 | # 第一次執行則要求 user 更新 /etc/hosts |
---|
32 | if [ ! -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 |
---|
45 | elif [ ${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 |
---|
57 | fi |
---|
58 | } |
---|
59 | |
---|
60 | ## [Main Menu] ## |
---|
61 | function main_menu(){ |
---|
62 | main_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)] ## |
---|
76 | function cluster_status(){ |
---|
77 | IP_list=$(cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#' | awk '{print $1}') |
---|
78 | clusterStatus="/tmp/clusterStatus" |
---|
79 | rm $clusterStatus 2> /dev/null |
---|
80 | #printf '%16s\t %11s\t %10s\t %25s\n' "[IP] [Hostanme] [Network] [Dtatnode & Tasktracker]" >>$clusterStatus |
---|
81 | echo -e "[IP] \t\t [Hostname] \t [Network] \t [Dtatnode & Tasktracker]" >>$clusterStatus |
---|
82 | echo -e "------------------------------------------------------------------------" >>$clusterStatus |
---|
83 | echo -e "\nStart to check cluster..." |
---|
84 | for ip in $IP_list |
---|
85 | do |
---|
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 |
---|
105 | done |
---|
106 | |
---|
107 | read -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)] ## |
---|
113 | function 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}') |
---|
117 | LIST_status="off" |
---|
118 | allpart="/tmp/allpart" |
---|
119 | runNodes="/tmp/runNodes" |
---|
120 | clusterSetup="/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 則先到機器選單 |
---|
130 | if [ "$(cat $allpart)" == "All" ]; then |
---|
131 | cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#' | awk '{print $1}' >$runNodes |
---|
132 | elif [ "$(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 |
---|
136 | else |
---|
137 | exit |
---|
138 | fi |
---|
139 | |
---|
140 | if [ -z "$(cat $runNodes)" ]; then |
---|
141 | exit |
---|
142 | else |
---|
143 | $(cat $runNodes) | sed -i 's/"//g' $runNodes |
---|
144 | fi |
---|
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 | |
---|
153 | if [ "$(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 |
---|
162 | elif [ "$(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 |
---|
171 | elif [ "$(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 |
---|
182 | else |
---|
183 | exit |
---|
184 | fi |
---|
185 | } |
---|
186 | |
---|
187 | ## [Tomcat Severice start/stop/restart] ## |
---|
188 | function tomcat_switch(){ |
---|
189 | pid_tc=$(ps axw -eo pid,command | grep "catalina" | grep "java" | grep "start" | awk '{print $1}') |
---|
190 | #echo $pid_tc |
---|
191 | #read |
---|
192 | if [ -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 |
---|
195 | else |
---|
196 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Status]" \ |
---|
197 | --msgbox "Tomcat is not running in this system !!!" 7 50 |
---|
198 | fi |
---|
199 | tomcatSwitch="/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 | |
---|
207 | if [ "$(cat $tomcatSwitch)" == "exit" ]; then |
---|
208 | exit 0 |
---|
209 | fi |
---|
210 | |
---|
211 | # jude $Tomcat_HOME/bin/startup.sh |
---|
212 | if [ ! -e $Tomcat_HOME/bin/startup.sh ]; then |
---|
213 | echo -e "\nDon't fine $Tomcat_HOME/bin/startup.sh" |
---|
214 | exit |
---|
215 | fi |
---|
216 | |
---|
217 | if [ ! -e $Tomcat_HOME/bin/shudown.sh ]; then |
---|
218 | echo -e "\nDon't fine $Tomcat_HOME/bin/shutdown.sh" |
---|
219 | exit |
---|
220 | fi |
---|
221 | |
---|
222 | if [ "$(cat $tomcatSwitch)" == "start" ]; then |
---|
223 | echo "$Tomcat_HOME/bin/startup.sh" |
---|
224 | $Tomcat_HOME/bin/startup.sh |
---|
225 | elif [ "$(cat $tomcatSwitch)" == "stop" ]; then |
---|
226 | echo "$Tomcat_HOME/bin/shutdown.sh" |
---|
227 | $Tomcat_HOME/bin/shutdown.sh |
---|
228 | elif [ "$(cat $tomcatSwitch)" == "restart" ]; then |
---|
229 | echo "$Tomcat_HOME/bin/restart.sh" |
---|
230 | $Tomcat_HOME/bin/shutdown.sh |
---|
231 | $Tomcat_HOME/bin/startup.sh |
---|
232 | else |
---|
233 | exit 0 |
---|
234 | fi |
---|
235 | } |
---|
236 | |
---|
237 | ## [Tomcat Port Change] ## |
---|
238 | function tomcat_port(){ |
---|
239 | |
---|
240 | if [ ! -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 |
---|
244 | fi |
---|
245 | tomcatPort="/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 | |
---|
249 | if [ -z "$(cat $tomcatPort)" ]; then |
---|
250 | exit |
---|
251 | else |
---|
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 |
---|
256 | fi |
---|
257 | |
---|
258 | |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | ## [Language Change] ## |
---|
263 | function lang_switch(){ |
---|
264 | echo -e "lang_switch" |
---|
265 | langSwitch="/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 | # |
---|
272 | if [ "$(cat $langSwitch)" == "Exit" ]; then |
---|
273 | exit |
---|
274 | else |
---|
275 | echo "source $(cat $langSwitch)" |
---|
276 | fi |
---|
277 | } |
---|
278 | |
---|
279 | |
---|
280 | # [Main Code] # |
---|
281 | if [ $USER != "nutchuser" ]; then |
---|
282 | echo -e "\nYour identify is not nutchuser, plz change to \"nutchuser\" !!!" |
---|
283 | exit |
---|
284 | fi |
---|
285 | |
---|
286 | prepare_check |
---|
287 | main_menu |
---|
288 | case $(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;; |
---|
299 | esac |
---|