riteshchikatwar

Untitled

Nov 2nd, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. #! /bin/bash
  2.  
  3. set -e
  4. ME=`basename $0`
  5.  
  6. # TODO: allow parameter defaults to be modified via command-line
  7. # options rather than by editing script
  8. # parameters section: BEGIN
  9.  
  10. # put list of hosts here. passwordless ssh to these should work
  11. HOSTS="host1 host2 host3"
  12.  
  13. MON_INTRVL=10
  14. MON_DURATION=600
  15.  
  16. # output files are collected into a sub-dir here
  17. DEST_DIR="."
  18.  
  19. # parameters section: END
  20.  
  21. # commands and output-file-prefixes
  22. CMDS="sar iostat top"
  23. CMD_FILES="sar iostat top"
  24.  
  25. function print_usage
  26. {
  27. echo "Usage: $ME"
  28. echo " $ME {check|start|stop|cleanup}"
  29. }
  30.  
  31.  
  32. function ssh_check
  33. {
  34. echo
  35. echo "checking ssh connectivity... "
  36. for h in ${HOSTS}; do
  37. echo -n "${h}: "
  38. ssh -o ConnectTimeout=20 ${h} "hostname"
  39. done
  40. echo "passed!"
  41. echo
  42. }
  43.  
  44. function start_commands
  45. {
  46. echo
  47. echo "starting stats collection on hosts..."
  48. for h in ${HOSTS}; do
  49. echo -n "${h}: "
  50. ssh ${h} "nohup sar -n DEV -BdpqruW $MON_INTRVL > /tmp/sar.${h}.txt 2>&1 < /dev/null &"
  51. ssh ${h} "nohup iostat -dkNtx $MON_INTRVL > /tmp/iostat.${h}.txt 2>&1 < /dev/null &"
  52. ssh ${h} "nohup top -bH -d $MON_INTRVL > /tmp/top.${h}.txt 2>&1 < /dev/null &"
  53. echo "done"
  54. done
  55. echo
  56. }
  57.  
  58. function stop_commands
  59. {
  60. echo
  61. echo "stopping stats collection on... "
  62. for h in ${HOSTS}; do
  63. echo -n "${h}: "
  64. for cmd in ${CMDS}; do
  65. ssh ${h} "pkill -x ${cmd}"
  66. done
  67. echo "done"
  68. done
  69. echo
  70. }
  71.  
  72. function stop_n_gather
  73. {
  74. local ts
  75. local stats_dir
  76.  
  77. stop_commands
  78.  
  79. ts=`date +"%F_%s"`
  80. stats_dir="${DEST_DIR}/run_${ts}"
  81. mkdir ${stats_dir}
  82.  
  83. echo "gathering stats from... "
  84. for h in ${HOSTS}; do
  85. echo -n "${h}: "
  86. for prfx in ${CMD_FILES}; do
  87. scp -q ${h}:/tmp/${prfx}.${h}.txt ${stats_dir}
  88. done
  89. echo "done"
  90. done
  91. echo "stats gathered in directory: ${stats_dir}"
  92. }
  93.  
  94. if [ $# -eq 0 ]; then
  95. :
  96. elif [ "$1" = "check" ]; then
  97. ssh_check
  98. exit 0
  99. elif [ "$1" = "start" ]; then
  100. start_commands
  101. exit 0
  102. elif [ "$1" = "stop" ]; then
  103. stop_n_gather
  104. exit 0
  105. elif [ "$1" = "cleanup" ]; then
  106. set +e
  107. stop_commands
  108. exit 0
  109. else
  110. print_usage
  111. exit 1
  112. fi
  113.  
  114. start_commands
  115.  
  116. trap 'stop_n_gather' SIGINT
  117.  
  118. echo -n "monitoring for ${MON_DURATION} seconds:"
  119. date
  120. sleep ${MON_DURATION}
  121. echo -n "monitoring done:"
  122. date
  123.  
  124. stop_n_gather
Advertisement
Add Comment
Please, Sign In to add comment