| 1 | #!/bin/bash |
| 2 | # SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | |
| 4 | ################################################################################################ |
| 5 | #### #### |
| 6 | #### GLOBAL VARIABLES #### |
| 7 | #### #### |
| 8 | ################################################################################################ |
| 9 | |
| 10 | # The current time |
| 11 | CT=$(date +'%s') |
| 12 | |
| 13 | # The previous time |
| 14 | PT=$((CT - 30)) |
| 15 | |
| 16 | # The output directory where we will store the results and error |
| 17 | OUTDIR="tests" |
| 18 | OUTEDIR="encoded_tests" |
| 19 | OUTOPTDIR="options" |
| 20 | ERRDIR="etests" |
| 21 | NOCOLOR='\033[0' |
| 22 | RED='\033[0;31m' |
| 23 | GREEN='\033[0;32m' |
| 24 | |
| 25 | ################################################################################################ |
| 26 | #### #### |
| 27 | #### FUNCTIONS #### |
| 28 | #### #### |
| 29 | ################################################################################################ |
| 30 | |
| 31 | # Print error message and close script |
| 32 | netdata_print_error(){ |
| 33 | echo "${RED} Closing due error \"$1\" code \"$2\" ${NOCOLOR}" |
| 34 | exit 1 |
| 35 | } |
| 36 | |
| 37 | # Print the header message of the function |
| 38 | netdata_print_header() { |
| 39 | echo "$1" |
| 40 | } |
| 41 | |
| 42 | # Create the main directory where the results will be stored |
| 43 | netdata_create_directory() { |
| 44 | netdata_print_header "Creating directory $1" |
| 45 | if [ ! -d "$1" ]; then |
| 46 | mkdir "$1" |
| 47 | TEST=$? |
| 48 | if [ $TEST -ne 0 ]; then |
| 49 | netdata_print_error "Cannot create directory $?" |
| 50 | fi |
| 51 | else |
| 52 | echo "Working with directory $OUTDIR" |
| 53 | fi |
| 54 | } |
| 55 | |
| 56 | #Check whether download did not have problem |
| 57 | netdata_test_download(){ |
| 58 | grep "HTTP/1.1 200 OK" "$1" 2>/dev/null 1>/dev/null |
| 59 | TEST=$? |
| 60 | if [ $TEST -ne 0 ]; then |
| 61 | netdata_print_error "Cannot do download of the page $2" $? |
| 62 | exit 1 |
| 63 | fi |
| 64 | } |
| 65 | |
| 66 | #Check whether download had a problem |
| 67 | netdata_error_test(){ |
| 68 | grep "HTTP/1.1 200 OK" "$1" 2>/dev/null 1>/dev/null |
| 69 | TEST=$? |
| 70 | if [ $TEST -eq 0 ]; then |
| 71 | netdata_print_error "The page $2 did not answer with an error" $? |
| 72 | exit 1 |
| 73 | fi |
| 74 | } |
| 75 | |
| 76 | |
| 77 | # Download information from Netdata |
| 78 | netdata_download_various() { |
| 79 | netdata_print_header "Getting $2" |
| 80 | curl -v -k --create-dirs -o "$OUTDIR/$3.out" "$1/$2" 2> "$OUTDIR/$3.err" |
| 81 | netdata_test_download "$OUTDIR/$3.err" "$1/$2" |
| 82 | } |
| 83 | |
| 84 | netdata_download_various_with_options() { |
| 85 | netdata_print_header "Getting options for $2" |
| 86 | curl -X OPTIONS -v -k --create-dirs -o "$OUTOPTDIR/$3.out" "$1/$2" 2> "$OUTOPTDIR/$3.err" |
| 87 | netdata_test_download "$OUTOPTDIR/$3.err" "$1/$2" |
| 88 | } |
| 89 | |
| 90 | # Download information from Netdata |
| 91 | netdata_wrong_request_various() { |
| 92 | netdata_print_header "Getting $2" |
| 93 | curl -v -k --create-dirs -o "$ERRDIR/$3.out" "$1/$2" 2> "$ERRDIR/$3.err" |
| 94 | netdata_error_test "$ERRDIR/$3.err" "$1/$2" |
| 95 | } |
| 96 | |
| 97 | # Download charts from Netdata |
| 98 | netdata_download_charts() { |
| 99 | curl -v -k --create-dirs -o "$OUTDIR/charts.out" "$1/$2" 2> "$OUTDIR/charts.err" |
| 100 | netdata_test_download "$OUTDIR/charts.err" "$1/$2" |
| 101 | |
| 102 | #Rewrite the next |
| 103 | grep -w "id" tests/charts.out| cut -d: -f2 | grep "\"," | sed s/,//g | sort |
| 104 | } |
| 105 | |
| 106 | #Test options for a specific chart |
| 107 | netdata_download_chart() { |
| 108 | SEPARATOR="&" |
| 109 | EQUAL="=" |
| 110 | OUTD=$OUTDIR |
| 111 | ENCODED=" " |
| 112 | for I in $(seq 0 1); do |
| 113 | if [ "$I" -eq "1" ] ; then |
| 114 | SEPARATOR="%26" |
| 115 | EQUAL="%3D" |
| 116 | OUTD=$OUTEDIR |
| 117 | ENCODED="encoded" |
| 118 | fi |
| 119 | |
| 120 | NAME=${3//\"/} |
| 121 | netdata_print_header "Getting data for $NAME using $4 $ENCODED" |
| 122 | |
| 123 | LDIR=$OUTD"/"$4 |
| 124 | |
| 125 | LURL="$1/$2$EQUAL$NAME" |
| 126 | |
| 127 | NAME=$NAME"_$4" |
| 128 | |
| 129 | curl -v -k --create-dirs -o "$LDIR/$NAME.out" "$LURL" 2> "$LDIR/$NAME.err" |
| 130 | netdata_test_download "$LDIR/$NAME.err" "$LURL" |
| 131 | |
| 132 | UFILES=( "points" "before" "after" ) |
| 133 | COUNTER=0 |
| 134 | for OPT in "points=100" "before=$PT" "after=$CT" ; |
| 135 | do |
| 136 | LURL="$LURL$SEPARATOR$OPT" |
| 137 | LFILE=$NAME"_${UFILES[$COUNTER]}"; |
| 138 | |
| 139 | curl -v -k --create-dirs -o "$LDIR/$LFILE.out" "$LURL" 2> "$LDIR/$LFILE.err" |
| 140 | netdata_test_download "$LDIR/$LFILE.err" "$LURL" |
| 141 | |
| 142 | COUNTER=$((COUNTER + 1)) |
| 143 | done |
| 144 | |
| 145 | LURL="$LURL&group$EQUAL" |
| 146 | for OPT in "min" "max" "sum" "median" "stddev" "cv" "ses" "des" "incremental_sum" "average"; |
| 147 | do |
| 148 | TURL=$LURL$OPT |
| 149 | TFILE=$NAME"_$OPT"; |
| 150 | curl -v -k --create-dirs -o "$LDIR/$TFILE.out" "$TURL" 2> "$LDIR/$TFILE.err" |
| 151 | netdata_test_download "$LDIR/$TFILE.err" "$TURL" |
| 152 | for MORE in "jsonp" "json" "ssv" "csv" "datatable" "datasource" "tsv" "ssvcomma" "html" "array"; |
| 153 | do |
| 154 | TURL=$TURL"&format="$MORE |
| 155 | TFILE=$NAME"_$OPT""_$MORE"; |
| 156 | curl -v -k --create-dirs -o "$LDIR/$TFILE.out" "$TURL" 2> "$LDIR/$TFILE.err" |
| 157 | netdata_test_download "$LDIR/$TFILE.err" "$TURL" |
| 158 | done |
| 159 | done |
| 160 | |
| 161 | LURL="$LURL$OPT>ime=60" |
| 162 | NFILE=$NAME"_gtime" |
| 163 | curl -v -k --create-dirs -o "$LDIR/$NFILE.out" "$TURL" 2> "$LDIR/$NFILE.err" |
| 164 | netdata_test_download "$LDIR/$NFILE.err" "$LURL" |
| 165 | |
| 166 | LURL="$LURL$OPT&options=percentage" |
| 167 | NFILE=$NAME"_percentage" |
| 168 | curl -v -k --create-dirs -o "$LDIR/$NFILE.out" "$TURL" 2> "$LDIR/$NFILE.err" |
| 169 | netdata_test_download "$LDIR/$NFILE.err" "$LURL" |
| 170 | |
| 171 | LURL="$LURL$OPT&dimensions=system%7Cnice" |
| 172 | NFILE=$NAME"_dimension" |
| 173 | curl -v -k --create-dirs -o "$LDIR/$NFILE.out" "$TURL" 2> "$LDIR/$NFILE.err" |
| 174 | netdata_test_download "$LDIR/$NFILE.err" "$LURL" |
| 175 | |
| 176 | LURL="$LURL$OPT&label=testing" |
| 177 | NFILE=$NAME"_label" |
| 178 | curl -v -k --create-dirs -o "$LDIR/$NFILE.out" "$TURL" 2> "$LDIR/$NFILE.err" |
| 179 | netdata_test_download "$LDIR/$NFILE.err" "$LURL" |
| 180 | done |
| 181 | } |
| 182 | |
| 183 | # Download information from Netdata |
| 184 | netdata_download_allmetrics() { |
| 185 | netdata_print_header "Getting All metrics" |
| 186 | LURL="$1/api/v1/allmetrics?format=" |
| 187 | for FMT in "shell" "prometheus" "prometheus_all_hosts" "json" ; |
| 188 | do |
| 189 | TURL=$LURL$FMT |
| 190 | for OPT in "yes" "no"; |
| 191 | do |
| 192 | if [ "$FMT" == "prometheus" ]; then |
| 193 | TURL="$TURL&help=$OPT&types=$OPT×tamps=$OPT" |
| 194 | fi |
| 195 | TURL="$TURL&names=$OPT&oldunits=$OPT&hideunits=$OPT&prefix=ND" |
| 196 | |
| 197 | NAME="allmetrics_$FMT" |
| 198 | echo "$OUTDIR/$2/$NAME.out" |
| 199 | curl -v -k --create-dirs -o "$OUTDIR/$2/$NAME.out" "$TURL" 2> "$OUTDIR/$2/$NAME.err" |
| 200 | netdata_test_download "$OUTDIR/$2/$NAME.err" "$TURL" |
| 201 | done |
| 202 | done |
| 203 | } |
| 204 | |
| 205 | |
| 206 | #################################################### |
| 207 | #### #### |
| 208 | #### MAIN ROUTINE #### |
| 209 | #### #### |
| 210 | #################################################### |
| 211 | MURL="http://127.0.0.1:19999" |
| 212 | |
| 213 | if [ -n "$1" ]; then |
| 214 | MURL="$1" |
| 215 | fi |
| 216 | |
| 217 | netdata_create_directory $OUTDIR |
| 218 | netdata_create_directory $OUTEDIR |
| 219 | netdata_create_directory $OUTOPTDIR |
| 220 | netdata_create_directory $ERRDIR |
| 221 | |
| 222 | wget --no-check-certificate --execute="robots = off" --mirror --convert-links --no-parent "$MURL" |
| 223 | TEST=$? |
| 224 | if [ $TEST -ne "0" ] ; then |
| 225 | echo "Cannot connect to Netdata" |
| 226 | exit 1 |
| 227 | fi |
| 228 | |
| 229 | netdata_download_various "$MURL" "netdata.conf" "netdata.conf" |
| 230 | |
| 231 | netdata_download_various_with_options "$MURL" "netdata.conf" "netdata.conf" |
| 232 | |
| 233 | netdata_wrong_request_various "$MURL" "api/v15/info?this%20could%20not%20be%20here" "err_version" |
| 234 | |
| 235 | netdata_wrong_request_various "$MURL" "api/v1/\(*@&$\!$%%5E\)\!$*%&\)\!$*%%5E*\!%5E%\!%5E$%\!%5E%\(\!*%5E*%5E%\(*@&$%5E%\(\!%5E#*&\!^#$*&\!^%\)@\($%^\)\!*&^\(\!*&^#$&#$\)\!$%^\)\!$*%&\)#$\!^#*$^\!\(*#^#\)\!%^\!\)$*%&\!\(*&$\!^#$*&^\!*#^$\!*^\)%\(\!*&$%\)\(\!&#$\!^*#&$^\!*^%\)\!$%\)\!\(&#$\!^#*&^$" "err_version2" |
| 236 | |
| 237 | netdata_download_various "$MURL" "api/v1/info" "info" |
| 238 | netdata_download_various_with_options "$MURL" "api/v1/info" "info" |
| 239 | netdata_download_various "$MURL" "api/v1/info?this%20could%20not%20be%20here" "err_info" |
| 240 | |
| 241 | netdata_print_header "Getting all the netdata charts" |
| 242 | CHARTS=$( netdata_download_charts "$MURL" "api/v1/charts" ) |
| 243 | WCHARTS=$( netdata_download_charts "$MURL" "api/v1/charts?this%20could%20not%20be%20here" ) |
| 244 | WCHARTS2=$( netdata_download_charts "$MURL" "api/v1/charts%3fthis%20could%20not%20be%20here" ) |
| 245 | |
| 246 | if [ ${#CHARTS[@]} -ne ${#WCHARTS[@]} ]; then |
| 247 | echo "The number of charts does not match with division not encoded."; |
| 248 | exit 2; |
| 249 | elif [ ${#CHARTS[@]} -ne ${#WCHARTS2[@]} ]; then |
| 250 | echo "The number of charts does not match when everything is encoded"; |
| 251 | exit 3; |
| 252 | fi |
| 253 | |
| 254 | netdata_wrong_request_various "$MURL" "api/v1/chart" "err_chart_without_chart" |
| 255 | netdata_wrong_request_various "$MURL" "api/v1/chart?_=234231424242" "err_chart_arg" |
| 256 | |
| 257 | netdata_download_various "$MURL" "api/v1/chart?chart=cpu.cpu0_interrupts&_=234231424242" "chart_cpu_with_more_args" |
| 258 | netdata_download_various_with_options "$MURL" "api/v1/chart?chart=cpu.cpu0_interrupts&_=234231424242" "chart_cpu_with_more_args" |
| 259 | |
| 260 | netdata_download_various "$MURL" "api/v1/chart%3Fchart=cpu.cpu0_interrupts&_=234231424242" "chart_cpu_with_more_args_encoded" |
| 261 | netdata_download_various_with_options "$MURL" "api/v1/chart%3Fchart=cpu.cpu0_interrupts&_=234231424242" "chart_cpu_with_more_args_encoded" |
| 262 | netdata_download_various "$MURL" "api/v1/chart%3Fchart=cpu.cpu0_interrupts%26_=234231424242" "chart_cpu_with_more_args_encoded2" |
| 263 | netdata_download_various "$MURL" "api/v1/chart%3Fchart%3Dcpu.cpu0_interrupts%26_%3D234231424242" "chart_cpu_with_more_args_encoded3" |
| 264 | |
| 265 | netdata_create_directory "$OUTDIR/chart" |
| 266 | for I in $CHARTS ; do |
| 267 | NAME=${I//\"/} |
| 268 | netdata_download_various "$MURL" "api/v1/chart?chart=$NAME" "chart/$NAME" |
| 269 | done |
| 270 | |
| 271 | netdata_wrong_request_various "$MURL" "api/v1/alarm_variables" "err_alarm_variables_without_chart" |
| 272 | netdata_wrong_request_various "$MURL" "api/v1/alarm_variables?_=234231424242" "err_alarm_variables_arg" |
| 273 | netdata_download_various "$MURL" "api/v1/alarm_variables?chart=cpu.cpu0_interrupts&_=234231424242" "alarm_cpu_with_more_args" |
| 274 | |
| 275 | netdata_create_directory "$OUTDIR/alarm_variables" |
| 276 | for I in $CHARTS ; do |
| 277 | NAME=${I//\"/} |
| 278 | netdata_download_various "$MURL" "api/v1/alarm_variables?chart=$NAME" "alarm_variables/$NAME" |
| 279 | done |
| 280 | |
| 281 | netdata_create_directory "$OUTDIR/badge" |
| 282 | netdata_create_directory "$OUTEDIR/badge" |
| 283 | for I in $CHARTS ; do |
| 284 | netdata_download_chart "$MURL" "api/v1/badge.svg?chart" "$I" "badge" |
| 285 | done |
| 286 | |
| 287 | netdata_create_directory "$OUTDIR/allmetrics" |
| 288 | netdata_download_allmetrics "$MURL" "allmetrics" |
| 289 | |
| 290 | netdata_download_various "$MURL" "api/v1/alarms?all" "alarms_all" |
| 291 | netdata_download_various "$MURL" "api/v1/alarms?active" "alarms_active" |
| 292 | netdata_download_various "$MURL" "api/v1/alarms" "alarms_nothing" |
| 293 | |
| 294 | netdata_download_various "$MURL" "api/v1/alarm_log?after" "alarm_without" |
| 295 | netdata_download_various "$MURL" "api/v1/alarm_log" "alarm_nothing" |
| 296 | netdata_download_various "$MURL" "api/v1/alarm_log?after&_=$PT" "alarm_log" |
| 297 | |
| 298 | netdata_create_directory "$OUTDIR/data" |
| 299 | netdata_create_directory "$OUTEDIR/data" |
| 300 | for I in $CHARTS ; do |
| 301 | netdata_download_chart "$MURL" "api/v1/data?chart" "$I" "data" |
| 302 | break; |
| 303 | done |
| 304 | |
| 305 | echo -e "${GREEN}ALL the URLS got 200 as answer! ${NOCOLOR}" |
| 306 | |
| 307 | exit 0 |