health-cmdapi-test.sh.in 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #!/usr/bin/env bash
  2. # shellcheck disable=SC1117,SC2034,SC2059,SC2086,SC2181
  3. NETDATA_VARLIB_DIR="@varlibdir_POST@"
  4. check () {
  5. sec=1
  6. echo -e " ${GRAY}Check: '${1}' in $sec sec"
  7. sleep $sec
  8. number=$RANDOM
  9. resp=$(curl -s "http://$URL/api/v1/alarms?all&$number")
  10. r=$(echo "${resp}" | \
  11. python3 -c "import sys, json; d=json.load(sys.stdin); \
  12. print(\
  13. d['alarms']['system.cpu.10min_cpu_usage']['disabled'], \
  14. d['alarms']['system.cpu.10min_cpu_usage']['silenced'] , \
  15. d['alarms']['system.cpu.10min_cpu_iowait']['disabled'], \
  16. d['alarms']['system.cpu.10min_cpu_iowait']['silenced'], \
  17. d['alarms']['system.load.load_trigger']['disabled'], \
  18. d['alarms']['system.load.load_trigger']['silenced'], \
  19. );" 2>&1)
  20. if [ $? -ne 0 ] ; then
  21. echo -e " ${RED}ERROR: Unexpected response stored in /tmp/resp-$number.json"
  22. echo "$resp" > /tmp/resp-$number.json
  23. err=$((err+1))
  24. iter=0
  25. elif [ "${r}" != "${2}" ] ; then
  26. echo -e " ${GRAY}WARNING: 'Got ${r}'. Expected '${2}'"
  27. iter=$((iter+1))
  28. if [ $iter -lt 10 ] ; then
  29. echo -e " ${GRAY}Repeating test "
  30. check "$1" "$2"
  31. else
  32. echo -e " ${RED}ERROR: 'Got ${r}'. Expected '${2}'"
  33. iter=0
  34. err=$((err+1))
  35. fi
  36. else
  37. echo -e " ${GREEN}Success"
  38. iter=0
  39. fi
  40. }
  41. cmd () {
  42. echo -e "${WHITE}Cmd '${1}'"
  43. echo -en " ${GRAY}Expecting '${2}' : "
  44. RESPONSE=$(curl -s "http://$URL/api/v1/manage/health?${1}" -H "X-Auth-Token: $TOKEN" 2>&1)
  45. if [ "${RESPONSE}" != "${2}" ] ; then
  46. echo -e "${RED}ERROR: Response '${RESPONSE}'"
  47. err=$((err+1))
  48. else
  49. echo -e "${GREEN}Success"
  50. fi
  51. }
  52. check_list() {
  53. RESPONSE=$(curl -s "http://$URL/api/v1/manage/health?cmd=LIST" -H "X-Auth-Token: $TOKEN" 2>&1)
  54. NAME="$1-list.json"
  55. echo $RESPONSE > $NAME
  56. diff $NAME expected_list/$NAME 1>/dev/null 2>&1
  57. if [ $? -eq 0 ]; then
  58. echo -e "${GREEN}Success: The list command got the correct answer for $NAME!"
  59. else
  60. echo -e "${RED}ERROR: the files $NAME and expected_list/$NAME does not match."
  61. exit 1
  62. fi
  63. }
  64. WHITE='\033[0;37m'
  65. RED='\033[0;31m'
  66. GREEN='\033[0;32m'
  67. GRAY='\033[0;37m'
  68. SETUP=0
  69. RESTART=0
  70. CLEANUP=0
  71. TEST=0
  72. URL="localhost:19999"
  73. err=0
  74. HEALTH_CMDAPI_MSG_AUTHERROR="Auth Error"
  75. HEALTH_CMDAPI_MSG_SILENCEALL="All alarm notifications are silenced"
  76. HEALTH_CMDAPI_MSG_DISABLEALL="All health checks are disabled"
  77. HEALTH_CMDAPI_MSG_RESET="All health checks and notifications are enabled"
  78. HEALTH_CMDAPI_MSG_DISABLE="Health checks disabled for alarms matching the selectors"
  79. HEALTH_CMDAPI_MSG_SILENCE="Alarm notifications silenced for alarms matching the selectors"
  80. HEALTH_CMDAPI_MSG_ADDED="Alarm selector added"
  81. HEALTH_CMDAPI_MSG_INVALID_KEY="Invalid key. Ignoring it."
  82. HEALTH_CMDAPI_MSG_STYPEWARNING="WARNING: Added alarm selector to silence/disable alarms without a SILENCE or DISABLE command."
  83. HEALTH_CMDAPI_MSG_NOSELECTORWARNING="WARNING: SILENCE or DISABLE command is ineffective without defining any alarm selectors."
  84. if [ -f "${NETDATA_VARLIB_DIR}/netdata.api.key" ] ;then
  85. read -r CORRECT_TOKEN < "${NETDATA_VARLIB_DIR}/netdata.api.key"
  86. else
  87. echo "${NETDATA_VARLIB_DIR}/netdata.api.key not found"
  88. exit 1
  89. fi
  90. # Set correct token
  91. TOKEN="${CORRECT_TOKEN}"
  92. # Test default state
  93. cmd "cmd=RESET" "$HEALTH_CMDAPI_MSG_RESET"
  94. check "Default State" "False False False False False False"
  95. check_list "RESET"
  96. # Test auth failure
  97. TOKEN="Wrong token"
  98. cmd "cmd=DISABLE ALL" "$HEALTH_CMDAPI_MSG_AUTHERROR"
  99. check "Default State" "False False False False False False"
  100. check_list "DISABLE_ALL_ERROR"
  101. # Set correct token
  102. TOKEN="${CORRECT_TOKEN}"
  103. # Test disable
  104. cmd "cmd=DISABLE ALL" "$HEALTH_CMDAPI_MSG_DISABLEALL"
  105. check "All disabled" "True False True False True False"
  106. check_list "DISABLE_ALL"
  107. # Reset
  108. cmd "cmd=RESET" "$HEALTH_CMDAPI_MSG_RESET"
  109. check "Default State" "False False False False False False"
  110. check_list "RESET"
  111. # Test silence
  112. cmd "cmd=SILENCE ALL" "$HEALTH_CMDAPI_MSG_SILENCEALL"
  113. check "All silenced" "False True False True False True"
  114. check_list "SILENCE_ALL"
  115. # Reset
  116. cmd "cmd=RESET" "$HEALTH_CMDAPI_MSG_RESET"
  117. check "Default State" "False False False False False False"
  118. check_list "RESET"
  119. # Add silencer by name
  120. printf -v resp "$HEALTH_CMDAPI_MSG_SILENCE\n$HEALTH_CMDAPI_MSG_ADDED"
  121. cmd "cmd=SILENCE&alarm=*10min_cpu_usage *load_trigger" "${resp}"
  122. check "Silence notifications for alarm1 and load_trigger" "False True False False False True"
  123. check_list "SILENCE_ALARM_CPU_USAGE_LOAD_TRIGGER"
  124. # Convert to disable health checks
  125. cmd "cmd=DISABLE" "$HEALTH_CMDAPI_MSG_DISABLE"
  126. check "Disable notifications for alarm1 and load_trigger" "True False False False True False"
  127. check_list "DISABLE"
  128. # Convert back to silence notifications
  129. cmd "cmd=SILENCE" "$HEALTH_CMDAPI_MSG_SILENCE"
  130. check "Silence notifications for alarm1 and load_trigger" "False True False False False True"
  131. check_list "SILENCE"
  132. # Add second silencer by name
  133. cmd "alarm=*10min_cpu_iowait" "$HEALTH_CMDAPI_MSG_ADDED"
  134. check "Silence notifications for alarm1,alarm2 and load_trigger" "False True False True False True"
  135. check_list "ALARM_CPU_IOWAIT"
  136. # Reset
  137. cmd "cmd=RESET" "$HEALTH_CMDAPI_MSG_RESET"
  138. check_list "RESET"
  139. # Add silencer by chart
  140. printf -v resp "$HEALTH_CMDAPI_MSG_DISABLE\n$HEALTH_CMDAPI_MSG_ADDED"
  141. cmd "cmd=DISABLE&chart=system.load" "${resp}"
  142. check "Default State" "False False False False True False"
  143. check_list "DISABLE_SYSTEM_LOAD"
  144. # Add silencer by context
  145. cmd "context=system.cpu" "$HEALTH_CMDAPI_MSG_ADDED"
  146. check "Default State" "True False True False True False"
  147. check_list "CONTEXT_SYSTEM_CPU"
  148. # Reset
  149. cmd "cmd=RESET" "$HEALTH_CMDAPI_MSG_RESET"
  150. check_list "RESET"
  151. # Add second condition to a selector (AND)
  152. printf -v resp "$HEALTH_CMDAPI_MSG_SILENCE\n$HEALTH_CMDAPI_MSG_ADDED"
  153. cmd "cmd=SILENCE&alarm=*10min_cpu_usage *load_trigger&chart=system.load" "${resp}"
  154. check "Silence notifications load_trigger" "False False False False False True"
  155. check_list "SILENCE_ALARM_CPU_USAGE"
  156. # Add second selector with two conditions
  157. cmd "alarm=*10min_cpu_usage *load_trigger&context=system.cpu" "$HEALTH_CMDAPI_MSG_ADDED"
  158. check "Silence notifications load_trigger" "False True False False False True"
  159. check_list "ALARM_CPU_USAGE"
  160. # Reset
  161. cmd "cmd=RESET" "$HEALTH_CMDAPI_MSG_RESET"
  162. check_list "RESET"
  163. # Add silence command
  164. cmd "cmd=SILENCE" "$HEALTH_CMDAPI_MSG_SILENCE"
  165. check "Silence family load" "False False False False False True"
  166. check_list "SILENCE_2"
  167. # Reset
  168. cmd "cmd=RESET" "$HEALTH_CMDAPI_MSG_RESET"
  169. check_list "RESET"
  170. # Add command without silencers
  171. printf -v resp "$HEALTH_CMDAPI_MSG_SILENCE\n$HEALTH_CMDAPI_MSG_NOSELECTORWARNING"
  172. cmd "cmd=SILENCE" "${resp}"
  173. check "Command with no selector" "False False False False False False"
  174. check_list "SILENCE_3"
  175. # Add hosts silencer
  176. cmd "hosts=*" "$HEALTH_CMDAPI_MSG_ADDED"
  177. check "Silence all hosts" "False True False True False True"
  178. check_list "HOSTS"
  179. # Reset
  180. cmd "cmd=RESET" "$HEALTH_CMDAPI_MSG_RESET"
  181. check_list "RESET"
  182. if [ $err -gt 0 ] ; then
  183. echo "$err error(s) found"
  184. exit 1
  185. fi