charts.d.plugin.in 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # netdata
  4. # real-time performance and health monitoring, done right!
  5. # (C) 2017 Costa Tsaousis <costa@tsaousis.gr>
  6. # GPL v3+
  7. #
  8. # charts.d.plugin allows easy development of BASH plugins
  9. #
  10. # if you need to run parallel charts.d processes, link this file to a different name
  11. # in the same directory, with a .plugin suffix and netdata will start both of them,
  12. # each will have a different config file and modules configuration directory.
  13. #
  14. export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
  15. PROGRAM_FILE="$0"
  16. PROGRAM_NAME="$(basename $0)"
  17. PROGRAM_NAME="${PROGRAM_NAME/.plugin/}"
  18. MODULE_NAME="main"
  19. # -----------------------------------------------------------------------------
  20. # create temp dir
  21. debug=0
  22. TMP_DIR=
  23. chartsd_cleanup() {
  24. trap '' EXIT QUIT HUP INT TERM
  25. if [ ! -z "$TMP_DIR" -a -d "$TMP_DIR" ]; then
  26. [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: cleaning up temporary directory $TMP_DIR ..."
  27. rm -rf "$TMP_DIR"
  28. fi
  29. echo "EXIT"
  30. exit 0
  31. }
  32. trap chartsd_cleanup EXIT QUIT HUP INT TERM
  33. if [ $UID = "0" ]; then
  34. TMP_DIR="$(mktemp -d /var/run/netdata-${PROGRAM_NAME}-XXXXXXXXXX)"
  35. else
  36. TMP_DIR="$(mktemp -d /tmp/.netdata-${PROGRAM_NAME}-XXXXXXXXXX)"
  37. fi
  38. logdate() {
  39. date "+%Y-%m-%d %H:%M:%S"
  40. }
  41. log() {
  42. local status="${1}"
  43. shift
  44. echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${MODULE_NAME}: ${*}"
  45. }
  46. warning() {
  47. log WARNING "${@}"
  48. }
  49. error() {
  50. log ERROR "${@}"
  51. }
  52. info() {
  53. log INFO "${@}"
  54. }
  55. fatal() {
  56. log FATAL "${@}"
  57. echo "DISABLE"
  58. exit 1
  59. }
  60. debug() {
  61. [ $debug -eq 1 ] && log DEBUG "${@}"
  62. }
  63. # -----------------------------------------------------------------------------
  64. # check a few commands
  65. require_cmd() {
  66. local x=$(which "${1}" 2>/dev/null || command -v "${1}" 2>/dev/null)
  67. if [ -z "${x}" -o ! -x "${x}" ]; then
  68. warning "command '${1}' is not found in ${PATH}."
  69. eval "${1^^}_CMD=\"\""
  70. return 1
  71. fi
  72. eval "${1^^}_CMD=\"${x}\""
  73. return 0
  74. }
  75. require_cmd date || exit 1
  76. require_cmd sed || exit 1
  77. require_cmd basename || exit 1
  78. require_cmd dirname || exit 1
  79. require_cmd cat || exit 1
  80. require_cmd grep || exit 1
  81. require_cmd egrep || exit 1
  82. require_cmd mktemp || exit 1
  83. require_cmd awk || exit 1
  84. require_cmd timeout || exit 1
  85. require_cmd curl || exit 1
  86. # -----------------------------------------------------------------------------
  87. [ $((BASH_VERSINFO[0])) -lt 4 ] && fatal "BASH version 4 or later is required, but found version: ${BASH_VERSION}. Please upgrade."
  88. info "started from '$PROGRAM_FILE' with options: $*"
  89. # -----------------------------------------------------------------------------
  90. # internal defaults
  91. # netdata exposes a few environment variables for us
  92. [ -z "${NETDATA_PLUGINS_DIR}" ] && NETDATA_PLUGINS_DIR="$(dirname "${0}")"
  93. [ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="@configdir_POST@"
  94. [ -z "${NETDATA_STOCK_CONFIG_DIR}" ] && NETDATA_STOCK_CONFIG_DIR="@libconfigdir_POST@"
  95. pluginsd="${NETDATA_PLUGINS_DIR}"
  96. stockconfd="${NETDATA_STOCK_CONFIG_DIR}/${PROGRAM_NAME}"
  97. userconfd="${NETDATA_USER_CONFIG_DIR}/${PROGRAM_NAME}"
  98. olduserconfd="${NETDATA_USER_CONFIG_DIR}"
  99. chartsd="$pluginsd/../charts.d"
  100. minimum_update_frequency="${NETDATA_UPDATE_EVERY-1}"
  101. update_every=${minimum_update_frequency} # this will be overwritten by the command line
  102. # work around for non BASH shells
  103. charts_create="_create"
  104. charts_update="_update"
  105. charts_check="_check"
  106. charts_underscore="_"
  107. # when making iterations, charts.d can loop more frequently
  108. # to prevent plugins missing iterations.
  109. # this is a percentage relative to update_every to align its
  110. # iterations.
  111. # The minimum is 10%, the maximum 100%.
  112. # So, if update_every is 1 second and time_divisor is 50,
  113. # charts.d will iterate every 500ms.
  114. # Charts will be called to collect data only if the time
  115. # passed since the last time the collected data is equal or
  116. # above their update_every.
  117. time_divisor=50
  118. # number of seconds to run without restart
  119. # after this time, charts.d.plugin will exit
  120. # netdata will restart it
  121. restart_timeout=$((3600 * 4))
  122. # check if the charts.d plugins are using global variables
  123. # they should not.
  124. # It does not currently support BASH v4 arrays, so it is
  125. # disabled
  126. dryrunner=0
  127. # check for timeout command
  128. check_for_timeout=1
  129. # the default enable/disable value for all charts
  130. enable_all_charts="yes"
  131. # -----------------------------------------------------------------------------
  132. # parse parameters
  133. check=0
  134. chart_only=
  135. while [ ! -z "$1" ]; do
  136. if [ "$1" = "check" ]; then
  137. check=1
  138. shift
  139. continue
  140. fi
  141. if [ "$1" = "debug" -o "$1" = "all" ]; then
  142. debug=1
  143. shift
  144. continue
  145. fi
  146. if [ -f "$chartsd/$1.chart.sh" ]; then
  147. debug=1
  148. chart_only="$(echo $1.chart.sh | sed "s/\.chart\.sh$//g")"
  149. shift
  150. continue
  151. fi
  152. if [ -f "$chartsd/$1" ]; then
  153. debug=1
  154. chart_only="$(echo $1 | sed "s/\.chart\.sh$//g")"
  155. shift
  156. continue
  157. fi
  158. # number check
  159. n="$1"
  160. x=$((n))
  161. if [ "$x" = "$n" ]; then
  162. shift
  163. update_every=$x
  164. [ $update_every -lt $minimum_update_frequency ] && update_every=$minimum_update_frequency
  165. continue
  166. fi
  167. fatal "Cannot understand parameter $1. Aborting."
  168. done
  169. # -----------------------------------------------------------------------------
  170. # loop control
  171. # default sleep function
  172. LOOPSLEEPMS_HIGHRES=0
  173. now_ms=
  174. current_time_ms_default() {
  175. now_ms="$(date +'%s')000"
  176. }
  177. current_time_ms="current_time_ms_default"
  178. current_time_ms_accuracy=1
  179. mysleep="sleep"
  180. # if found and included, this file overwrites loopsleepms()
  181. # and current_time_ms() with a high resolution timer function
  182. # for precise looping.
  183. source "$pluginsd/loopsleepms.sh.inc"
  184. [ $? -ne 0 ] && error "Failed to load '$pluginsd/loopsleepms.sh.inc'."
  185. # -----------------------------------------------------------------------------
  186. # load my configuration
  187. for myconfig in "${NETDATA_STOCK_CONFIG_DIR}/${PROGRAM_NAME}.conf" "${NETDATA_USER_CONFIG_DIR}/${PROGRAM_NAME}.conf"; do
  188. if [ -f "$myconfig" ]; then
  189. source "$myconfig"
  190. if [ $? -ne 0 ]; then
  191. error "Config file '$myconfig' loaded with errors."
  192. else
  193. info "Configuration file '$myconfig' loaded."
  194. fi
  195. else
  196. warning "Configuration file '$myconfig' not found."
  197. fi
  198. done
  199. # make sure time_divisor is right
  200. time_divisor=$((time_divisor))
  201. [ $time_divisor -lt 10 ] && time_divisor=10
  202. [ $time_divisor -gt 100 ] && time_divisor=100
  203. # we check for the timeout command, after we load our
  204. # configuration, so that the user may overwrite the
  205. # timeout command we use, providing a function that
  206. # can emulate the timeout command we need:
  207. # > timeout SECONDS command ...
  208. if [ $check_for_timeout -eq 1 ]; then
  209. require_cmd timeout || exit 1
  210. fi
  211. # -----------------------------------------------------------------------------
  212. # internal checks
  213. # netdata passes the requested update frequency as the first argument
  214. update_every=$((update_every + 1 - 1)) # makes sure it is a number
  215. test $update_every -eq 0 && update_every=1 # if it is zero, make it 1
  216. # check the charts.d directory
  217. [ ! -d "$chartsd" ] && fatal "cannot find charts directory '$chartsd'"
  218. # -----------------------------------------------------------------------------
  219. # library functions
  220. fixid() {
  221. echo "$*" |
  222. tr -c "[A-Z][a-z][0-9]" "_" |
  223. sed -e "s|^_\+||g" -e "s|_\+$||g" -e "s|_\+|_|g" |
  224. tr "[A-Z]" "[a-z]"
  225. }
  226. isvarset() {
  227. [ -n "$1" ] && [ "$1" != "unknown" ] && [ "$1" != "none" ]
  228. return $?
  229. }
  230. getosid() {
  231. if isvarset "${NETDATA_CONTAINER_OS_ID}"; then
  232. echo "${NETDATA_CONTAINER_OS_ID}"
  233. else
  234. echo "${NETDATA_SYSTEM_OS_ID}"
  235. fi
  236. }
  237. run() {
  238. local ret pid="${BASHPID}" t
  239. if [ "z${1}" = "z-t" -a "${2}" != "0" ]; then
  240. t="${2}"
  241. shift 2
  242. timeout "${t}" "${@}" 2>"${TMP_DIR}/run.${pid}"
  243. ret=$?
  244. else
  245. "${@}" 2>"${TMP_DIR}/run.${pid}"
  246. ret=$?
  247. fi
  248. if [ ${ret} -ne 0 ]; then
  249. {
  250. printf "$(logdate): ${PROGRAM_NAME}: ${status}: ${MODULE_NAME}: command '"
  251. printf "%q " "${@}"
  252. printf "' failed with code ${ret}:\n --- BEGIN TRACE ---\n"
  253. cat "${TMP_DIR}/run.${pid}"
  254. printf " --- END TRACE ---\n"
  255. } >&2
  256. fi
  257. rm -f "${TMP_DIR}/run.${pid}"
  258. return ${ret}
  259. }
  260. # convert any floating point number
  261. # to integer, give a multiplier
  262. # the result is stored in ${FLOAT2INT_RESULT}
  263. # so that no fork is necessary
  264. # the multiplier must be a power of 10
  265. float2int() {
  266. local f m="$2" a b l v=($1)
  267. f=${v[0]}
  268. # the length of the multiplier - 1
  269. l=$((${#m} - 1))
  270. # check if the number is in scientific notation
  271. if [[ ${f} =~ ^[[:space:]]*(-)?[0-9.]+(e|E)(\+|-)[0-9]+ ]]; then
  272. # convert it to decimal
  273. # unfortunately, this fork cannot be avoided
  274. # if you know of a way to avoid it, please let me know
  275. f=$(printf "%0.${l}f" ${f})
  276. fi
  277. # split the floating point number
  278. # in integer (a) and decimal (b)
  279. a=${f/.*/}
  280. b=${f/*./}
  281. # if the integer part is missing
  282. # set it to zero
  283. [ -z "${a}" ] && a="0"
  284. # strip leading zeros from the integer part
  285. # base 10 conversion
  286. a=$((10#$a))
  287. # check the length of the decimal part
  288. # against the length of the multiplier
  289. if [ ${#b} -gt ${l} ]; then
  290. # too many digits - take the most significant
  291. b=${b:0:l}
  292. elif [ ${#b} -lt ${l} ]; then
  293. # too few digits - pad with zero on the right
  294. local z="00000000000000000000000" r=$((l - ${#b}))
  295. b="${b}${z:0:r}"
  296. fi
  297. # strip leading zeros from the decimal part
  298. # base 10 conversion
  299. b=$((10#$b))
  300. # store the result
  301. FLOAT2INT_RESULT=$(((a * m) + b))
  302. }
  303. # -----------------------------------------------------------------------------
  304. # charts check functions
  305. all_charts() {
  306. cd "$chartsd"
  307. [ $? -ne 0 ] && error "cannot cd to $chartsd" && return 1
  308. ls *.chart.sh | sed "s/\.chart\.sh$//g"
  309. }
  310. declare -A charts_enable_keyword=(
  311. ['apache']="force"
  312. ['cpu_apps']="force"
  313. ['cpufreq']="force"
  314. ['example']="force"
  315. ['exim']="force"
  316. ['hddtemp']="force"
  317. ['load_average']="force"
  318. ['mem_apps']="force"
  319. ['mysql']="force"
  320. ['nginx']="force"
  321. ['phpfpm']="force"
  322. ['postfix']="force"
  323. ['sensors']="force"
  324. ['squid']="force"
  325. ['tomcat']="force"
  326. )
  327. declare -A obsolete_charts=(
  328. ['apache']="python.d.plugin module"
  329. ['cpu_apps']="apps.plugin"
  330. ['cpufreq']="proc plugin"
  331. ['exim']="python.d.plugin module"
  332. ['hddtemp']="python.d.plugin module"
  333. ['load_average']="proc plugin"
  334. ['mem_apps']="proc plugin"
  335. ['mysql']="python.d.plugin module"
  336. ['nginx']="python.d.plugin module"
  337. ['phpfpm']="python.d.plugin module"
  338. ['postfix']="python.d.plugin module"
  339. ['squid']="python.d.plugin module"
  340. ['tomcat']="python.d.plugin module"
  341. )
  342. all_enabled_charts() {
  343. local charts enabled required
  344. # find all enabled charts
  345. for chart in $(all_charts); do
  346. MODULE_NAME="${chart}"
  347. if [ -n "${obsolete_charts["$MODULE_NAME"]}" ]; then
  348. debug "is replaced by ${obsolete_charts["$MODULE_NAME"]}, skipping it."
  349. continue
  350. fi
  351. eval "enabled=\$$chart"
  352. if [ -z "${enabled}" ]; then
  353. enabled="${enable_all_charts}"
  354. fi
  355. required="${charts_enable_keyword[${chart}]}"
  356. [ -z "${required}" ] && required="yes"
  357. if [ ! "${enabled}" = "${required}" ]; then
  358. info "is disabled. Add a line with $chart=$required in '${NETDATA_USER_CONFIG_DIR}/${PROGRAM_NAME}.conf' to enable it (or remove the line that disables it)."
  359. else
  360. debug "is enabled for auto-detection."
  361. local charts="$charts $chart"
  362. fi
  363. done
  364. MODULE_NAME="main"
  365. local charts2=
  366. for chart in $charts; do
  367. MODULE_NAME="${chart}"
  368. # check the enabled charts
  369. local check="$(cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_check()")"
  370. if [ -z "$check" ]; then
  371. error "module '$chart' does not seem to have a $chart$charts_check() function. Disabling it."
  372. continue
  373. fi
  374. local create="$(cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_create()")"
  375. if [ -z "$create" ]; then
  376. error "module '$chart' does not seem to have a $chart$charts_create() function. Disabling it."
  377. continue
  378. fi
  379. local update="$(cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_update()")"
  380. if [ -z "$update" ]; then
  381. error "module '$chart' does not seem to have a $chart$charts_update() function. Disabling it."
  382. continue
  383. fi
  384. # check its config
  385. #if [ -f "$userconfd/$chart.conf" ]
  386. #then
  387. # if [ ! -z "$( cat "$userconfd/$chart.conf" | sed "s/^ \+//g" | grep -v "^$" | grep -v "^#" | grep -v "^$chart$charts_underscore" )" ]
  388. # then
  389. # error "module's $chart config $userconfd/$chart.conf should only have lines starting with $chart$charts_underscore . Disabling it."
  390. # continue
  391. # fi
  392. #fi
  393. #if [ $dryrunner -eq 1 ]
  394. # then
  395. # "$pluginsd/charts.d.dryrun-helper.sh" "$chart" "$chartsd/$chart.chart.sh" "$userconfd/$chart.conf" >/dev/null
  396. # if [ $? -ne 0 ]
  397. # then
  398. # error "module's $chart did not pass the dry run check. This means it uses global variables not starting with $chart. Disabling it."
  399. # continue
  400. # fi
  401. #fi
  402. local charts2="$charts2 $chart"
  403. done
  404. MODULE_NAME="main"
  405. echo $charts2
  406. debug "enabled charts: $charts2"
  407. }
  408. # -----------------------------------------------------------------------------
  409. # load the charts
  410. suffix_retries="_retries"
  411. suffix_update_every="_update_every"
  412. active_charts=
  413. for chart in $(all_enabled_charts); do
  414. MODULE_NAME="${chart}"
  415. debug "loading module: '$chartsd/$chart.chart.sh'"
  416. source "$chartsd/$chart.chart.sh"
  417. [ $? -ne 0 ] && warning "Module '$chartsd/$chart.chart.sh' loaded with errors."
  418. # first load the stock config
  419. if [ -f "$stockconfd/$chart.conf" ]; then
  420. debug "loading module configuration: '$stockconfd/$chart.conf'"
  421. source "$stockconfd/$chart.conf"
  422. [ $? -ne 0 ] && warning "Config file '$stockconfd/$chart.conf' loaded with errors."
  423. else
  424. debug "not found module configuration: '$stockconfd/$chart.conf'"
  425. fi
  426. # then load the user config (it overwrites the stock)
  427. if [ -f "$userconfd/$chart.conf" ]; then
  428. debug "loading module configuration: '$userconfd/$chart.conf'"
  429. source "$userconfd/$chart.conf"
  430. [ $? -ne 0 ] && warning "Config file '$userconfd/$chart.conf' loaded with errors."
  431. else
  432. debug "not found module configuration: '$userconfd/$chart.conf'"
  433. if [ -f "$olduserconfd/$chart.conf" ]; then
  434. # support for very old netdata that had the charts.d module configs in /etc/netdata
  435. info "loading module configuration from obsolete location: '$olduserconfd/$chart.conf'"
  436. source "$olduserconfd/$chart.conf"
  437. [ $? -ne 0 ] && warning "Config file '$olduserconfd/$chart.conf' loaded with errors."
  438. fi
  439. fi
  440. eval "dt=\$$chart$suffix_update_every"
  441. dt=$((dt + 1 - 1)) # make sure it is a number
  442. if [ $dt -lt $update_every ]; then
  443. eval "$chart$suffix_update_every=$update_every"
  444. fi
  445. $chart$charts_check
  446. if [ $? -eq 0 ]; then
  447. debug "module '$chart' activated"
  448. active_charts="$active_charts $chart"
  449. else
  450. error "module's '$chart' check() function reports failure."
  451. fi
  452. done
  453. MODULE_NAME="main"
  454. debug "activated modules: $active_charts"
  455. # -----------------------------------------------------------------------------
  456. # check overwrites
  457. # enable work time reporting
  458. debug_time=
  459. test $debug -eq 1 && debug_time=tellwork
  460. # if we only need a specific chart, remove all the others
  461. if [ ! -z "${chart_only}" ]; then
  462. debug "requested to run only for: '${chart_only}'"
  463. check_charts=
  464. for chart in $active_charts; do
  465. if [ "$chart" = "$chart_only" ]; then
  466. check_charts="$chart"
  467. break
  468. fi
  469. done
  470. active_charts="$check_charts"
  471. fi
  472. debug "activated charts: $active_charts"
  473. # stop if we just need a pre-check
  474. if [ $check -eq 1 ]; then
  475. info "CHECK RESULT"
  476. info "Will run the charts: $active_charts"
  477. exit 0
  478. fi
  479. # -----------------------------------------------------------------------------
  480. cd "${TMP_DIR}" || exit 1
  481. # -----------------------------------------------------------------------------
  482. # create charts
  483. run_charts=
  484. for chart in $active_charts; do
  485. MODULE_NAME="${chart}"
  486. debug "calling '$chart$charts_create()'..."
  487. $chart$charts_create
  488. if [ $? -eq 0 ]; then
  489. run_charts="$run_charts $chart"
  490. debug "'$chart' initialized."
  491. else
  492. error "module's '$chart' function '$chart$charts_create()' reports failure."
  493. fi
  494. done
  495. MODULE_NAME="main"
  496. debug "run_charts='$run_charts'"
  497. # -----------------------------------------------------------------------------
  498. # update dimensions
  499. [ -z "$run_charts" ] && fatal "No charts to collect data from."
  500. keepalive() {
  501. if [ ! -t 1 ] && ! printf "\n"; then
  502. chartsd_cleanup
  503. fi
  504. }
  505. declare -A charts_last_update=() charts_update_every=() charts_retries=() charts_next_update=() charts_run_counter=() charts_serial_failures=()
  506. global_update() {
  507. local exit_at \
  508. c=0 dt ret last_ms exec_start_ms exec_end_ms \
  509. chart now_charts=() next_charts=($run_charts) \
  510. next_ms x seconds millis
  511. # return the current time in ms in $now_ms
  512. ${current_time_ms}
  513. exit_at=$((now_ms + (restart_timeout * 1000)))
  514. for chart in $run_charts; do
  515. eval "charts_update_every[$chart]=\$$chart$suffix_update_every"
  516. test -z "${charts_update_every[$chart]}" && charts_update_every[$chart]=$update_every
  517. eval "charts_retries[$chart]=\$$chart$suffix_retries"
  518. test -z "${charts_retries[$chart]}" && charts_retries[$chart]=10
  519. charts_last_update[$chart]=$((now_ms - (now_ms % (charts_update_every[$chart] * 1000))))
  520. charts_next_update[$chart]=$((charts_last_update[$chart] + (charts_update_every[$chart] * 1000)))
  521. charts_run_counter[$chart]=0
  522. charts_serial_failures[$chart]=0
  523. echo "CHART netdata.plugin_chartsd_$chart '' 'Execution time for $chart plugin' 'milliseconds / run' charts.d netdata.plugin_charts area 145000 ${charts_update_every[$chart]} '' '' '$chart'"
  524. echo "DIMENSION run_time 'run time' absolute 1 1"
  525. done
  526. # the main loop
  527. while [ "${#next_charts[@]}" -gt 0 ]; do
  528. keepalive
  529. c=$((c + 1))
  530. now_charts=("${next_charts[@]}")
  531. next_charts=()
  532. # return the current time in ms in $now_ms
  533. ${current_time_ms}
  534. for chart in "${now_charts[@]}"; do
  535. MODULE_NAME="${chart}"
  536. if [ ${now_ms} -ge ${charts_next_update[$chart]} ]; then
  537. last_ms=${charts_last_update[$chart]}
  538. dt=$((now_ms - last_ms))
  539. charts_last_update[$chart]=${now_ms}
  540. while [ ${charts_next_update[$chart]} -lt ${now_ms} ]; do
  541. charts_next_update[$chart]=$((charts_next_update[$chart] + (charts_update_every[$chart] * 1000)))
  542. done
  543. # the first call should not give a duration
  544. # so that netdata calibrates to current time
  545. dt=$((dt * 1000))
  546. charts_run_counter[$chart]=$((charts_run_counter[$chart] + 1))
  547. if [ ${charts_run_counter[$chart]} -eq 1 ]; then
  548. dt=
  549. fi
  550. exec_start_ms=$now_ms
  551. $chart$charts_update $dt
  552. ret=$?
  553. # return the current time in ms in $now_ms
  554. ${current_time_ms}
  555. exec_end_ms=$now_ms
  556. echo "BEGIN netdata.plugin_chartsd_$chart $dt"
  557. echo "SET run_time = $((exec_end_ms - exec_start_ms))"
  558. echo "END"
  559. if [ $ret -eq 0 ]; then
  560. charts_serial_failures[$chart]=0
  561. next_charts+=($chart)
  562. else
  563. charts_serial_failures[$chart]=$((charts_serial_failures[$chart] + 1))
  564. if [ ${charts_serial_failures[$chart]} -gt ${charts_retries[$chart]} ]; then
  565. error "module's '$chart' update() function reported failure ${charts_serial_failures[$chart]} times. Disabling it."
  566. else
  567. error "module's '$chart' update() function reports failure. Will keep trying for a while."
  568. next_charts+=($chart)
  569. fi
  570. fi
  571. else
  572. next_charts+=($chart)
  573. fi
  574. done
  575. MODULE_NAME="${chart}"
  576. # wait the time you are required to
  577. next_ms=$((now_ms + (update_every * 1000 * 100)))
  578. for x in "${charts_next_update[@]}"; do [ ${x} -lt ${next_ms} ] && next_ms=${x}; done
  579. next_ms=$((next_ms - now_ms))
  580. if [ ${LOOPSLEEPMS_HIGHRES} -eq 1 -a ${next_ms} -gt 0 ]; then
  581. next_ms=$((next_ms + current_time_ms_accuracy))
  582. seconds=$((next_ms / 1000))
  583. millis=$((next_ms % 1000))
  584. if [ ${millis} -lt 10 ]; then
  585. millis="00${millis}"
  586. elif [ ${millis} -lt 100 ]; then
  587. millis="0${millis}"
  588. fi
  589. debug "sleeping for ${seconds}.${millis} seconds."
  590. ${mysleep} ${seconds}.${millis}
  591. else
  592. debug "sleeping for ${update_every} seconds."
  593. ${mysleep} $update_every
  594. fi
  595. test ${now_ms} -ge ${exit_at} && exit 0
  596. done
  597. fatal "nothing left to do, exiting..."
  598. }
  599. global_update