preflight 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/bin/bash
  2. ################################################################################
  3. #
  4. # HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #################################################################################
  18. declare -a arr0
  19. declare -a arr1
  20. declare -a arr2
  21. declare -a arrPIDName
  22. declare -a arrPIDPath
  23. declare -a arrDep
  24. if [ "${1:0:1}" == '-' ]; then
  25. if [ $# -lt 2 ]; then
  26. echo usage: $0 -p=PIDPath[,PIDPath,...] -n=PIDName[,PIDName,...] [-m=YES/NO] [-d=dependency,dependency,...]
  27. exit 1
  28. fi
  29. dfOption='-l'
  30. depAll=0
  31. for (( i=1;$i<=$#;i=$i+1 )) do
  32. case ${!i:0:3} in '-p=')
  33. IFS=, read -a arrPIDPath <<< "${!i:3}";;
  34. '-n=')
  35. IFS=, read -a arrPIDName <<< "${!i:3}";;
  36. '-d=')
  37. if [ "${!i:3}" == 'ALL' ]; then
  38. depAll=1
  39. else
  40. IFS=, read -a arrDep <<< "${!i:3}"
  41. fi
  42. ;;
  43. '-m=')
  44. if [ "${!i:3}" == 'YES' ]; then
  45. dfOption='-a'
  46. fi
  47. ;;
  48. *)
  49. echo "wrong augument:${!i}";;
  50. esac
  51. done
  52. lPath=${#arrPIDPath[*]}
  53. if [ ${#arrPIDName[@]} -gt $lPath ]; then
  54. lastPath=${arrPIDPath[lPath-1]}
  55. for (( i = $lPath; i < ${#arrPIDName[@]}; i++)); do
  56. arrPIDPath[i]=$lastPath
  57. done
  58. fi
  59. #find machineInfo and output:
  60. checkSwapLine=`free | head -n 3 | tail -n 1 | awk '{print $1}'`
  61. if ! [ "$checkSwapLine" = "Swap:" ]; then
  62. mem=`free | head -n 3 | tail -n 1 | awk '{print $3,$4}'`
  63. else
  64. mem=`free | head -n 2 | tail -n 1 | awk '{print $2-$7,$7}'`
  65. fi
  66. cpu=`vmstat 1 2 | tail -n 1 | awk '{print $15}'`
  67. echo CPU-Idle: $cpu%
  68. cuptime=`uptime | cut -f 1,2 -d ','`
  69. echo ComputerUpTime: $cuptime
  70. echo ---SpaceUsedAndFree---
  71. swap=`free | tail -n 1 | awk '{print $3,$4}'`
  72. echo Physical Memory: $mem
  73. echo Swap: $swap
  74. i=0
  75. for name in `df $dfOption | tail -n +2 | awk '{if(NF>=4) print $NF}'`; do
  76. arr0[i]=$name
  77. i=$((i+1))
  78. done
  79. i=0
  80. for used in `df $dfOption | tail -n +2 | awk '{if(NF>=4) print $(NF-3)}'`; do
  81. arr1[i]=$used
  82. i=$((i+1))
  83. done
  84. i=0
  85. for free in `df $dfOption | tail -n +2 | awk '{if(NF>=4) print $(NF-2)}'`; do
  86. arr2[i]=$free
  87. i=$((i+1))
  88. done
  89. for j in $(seq 0 $((i-1))); do
  90. echo ${arr0[$j]}: ${arr1[$j]} ${arr2[$j]}
  91. done
  92. # foreach PIDName in arrPIDName do: find process info and output
  93. echo ---ProcessList1---
  94. i=0
  95. for (( i = 0; i < ${#arrPIDName[@]}; i++)); do
  96. full=${arrPIDPath[i]}/${arrPIDName[i]}.pid
  97. pid=`cat $full`
  98. elapsed=`ps -o etime $pid | sed -e '1d' | tail -n 1`
  99. echo Process ${arrPIDPath[i]}/${arrPIDName[i]}: $pid $elapsed
  100. done
  101. #read other processes
  102. echo ---ProcessList2---
  103. if [[ $depAll -gt 0 ]]; then
  104. ps -el | tail -n +1 | awk '{print $4,$14;}'
  105. else
  106. echo 'NAME:PID CMD'
  107. for (( i = 0; i < ${#arrDep[@]}; i++)); do
  108. aDep=${arrDep[i]}
  109. aDepInit=init_$aDep
  110. #depCHK=$(ps aux | grep $aDep | grep -v grep | grep -v $aDepInit | wc -l)
  111. depCHK=$(ps -el | grep $aDep | grep -v grep | grep -v $aDepInit | awk '{print $4,$14;}')
  112. echo $aDep:$depCHK
  113. done
  114. fi
  115. else
  116. i=0
  117. if [ $# -lt 2 ]; then
  118. echo usage: $0 dir command
  119. exit 1
  120. fi
  121. #full=$1/$2
  122. #pid=`ps -ef | grep $full | grep -v grep | awk '{print $2}'`
  123. full=$1/$2.pid
  124. pid=`cat $full`
  125. echo ProcessID: $pid
  126. elapsed=`ps -o etime $pid | sed -e '1d' | tail -n 1`
  127. echo ProcessUpTime: $elapsed
  128. mem=`free | head -n 3 | tail -n 1 | awk '{print $3,$4}'`
  129. cpu=`vmstat 1 2 | tail -n 1 | awk '{print $15}'`
  130. echo CPU-Idle: $cpu%
  131. cuptime=`uptime | cut -f 1,2 -d ','`
  132. echo ComputerUpTime: $cuptime
  133. echo ---SpaceUsedAndFree---
  134. swap=`free | tail -n 1 | awk '{print $3,$4}'`
  135. echo Physical Memory: $mem
  136. echo Swap: $swap
  137. for name in `df -l | tail -n +2 | awk '{if(NF>=4) print $NF}'`
  138. do
  139. arr0[i]=$name
  140. i=$((i+1))
  141. done
  142. i=0
  143. for used in `df -l | tail -n +2 | awk '{if(NF>=4) print $(NF-3)}'`
  144. do
  145. arr1[i]=$used
  146. i=$((i+1))
  147. done
  148. i=0
  149. for free in `df -l | tail -n +2 | awk '{if(NF>=4) print $(NF-2)}'`
  150. do
  151. arr2[i]=$free
  152. i=$((i+1))
  153. done
  154. for j in $(seq 0 $((i-1)))
  155. do
  156. echo ${arr0[$j]}: ${arr1[$j]} ${arr2[$j]}
  157. done
  158. fi