]> arthur.barton.de Git - netdata.git/blob - plugins.d/fping.plugin
uniform logging from all scripts
[netdata.git] / plugins.d / fping.plugin
1 #!/usr/bin/env bash
2
3 # netdata
4 # real-time performance and health monitoring, done right!
5 # (C) 2016 Costa Tsaousis <costa@tsaousis.gr>
6 # GPL v3+
7 #
8 # This plugin requires a special version of fping.
9 # Get it from https://github.com/ktsaou/fping
10 # and build it, like this:
11 #
12 # cd /usr/src
13 # git clone https://github.com/ktsaou/fping.git fping-netdata.git
14 # cd fping-netdata.git
15 # ./autogen.sh
16 # ./configure --prefix=/usr/local
17 # make
18 # cp src/fping /usr/local/bin/
19 # chown root:root /usr/local/bin/fping
20 # chmod 4755 /usr/local/bin/fping
21 #
22 # Then, create /etc/netdata/fping.conf
23 # and set the configuration options given below
24
25 export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/sbin"
26 export LC_ALL=C
27
28 # -----------------------------------------------------------------------------
29
30 PROGRAM_NAME="$(basename "${0}")"
31
32 logdate() {
33     date "+%Y-%m-%d %H:%M:%S"
34 }
35
36 log() {
37     local status="${1}"
38     shift
39
40     echo >&2 "$(logdate): ${PROGRAM_NAME}: ${status}: ${*}"
41
42 }
43
44 warning() {
45     log WARNING "${@}"
46 }
47
48 error() {
49     log ERROR "${@}"
50 }
51
52 info() {
53     log INFO "${@}"
54 }
55
56 fatal() {
57     log FATAL "${@}"
58         echo "DISABLE"
59     exit 1
60 }
61
62 debug=0
63 debug() {
64     [ $debug -eq 1 ] && log DEBUG "${@}"
65 }
66
67 # -----------------------------------------------------------------------------
68
69 # the frequency to send info to netdata
70 # passed by netdata as the first parameter
71 update_every="${1-1}"
72
73 # the netdata configuration directory
74 # passed by netdata as an environment variable
75 NETDATA_CONFIG_DIR="${NETDATA_CONFIG_DIR-/etc/netdata}"
76
77 # -----------------------------------------------------------------------------
78 # configuration options
79 # can be overwritten at /etc/netdata/fping.conf
80
81 # the fping binary to use
82 # we need one that can output netdata friendly info
83 fping="$(which fping || command -v fping)"
84
85 # a space separated list of hosts to fping
86 hosts=""
87
88 # the time in milliseconds (1 sec = 1000 ms)
89 # to ping the hosts - by default 2 pings per iteration
90 ping_every="$((update_every * 1000 / 2))"
91
92 # how many retries to make if a host does not respond
93 retries=1
94
95 # -----------------------------------------------------------------------------
96 # load the configuration file
97
98 if [ ! -f "${NETDATA_CONFIG_DIR}/fping.conf" ]
99 then
100         fatal "configuration file '${NETDATA_CONFIG_DIR}/fping.conf' not found - nothing to do."
101 fi
102
103 source "${NETDATA_CONFIG_DIR}/fping.conf"
104
105 if [ -z "${hosts}" ]
106 then
107         fatal "no hosts configued in '${NETDATA_CONFIG_DIR}/fping.conf' - nothing to do."
108 fi
109
110 if [ -z "${fping}" -o ! -x "${fping}" ]
111 then
112         fatal "command '${fping}' is not executable - cannot proceed."
113 fi
114
115 # the fping options we will use
116 options=( -N -l -R -Q ${update_every} -p ${ping_every} -r ${retries} ${hosts} )
117
118 # execute fping
119 exec "${fping}" "${options[@]}"
120
121 # if we cannot execute fping, stop
122 fatal "command '${fping} ${options[@]}' failed to be executed."