ioping.plugin.in 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. # This plugin requires a latest version of ioping.
  9. # You can compile it from source, by running me with option: install
  10. export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/sbin"
  11. export LC_ALL=C
  12. usage="$(basename "$0") [install] [-h] [-e]
  13. where:
  14. install install ioping binary
  15. -e, --env path to environment file (defaults to '/etc/netdata/.environment'
  16. -h show this help text"
  17. INSTALL=0
  18. ENVIRONMENT_FILE="/etc/netdata/.environment"
  19. while :; do
  20. case "$1" in
  21. -h | --help)
  22. echo "$usage" >&2
  23. exit 1
  24. ;;
  25. install)
  26. INSTALL=1
  27. shift
  28. ;;
  29. -e | --env)
  30. ENVIRONMENT_FILE="$2"
  31. shift 2
  32. ;;
  33. -*)
  34. echo "$usage" >&2
  35. exit 1
  36. ;;
  37. *) break ;;
  38. esac
  39. done
  40. if [ "$INSTALL" == "1" ]
  41. then
  42. [ "${UID}" != 0 ] && echo >&2 "Please run me as root. This will install a single binary file: /usr/libexec/netdata/plugins.d/ioping." && exit 1
  43. source "${ENVIRONMENT_FILE}" || exit 1
  44. run() {
  45. printf >&2 " > "
  46. printf >&2 "%q " "${@}"
  47. printf >&2 "\n"
  48. "${@}" || exit 1
  49. }
  50. download() {
  51. local git="$(which git 2>/dev/null || command -v git 2>/dev/null)"
  52. [ ! -z "${git}" ] && run git clone "${1}" "${2}" && return 0
  53. echo >&2 "Cannot find 'git' in this system." && exit 1
  54. }
  55. tmp=$(mktemp -d /tmp/netdata-ioping-XXXXXX)
  56. [ ! -d "${NETDATA_PREFIX}/usr/libexec/netdata" ] && run mkdir -p "${NETDATA_PREFIX}/usr/libexec/netdata"
  57. run cd "${tmp}"
  58. if [ -d ioping-netdata ]
  59. then
  60. run rm -rf ioping-netdata || exit 1
  61. fi
  62. download 'https://github.com/netdata/ioping.git' 'ioping-netdata'
  63. [ $? -ne 0 ] && exit 1
  64. run cd ioping-netdata || exit 1
  65. INSTALL_PATH="${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/ioping"
  66. run make clean
  67. run make
  68. run mv ioping "${INSTALL_PATH}"
  69. run chown root:"${NETDATA_GROUP}" "${INSTALL_PATH}"
  70. run chmod 4750 "${INSTALL_PATH}"
  71. echo >&2
  72. echo >&2 "All done, you have a compatible ioping now at ${INSTALL_PATH}."
  73. echo >&2
  74. exit 0
  75. fi
  76. # -----------------------------------------------------------------------------
  77. PROGRAM_NAME="$(basename "${0}")"
  78. logdate() {
  79. date "+%Y-%m-%d %H:%M:%S"
  80. }
  81. log() {
  82. local status="${1}"
  83. shift
  84. echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"
  85. }
  86. warning() {
  87. log WARNING "${@}"
  88. }
  89. error() {
  90. log ERROR "${@}"
  91. }
  92. info() {
  93. log INFO "${@}"
  94. }
  95. fatal() {
  96. log FATAL "${@}"
  97. echo "DISABLE"
  98. exit 1
  99. }
  100. debug=0
  101. debug() {
  102. [ $debug -eq 1 ] && log DEBUG "${@}"
  103. }
  104. # -----------------------------------------------------------------------------
  105. # store in ${plugin} the name we run under
  106. # this allows us to copy/link ioping.plugin under a different name
  107. # to have multiple ioping plugins running with different settings
  108. plugin="${PROGRAM_NAME/.plugin/}"
  109. # -----------------------------------------------------------------------------
  110. # the frequency to send info to netdata
  111. # passed by netdata as the first parameter
  112. update_every="${1-1}"
  113. # the netdata configuration directory
  114. # passed by netdata as an environment variable
  115. [ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="@configdir_POST@"
  116. [ -z "${NETDATA_STOCK_CONFIG_DIR}" ] && NETDATA_STOCK_CONFIG_DIR="@libconfigdir_POST@"
  117. # the netdata directory for internal binaries
  118. [ -z "${NETDATA_PLUGINS_DIR}" ] && NETDATA_PLUGINS_DIR="@pluginsdir_POST@"
  119. # -----------------------------------------------------------------------------
  120. # configuration options
  121. # can be overwritten at /etc/netdata/ioping.conf
  122. # the ioping binary to use
  123. # we need one that can output netdata friendly info (supporting: -N)
  124. # if you have multiple versions, put here the full filename of the right one
  125. ioping="${NETDATA_PLUGINS_DIR}/ioping"
  126. # the destination to ioping
  127. destination=""
  128. # the request size in bytes to ping the disk
  129. request_size="4k"
  130. # ioping options
  131. ioping_opts="-T 1000000"
  132. # -----------------------------------------------------------------------------
  133. # load the configuration files
  134. for CONFIG in "${NETDATA_STOCK_CONFIG_DIR}/${plugin}.conf" "${NETDATA_USER_CONFIG_DIR}/${plugin}.conf"; do
  135. if [ -f "${CONFIG}" ]; then
  136. info "Loading config file '${CONFIG}'..."
  137. source "${CONFIG}"
  138. [ $? -ne 0 ] && error "Failed to load config file '${CONFIG}'."
  139. elif [[ $CONFIG =~ ^$NETDATA_USER_CONFIG_DIR ]]; then
  140. warning "Cannot find file '${CONFIG}'."
  141. fi
  142. done
  143. if [ -z "${destination}" ]
  144. then
  145. fatal "destination is not configured - nothing to do."
  146. fi
  147. if [ ! -f "${ioping}" ]
  148. then
  149. fatal "ioping command is not found. Please set its full path in '${NETDATA_USER_CONFIG_DIR}/${plugin}.conf'"
  150. fi
  151. if [ ! -x "${ioping}" ]
  152. then
  153. fatal "ioping command '${ioping}' is not executable - cannot proceed."
  154. fi
  155. # the ioping options we will use
  156. options=( -N -i ${update_every} -s ${request_size} ${ioping_opts} ${destination} )
  157. # execute ioping
  158. info "starting ioping: ${ioping} ${options[*]}"
  159. exec "${ioping}" "${options[@]}"
  160. # if we cannot execute ioping, stop
  161. fatal "command '${ioping} ${options[*]}' failed to be executed (returned code $?)."