fping.plugin.in 5.4 KB

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