alarm.sh.in 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/bash
  2. #The health directory to put the alarms
  3. HEALTHDIR="@configdir_POST@/health.d/"
  4. #output directory
  5. OUTDIR="workdir/"
  6. #url to do download
  7. QUERY="/api/v1/alarms?active"
  8. MURL="http://localhost:19999$QUERY"
  9. #error messages
  10. RED='\033[0;31m'
  11. GREEN='\033[0;32m'
  12. NOCOLOR='\033[0m'
  13. MYCDIR="$(pwd)"
  14. CONFFILE="$MYCDIR/netdata.conf"
  15. change_alarm_file() {
  16. if [ -f "$1" ]; then
  17. rm "$1"
  18. fi
  19. #copy keeping the permissions
  20. cp -a "$2" "$3"
  21. }
  22. netdata_test_download() {
  23. OPT="-e"
  24. if [ "$3" == "I" ]; then
  25. OPT="-v"
  26. fi
  27. grep "HTTP/1.1 200 OK" "$1" 2>/dev/null 1>/dev/null
  28. TEST="$?"
  29. if [ "$TEST" -ne "0" ]; then
  30. echo -e "${RED} Error to get the alarms. ${NOCOLOR}"
  31. kill "$5"
  32. rm "$HEALTHDIR/ram.conf"
  33. exit 1
  34. fi
  35. COUNT=$(grep -w "\"last_repeat\":" "$2" | grep -c "$OPT" "\"0\"")
  36. if [ "$COUNT" -eq "0" ]; then
  37. echo -e "${RED} Netdata gave an unexpected result when alarm repetition is $4 ${NOCOLOR}"
  38. killall "$5"
  39. rm "$HEALTHDIR/ram.conf"
  40. exit 1
  41. fi
  42. echo -e "${GREEN} I got the expected result ${NOCOLOR}"
  43. }
  44. get_the_logs() {
  45. curl -v -k --create-dirs -o "$OUTDIR/$1.out" "$MURL" 2> "$OUTDIR/$1.err"
  46. netdata_test_download "$OUTDIR/$1.err" "$OUTDIR/$1.out" "$2" "$3" "$4"
  47. }
  48. process_data() {
  49. SEC=120
  50. netdata -c "$CONFFILE" -D &
  51. NETDATAPID=$!
  52. echo -e "${NOCOLOR}Sleeping during $SEC seconds to create alarm entries"
  53. sleep $SEC
  54. get_the_logs "$1" "$2" "$3" "$NETDATAPID"
  55. kill $NETDATAPID
  56. }
  57. mkdir "$OUTDIR"
  58. CREATEDIR="$?"
  59. if [ "$CREATEDIR" -ne "0" ]; then
  60. echo -e "${RED}Cannot create the output directory, it already exists. The test will overwrite previous results. ${NOCOLOR}"
  61. fi
  62. change_alarm_file "./0" "ram_without_repetition.conf" "$HEALTHDIR/ram.conf"
  63. cp -a netdata.conf_without_repetition netdata.conf
  64. process_data "ram_without" "K" "not activated."
  65. rm netdata.conf
  66. change_alarm_file "$HEALTHDIR/ram.conf" "ram_with_repetition.conf" "$HEALTHDIR/ram.conf"
  67. cp -a netdata.conf_with_repetition netdata.conf
  68. process_data "ram_with" "I" "activated."
  69. rm netdata.conf
  70. echo -e "${GREEN} all the tests were successful ${NOCOLOR}"
  71. rm "$HEALTHDIR/ram.conf"
  72. rm -rf $OUTDIR