request.sh.in 11 KB

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