charts.d.plugin.in 20 KB

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