[162] | 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 | |
---|
[172] | 11 | # [Variables Declaration] # |
---|
[162] | 12 | DIALOG=dialog |
---|
| 13 | main_choice= |
---|
| 14 | |
---|
[172] | 15 | # [PATH Declaration] # |
---|
[171] | 16 | NutchEZ_Install_PATH=/opt/nutchez |
---|
[176] | 17 | Tomcat_HOME=/opt/nutchez/tomcat |
---|
[171] | 18 | NutchEZ_HOME=/home/nutchuser/nutchez |
---|
| 19 | |
---|
[172] | 20 | # [Functions Declaration] # |
---|
| 21 | ## [Prepare Check] ## |
---|
[162] | 22 | function prepare_check(){ |
---|
[172] | 23 | # 若無 nutchez_nodes 則跳出 |
---|
| 24 | if [ ! -e "${NutchEZ_HOME}/system/nutch_nodes" ]; then |
---|
[165] | 25 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[nutch_nodes Files]" \ |
---|
[172] | 26 | --msgbox "Fail: Don't find $NutchEZ_HOME/system/nutch_nodes !!!" 10 40 |
---|
[162] | 27 | exit |
---|
| 28 | fi |
---|
| 29 | |
---|
[172] | 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 |
---|
[162] | 35 | |
---|
[172] | 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 |
---|
[165] | 48 | |
---|
[172] | 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 |
---|
[162] | 58 | } |
---|
| 59 | |
---|
[172] | 60 | ## [Main Menu] ## |
---|
[162] | 61 | function main_menu(){ |
---|
| 62 | main_choice="/tmp/main_choice" |
---|
| 63 | |
---|
| 64 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" \ |
---|
[165] | 65 | --title "[Management Options]" \ |
---|
[177] | 66 | --menu "Plz choose: " 15 60 6 \ |
---|
[174] | 67 | "cluster_status" "Check cluster status" \ |
---|
[165] | 68 | "cluster_setup" "Setup your datanode & tasktracker" \ |
---|
[162] | 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 | |
---|
[174] | 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 |
---|
[176] | 81 | echo -e "[IP] \t\t [Hostname] \t [Network] \t [Dtatnode & Tasktracker]" >>$clusterStatus |
---|
[174] | 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 | |
---|
[175] | 101 | [ -z "$Task_Data" ] || Task_Data_status="running" |
---|
[174] | 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 | |
---|
[172] | 112 | ## [Cluster Setup (datanode & tasktracker)] ## |
---|
[162] | 113 | function cluster_setup(){ |
---|
[172] | 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" |
---|
[162] | 121 | |
---|
[172] | 122 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" \ |
---|
| 123 | --title "[Management Options]" \ |
---|
[177] | 124 | --menu "Plz choose: " 15 55 3 \ |
---|
[172] | 125 | "All" "All nodes" \ |
---|
[177] | 126 | "Part" "Choose part nodess" \ |
---|
| 127 | "Exit" "Exit" 2>$allpart |
---|
[162] | 128 | |
---|
[172] | 129 | # 判斷是否選 all 和 part,若是 all 直接到服務選單,若是 part 則先到機器選單 |
---|
[177] | 130 | if [ "$(cat $allpart)" == "All" ]; then |
---|
[172] | 131 | cat $NutchEZ_HOME/system/nutch_nodes | grep -v '^$' | grep -v '#' | awk '{print $1}' >$runNodes |
---|
[177] | 132 | elif [ "$(cat $allpart)" == "Part" ]; then |
---|
[172] | 133 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC = " --title "[Datanode & Tasktracker Nodes Choose]" \ |
---|
[177] | 134 | --checklist "datanode & tasktracker choose: " 15 55 7 $(cat $NutchEZ_HOME/system/nutch_nodes | \ |
---|
[172] | 135 | grep -v '^$' | grep -v '#' | awk '{print $1 "\t" $2 "\t" "$LIST_status" }') 2>$runNodes |
---|
[177] | 136 | else |
---|
| 137 | exit |
---|
[172] | 138 | fi |
---|
| 139 | |
---|
[177] | 140 | if [ -z "$(cat $runNodes)" ]; then |
---|
| 141 | exit |
---|
| 142 | fi |
---|
| 143 | |
---|
[165] | 144 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Datanode & Tasktracker Setup]" \ |
---|
[176] | 145 | --menu "Plz choose: " 15 65 4 \ |
---|
[172] | 146 | "start" "Start datanodes & tasktrackers" \ |
---|
| 147 | "stop" "Stop datanodes & tasktrackers" \ |
---|
| 148 | "restart" "restart datanodes & tasktrackers" \ |
---|
[162] | 149 | "exit" "Exit" 2>$clusterSetup |
---|
| 150 | |
---|
| 151 | if [ "$(cat $clusterSetup)" == "start" ]; then |
---|
[176] | 152 | echo -e "\n[Start datanode & tasktracker]" |
---|
[172] | 153 | for node in $(cat $runNodes) |
---|
[162] | 154 | do |
---|
[176] | 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" |
---|
[172] | 158 | [ $? == "0" ] || echo "Can't ssh to $node !!!" |
---|
[162] | 159 | done |
---|
| 160 | elif [ "$(cat $clusterSetup)" == "stop" ]; then |
---|
[176] | 161 | echo -e "\n[Stop datanode & tasktracker]" |
---|
[172] | 162 | for node in $(cat $runNodes) |
---|
[162] | 163 | do |
---|
[176] | 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" |
---|
[172] | 167 | [ $? == "0" ] || echo "Can't ssh to $node !!!" |
---|
[162] | 168 | done |
---|
[177] | 169 | elif [ "$(cat $clusterSetup)" == "restart" ]; then |
---|
[176] | 170 | echo -e "\n[Restart datanode & tasktracker]" |
---|
[172] | 171 | for node in $(cat $runNodes) |
---|
| 172 | do |
---|
[176] | 173 | echo -e "restart $node datanode & tasktracker !" |
---|
| 174 | ssh -o StrictHostKeyChecking=no $node "$NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop datanode \ |
---|
[177] | 175 | ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh stop tasktracker \ |
---|
| 176 | ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start datanode \ |
---|
[176] | 177 | ; $NutchEZ_Install_PATH/nutch/bin/hadoop-daemon.sh start tasktracker" |
---|
[172] | 178 | [ $? == "0" ] || echo "Can't ssh to $node !!!" |
---|
| 179 | done |
---|
[177] | 180 | else |
---|
| 181 | exit |
---|
[162] | 182 | fi |
---|
| 183 | } |
---|
| 184 | |
---|
[172] | 185 | ## [Tomcat Severice start/stop/restart] ## |
---|
[162] | 186 | function tomcat_switch(){ |
---|
| 187 | pid_tc=$(ps axw -eo pid,command | grep "catalina" | grep "java" | grep "start" | awk '{print $1}') |
---|
| 188 | #echo $pid_tc |
---|
| 189 | #read |
---|
| 190 | if [ -n "$pid_tc" ]; then |
---|
[165] | 191 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Status]" \ |
---|
| 192 | --msgbox "Tomcat is runing in this system !!!" 7 50 |
---|
[162] | 193 | else |
---|
[165] | 194 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Status]" \ |
---|
| 195 | --msgbox "Tomcat is not running in this system !!!" 7 50 |
---|
[162] | 196 | fi |
---|
| 197 | tomcatSwitch="/tmp/tomcatSwitch" |
---|
[165] | 198 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Service Options]" \ |
---|
[162] | 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 | |
---|
[177] | 205 | if [ "$(cat $tomcatSwitch)" == "exit" ]; then |
---|
| 206 | exit 0 |
---|
| 207 | fi |
---|
| 208 | |
---|
| 209 | # jude $Tomcat_HOME/bin/startup.sh |
---|
| 210 | if [ ! -e $Tomcat_HOME/bin/startup.sh ]; then |
---|
| 211 | echo -e "\nDon't fine $Tomcat_HOME/bin/startup.sh" |
---|
| 212 | exit |
---|
| 213 | fi |
---|
| 214 | |
---|
| 215 | if [ ! -e $Tomcat_HOME/bin/shudown.sh ]; then |
---|
| 216 | echo -e "\nDon't fine $Tomcat_HOME/bin/shudown.sh" |
---|
| 217 | exit |
---|
| 218 | fi |
---|
| 219 | |
---|
[162] | 220 | if [ "$(cat $tomcatSwitch)" == "start" ]; then |
---|
[176] | 221 | echo "$Tomcat_HOME/bin/startup.sh" |
---|
| 222 | $Tomcat_HOME/bin/startup.sh |
---|
[162] | 223 | elif [ "$(cat $tomcatSwitch)" == "stop" ]; then |
---|
[176] | 224 | echo "$Tomcat_HOME/bin/shutdown.sh" |
---|
| 225 | $Tomcat_HOME/bin/shutdown.sh |
---|
[162] | 226 | elif [ "$(cat $tomcatSwitch)" == "restart" ]; then |
---|
[176] | 227 | echo "$Tomcat_HOME/bin/restart.sh" |
---|
| 228 | $Tomcat_HOME/bin/shutdown.sh |
---|
| 229 | $Tomcat_HOME/bin/startup.sh |
---|
[162] | 230 | else |
---|
| 231 | exit 0 |
---|
| 232 | fi |
---|
| 233 | } |
---|
| 234 | |
---|
[172] | 235 | ## [Tomcat Port Change] ## |
---|
[162] | 236 | function tomcat_port(){ |
---|
| 237 | |
---|
[176] | 238 | if [ ! -e $Tomcat_HOME/conf/server.xml ]; then |
---|
[165] | 239 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Tomcat Servel.xml]" \ |
---|
[176] | 240 | --msgbox "Fial: Don't find $Tomcat_HOME/conf/server.xml !!!" 10 50 |
---|
[162] | 241 | exit |
---|
| 242 | fi |
---|
| 243 | tomcatPort="/tmp/tomcatPort" |
---|
[165] | 244 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC = " --title "[Tomcat Port Change]" \ |
---|
[162] | 245 | --inputbox "Plz input port number for tomcat (default is 8080)" 10 55 2>$tomcatPort |
---|
| 246 | |
---|
[177] | 247 | if [ -z "$(cat $tomcatPort)" ]; then |
---|
[162] | 248 | exit |
---|
| 249 | else |
---|
[176] | 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 |
---|
[162] | 254 | fi |
---|
[176] | 255 | |
---|
| 256 | |
---|
[162] | 257 | } |
---|
| 258 | |
---|
| 259 | |
---|
[172] | 260 | ## [Language Change] ## |
---|
[162] | 261 | function lang_switch(){ |
---|
| 262 | echo -e "lang_switch" |
---|
| 263 | langSwitch="/tmp/langSwitch" |
---|
[165] | 264 | $DIALOG --clear --backtitle "= [NutchEz Management Interface] ~by NCHC =" --title "[Language Swaitch]" \ |
---|
[162] | 265 | --menu "Plz choose: " 15 55 3 \ |
---|
| 266 | "English" "English" \ |
---|
[177] | 267 | "Chinese" "Chinese" \ |
---|
| 268 | "Exit" "Exit" 2>$langSwitch |
---|
[162] | 269 | # |
---|
[177] | 270 | if [ "$(cat $langSwitch)" == "Exit" ]; then |
---|
| 271 | exit |
---|
| 272 | else |
---|
| 273 | echo "source $(cat $langSwitch)" |
---|
| 274 | fi |
---|
[162] | 275 | } |
---|
| 276 | |
---|
| 277 | |
---|
[172] | 278 | # [Main Code] # |
---|
[162] | 279 | if [ $USER != "nutchuser" ]; then |
---|
| 280 | echo -e "\nYour identify is not nutchuser, plz change to \"nutchuser\" !!!" |
---|
| 281 | exit |
---|
| 282 | fi |
---|
| 283 | |
---|
| 284 | prepare_check |
---|
| 285 | main_menu |
---|
| 286 | case $(cat $main_choice) in |
---|
[174] | 287 | "cluster_status") |
---|
| 288 | cluster_status;; |
---|
[162] | 289 | "cluster_setup") |
---|
| 290 | cluster_setup;; |
---|
| 291 | "tomcat_switch") |
---|
| 292 | tomcat_switch;; |
---|
| 293 | "tomcat_port") |
---|
| 294 | tomcat_port;; |
---|
| 295 | "lang_switch") |
---|
| 296 | lang_switch;; |
---|
| 297 | esac |
---|