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