ioping.plugin.in 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. LOG_LEVEL_ERR=1
  79. LOG_LEVEL_WARN=2
  80. LOG_LEVEL_INFO=3
  81. LOG_LEVEL="$LOG_LEVEL_INFO"
  82. set_log_severity_level() {
  83. case ${NETDATA_LOG_SEVERITY_LEVEL,,} in
  84. "info") LOG_LEVEL="$LOG_LEVEL_INFO";;
  85. "warn" | "warning") LOG_LEVEL="$LOG_LEVEL_WARN";;
  86. "err" | "error") LOG_LEVEL="$LOG_LEVEL_ERR";;
  87. esac
  88. }
  89. set_log_severity_level
  90. logdate() {
  91. date "+%Y-%m-%d %H:%M:%S"
  92. }
  93. log() {
  94. local status="${1}"
  95. shift
  96. echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"
  97. }
  98. info() {
  99. [[ -n "$LOG_LEVEL" && "$LOG_LEVEL_INFO" -gt "$LOG_LEVEL" ]] && return
  100. log INFO "${@}"
  101. }
  102. warning() {
  103. [[ -n "$LOG_LEVEL" && "$LOG_LEVEL_WARN" -gt "$LOG_LEVEL" ]] && return
  104. log WARNING "${@}"
  105. }
  106. error() {
  107. [[ -n "$LOG_LEVEL" && "$LOG_LEVEL_ERR" -gt "$LOG_LEVEL" ]] && return
  108. log ERROR "${@}"
  109. }
  110. fatal() {
  111. log FATAL "${@}"
  112. echo "DISABLE"
  113. exit 1
  114. }
  115. debug=0
  116. debug() {
  117. [ $debug -eq 1 ] && log DEBUG "${@}"
  118. }
  119. # -----------------------------------------------------------------------------
  120. # store in ${plugin} the name we run under
  121. # this allows us to copy/link ioping.plugin under a different name
  122. # to have multiple ioping plugins running with different settings
  123. plugin="${PROGRAM_NAME/.plugin/}"
  124. # -----------------------------------------------------------------------------
  125. # the frequency to send info to netdata
  126. # passed by netdata as the first parameter
  127. update_every="${1-1}"
  128. # the netdata configuration directory
  129. # passed by netdata as an environment variable
  130. [ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="@configdir_POST@"
  131. [ -z "${NETDATA_STOCK_CONFIG_DIR}" ] && NETDATA_STOCK_CONFIG_DIR="@libconfigdir_POST@"
  132. # the netdata directory for internal binaries
  133. [ -z "${NETDATA_PLUGINS_DIR}" ] && NETDATA_PLUGINS_DIR="@pluginsdir_POST@"
  134. # -----------------------------------------------------------------------------
  135. # configuration options
  136. # can be overwritten at /etc/netdata/ioping.conf
  137. # the ioping binary to use
  138. # we need one that can output netdata friendly info (supporting: -N)
  139. # if you have multiple versions, put here the full filename of the right one
  140. ioping="${NETDATA_PLUGINS_DIR}/ioping"
  141. # the destination to ioping
  142. destination=""
  143. # the request size in bytes to ping the disk
  144. request_size="4k"
  145. # ioping options
  146. ioping_opts="-T 1000000"
  147. # -----------------------------------------------------------------------------
  148. # load the configuration files
  149. for CONFIG in "${NETDATA_STOCK_CONFIG_DIR}/${plugin}.conf" "${NETDATA_USER_CONFIG_DIR}/${plugin}.conf"; do
  150. if [ -f "${CONFIG}" ]; then
  151. info "Loading config file '${CONFIG}'..."
  152. source "${CONFIG}"
  153. [ $? -ne 0 ] && error "Failed to load config file '${CONFIG}'."
  154. elif [[ $CONFIG =~ ^$NETDATA_USER_CONFIG_DIR ]]; then
  155. warning "Cannot find file '${CONFIG}'."
  156. fi
  157. done
  158. if [ -z "${destination}" ]
  159. then
  160. fatal "destination is not configured - nothing to do."
  161. fi
  162. if [ ! -f "${ioping}" ]
  163. then
  164. fatal "ioping command is not found. Please set its full path in '${NETDATA_USER_CONFIG_DIR}/${plugin}.conf'"
  165. fi
  166. if [ ! -x "${ioping}" ]
  167. then
  168. fatal "ioping command '${ioping}' is not executable - cannot proceed."
  169. fi
  170. # the ioping options we will use
  171. options=( -N -i ${update_every} -s ${request_size} ${ioping_opts} ${destination} )
  172. # execute ioping
  173. info "starting ioping: ${ioping} ${options[*]}"
  174. exec "${ioping}" "${options[@]}"
  175. # if we cannot execute ioping, stop
  176. fatal "command '${ioping} ${options[*]}' failed to be executed (returned code $?)."