fping.plugin.in 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 fping.
  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. if [ "${1}" = "install" ]
  13. then
  14. [ "${UID}" != 0 ] && echo >&2 "Please run me as root. This will install a single binary file: /usr/local/bin/fping." && exit 1
  15. [ -z "${2}" ] && fping_version="5.1" || fping_version="${2}"
  16. run() {
  17. printf >&2 " > "
  18. printf >&2 "%q " "${@}"
  19. printf >&2 "\n"
  20. "${@}" || exit 1
  21. }
  22. download() {
  23. local curl="$(which curl 2>/dev/null || command -v curl 2>/dev/null)"
  24. [ ! -z "${curl}" ] && run curl -s -L "${1}" && return 0
  25. local wget="$(which wget 2>/dev/null || command -v wget 2>/dev/null)"
  26. [ ! -z "${wget}" ] && run wget -q -O - "${1}" && return 0
  27. echo >&2 "Cannot find 'curl' or 'wget' in this system." && exit 1
  28. }
  29. [ ! -d /usr/src ] && run mkdir -p /usr/src
  30. [ ! -d /usr/local/bin ] && run mkdir -p /usr/local/bin
  31. run cd /usr/src
  32. if [ -d "fping-${fping_version}" ]
  33. then
  34. run rm -rf "fping-${fping_version}" || exit 1
  35. fi
  36. download "https://github.com/schweikert/fping/releases/download/v${fping_version}/fping-${fping_version}.tar.gz" | run tar -zxvpf -
  37. [ $? -ne 0 ] && exit 1
  38. run cd "fping-${fping_version}" || exit 1
  39. run ./configure --prefix=/usr/local
  40. run make clean
  41. run make
  42. if [ -f /usr/local/bin/fping ]
  43. then
  44. run mv -f /usr/local/bin/fping /usr/local/bin/fping.old
  45. fi
  46. run mv src/fping /usr/local/bin/fping
  47. run chown root:root /usr/local/bin/fping
  48. run chmod 4755 /usr/local/bin/fping
  49. echo >&2
  50. echo >&2 "All done, you have a compatible fping now at /usr/local/bin/fping."
  51. echo >&2
  52. fping="$(which fping 2>/dev/null || command -v fping 2>/dev/null)"
  53. if [ "${fping}" != "/usr/local/bin/fping" ]
  54. then
  55. echo >&2 "You have another fping installed at: ${fping}."
  56. echo >&2 "Please set:"
  57. echo >&2
  58. echo >&2 " fping=\"/usr/local/bin/fping\""
  59. echo >&2
  60. echo >&2 "at /etc/netdata/fping.conf"
  61. echo >&2
  62. fi
  63. exit 0
  64. fi
  65. # -----------------------------------------------------------------------------
  66. PROGRAM_NAME="$(basename "${0}")"
  67. logdate() {
  68. date "+%Y-%m-%d %H:%M:%S"
  69. }
  70. log() {
  71. local status="${1}"
  72. shift
  73. echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"
  74. }
  75. warning() {
  76. log WARNING "${@}"
  77. }
  78. error() {
  79. log ERROR "${@}"
  80. }
  81. info() {
  82. log INFO "${@}"
  83. }
  84. fatal() {
  85. log FATAL "${@}"
  86. echo "DISABLE"
  87. exit 1
  88. }
  89. debug=0
  90. debug() {
  91. [ $debug -eq 1 ] && log DEBUG "${@}"
  92. }
  93. # -----------------------------------------------------------------------------
  94. # store in ${plugin} the name we run under
  95. # this allows us to copy/link fping.plugin under a different name
  96. # to have multiple fping plugins running with different settings
  97. plugin="${PROGRAM_NAME/.plugin/}"
  98. # -----------------------------------------------------------------------------
  99. # the frequency to send info to netdata
  100. # passed by netdata as the first parameter
  101. update_every="${1-1}"
  102. # the netdata configuration directory
  103. # passed by netdata as an environment variable
  104. [ -z "${NETDATA_USER_CONFIG_DIR}" ] && NETDATA_USER_CONFIG_DIR="@configdir_POST@"
  105. [ -z "${NETDATA_STOCK_CONFIG_DIR}" ] && NETDATA_STOCK_CONFIG_DIR="@libconfigdir_POST@"
  106. # -----------------------------------------------------------------------------
  107. # configuration options
  108. # can be overwritten at /etc/netdata/fping.conf
  109. # the fping binary to use
  110. # we need one that can output netdata friendly info (supporting: -N)
  111. # if you have multiple versions, put here the full filename of the right one
  112. fping="$( which fping 2>/dev/null || command -v fping 2>/dev/null )"
  113. # a space separated list of hosts to fping
  114. # we suggest to put names here and the IPs of these names in /etc/hosts
  115. hosts=""
  116. # the time in milliseconds (1 sec = 1000 ms)
  117. # to ping the hosts - by default 5 pings per host per iteration
  118. ping_every="$((update_every * 1000 / 5))"
  119. # fping options
  120. fping_opts="-R -b 56 -i 1 -r 0 -t 5000"
  121. # -----------------------------------------------------------------------------
  122. # load the configuration files
  123. for CONFIG in "${NETDATA_STOCK_CONFIG_DIR}/${plugin}.conf" "${NETDATA_USER_CONFIG_DIR}/${plugin}.conf"
  124. do
  125. if [ -f "${CONFIG}" ]
  126. then
  127. info "Loading config file '${CONFIG}'..."
  128. source "${CONFIG}"
  129. [ $? -ne 0 ] && error "Failed to load config file '${CONFIG}'."
  130. else
  131. warning "Cannot find file '${CONFIG}'."
  132. fi
  133. done
  134. if [ -z "${hosts}" ]
  135. then
  136. fatal "no hosts configured - nothing to do."
  137. fi
  138. if [ -z "${fping}" ]
  139. then
  140. fatal "fping command is not found. Please set its full path in '${NETDATA_USER_CONFIG_DIR}/${plugin}.conf'"
  141. fi
  142. if [ ! -x "${fping}" ]
  143. then
  144. fatal "fping command '${fping}' is not executable - cannot proceed."
  145. fi
  146. if [ ${ping_every} -lt 20 ]
  147. then
  148. warning "ping every was set to ${ping_every} but 20 is the minimum for non-root users. Setting it to 20 ms."
  149. ping_every=20
  150. fi
  151. # the fping options we will use
  152. options=( -N -l -Q ${update_every} -p ${ping_every} ${fping_opts} ${hosts} )
  153. # execute fping
  154. info "starting fping: ${fping} ${options[*]}"
  155. exec "${fping}" "${options[@]}"
  156. # if we cannot execute fping, stop
  157. fatal "command '${fping} ${options[*]}' failed to be executed (returned code $?)."