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