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