From b8e3533368caee3526544ba8772f96399da98a05 Mon Sep 17 00:00:00 2001 From: naught101 Date: Thu, 21 Sep 2017 14:43:46 +1000 Subject: [PATCH 1/4] Add original CCRC-top shell script by Eytan Rocheta --- shell/cctop.sh | 221 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 shell/cctop.sh diff --git a/shell/cctop.sh b/shell/cctop.sh new file mode 100644 index 0000000..421643c --- /dev/null +++ b/shell/cctop.sh @@ -0,0 +1,221 @@ +#!/bin/bash +################################################## +################################################## +# +# +# +# cctop: check memory usage on multiple servers +# usage: cctop -h +# +# +# +# SSH into each server and print total CPU +# usage (%), total MEM usage (%) and pids +# which are using MEM >=MEMVAL. CPU usage +# shows instantaneous usage (i.e. not always accurate) +# and uses mpstat 1 1. +# +# Users should set up passwordless access to +# all servers being SSHd into via: +# +# ssh-keygen -t rsa +# ssh-copy-id -i ~/.ssh/id_rsa.pub user@remote-system +# +# Add following to .bashrc: +# +# alias cctop='/dir/to/cctop.sh' +# +# +# Eytan Rocheta +# erocheta@gmail.com +# Version: 1.6 +# Date: 9/06/16 +# +# "I think I sprained my brain" -ZZ Top +################################################## +################################################## + +# Show pids which are using >= this % total memory (change with -m option) +mem_val=0.25 + +# Default username (change with -u option) +username="$USER" + +# Default domain (change with -d option) +domain=".ccrc.unsw.edu.au" + +################################################## +################################################## + +# Server info +slist_A="squall maelstrom blizzard monsoon" +ncpu_A=16 +nmem_A=256 + +slist_B="cyclone hurricane typhoon" +ncpu_B=12 +nmem_B=96 + +################################################## + +function usage +{ + echo "" + echo " Check memory usage on multiple CCRC servers." + echo " Usage: cctop [-u zID] [-m mem_val] " + echo " e.g. cctop -u zID 2 squall 4 -o" + echo " Missing server argument will default to new servers" + echo "" + echo " -u, zID Change user ID" + echo " -m, 0.25 Show PIDs using m% of memory" + echo " (0<=m<=100) default m=0.25" + echo " -a, --all Show all servers" + echo " -n, --new Show new servers" + echo " -o, --old Show old servers" + echo " -h, --help Show this help" + echo "" + echo " Option includes individual server numbers or names:" + echo " 1: squall 5: cyclone" + echo " 2: maelstrom 6: hurricane" + echo " 3: blizzard 7: typhoon" + echo " 4: monsoon " + echo "" + echo " Output:" + echo " Name | zID | STATUS | PID | CPU | MEM | MEM % | Runtine (hrs) | Program" +} + +################################################# + +function print_mem +{ + local server n_cpus n_mem cpu_percent mem_load mem_percent st + local fga fgs n1 n2 len i top_flag + for server in $server_list + do + host=$username"@"$server$domain + #TODO ^^ doesnt work when running from old servers - capilatising issue + printf '\e[1;34m%-20s\e[m' "${server^^}" #blue + + # select n_cpus and n_mem based on server groups + if [[ "${slist_A/$server}" != "${slist_A}" ]];then + n_cpus="$ncpu_A" + n_mem="$nmem_A" + top_flag=" -Mban1" # top -a sorts by mem usage but top -m for old servers. + elif [[ "${slist_B/$server}" != "${slist_B}" ]];then + n_cpus="$ncpu_B" + n_mem="$nmem_B" + top_flag=" -Mbmn1" + else + echo "Error with choice of server or server lists. Exiting" + exit 1 + fi + + # SSH in and get info: CPU, MEM, TOP, FINGER + cpu_percent=$(ssh "$host" mpstat 1 1 | grep "Average:" | awk '{print $3}') + mem_load=$(ssh "$host" free -g | grep "+ buffers/cache" | awk '{print $3}') + # 2"x" here so that only one zid field is converted to names below + st=$(ssh "$host" top "$top_flag" | sed '1,6d' | awk ' $10>="'"$mem_val"'" { \ + print $2"x",$2,$8,$1,$9,$5,$10,$11/60,$12 }' ) + mem_percent=$(bc <<< "scale=3;("$mem_load"/"$n_mem")*100") + fga=$(echo "$st" | awk ' match($2, /z/){print $2} ') #only finger strings start "z" + fgs=$(ssh "$host" finger $fga | grep "Name") # unfortunately removes duplicates + n1=($(echo "$fgs" | awk ' {print $2"x"}')) #an array of zIDs with an appended "x" + n2=($(echo "$fgs" | awk ' {print $4$5}')) # array of names + len=$(echo ${n1[*]} | wc -w) + for i in $(seq 0 $len); do #swap out zIDs with names + st=${st//${n1[i]}/${n2[i]}} + done + + echo -n " " + # print total CPU and MEM usage with colors + if [ $(printf "%.0f" "$cpu_percent") -ge 70 ]; then + printf '%s \e[1;31m%.4s%s\e[0m' "CPU:" $cpu_percent "%" #bold red + elif [ $(printf "%.0f" "$cpu_percent") -ge 50 ]; then + printf '%s \e[31m%.4s%s\e[0m' "CPU:" $cpu_percent "%" #light red + else + printf '%s \e[1;32m%.4s%s\e[0m' "CPU:" $cpu_percent "%" #green + fi + echo -n " " + if [ $(printf "%.0f" "$mem_percent") -ge 70 ]; then + printf '%s \e[1;31m%.4s%s\e[0m\n' "MEM:" $mem_percent "%" #bold red + elif [ $(printf "%.0f" "$mem_percent") -ge 50 ]; then + printf '%s \e[31m%.4s%s\e[0m\n' "MEM:" $mem_percent "%" #light red + else + printf '%s \e[1;32m%.4s%s\e[0m\n' "MEM:" $mem_percent "%" #green + fi + + if [ -n "$st" ]; then #if there are no values do not print empty space + printf "%-16.16s %s %-1s %-6.6s %-6.6s %-6.6s %-5.5s %-4.4s"hrs" %s\n" $st + fi + + echo "-----------------------------------------------------------------------------" + done +} + +################################################## + +function main +{ + if [[ "$#" -eq 0 ]]; then + echo "" + echo "-----------------------------------------------------------------------------" + server_list="$slist_A" + print_mem + echo "" + exit + else + while [[ "$1" != "" ]]; do + case "$1" in + -h | --help ) + usage + exit + ;; + -u | -user ) shift + username="$1" + ;; + -m | -mem ) shift + mem_val="$1" + ;; + -d | -dom ) shift + domain="$1" + ;; + 1 | squall ) server_list="$server_list squall" + ;; + 2 | maelstrom ) server_list="$server_list maelstrom" + ;; + 3 | blizzard ) server_list="$server_list blizzard" + ;; + 4 | monsoon ) server_list="$server_list monsoon" + ;; + 5 | cyclone ) server_list="$server_list cyclone" + ;; + 6 | hurricane ) server_list="$server_list hurricane" + ;; + 7 | typhoon ) server_list="$server_list typhoon" + ;; + -n | --new ) server_list="$server_list $slist_A" + ;; + -o | --old ) server_list="$server_list $slist_B" + ;; + -a | --all ) server_list="$server_list $slist_A $slist_B" + ;; + * ) usage + exit 1 + ;; + esac + shift + done + + if [[ -z "$server_list" ]]; then # if server_list is still empty + server_list="$server_list $slist_A" #(i.e. only opt -m or -u used) + fi #set server_list to default + echo "" + echo "-----------------------------------------------------------------------------" + print_mem #print output with given options + echo "" + + fi +} + +main "$@" + From 9213bc3e74284e10f33abcf9051f2c39978f9bdc Mon Sep 17 00:00:00 2001 From: naught101 Date: Thu, 21 Sep 2017 14:45:58 +1000 Subject: [PATCH 2/4] Move to better name, make executable --- shell/{cctop.sh => ccrc-top.sh} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename shell/{cctop.sh => ccrc-top.sh} (100%) mode change 100644 => 100755 diff --git a/shell/cctop.sh b/shell/ccrc-top.sh old mode 100644 new mode 100755 similarity index 100% rename from shell/cctop.sh rename to shell/ccrc-top.sh From 377b59dc830b0a37170c616d012de8b575ea4e36 Mon Sep 17 00:00:00 2001 From: naught101 Date: Thu, 21 Sep 2017 15:02:32 +1000 Subject: [PATCH 3/4] Update to work with replaced servers --- shell/ccrc-top.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/shell/ccrc-top.sh b/shell/ccrc-top.sh index 421643c..1a93e87 100755 --- a/shell/ccrc-top.sh +++ b/shell/ccrc-top.sh @@ -52,9 +52,9 @@ slist_A="squall maelstrom blizzard monsoon" ncpu_A=16 nmem_A=256 -slist_B="cyclone hurricane typhoon" -ncpu_B=12 -nmem_B=96 +slist_B="cyclone hurricane typhoon" +ncpu_B=16 +nmem_B=256 ################################################## @@ -104,7 +104,7 @@ function print_mem elif [[ "${slist_B/$server}" != "${slist_B}" ]];then n_cpus="$ncpu_B" n_mem="$nmem_B" - top_flag=" -Mbmn1" + top_flag=" -bn1" else echo "Error with choice of server or server lists. Exiting" exit 1 @@ -112,10 +112,10 @@ function print_mem # SSH in and get info: CPU, MEM, TOP, FINGER cpu_percent=$(ssh "$host" mpstat 1 1 | grep "Average:" | awk '{print $3}') - mem_load=$(ssh "$host" free -g | grep "+ buffers/cache" | awk '{print $3}') + mem_load=$(ssh "$host" free -g | grep "^Mem:" | awk '{print $4}') # 2"x" here so that only one zid field is converted to names below - st=$(ssh "$host" top "$top_flag" | sed '1,6d' | awk ' $10>="'"$mem_val"'" { \ - print $2"x",$2,$8,$1,$9,$5,$10,$11/60,$12 }' ) + st=$(ssh "$host" top "$top_flag" | sed '1,7d' | awk ' $10>="'"$mem_val"'" { \ + print $2"x",$2,$8,$1,$9,$5,$10,$11/60,$12"\n" }' ) mem_percent=$(bc <<< "scale=3;("$mem_load"/"$n_mem")*100") fga=$(echo "$st" | awk ' match($2, /z/){print $2} ') #only finger strings start "z" fgs=$(ssh "$host" finger $fga | grep "Name") # unfortunately removes duplicates From f64bb4c12770270c41fd06c5cf535e10e7de1a9f Mon Sep 17 00:00:00 2001 From: naught101 Date: Thu, 21 Sep 2017 15:14:31 +1000 Subject: [PATCH 4/4] Whitespace and name clean up --- shell/ccrc-top.sh | 274 +++++++++++++++++++++++----------------------- 1 file changed, 137 insertions(+), 137 deletions(-) diff --git a/shell/ccrc-top.sh b/shell/ccrc-top.sh index 1a93e87..7faeaef 100755 --- a/shell/ccrc-top.sh +++ b/shell/ccrc-top.sh @@ -1,29 +1,24 @@ #!/bin/bash ################################################## -################################################## -# -# -# -# cctop: check memory usage on multiple servers -# usage: cctop -h # +# ccrc-top: check memory usage on multiple servers +# usage: ccrc-top.sh -h # -# -# SSH into each server and print total CPU -# usage (%), total MEM usage (%) and pids -# which are using MEM >=MEMVAL. CPU usage +# SSH into each server and print total CPU +# usage (%), total MEM usage (%) and pids +# which are using MEM >=MEMVAL. CPU usage # shows instantaneous usage (i.e. not always accurate) # and uses mpstat 1 1. # -# Users should set up passwordless access to +# Users should set up passwordless access to # all servers being SSHd into via: # # ssh-keygen -t rsa # ssh-copy-id -i ~/.ssh/id_rsa.pub user@remote-system # -# Add following to .bashrc: +# Add following to .bashrc: # -# alias cctop='/dir/to/cctop.sh' +# alias ccrc-top.sh='/dir/to/ccrc-top.sh' # # # Eytan Rocheta @@ -33,10 +28,9 @@ # # "I think I sprained my brain" -ZZ Top ################################################## -################################################## # Show pids which are using >= this % total memory (change with -m option) -mem_val=0.25 +mem_val=0.25 # Default username (change with -u option) username="$USER" @@ -44,12 +38,11 @@ username="$USER" # Default domain (change with -d option) domain=".ccrc.unsw.edu.au" -################################################## ################################################## # Server info -slist_A="squall maelstrom blizzard monsoon" -ncpu_A=16 +slist_A="squall maelstrom blizzard monsoon" +ncpu_A=16 nmem_A=256 slist_B="cyclone hurricane typhoon" @@ -62,17 +55,17 @@ function usage { echo "" echo " Check memory usage on multiple CCRC servers." - echo " Usage: cctop [-u zID] [-m mem_val] " - echo " e.g. cctop -u zID 2 squall 4 -o" + echo " Usage: ccrc-top.sh [-u zID] [-m mem_val] " + echo " e.g. ccrc-top.sh -u zID 2 squall 4 -o" echo " Missing server argument will default to new servers" echo "" - echo " -u, zID Change user ID" - echo " -m, 0.25 Show PIDs using m% of memory" + echo " -u, zID Change user ID" + echo " -m, 0.25 Show PIDs using m% of memory" echo " (0<=m<=100) default m=0.25" - echo " -a, --all Show all servers" - echo " -n, --new Show new servers" - echo " -o, --old Show old servers" - echo " -h, --help Show this help" + echo " -a, --all Show all servers" + echo " -n, --new Show new servers" + echo " -o, --old Show old servers" + echo " -h, --help Show this help" echo "" echo " Option includes individual server numbers or names:" echo " 1: squall 5: cyclone" @@ -92,63 +85,70 @@ function print_mem local fga fgs n1 n2 len i top_flag for server in $server_list do - host=$username"@"$server$domain - #TODO ^^ doesnt work when running from old servers - capilatising issue - printf '\e[1;34m%-20s\e[m' "${server^^}" #blue - - # select n_cpus and n_mem based on server groups - if [[ "${slist_A/$server}" != "${slist_A}" ]];then - n_cpus="$ncpu_A" - n_mem="$nmem_A" - top_flag=" -Mban1" # top -a sorts by mem usage but top -m for old servers. - elif [[ "${slist_B/$server}" != "${slist_B}" ]];then - n_cpus="$ncpu_B" - n_mem="$nmem_B" - top_flag=" -bn1" - else - echo "Error with choice of server or server lists. Exiting" - exit 1 - fi - - # SSH in and get info: CPU, MEM, TOP, FINGER - cpu_percent=$(ssh "$host" mpstat 1 1 | grep "Average:" | awk '{print $3}') - mem_load=$(ssh "$host" free -g | grep "^Mem:" | awk '{print $4}') - # 2"x" here so that only one zid field is converted to names below - st=$(ssh "$host" top "$top_flag" | sed '1,7d' | awk ' $10>="'"$mem_val"'" { \ + host=$username"@"$server$domain + # TODO ^^ doesnt work when running from old servers - capilatising issue + printf '\e[1;34m%-20s\e[m' "${server^^}" # blue + + # select n_cpus and n_mem based on server groups + if [[ "${slist_A/$server}" != "${slist_A}" ]];then + n_cpus="$ncpu_A" + n_mem="$nmem_A" + top_flag=" -Mban1" # top -a sorts by mem usage but top -m for old servers. + elif [[ "${slist_B/$server}" != "${slist_B}" ]];then + n_cpus="$ncpu_B" + n_mem="$nmem_B" + top_flag=" -bn1" + else + echo "Error with choice of server or server lists. Exiting" + exit 1 + fi + + # SSH in and get info: CPU, MEM, TOP, FINGER + # CPU: + cpu_percent=$(ssh "$host" mpstat 1 1 | grep "Average:" | awk '{print $3}') + + # Memory: + mem_load=$(ssh "$host" free -g | grep "^Mem:" | awk '{print $4}') + + # Top: + # 2"x" here so that only one zid field is converted to names below + st=$(ssh "$host" top "$top_flag" | sed '1,7d' | awk ' $10>="'"$mem_val"'" { \ print $2"x",$2,$8,$1,$9,$5,$10,$11/60,$12"\n" }' ) - mem_percent=$(bc <<< "scale=3;("$mem_load"/"$n_mem")*100") - fga=$(echo "$st" | awk ' match($2, /z/){print $2} ') #only finger strings start "z" - fgs=$(ssh "$host" finger $fga | grep "Name") # unfortunately removes duplicates - n1=($(echo "$fgs" | awk ' {print $2"x"}')) #an array of zIDs with an appended "x" - n2=($(echo "$fgs" | awk ' {print $4$5}')) # array of names - len=$(echo ${n1[*]} | wc -w) - for i in $(seq 0 $len); do #swap out zIDs with names + mem_percent=$(bc <<< "scale=3;("$mem_load"/"$n_mem")*100") + + # Usernames + fga=$(echo "$st" | awk ' match($2, /z/){print $2} ') # only finger strings start "z" + fgs=$(ssh "$host" finger $fga | grep "Name") # unfortunately removes duplicates + n1=($(echo "$fgs" | awk ' {print $2"x"}')) # an array of zIDs with an appended "x" + n2=($(echo "$fgs" | awk ' {print $4$5}')) # array of names + len=$(echo ${n1[*]} | wc -w) + for i in $(seq 0 $len); do # swap out zIDs with names st=${st//${n1[i]}/${n2[i]}} - done - - echo -n " " - # print total CPU and MEM usage with colors - if [ $(printf "%.0f" "$cpu_percent") -ge 70 ]; then - printf '%s \e[1;31m%.4s%s\e[0m' "CPU:" $cpu_percent "%" #bold red - elif [ $(printf "%.0f" "$cpu_percent") -ge 50 ]; then - printf '%s \e[31m%.4s%s\e[0m' "CPU:" $cpu_percent "%" #light red - else - printf '%s \e[1;32m%.4s%s\e[0m' "CPU:" $cpu_percent "%" #green - fi - echo -n " " - if [ $(printf "%.0f" "$mem_percent") -ge 70 ]; then - printf '%s \e[1;31m%.4s%s\e[0m\n' "MEM:" $mem_percent "%" #bold red - elif [ $(printf "%.0f" "$mem_percent") -ge 50 ]; then - printf '%s \e[31m%.4s%s\e[0m\n' "MEM:" $mem_percent "%" #light red - else - printf '%s \e[1;32m%.4s%s\e[0m\n' "MEM:" $mem_percent "%" #green - fi - - if [ -n "$st" ]; then #if there are no values do not print empty space - printf "%-16.16s %s %-1s %-6.6s %-6.6s %-6.6s %-5.5s %-4.4s"hrs" %s\n" $st - fi - - echo "-----------------------------------------------------------------------------" + done + + echo -n " " + # print total CPU and MEM usage with colors + if [ $(printf "%.0f" "$cpu_percent") -ge 70 ]; then + printf '%s \e[1;31m%.4s%s\e[0m' "CPU:" $cpu_percent "%" # bold red + elif [ $(printf "%.0f" "$cpu_percent") -ge 50 ]; then + printf '%s \e[31m%.4s%s\e[0m' "CPU:" $cpu_percent "%" # light red + else + printf '%s \e[1;32m%.4s%s\e[0m' "CPU:" $cpu_percent "%" # green + fi + echo -n " " + if [ $(printf "%.0f" "$mem_percent") -ge 70 ]; then + printf '%s \e[1;31m%.4s%s\e[0m\n' "MEM:" $mem_percent "%" # bold red + elif [ $(printf "%.0f" "$mem_percent") -ge 50 ]; then + printf '%s \e[31m%.4s%s\e[0m\n' "MEM:" $mem_percent "%" # light red + else + printf '%s \e[1;32m%.4s%s\e[0m\n' "MEM:" $mem_percent "%" # green + fi + + if [ -n "$st" ]; then # if there are no values do not print empty space + printf "%-16.16s %s %-1s %-6.6s %-6.6s %-6.6s %-5.5s %-4.4s"hrs" %s\n" $st + fi + + echo "-----------------------------------------------------------------------------" done } @@ -157,65 +157,65 @@ function print_mem function main { if [[ "$#" -eq 0 ]]; then - echo "" - echo "-----------------------------------------------------------------------------" - server_list="$slist_A" - print_mem - echo "" - exit + echo "" + echo "-----------------------------------------------------------------------------" + server_list="$slist_A" + print_mem + echo "" + exit else - while [[ "$1" != "" ]]; do - case "$1" in - -h | --help ) - usage - exit - ;; - -u | -user ) shift - username="$1" - ;; - -m | -mem ) shift - mem_val="$1" - ;; - -d | -dom ) shift - domain="$1" - ;; - 1 | squall ) server_list="$server_list squall" - ;; - 2 | maelstrom ) server_list="$server_list maelstrom" - ;; - 3 | blizzard ) server_list="$server_list blizzard" - ;; - 4 | monsoon ) server_list="$server_list monsoon" - ;; - 5 | cyclone ) server_list="$server_list cyclone" - ;; - 6 | hurricane ) server_list="$server_list hurricane" - ;; - 7 | typhoon ) server_list="$server_list typhoon" - ;; - -n | --new ) server_list="$server_list $slist_A" - ;; - -o | --old ) server_list="$server_list $slist_B" - ;; - -a | --all ) server_list="$server_list $slist_A $slist_B" - ;; - * ) usage - exit 1 - ;; - esac - shift - done - - if [[ -z "$server_list" ]]; then # if server_list is still empty - server_list="$server_list $slist_A" #(i.e. only opt -m or -u used) - fi #set server_list to default - echo "" - echo "-----------------------------------------------------------------------------" - print_mem #print output with given options - echo "" + while [[ "$1" != "" ]]; do + case "$1" in + -h | --help ) + usage + exit + ;; + -u | -user ) shift + username="$1" + ;; + -m | -mem ) shift + mem_val="$1" + ;; + -d | -dom ) shift + domain="$1" + ;; + 1 | squall ) server_list="$server_list squall" + ;; + 2 | maelstrom ) server_list="$server_list maelstrom" + ;; + 3 | blizzard ) server_list="$server_list blizzard" + ;; + 4 | monsoon ) server_list="$server_list monsoon" + ;; + 5 | cyclone ) server_list="$server_list cyclone" + ;; + 6 | hurricane ) server_list="$server_list hurricane" + ;; + 7 | typhoon ) server_list="$server_list typhoon" + ;; + -n | --new ) server_list="$server_list $slist_A" + ;; + -o | --old ) server_list="$server_list $slist_B" + ;; + -a | --all ) server_list="$server_list $slist_A $slist_B" + ;; + * ) usage + exit 1 + ;; + esac + shift + done + + if [[ -z "$server_list" ]]; then # if server_list is still empty + server_list="$server_list $slist_A" # (i.e. only opt -m or -u used) + fi # set server_list to default + echo "" + echo "-----------------------------------------------------------------------------" + print_mem # print output with given options + echo "" fi } -main "$@" +main "$@"