ioping.plugin.in 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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:@sbindir_POST@"
  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. # logging
  78. PROGRAM_NAME="$(basename "${0}")"
  79. # these should be the same with syslog() priorities
  80. NDLP_EMERG=0 # system is unusable
  81. NDLP_ALERT=1 # action must be taken immediately
  82. NDLP_CRIT=2 # critical conditions
  83. NDLP_ERR=3 # error conditions
  84. NDLP_WARN=4 # warning conditions
  85. NDLP_NOTICE=5 # normal but significant condition
  86. NDLP_INFO=6 # informational
  87. NDLP_DEBUG=7 # debug-level messages
  88. # the max (numerically) log level we will log
  89. LOG_LEVEL=$NDLP_INFO
  90. set_log_min_priority() {
  91. case "${NETDATA_LOG_LEVEL,,}" in
  92. "emerg" | "emergency")
  93. LOG_LEVEL=$NDLP_EMERG
  94. ;;
  95. "alert")
  96. LOG_LEVEL=$NDLP_ALERT
  97. ;;
  98. "crit" | "critical")
  99. LOG_LEVEL=$NDLP_CRIT
  100. ;;
  101. "err" | "error")
  102. LOG_LEVEL=$NDLP_ERR
  103. ;;
  104. "warn" | "warning")
  105. LOG_LEVEL=$NDLP_WARN
  106. ;;
  107. "notice")
  108. LOG_LEVEL=$NDLP_NOTICE
  109. ;;
  110. "info")
  111. LOG_LEVEL=$NDLP_INFO
  112. ;;
  113. "debug")
  114. LOG_LEVEL=$NDLP_DEBUG
  115. ;;
  116. esac
  117. }
  118. set_log_min_priority
  119. log() {
  120. local level="${1}"
  121. shift 1
  122. [[ -n "$level" && -n "$LOG_LEVEL" && "$level" -gt "$LOG_LEVEL" ]] && return
  123. systemd-cat-native --log-as-netdata --newline="--NEWLINE--" <<EOFLOG
  124. INVOCATION_ID=${NETDATA_INVOCATION_ID}
  125. SYSLOG_IDENTIFIER=${PROGRAM_NAME}
  126. PRIORITY=${level}
  127. THREAD_TAG=ioping.plugin
  128. ND_LOG_SOURCE=collector
  129. MESSAGE=${MODULE_NAME}: ${*//\\n/--NEWLINE--}
  130. EOFLOG
  131. # AN EMPTY LINE IS NEEDED ABOVE
  132. }
  133. info() {
  134. log "$NDLP_INFO" "${@}"
  135. }
  136. warning() {
  137. log "$NDLP_WARN" "${@}"
  138. }
  139. error() {
  140. log "$NDLP_ERR" "${@}"
  141. }
  142. disable() {
  143. log "${@}"
  144. echo "DISABLE"
  145. exit 1
  146. }
  147. fatal() {
  148. disable "$NDLP_ALERT" "${@}"
  149. }
  150. debug() {
  151. log "$NDLP_DEBUG" "${@}"
  152. }
  153. # -----------------------------------------------------------------------------
  154. # store in ${plugin} the name we run under
  155. # this allows us to copy/link ioping.plugin under a different name
  156. # to have multiple ioping plugins running with different settings
  157. plugin="${PROGRAM_NAME/.plugin/}"
  158. # -----------------------------------------------------------------------------
  159. # the frequency to send info to netdata
  160. # passed by netdata as the first parameter
  161. update_every="${1-1}"
  162. # the netdata configuration directory
  163. # passed by netdata as an environment variable
  164. [ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="@configdir_POST@"
  165. [ -z "${NETDATA_STOCK_CONFIG_DIR}" ] && NETDATA_STOCK_CONFIG_DIR="@libconfigdir_POST@"
  166. # the netdata directory for internal binaries
  167. [ -z "${NETDATA_PLUGINS_DIR}" ] && NETDATA_PLUGINS_DIR="@pluginsdir_POST@"
  168. # -----------------------------------------------------------------------------
  169. # configuration options
  170. # can be overwritten at /etc/netdata/ioping.conf
  171. # the ioping binary to use
  172. # we need one that can output netdata friendly info (supporting: -N)
  173. # if you have multiple versions, put here the full filename of the right one
  174. ioping="${NETDATA_PLUGINS_DIR}/ioping"
  175. # the destination to ioping
  176. destination=""
  177. # the request size in bytes to ping the disk
  178. request_size="4k"
  179. # ioping options
  180. ioping_opts="-T 1000000"
  181. # -----------------------------------------------------------------------------
  182. # load the configuration files
  183. for CONFIG in "${NETDATA_STOCK_CONFIG_DIR}/${plugin}.conf" "${NETDATA_USER_CONFIG_DIR}/${plugin}.conf"; do
  184. if [ -f "${CONFIG}" ]; then
  185. debug "Loading config file '${CONFIG}'..."
  186. source "${CONFIG}"
  187. [ $? -ne 0 ] && warn "Failed to load config file '${CONFIG}'."
  188. elif [[ $CONFIG =~ ^$NETDATA_USER_CONFIG_DIR ]]; then
  189. debug "Cannot find file '${CONFIG}'."
  190. fi
  191. done
  192. if [ -z "${destination}" ]
  193. then
  194. disable $NDLP_DEBUG "destination is not configured - nothing to do."
  195. fi
  196. if [ ! -f "${ioping}" ]
  197. then
  198. disable $NDLP_ERR "ioping command is not found. Please set its full path in '${NETDATA_USER_CONFIG_DIR}/${plugin}.conf'"
  199. fi
  200. if [ ! -x "${ioping}" ]
  201. then
  202. disable $NDLP_ERR "ioping command '${ioping}' is not executable - cannot proceed."
  203. fi
  204. # the ioping options we will use
  205. options=( -N -i ${update_every} -s ${request_size} ${ioping_opts} ${destination} )
  206. # execute ioping
  207. debug "starting ioping: ${ioping} ${options[*]}"
  208. exec "${ioping}" "${options[@]}"
  209. # if we cannot execute ioping, stop
  210. error "command '${ioping} ${options[*]}' failed to be executed (returned code $?)."