Apache Tomcat Manager 1.0

 

Apache Tomcat Manager 1.0

Abbr. atm


Suitable for:
    Apache Tomcat 7.0.47


89676A512AD625916857EE981AA506E8  atm
FB340E537105341CFACDDD700F02FA7B  atm.ini

默认会排序,可以通过执行多条命令来抑制,如:
atm -m 2; atm -m 3; atm -m 1

atm.ini

1 # PRG_HOME_DIRS - program home directories, separated by ":"
2 PRG_HOME_DIRS=/opt/data/devel/apache-tomcat-7.0.47/p54321:/opt/data/devel/apache-tomcat-7.0.47/p6666:/opt/data/devel/apache-tomcat-7.0.47/p80
3 #EOF

atm

  1 #!/bin/sh
  2 
  3 # program home directories
  4 phd_arr=()
  5 
  6 echo_n(){
  7     echo -e "\033[30;32m$*\033[0m"
  8 }
  9 
 10 echo_e(){
 11     echo -e "\033[30;35m$*\033[0m"
 12 }
 13 
 14 show_usage(){
 15     echo
 16     echo_n "Apache Tomcat Manager 1.0"
 17     echo_n "Report bugs to <614616185@qq.com>."
 18     echo
 19     echo "Usage: ${0##*/} [OPTION]..."
 20     printf "\t%-8s%s\n" "-g" "Generate optimal 'PRG_HOME_DIRS'."
 21     printf "\t%-8s%s\n" "-l" "List program home directories."
 22     printf "\t%-8s%s\n" "-s" "Query program state."
 23     printf "\t\t%-8s%s\n" "-h" "State is 'Halted'."
 24     printf "\t\t%-8s%s\n" "-r" "State is 'Running'."
 25     printf "\t\t%-8s%s\n" "-a" "State is 'Abnormal'."
 26     printf "\t\t%-8s%s\n" "-ra" "State is 'Running' or 'Abnormal'."
 27     printf "\t%-8s%s\n" "-d" "Diagnosing program state."
 28     printf "\t%-8s%s\n" "-r" "Run program."
 29     printf "\t%-8s%s\n" "-h" "Halt program."
 30     printf "\t%-8s%s\n" "-k" "Kill program."
 31     printf "\t%-8s%s\n" "-fk" "Force kill program."
 32     printf "\t%-8s%s\n" "-m" "Monitoring log (Ctrl+\)."
 33     printf "\t%-8s%s\n" "-c" "Clean up the cache and garbage."
 34     printf "\t%-8s%s\n" "-R" "Rerun program."
 35     printf "\t%-8s%s\n" "-D" "Deploy web application archive."
 36     echo
 37 }
 38 
 39 init_prg_col_width(){
 40     chk_widest "${phd_arr[*]}"
 41     PHD_COL_W=$WIDEST
 42 }
 43 
 44 chk_widest(){
 45     local i= max=0 arr=($1)
 46     for((i=0;i<${#arr[*]};i++)); do
 47         if [ ${#arr[$i]} -gt $max ]; then
 48             max=${#arr[$i]}
 49         fi
 50     done
 51     WIDEST=$max
 52 }
 53 
 54 is_prg_id(){
 55     if (( ${#phd_arr[*]} > 0 )) && (( $1 >= 0 )) && (( $1 < ${#phd_arr[*]} )); then
 56         echo true
 57     else
 58         echo false
 59     fi
 60 }
 61 
 62 chk_process(){
 63     unset PID_ARR
 64     PID_ARR=()
 65     PID_SIZE=0
 66     local proc_part=${phd_arr[$1]}
 67     if [ ! -z "$proc_part" ]; then
 68         local search_result=`ps -ef | grep "$proc_part" | grep -v "grep $proc_part"`
 69         PID_ARR=(`echo "$search_result" | awk '{ print $2 }'`)
 70         PID_SIZE=${#PID_ARR[*]}
 71         return 1
 72     fi
 73 }
 74 
 75 chk_status(){
 76     local rtn=0 arr=("Not running" "OK" "Abnormal")
 77     chk_process $1
 78     rtn=$PID_SIZE
 79     if [ $PID_SIZE -gt 1 ]; then
 80         rtn=2
 81     fi
 82     STATE_LABEL=${arr[$rtn]}
 83     return $rtn
 84 }
 85 
 86 print_header(){
 87     printf "%4s\t%11s\t%${PHD_COL_W}s\n" "ID" "State" "Program Home Directory"
 88 }
 89 
 90 show_status(){
 91     if [ -z "$1" ]||`is_contains "$1" "-h -r -a -ra"`; then
 92         local i= t_body=
 93         for((i=0;i<${#phd_arr[*]};i++)); do
 94             chk_status $i
 95             local rtn=$?
 96             if [ -z "$1" ] || [[ "$1" = "-h" && (( $rtn == 0 )) ]] || [[ "$1" = "-r" && (( $rtn == 1 )) ]] || [[ "$1" = "-a" && (( $rtn == 2 )) ]] || [[ "$1" = "-ra" && (( $rtn != 0 )) ]]; then
 97                 local t_tr=`printf "%4s\t%11s\t%-${PHD_COL_W}s\n" "$(expr $i + 1)" "$STATE_LABEL" "${phd_arr[$i]}"`
 98                 if [ -z "$t_body" ]; then
 99                     t_body="$t_tr\n"
100                 else
101                     t_body="$t_body$t_tr\n"
102                 fi
103             fi
104         done
105         if [ -z "$t_body" ]; then
106             echo_e No data.
107         else
108             print_header
109             echo -e "$t_body"
110         fi
111     else
112         show_usage
113         exit
114     fi
115 }
116 
117 exec_prg(){
118     #for p in "$@"; do
119     #    echo [$p]
120     #done
121     local state_filter=
122     if `is_contains "$1" "-r -c"`; then
123         state_filter="-h"
124     elif `is_contains "$1" "-h -m"`; then
125         state_filter="-r"
126     elif `is_contains "$1" "-d -k -fk"`; then
127         state_filter="-ra"
128     fi
129     if [ $# -eq 1 ]; then
130         local result="`show_status $state_filter`"
131         echo "$result"
132         if [ `echo "$result" | wc -l` -gt 1 ]; then
133             echo -e "Examples:\n\t${0##*/} $1 8 4 2 1..."
134         fi
135     else
136         local opt1=$1 tmp_str=
137         while [ $# -gt 1 ]; do
138             local tpi=`to_positive_integer "$2"`
139             if [ ! -z "$tpi" ]&&[ $tpi -le ${#phd_arr[*]} ]; then
140                 if [ -z "$tmp_str" ]; then
141                     tmp_str=$tpi
142                 else
143                     tmp_str="$tmp_str $tpi"
144                 fi
145             fi
146             shift
147         done
148         local id= idx= executed_idxs= diagnose_header=true
149         for id in `echo -e "${tmp_str// /\\n}" | sort -nu`; do
150             idx=$(expr $id - 1)
151             if `is_prg_id $idx`; then
152                 chk_status $idx
153                 local rtn=$?
154                 if [ "$opt1" = "-R" ] || ( `is_contains "$opt1" "-c -r"` && (( $rtn == 0 )) ) || ( `is_contains "$opt1" "-h -m"` && (( $rtn == 1 )) ) || ( `is_contains "$opt1" "-d -k -fk"` && (( $rtn != 0 )) ); then
155                     local cmd=
156                     local phd=${phd_arr[$idx]}
157                     if [ "$opt1" = "-r" ]; then
158                         echo_n "Run ($id) '$phd'."
159                         cmd="$phd/bin/startup.sh"
160                     elif [ "$opt1" = "-h" ]; then
161                         echo_n "Halt ($id) '$phd'."
162                         cmd="$phd/bin/shutdown.sh"
163                     elif [ "$opt1" = "-m" ]; then
164                         echo_n "Monitoring ($id) '$phd'."
165                         pushd $phd/logs >/dev/null 2>&1
166                         tail -f catalina.out
167                         popd >/dev/null 2>&1
168                     elif [ "$opt1" = "-c" ]; then
169                         echo_n "Clean up ($id) '$phd'."
170                         echo -e "\t--> logs"
171                         pushd $phd/logs >/dev/null 2>&1
172                         rm -rfv *
173                         popd >/dev/null 2>&1
174                         echo -e "\t--> work"
175                         pushd $phd/work >/dev/null 2>&1
176                         rm -rfv *
177                         popd >/dev/null 2>&1
178                     elif [ "$opt1" = "-R" ]; then
179                         echo_n "Rerun ($id) '$phd'."
180                         if [ $rtn -eq 1 ]; then
181                             echo_n "Shutting down..."
182                             "$phd/bin/shutdown.sh" >/dev/null 2>&1
183                             sleep 1
184                         fi
185                         chk_status $idx
186                         if [ $? -eq 1 ]; then
187                             echo_n "Kill..."
188                             kill ${PID_ARR[*]} >/dev/null 2>&1
189                             sleep 1
190                         fi
191                         chk_status $idx
192                         if [ $? -ne 0 ]; then
193                             echo_n "Force kill..."
194                             kill -9 ${PID_ARR[*]} >/dev/null 2>&1
195                             sleep 1
196                         fi
197                         chk_status $idx
198                         if [ $? -eq 0 ]; then
199                             echo_n "Starting..."
200                             cmd="$phd/bin/startup.sh"
201                         else
202                             echo_e "Unable to end process."
203                         fi
204                     elif [ "$opt1" = "-d" ]; then
205                         if $diagnose_header; then
206                             printf "%4s\t%5s\t%11s\t%${PHD_COL_W}s\n" "ID" "PID" "State" "Program Home Directory"
207                             diagnose_header=false
208                         fi
209                         local d_pid=
210                         for d_pid in ${PID_ARR[*]}; do
211                             printf "%4s\t%5s\t%11s\t%-${PHD_COL_W}s\n" "$id" "$d_pid" "$STATE_LABEL" "$phd"
212                         done
213                     elif [ "$opt1" = "-k" ]; then
214                         echo_n "Kill ($id) '$phd'."
215                         cmd="kill ${PID_ARR[*]}"
216                     elif [ "$opt1" = "-fk" ]; then
217                         echo_n "Force kill ($id) '$phd'."
218                         cmd="kill -9 ${PID_ARR[*]}"
219                     fi
220                     if [ ! -z "$cmd" ]; then
221                         `$cmd >/dev/null 2>&1`
222                     fi
223                     if [ -z "$executed_idxs" ]; then
224                         executed_idxs=$idx
225                     else
226                         executed_idxs="$executed_idxs $idx"
227                     fi
228                 fi
229             fi
230         done
231         if [ ! -z "$executed_idxs" ]; then
232             if `is_contains "$opt1" "-r -h -k -fk -R"`; then
233                 print_header
234                 local idx=
235                 for idx in $executed_idxs; do
236                     chk_status $idx
237                     printf "%4s\t%11s\t%-${PHD_COL_W}s\n" "$(expr $idx + 1)" "$STATE_LABEL" "${phd_arr[$idx]}"
238                 done
239                 echo_n "The command completed successfully."
240             fi
241         else
242             echo_e Nothing to do.
243         fi
244     fi
245 }
246 
247 is_contains(){
248     local ps=
249     for ps in $2; do
250         if [ "$ps" = "$1" ]; then
251             echo true
252             return 1
253         fi
254     done
255     echo false
256 }
257 
258 chk_positive_integer(){
259     POS_INT=
260     local nstr=`trim "$1"`
261     echo "$nstr" | grep "[^0-9]" >/dev/null 2>&1
262     if [ $? -eq 1 ]&&[ ! -z "$nstr" ]; then
263         local t=$(expr $nstr + 0)
264         if (( $t > 0 )); then
265             POS_INT=$t
266             return 1
267         fi
268     fi
269 }
270 
271 to_positive_integer(){
272     chk_positive_integer "$1"
273     echo $POS_INT
274 }
275 
276 trim(){
277     echo $1
278 }
279 
280 one_slash(){
281     local src="$1"
282     while [[ "$src" == *//* ]]; do
283         local slash1="/" slash2="//"
284         src="${src//$slash2/$slash1}"
285     done
286     echo $src
287 }
288 
289 to_canonical_path(){
290     local path=`trim "$1"`
291     if [ ! -z "$path" ]&&[ -e "$path" ]; then
292         local result=
293         if [ -d "$path" ]; then
294             result=`cd "$path"; pwd`
295         elif [ -f "$path" ]; then
296             local directory=`dirname "$path"`
297             directory=`cd "$directory"; pwd`
298             local filename=`basename "$path"`
299             result="$directory/$filename"
300         fi
301         result=`one_slash "$result"`
302         echo $result
303     fi
304 }
305 
306 main(){
307     # Show usage
308     if [ -z "$1" ]; then
309         show_usage
310         exit
311     fi
312 
313     # Read configuration file
314     local line=
315     while read line; do
316         eval "$line"
317     done < ${0%%.*}.ini
318 
319     # Filling phd_arr
320     local phd= index=0
321     for phd in `echo -e "${PRG_HOME_DIRS//:/\\n}" | sort -u`; do
322         phd=`to_canonical_path "$phd"`
323         if [ ! -z "$phd" ]&&[ -x "$phd/bin/startup.sh" ]&&[ -x "$phd/bin/shutdown.sh" ]&&[ -d "$phd/webapps" ]&&[ -d "$phd/work" ]&&[ -d "$phd/logs" ]; then
324             phd_arr[$index]="$phd"
325             let "index+=1"
326         fi
327     done
328     local tmp_str="${phd_arr[*]}"
329     tmp_str=`echo -e "${tmp_str// /\\n}" | sort -u`
330     unset phd_arr
331     phd_arr=($tmp_str)
332 
333     # Check the quantity
334     local max_phds=99
335     if [ ${#phd_arr[*]} -gt $max_phds ]; then
336         echo_e "Error: 'PRG_HOME_DIRS' exceeds $max_phds."
337         exit
338     fi
339 
340     # Initialize the maximum width of the column
341     init_prg_col_width
342 
343     # Parameter matching
344     if [ "$1" == "-g" ]; then
345         local tmp_str="${phd_arr[*]}"
346         echo -e "PRG_HOME_DIRS=${tmp_str// /:}"
347     elif [ "$1" == "-l" ]; then
348         if [ ${#phd_arr[*]} -gt 0 ]; then
349             printf "%4s\t%${PHD_COL_W}s\n" "ID" "Program Home Directory"
350             local i=
351             for((i=0;i<${#phd_arr[*]};i++)); do
352                 printf "%4d\t%-${PHD_COL_W}s\n" "$(expr $i + 1)" "${phd_arr[$i]}"
353             done
354         else
355             echo_e No data.
356         fi
357     elif [ "$1" == "-s" ]; then
358         show_status $2
359     elif `is_contains "$1" "-d -r -h -k -fk -m -c -R"`; then
360         exec_prg "$@"
361     elif [ "$1" = "-D" ]; then
362         local id=`to_positive_integer "$2"`
363         if [ ! -z "$id" ]&&[ $id -le ${#phd_arr[*]} ]; then
364             local idx=$(expr $id - 1)
365             local tmp_str=
366             local phd=${phd_arr[$idx]}
367             while [ $# -gt 2 ]; do
368                 echo "Checking WAR file. '$3'"
369                 local war_file=`trim "$3"`
370                 local is_ok=false
371                 if [ -z "$war_file" ]; then
372                     echo "Cause: Empty string."
373                 elif [[ "$war_file" == *" "* ]]; then
374                     echo "Cause: Path contains spaces."
375                 elif [ ! -f "$war_file" ]; then
376                     echo "Cause: No such file."
377                 else
378                     local suffix="${war_file##*.}"
379                     if [ "$suffix" != "war" ]; then
380                         echo "Cause: File suffix is not '.war'."
381                     else
382                         local name=`basename "$war_file" .war`
383                         if [ "$name" = "$war_file" ]; then
384                             echo "Cause: Named 'war/.war'."
385                         elif `is_contains "$war_file" "..war ...war"`; then
386                             echo "Cause: Named '..war/...war'."
387                         else
388                             local filepath=`to_canonical_path "$war_file"`
389                             unzip -t "$filepath" >/dev/null 2>&1
390                             if [ $? != 0 ]; then
391                                 echo "Cause: Invalid WAR file."
392                             else
393                                 is_ok=true
394                                 if [ -z "$tmp_str" ]; then
395                                     tmp_str="$filepath"
396                                 else
397                                     tmp_str="$tmp_str $filepath"
398                                 fi
399                             fi
400                         fi
401                     fi
402                 fi
403                 if $is_ok; then
404                     echo_n Passed.
405                 else
406                     echo_e Failed.
407                 fi
408                 shift
409             done
410             if [ ! -z "$tmp_str" ]; then
411                 local deployed=false dp_count=0
412                 echo_n "Deploying ($id) '$phd'."
413                 chk_status $idx
414                 if [ $? -eq 1 ]; then
415                     echo_n "Shutting down..."
416                     "$phd/bin/shutdown.sh" >/dev/null 2>&1
417                     sleep 1
418                 fi
419                 chk_status $idx
420                 if [ $? -eq 1 ]; then
421                     echo_n "Kill..."
422                     kill ${PID_ARR[*]} >/dev/null 2>&1
423                     sleep 1
424                 fi
425                 chk_status $idx
426                 if [ $? -ne 0 ]; then
427                     echo_n "Force kill..."
428                     kill -9 ${PID_ARR[*]} >/dev/null 2>&1
429                     sleep 1
430                 fi
431                 chk_status $idx
432                 if [ $? -eq 0 ]; then
433                     local wfp=
434                     for wfp in `echo -e "${tmp_str// /\\n}" | sort -u`; do
435                         local prj_name=`basename "$wfp" .war`
436                         echo_n "Removing file..."
437                         rm -rfv "$phd/webapps/$prj_name"
438                         # cp: ... are the same file
439                         echo_n "Copying file..."
440                         cp -v "$wfp" "$phd/webapps"
441                         let "dp_count+=1"
442                     done
443                     if [ $dp_count != 0 ]; then
444                         echo_n "Starting..."
445                         "$phd/bin/startup.sh" >/dev/null 2>&1
446                         deployed=true
447                     fi
448                 else
449                     echo_e "Unable to end process."
450                 fi
451                 if $deployed; then
452                     echo Deployed projects: $dp_count
453                     print_header
454                     chk_status $idx
455                     printf "%4s\t%11s\t%-${PHD_COL_W}s\n" "$(expr $idx + 1)" "$STATE_LABEL" "${phd_arr[$idx]}"
456                     echo_n "Project deployment has been completed."
457                 else
458                     echo_e "Project deployment is not completed."
459                 fi
460             else
461                 echo_e No files available.
462             fi
463         else
464             local result="`show_status`"
465             echo "$result"
466             if [ `echo "$result" | wc -l` -gt 1 ]; then
467                 echo -e "Examples:\n\t${0##*/} $1 1 mywebapp1.war mywebapp2.war..."
468             fi
469         fi
470     else
471         show_usage
472     fi
473 }
474 
475 phd_arr=()
476 main "$@"
477 unset phd_arr
478 #declare
479 
480 :<<EOF
481 TODO: 
482 is_positive_number()
483 is_positive_integer()
484 is_negative_integer()
485 is_negative_number()
486 is_numeric()
487 is_integer()
488 # greater than 0
489 is_gt0()
490 to_numeric()
491 to_integer()
492 EOF

附件1

posted @ 2019-07-20 23:58  RMS365  阅读(308)  评论(1编辑  收藏  举报