| 1 | #!/bin/bash |
| 2 | |
| 3 | #The health directory to put the alarms |
| 4 | HEALTHDIR="@configdir_POST@/health.d/" |
| 5 | |
| 6 | #the current time |
| 7 | OUTDIR="alarms" |
| 8 | QUERY="/api/v1/alarms?all" |
| 9 | MURL="http://localhost:19999$QUERY" |
| 10 | |
| 11 | #error messages |
| 12 | RED='\033[0;31m' |
| 13 | GREEN='\033[0;32m' |
| 14 | NOCOLOR='\033[0m' |
| 15 | |
| 16 | ALARMTEST="dev_dim_template" |
| 17 | |
| 18 | change_alarm_file() { |
| 19 | if [ -f "$1" ]; then |
| 20 | rm "$1" |
| 21 | fi |
| 22 | |
| 23 | #copy keeping the permissions |
| 24 | cp -a "$2" "$3" |
| 25 | } |
| 26 | |
| 27 | netdata_test_download() { |
| 28 | grep "HTTP/1.1 200 OK" "$1" 2>/dev/null 1>/dev/null |
| 29 | TEST="$?" |
| 30 | if [ "$TEST" -ne "0" ]; then |
| 31 | echo -e "${RED} Error to get the alarm log. ${NOCOLOR}" |
| 32 | exit 1 |
| 33 | fi |
| 34 | |
| 35 | TOTALARM=$(grep "$ALARMTEST" "$2" | grep name | cut -d: -f2 | grep -c "$ALARMTEST") |
| 36 | |
| 37 | if [ "$TOTALARM" -ne "$3" ]; then |
| 38 | echo -e "${RED} The number of actives alarms with the name $SYSTEMALARM is wrong ${NOCOLOR}" |
| 39 | exit 1 |
| 40 | fi |
| 41 | } |
| 42 | |
| 43 | get_the_logs() { |
| 44 | curl -v -k --create-dirs -o "$OUTDIR/$1.out" "$MURL" 2> "$OUTDIR/$1.err" |
| 45 | netdata_test_download "$OUTDIR/$1.err" "$OUTDIR/$1.out" "$2" |
| 46 | } |
| 47 | |
| 48 | process_data() { |
| 49 | netdata -D & |
| 50 | NETDATAPID=$! |
| 51 | echo -e "${NOCOLOR}Sleeping during 15 seconds to create alarms" |
| 52 | sleep 15 |
| 53 | kill $NETDATAPID |
| 54 | get_the_logs "$1" "$2" |
| 55 | } |
| 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 | |
| 63 | if [ -n "$1" ]; then |
| 64 | MURL="$1$QUERY" |
| 65 | fi |
| 66 | |
| 67 | change_alarm_file "./0" "system_cpu.conf.unique_alarm" "$HEALTHDIR/dim_double_without_template.conf" |
| 68 | process_data "double_without_template" 3 "$HEALTHDIR/dim_double_without_template.conf" |
| 69 | |
| 70 | change_alarm_file "$HEALTHDIR/dim_double_without_template.conf" "system_cpu.conf.alarm_foreach" "$HEALTHDIR/dim_foreach_without_template.conf" |
| 71 | process_data "foreach_without_template" 3 "$HEALTHDIR/dim_foreach_without_template.conf" |
| 72 | |
| 73 | change_alarm_file "$HEALTHDIR/dim_foreach_without_template.conf" "system_cpu.conf.alarm_foreach_sp" "$HEALTHDIR/dim_foreach_without_template_sp.conf" |
| 74 | process_data "foreach_without_template" 10 "$HEALTHDIR/dim_foreach_without_template_sp.conf" |
| 75 | |
| 76 | change_alarm_file "$HEALTHDIR/dim_foreach_without_template_sp.conf" "system_cpu.conf.template_alarm" "$HEALTHDIR/dim_double_with_template.conf" |
| 77 | process_data "double_with_template" 3 "$HEALTHDIR/dim_double_with_template.conf" |
| 78 | |
| 79 | change_alarm_file "$HEALTHDIR/dim_double_with_template.conf" "system_cpu.conf.template_foreach" "$HEALTHDIR/dim_foreach_with_template.conf" |
| 80 | process_data "foreach_with_template" 3 "$HEALTHDIR/dim_foreach_with_template.conf" |
| 81 | |
| 82 | change_alarm_file "$HEALTHDIR/dim_foreach_with_template.conf" "system_cpu.conf.template_foreach_sp" "$HEALTHDIR/dim_foreach_with_template_sp.conf" |
| 83 | process_data "foreach_with_template" 10 "$HEALTHDIR/dim_foreach_with_template_sp.conf" |
| 84 | |
| 85 | rm "$HEALTHDIR/dim_foreach_with_template_sp.conf" |
| 86 | rm -rf "$OUTDIR" |
| 87 | |
| 88 | echo -e "${GREEN} all the tests were successful ${NOCOLOR}" |