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 | NutchEZ_HOME=/home/nutchuser/nutchez |
---|
18 | |
---|
19 | # [Functions Declaration] # |
---|
20 | ## [Prepare Check] ## |
---|
21 | function prepare_check(){ |
---|
22 | # 若無 nutchez_nodes 則跳出 |
---|
23 | if [ ! -e "${NutchEZ_HOME}/system/nutch_nodes" ]; then |
---|
24 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[nutch_nodes Files]" \ |
---|
25 | --msgbox "Fail: Don't find $NutchEZ_HOME/system/nutch_nodes !!!" 10 40 |
---|
26 | exit |
---|
27 | fi |
---|
28 | |
---|
29 | # 判斷 nutch_nodes 和 nutch_nodes.back |
---|
30 | # 第一次執行則要求 user 更新 /etc/hosts |
---|
31 | if [ ! -e "${NutchEZ_HOME}/system/nutch_nodes.bak" ]; then |
---|
32 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[/etc/host Update]" \ |
---|
33 | --yesno "First execut nutchez management interface.\nPlz update /etc/hosts !!!" 10 55 |
---|
34 | |
---|
35 | echo -e "Plz input root password to update /etc/hosts !!!" |
---|
36 | ./duplicate_del "${NutchEZ_HOME}/system/nutch_nodes" |
---|
37 | su root -c "./duplicate_del /etc/hosts && ./add_hosts ${NutchEZ_HOME}/system/nutch_nodes /etc/hosts" |
---|
38 | # 若密碼輸入失敗則跳出 |
---|
39 | if [ $? == "1" ]; then |
---|
40 | echo "Root password is error ! Plz check root password !!!" |
---|
41 | exit |
---|
42 | fi |
---|
43 | # 若 nutch_nodes 被更新過,則要求更新 /etc/hosts |
---|
44 | elif [ ${NutchEZ_HOME}/system/nutch_nodes -nt ${NutchEZ_HOME}/system/nutch_nodes.bak ]; then |
---|
45 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[/etc/host Update]" \ |
---|
46 | --yesno "nutch_nodes already be modified.\nPlz update /etc/hosts !!!" 10 55 |
---|
47 | |
---|
48 | echo -e "Plz input root password to update /etc/hosts !!!" |
---|
49 | ./duplicate_del "${NutchEZ_HOME}/system/nutch_nodes" |
---|
50 | su root -c "./duplicate_del /etc/hosts && ./add_hosts ${NutchEZ_HOME}/system/nutch_nodes /etc/hosts" |
---|
51 | # 若密碼輸入失敗則跳出 |
---|
52 | if [ $? == "1" ]; then |
---|
53 | echo "Root password is error ! Plz check root password !!!" |
---|
54 | exit |
---|
55 | fi |
---|
56 | fi |
---|
57 | |
---|
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 55 5 \ |
---|
67 | "cluster_setup" "Setup your datanode & tasktracker" \ |
---|
68 | "tomcat_switch" "Start/Stop/Restart Tomcat" \ |
---|
69 | "tomcat_port" "Change Tomcat port" \ |
---|
70 | "lang_switch" "Change language" \ |
---|
71 | "exit" "Exit" 2>$main_choice |
---|
72 | } |
---|
73 | |
---|
74 | ## [Cluster Setup (datanode & tasktracker)] ## |
---|
75 | function cluster_setup(){ |
---|
76 | # 從 nutch_nodes 讀出 ip 和 hostname |
---|
77 | #IP_list=$(cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#' | awk '{print $1}') |
---|
78 | #HOST_list=$(cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#' | awk '{print $2}') |
---|
79 | LIST_status="off" |
---|
80 | allpart="/tmp/allpart" |
---|
81 | runNodes="/tmp/runNodes" |
---|
82 | clusterSetup="/tmp/clusterSetup" |
---|
83 | |
---|
84 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" \ |
---|
85 | --title "[Management Options]" \ |
---|
86 | --menu "Plz choose: " 15 55 2 \ |
---|
87 | "All" "All nodes" \ |
---|
88 | "Part" "choose part nodess" 2>$allpart |
---|
89 | |
---|
90 | # 判斷是否選 all 和 part,若是 all 直接到服務選單,若是 part 則先到機器選單 |
---|
91 | if [ $(cat $allpart) == "All" ]; then |
---|
92 | cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#' | awk '{print $1}' >$runNodes |
---|
93 | else |
---|
94 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC = " --title "[Datanode & Tasktracker Nodes Choose]" \ |
---|
95 | --checklist "datanode & tasktracker choose: " 15 55 7 $(cat $NutchEZ_HOME/system/nutch_nodes | \ |
---|
96 | grep -v '^$' | grep -v '#' | awk '{print $1 "\t" $2 "\t" "$LIST_status" }') 2>$runNodes |
---|
97 | fi |
---|
98 | |
---|
99 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Datanode & Tasktracker Setup]" \ |
---|
100 | --menu "Plz choose: " 10 65 3 \ |
---|
101 | "start" "Start datanodes & tasktrackers" \ |
---|
102 | "stop" "Stop datanodes & tasktrackers" \ |
---|
103 | "restart" "restart datanodes & tasktrackers" \ |
---|
104 | "exit" "Exit" 2>$clusterSetup |
---|
105 | |
---|
106 | if [ "$(cat $clusterSetup)" == "start" ]; then |
---|
107 | for node in $(cat $runNodes) |
---|
108 | do |
---|
109 | echo -e "start !" |
---|
110 | echo -e "ssh -o StrictHostKeyChecking=no $node /opt/nutchez/nutch/bin/hadoop-daemon.sh start datanode" |
---|
111 | ssh -o StrictHostKeyChecking=no $node "/opt/nutchez/nutch/bin/hadoop-daemon.sh start datanode" |
---|
112 | echo -e "ssh -o StrictHostKeyChecking=no $node /opt/nutchez/nutch/bin/hadoop-daemon.sh start tasktracker" |
---|
113 | ssh -o StrictHostKeyChecking=no $node "/opt/nutchez/nutch/bin/hadoop-daemon.sh start tasktracker" |
---|
114 | [ $? == "0" ] || echo "Can't ssh to $node !!!" |
---|
115 | done |
---|
116 | elif [ "$(cat $clusterSetup)" == "stop" ]; then |
---|
117 | for node in $(cat $runNodes) |
---|
118 | do |
---|
119 | echo -e "stop !" |
---|
120 | echo -e "ssh -o StrictHostKeyChecking=no $node /opt/nutchez/nutch/bin/hadoop-daemon.sh stop datanode" |
---|
121 | ssh -o StrictHostKeyChecking=no $node "/opt/nutchez/nutch/bin/hadoop-daemon.sh stop datanode" |
---|
122 | echo -e "ssh -o StrictHostKeyChecking=no $node /opt/nutchez/nutch/bin/hadoop-daemon.sh stop tasktracker" |
---|
123 | ssh -o StrictHostKeyChecking=no $node "/opt/nutchez/nutch/bin/hadoop-daemon.sh stop tasktracker" |
---|
124 | [ $? == "0" ] || echo "Can't ssh to $node !!!" |
---|
125 | done |
---|
126 | else |
---|
127 | for node in $(cat $runNodes) |
---|
128 | do |
---|
129 | echo -e "restart !" |
---|
130 | ssh -o StrictHostKeyChecking=no $node "/opt/nutchez/nutch/bin/hadoop-daemon.sh stop datanode" |
---|
131 | ssh -o StrictHostKeyChecking=no $node "/opt/nutchez/nutch/bin/hadoop-daemon.sh stop tasktracker" |
---|
132 | ssh -o StrictHostKeyChecking=no $node "/opt/nutchez/nutch/bin/hadoop-daemon.sh start datanode" |
---|
133 | ssh -o StrictHostKeyChecking=no $node "/opt/nutchez/nutch/bin/hadoop-daemon.sh start tasktracker" |
---|
134 | [ $? == "0" ] || echo "Can't ssh to $node !!!" |
---|
135 | done |
---|
136 | |
---|
137 | fi |
---|
138 | } |
---|
139 | |
---|
140 | ## [Tomcat Severice start/stop/restart] ## |
---|
141 | function tomcat_switch(){ |
---|
142 | pid_tc=$(ps axw -eo pid,command | grep "catalina" | grep "java" | grep "start" | awk '{print $1}') |
---|
143 | #echo $pid_tc |
---|
144 | #read |
---|
145 | if [ -n "$pid_tc" ]; then |
---|
146 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Status]" \ |
---|
147 | --msgbox "Tomcat is runing in this system !!!" 7 50 |
---|
148 | else |
---|
149 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Status]" \ |
---|
150 | --msgbox "Tomcat is not running in this system !!!" 7 50 |
---|
151 | fi |
---|
152 | tomcatSwitch="/tmp/tomcatSwitch" |
---|
153 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Service Options]" \ |
---|
154 | --menu "Plz choose: " 15 55 4 \ |
---|
155 | "start" "Start tomcat" \ |
---|
156 | "stop" "Stop tomcat" \ |
---|
157 | "restart" "Restart tomcat" \ |
---|
158 | "exit" "Exit" 2>$tomcatSwitch |
---|
159 | |
---|
160 | if [ "$(cat $tomcatSwitch)" == "start" ]; then |
---|
161 | echo "/opt/nutchez/tomcat/bin/startup.sh" |
---|
162 | /opt/nutchez/tomcat/bin/startup.sh |
---|
163 | elif [ "$(cat $tomcatSwitch)" == "stop" ]; then |
---|
164 | echo "/opt/nutchez/tomcat/bin/shutdown.sh" |
---|
165 | /opt/nutchez/tomcat/bin/shutdown.sh |
---|
166 | elif [ "$(cat $tomcatSwitch)" == "restart" ]; then |
---|
167 | echo "/opt/nutchez/tomcat/bin/restart.sh" |
---|
168 | /opt/nutchez/tomcat/bin/shutdown.sh |
---|
169 | /opt/nutchez/tomcat/bin/startup.sh |
---|
170 | else |
---|
171 | exit 0 |
---|
172 | fi |
---|
173 | } |
---|
174 | |
---|
175 | ## [Tomcat Port Change] ## |
---|
176 | function tomcat_port(){ |
---|
177 | |
---|
178 | if [ ! -e /opt/nutchez/tomcat/conf/server.xml ]; then |
---|
179 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Servel.xml]" \ |
---|
180 | --msgbox "Fial: Don't find /opt/nutchez/tomcat/conf/server.xml !!!" 10 50 |
---|
181 | exit |
---|
182 | fi |
---|
183 | tomcatPort="/tmp/tomcatPort" |
---|
184 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC = " --title "[Tomcat Port Change]" \ |
---|
185 | --inputbox "Plz input port number for tomcat (default is 8080)" 10 55 2>$tomcatPort |
---|
186 | |
---|
187 | if [ -z $(cat $tomcatPort) ]; then |
---|
188 | exit |
---|
189 | else |
---|
190 | modify_line_nu=$(cat -n /opt/nutchez/tomcat/conf/server.xml | grep -v SSL | grep 'HTTP/1.1' | grep '<Connector' | awk '{print $1}') |
---|
191 | sed -i "${modify_line_nu}c <Connector port="$tomcatPort" protocol="HTTP/1.1"" /opt/nutchez/tomcat/conf/server.xml |
---|
192 | fi |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | ## [Language Change] ## |
---|
197 | function lang_switch(){ |
---|
198 | echo -e "lang_switch" |
---|
199 | langSwitch="/tmp/langSwitch" |
---|
200 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Language Swaitch]" \ |
---|
201 | --menu "Plz choose: " 15 55 3 \ |
---|
202 | "English" "English" \ |
---|
203 | "Chinese" "Chinese" 2>$langSwitch |
---|
204 | # |
---|
205 | echo "source $(cat $langSwitch)" |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | # [Main Code] # |
---|
210 | if [ $USER != "nutchuser" ]; then |
---|
211 | echo -e "\nYour identify is not nutchuser, plz change to \"nutchuser\" !!!" |
---|
212 | exit |
---|
213 | fi |
---|
214 | |
---|
215 | prepare_check |
---|
216 | main_menu |
---|
217 | case $(cat $main_choice) in |
---|
218 | "cluster_setup") |
---|
219 | cluster_setup;; |
---|
220 | "tomcat_switch") |
---|
221 | tomcat_switch;; |
---|
222 | "tomcat_port") |
---|
223 | tomcat_port;; |
---|
224 | "lang_switch") |
---|
225 | lang_switch;; |
---|
226 | esac |
---|