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 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)] ## |
---|
77 | function cluster_status(){ |
---|
78 | IP_list=$(cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#' | awk '{print $1}') |
---|
79 | clusterStatus="/tmp/clusterStatus" |
---|
80 | rm $clusterStatus 2> /dev/null |
---|
81 | #printf '%16s\t %11s\t %10s\t %25s\n' "[IP] [Hostanme] [Network] [Dtatnode & Tasktracker]" >>$clusterStatus |
---|
82 | echo -e "[IP] \t\t [Hostname] \t [Network] \t [Dtatnode & Tasktracker]" >>$clusterStatus |
---|
83 | echo -e "------------------------------------------------------------------------" >>$clusterStatus |
---|
84 | echo -e "\nStart to check cluster..." |
---|
85 | for ip in $IP_list |
---|
86 | do |
---|
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 |
---|
106 | done |
---|
107 | |
---|
108 | read -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)] ## |
---|
115 | function server_setup(){ |
---|
116 | serverSetup=/tmp/serverSetup |
---|
117 | pid_name=$(jps | grep NameNode) |
---|
118 | pid_job=$(jps | grep JobTracker) |
---|
119 | |
---|
120 | if [ -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 |
---|
123 | elif [ -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 |
---|
126 | elif [ -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 |
---|
129 | else |
---|
130 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Server Status]" \ |
---|
131 | --msgbox "Namenode & Jobtracker are running" 7 50 |
---|
132 | fi |
---|
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 | |
---|
141 | if [ "$(cat $serverSetup)" == "start" ]; then |
---|
142 | $NutchEZ_Install_PATH/nutch/bin/start-dfs.sh |
---|
143 | $NutchEZ_Install_PATH/nutch/bin/start-mapred.sh |
---|
144 | elif [ "$(cat $serverSetup)" == "stop" ]; then |
---|
145 | $NutchEZ_Install_PATH/nutch/bin/stop-dfs.sh |
---|
146 | $NutchEZ_Install_PATH/nutch/bin/stop-mapred.sh |
---|
147 | elif [ "$(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 |
---|
152 | else |
---|
153 | exit |
---|
154 | fi |
---|
155 | |
---|
156 | |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | ## [Cluster Setup (datanode & tasktracker)] ## |
---|
161 | function 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}') |
---|
165 | LIST_status="off" |
---|
166 | allpart="/tmp/allpart" |
---|
167 | runNodes="/tmp/runNodes" |
---|
168 | clusterSetup="/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 則先到機器選單 |
---|
178 | if [ "$(cat $allpart)" == "All" ]; then |
---|
179 | cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#' | awk '{print $1}' >$runNodes |
---|
180 | elif [ "$(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 |
---|
184 | else |
---|
185 | exit |
---|
186 | fi |
---|
187 | |
---|
188 | if [ -z "$(cat $runNodes)" ]; then |
---|
189 | exit |
---|
190 | else |
---|
191 | $(cat $runNodes) | sed -i 's/"//g' $runNodes |
---|
192 | fi |
---|
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 | |
---|
201 | if [ "$(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 |
---|
210 | elif [ "$(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 |
---|
219 | elif [ "$(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 |
---|
230 | else |
---|
231 | exit |
---|
232 | fi |
---|
233 | } |
---|
234 | |
---|
235 | ## [Tomcat Severice start/stop/restart] ## |
---|
236 | function tomcat_switch(){ |
---|
237 | pid_tc=$(ps axw -eo pid,command | grep "catalina" | grep "java" | grep "start" | awk '{print $1}') |
---|
238 | #echo $pid_tc |
---|
239 | #read |
---|
240 | if [ -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 |
---|
243 | else |
---|
244 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Status]" \ |
---|
245 | --msgbox "Tomcat is not running in this system !!!" 7 50 |
---|
246 | fi |
---|
247 | tomcatSwitch="/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 | |
---|
255 | if [ "$(cat $tomcatSwitch)" == "exit" ]; then |
---|
256 | exit 0 |
---|
257 | fi |
---|
258 | |
---|
259 | # jude $Tomcat_HOME/bin/startup.sh |
---|
260 | if [ ! -e $Tomcat_HOME/bin/startup.sh ]; then |
---|
261 | echo -e "\nDon't fine $Tomcat_HOME/bin/startup.sh" |
---|
262 | exit |
---|
263 | fi |
---|
264 | |
---|
265 | if [ ! -e $Tomcat_HOME/bin/shutdown.sh ]; then |
---|
266 | echo -e "\nDon't fine $Tomcat_HOME/bin/shutdown.sh" |
---|
267 | exit |
---|
268 | fi |
---|
269 | |
---|
270 | if [ "$(cat $tomcatSwitch)" == "start" ]; then |
---|
271 | echo "$Tomcat_HOME/bin/startup.sh" |
---|
272 | $Tomcat_HOME/bin/startup.sh |
---|
273 | elif [ "$(cat $tomcatSwitch)" == "stop" ]; then |
---|
274 | echo "$Tomcat_HOME/bin/shutdown.sh" |
---|
275 | $Tomcat_HOME/bin/shutdown.sh |
---|
276 | elif [ "$(cat $tomcatSwitch)" == "restart" ]; then |
---|
277 | echo "$Tomcat_HOME/bin/restart.sh" |
---|
278 | $Tomcat_HOME/bin/shutdown.sh |
---|
279 | $Tomcat_HOME/bin/startup.sh |
---|
280 | else |
---|
281 | exit 0 |
---|
282 | fi |
---|
283 | } |
---|
284 | |
---|
285 | ## [Tomcat Port Change] ## |
---|
286 | function tomcat_port(){ |
---|
287 | |
---|
288 | if [ ! -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 |
---|
292 | fi |
---|
293 | tomcatPort="/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 | |
---|
297 | if [ -z "$(cat $tomcatPort)" ]; then |
---|
298 | exit |
---|
299 | else |
---|
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 |
---|
304 | fi |
---|
305 | |
---|
306 | |
---|
307 | } |
---|
308 | |
---|
309 | |
---|
310 | ## [Language Change] ## |
---|
311 | function lang_switch(){ |
---|
312 | echo -e "lang_switch" |
---|
313 | langSwitch="/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 | # |
---|
320 | if [ "$(cat $langSwitch)" == "Exit" ]; then |
---|
321 | exit |
---|
322 | else |
---|
323 | echo "source $(cat $langSwitch)" |
---|
324 | fi |
---|
325 | } |
---|
326 | |
---|
327 | |
---|
328 | # [Main Code] # |
---|
329 | if [ $USER != "nutchuser" ]; then |
---|
330 | echo -e "\nYour identify is not nutchuser, plz change to \"nutchuser\" !!!" |
---|
331 | exit |
---|
332 | fi |
---|
333 | |
---|
334 | prepare_check |
---|
335 | main_menu |
---|
336 | case $(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;; |
---|
349 | esac |
---|