hue_mem_cron_cm.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/bin/bash
  2. #This script will check CM to see if Hue is using too much memory
  3. #then will restart if necessary.
  4. CM_HOSTNAME="cdh53-1.qa.test.com"
  5. CM_PORT="7180"
  6. CM_USERNAME="admin"
  7. CM_PASSWORD="admin"
  8. KILL_ME=5000 #This is the number of MB at which it will kill.
  9. #Starting with 5000(5gb)
  10. VERBOSE=true #true then this writes out the proc info each time it runs, leave blank
  11. #to only write out when we kill the process
  12. LOG_FILE=/var/log/hue_mem_cron.log
  13. ROTATE_SIZE=10 #MB before rotating, size in MB before rotating log to .1, we only keep
  14. #2 log files, so 20MB max
  15. TMP_LOCATION=/tmp/hue_mem_cron
  16. main()
  17. {
  18. DATE=`date '+%Y%m%d-%H%M'`
  19. YEAR=`date '+%Y'`
  20. MONTH=`date '+%m'`
  21. DAY=`date '+%d'`
  22. HOUR=`date '+%H'`
  23. MINUTE=`date '+%M'`
  24. YEAR_PRIOR=`date --date='1 minutes ago' '+%Y'`
  25. MONTH_PRIOR=`date --date='1 minutes ago' '+%m'`
  26. DAY_PRIOR=`date --date='1 minutes ago' '+%d'`
  27. HOUR_PRIOR=`date --date='1 minutes ago' '+%H'`
  28. MINUTE_PRIOR=`date --date='1 minutes ago' '+%M'`
  29. MEM_JSON_FILE=${TMP_LOCATION}/mem.json
  30. MB_BYTES="1048576"
  31. mkdir -p ${TMP_LOCATION}
  32. if [[ -f ${LOG_FILE} ]]
  33. then
  34. LOG_SIZE=`du -sm ${LOG_FILE} | awk '{print $1}'`
  35. if [ ${LOG_SIZE} -gt ${ROTATE_SIZE} ]
  36. then
  37. mv ${LOG_FILE} ${LOG_FILE}.1
  38. fi
  39. fi
  40. MEM_API_URL="/api/v6/timeseries?query=select+mem_rss+where+roleType+%3D+HUE_SERVER&contentType=application%2Fjson&from=${YEAR_PRIOR}-${MONTH_PRIOR}-${DAY_PRIOR}T${HOUR_PRIOR}%3A${MINUTE_PRIOR}%3A00.000Z&to=${YEAR}-${MONTH}-${DAY}T${HOUR}%3A${MINUTE}%3A00.000Z"
  41. #Get memory usage for all Hue roles:
  42. curl -X GET -u ${CM_USERNAME}:${CM_PASSWORD} -i -o ${MEM_JSON_FILE} "http://${CM_HOSTNAME}:${CM_PORT}${MEM_API_URL}"
  43. while read -r LINE
  44. do
  45. if [[ ${LINE} =~ .*clusterName* ]]
  46. then
  47. CLUSTERNAME=`echo ${LINE} | awk -F\" '{print $4}'`
  48. fi
  49. if [[ ${LINE} =~ .*serviceName* ]]
  50. then
  51. SERVICENAME=`echo ${LINE} | awk -F\" '{print $4}'`
  52. fi
  53. if [[ ${LINE} =~ .*roleName* ]]
  54. then
  55. ROLENAME=`echo ${LINE} | awk -F\" '{print $4}'`
  56. fi
  57. if [[ ${LINE} =~ .*value* ]]
  58. then
  59. MEM=`echo ${LINE} | awk '{print $3}' | awk -F, '{print $1}'`
  60. MEM=`printf "%.f" $MEM` # convert from scientific to decimal
  61. MEM_MB=`expr ${MEM} / ${MB_BYTES}`
  62. debug "${DATE} - ROLENAME: ${ROLENAME} - MEM: ${MEM} - MEM_MB: ${MEM_MB}"
  63. if [ ${MEM_MB} -gt ${KILL_ME} ]
  64. then
  65. echo "${DATE} - Restart the Hue Process: Too much memory: ${MEM_MB} : ROLENAME: ${ROLENAME}" >> ${LOG_FILE}
  66. RESTART_API_URL="/api/v8/clusters/${CLUSTERNAME}/services/${SERVICENAME}/roleCommands/restart"
  67. curl -X POST -u ${CM_USERNAME}:${CM_PASSWORD} -i -H "content-type:application/json" -d "{\"items\" : [\"${ROLENAME}\"]}" "http://${CM_HOSTNAME}:${CM_PORT}${RESTART_API_URL}"
  68. exit 0
  69. fi
  70. fi
  71. done < <(cat ${MEM_JSON_FILE})
  72. }
  73. debug()
  74. {
  75. if [[ ! -z $VERBOSE ]]
  76. then
  77. echo "$1" >> ${LOG_FILE}
  78. fi
  79. }
  80. main "$@"