]> arthur.barton.de Git - netdata.git/blob - netdata-installer.sh
added more installer options (--dont-wait, --dont-start-it, --help) and info
[netdata.git] / netdata-installer.sh
1 #!/bin/bash
2
3 LC_ALL=C
4
5 # you can set CFLAGS before running installer
6 CFLAGS="${CFLAGS--O3}"
7
8 ME="$0"
9 DONOTSTART=0
10 DONOTWAIT=0
11 NETDATA_PREFIX=
12 ZLIB_IS_HERE=0
13
14 usage() {
15         cat <<USAGE
16
17 ${ME} <installer options>
18
19 Valid <installer options> are:
20
21    --install /PATH/TO/INSTALL
22
23         If your give: --install /opt
24         netdata will be installed in /opt/netdata
25
26    --dont-start-it
27
28         Do not (re)start netdata.
29         Just install it.
30
31    --dont-wait
32
33         Do not wait for the user to press ENTER.
34         Start immediately building it.
35
36    --zlib-is-really-here
37
38         If you get errors about missing zlib,
39         but you know it is available,
40         you have a broken pkg-config.
41         Use this option to allow it continue
42         without checking pkg-config.
43
44 Netdata will by default be compiled with gcc optimization -O3
45 If you need to pass different CFLAGS, use something like this:
46
47   CFLAGS="<gcc options>" $ME <installer options>
48
49 For the installer to complete successfully, you will need
50 these packages installed:
51
52    gcc make autoconf automake pkg-config zlib1g-dev
53
54 For the plugins, you will at least need:
55
56    curl node
57
58 USAGE
59 }
60
61 while [ ! -z "${1}" ]
62 do
63         if [ "$1" = "--install" ]
64                 then
65                 NETDATA_PREFIX="${2}/netdata"
66                 shift 2
67         elif [ "$1" = "--zlib-is-really-here" ]
68                 then
69                 ZLIB_IS_HERE=1
70                 shift 1
71         elif [ "$1" = "--dont-start-it" ]
72                 then
73                 DONOTSTART=1
74                 shift 1
75         elif [ "$1" = "--dont-wait" ]
76                 then
77                 DONOTWAIT=1
78                 shift 1
79         elif [ "$1" = "--help" -o "$1" = "-h" ]
80                 then
81                 usage
82                 exit 1
83         else
84                 echo >&2
85                 echo >&2 "ERROR:"
86                 echo >&2 "I cannot understand option '$1'."
87                 usage
88                 exit 1
89         fi
90 done
91
92 cat <<BANNER
93
94 Welcome to netdata!
95 Nice to see you are giving it a try!
96
97 You are about to build and install netdata to your system.
98
99 By default netdata will be installed as any other package:
100
101   - the daemon    at /usr/sbin/netdata
102   - config files  at /etc/netdata
103   - web files     at /usr/share/web/netdata
104   - plugins       at /usr/libexec/netdata
105   - cache files   at /var/cache/netdata
106   - log files     at /var/log/netdata
107
108 If you want to install it to another directory, run the
109 installer like this:
110
111   $ME --install /path/to/install
112
113 A new directory called netdata will be created as
114
115   /path/to/install/netdata
116
117 and all of netdata will be installed into it.
118 Nothing will be touched on your system.
119
120 This installer accepts more options.
121 Run it with --help for help.
122
123 BANNER
124
125 if [ ! "${UID}" = 0 -o "$1" = "-h" -o "$1" = "--help" ]
126         then
127         echo >&2
128         echo >&2 "You have to run netdata as root."
129         echo >&2 "The netdata daemon will drop priviliges"
130         echo >&2 "but you have to start it as root."
131         echo >&2
132         exit 1
133 fi
134
135 if [ ${DONOTWAIT} -eq 0 ]
136         then
137         if [ ! -z "${NETDATA_PREFIX}" ]
138                 then
139                 read -p "Press ENTER to build and install netdata to '${NETDATA_PREFIX}' > "
140         else
141                 read -p "Press ENTER to build and install netdata to your system > "
142         fi
143 fi
144
145 # reload the profile
146 [ -f /etc/profile ] && . /etc/profile
147
148 build_error() {
149         cat <<EOF
150
151 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
152
153 Sorry! NetData failed to build...
154
155 You many need to check these:
156
157 1. The package zlib1g-dev has to be installed.
158
159 2. You need basic build tools installed, like:
160
161    gcc make autoconf automake pkg-config
162
163 3. If your system cannot find ZLIB, although it is installed
164    run me with the option:  --zlib-is-really-here
165
166 If you still cannot get it to build, ask for help at github:
167
168    https://github.com/firehol/netdata/issues
169
170
171 EOF
172         trap - EXIT
173         exit 1
174 }
175
176 run() {
177         printf >&2 ":-----------------------------------------------------------------------------\n"
178         printf >&2 "Running command:\n"
179         printf >&2 "\n"
180         printf >&2 "%q " "${@}"
181         printf >&2 "\n"
182         printf >&2 "\n"
183
184         "${@}"
185 }
186
187 if [ ${ZLIB_IS_HERE} -eq 1 ]
188         then
189         shift
190         echo >&2 "ok, assuming zlib is really installed."
191         export ZLIB_CFLAGS=" "
192         export ZLIB_LIBS="-lz"
193 fi
194
195 trap build_error EXIT
196
197 echo >&2 "Running ./autogen.sh ..."
198 run ./autogen.sh || exit 1
199
200 echo >&2 "Running ./configure ..."
201 run ./configure \
202         --prefix="${NETDATA_PREFIX}/usr" \
203         --sysconfdir="${NETDATA_PREFIX}/etc" \
204         --localstatedir="${NETDATA_PREFIX}/var" \
205         --with-zlib --with-math --with-user=netdata \
206         CFLAGS="${CFLAGS}" || exit 1
207
208 # remove the build_error hook
209 trap - EXIT
210
211 if [ -f src/netdata ]
212         then
213         echo >&2 "Cleaning a possibly old compilation ..."
214         run make clean
215 fi
216
217 echo >&2 "Compiling netdata ..."
218 run make || exit 1
219
220 echo >&2 "Installing netdata ..."
221 run make install || exit 1
222
223 echo >&2 "Adding netdata user group ..."
224 getent group netdata > /dev/null || run groupadd -r netdata
225
226 echo >&2 "Adding netdata user account ..."
227 getent passwd netdata > /dev/null || run useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
228
229
230
231 # -----------------------------------------------------------------------------
232 # load options from the configuration file
233
234 # create an empty config if it does not exist
235 [ ! -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ] && touch "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
236
237 # function to extract values from the config file
238 config_option() {
239         local key="${1}" value="${2}" line=
240
241         if [ -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
242                 then
243                 line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "${NETDATA_PREFIX}/etc/netdata/netdata.conf" | head -n 1 )"
244                 [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
245         fi
246
247         echo "${value}"
248 }
249
250 # user
251 defuser="netdata"
252 [ ! "${UID}" = "0" ] && defuser="${USER}"
253 NETDATA_USER="$( config_option "run as user" "${defuser}" )"
254
255 # debug flags
256 defdebug=0
257 NETDATA_DEBUG="$( config_option "debug flags" ${defdebug} )"
258
259 # port
260 defport=19999
261 NETDATA_PORT="$( config_option "port" ${defport} )"
262
263 # directories
264 NETDATA_CACHE_DIR="$( config_option "cache directory" "${NETDATA_PREFIX}/var/cache/netdata" )"
265 NETDATA_WEB_DIR="$( config_option "web files directory" "${NETDATA_PREFIX}/usr/share/netdata/web" )"
266 NETDATA_LOG_DIR="$( config_option "log directory" "${NETDATA_PREFIX}/var/log/netdata" )"
267 NETDATA_CONF_DIR="$( config_option "config directory" "${NETDATA_PREFIX}/etc/netdata" )"
268
269
270 # -----------------------------------------------------------------------------
271 # prepare the directories
272
273 echo >&2 "Fixing directory permissions for user ${NETDATA_USER}..."
274 for x in "${NETDATA_WEB_DIR}" "${NETDATA_CONF_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"
275 do
276         if [ ! -d "${x}" ]
277                 then
278                 echo >&2 "Creating directory '${x}'"
279                 run mkdir -p "${x}" || exit 1
280         fi
281         run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${x}" || echo >&2 "WARNING: Cannot change the ownership of the files in directory ${x} to ${NETDATA_USER}..."
282         run chmod 0775 "${x}" || echo >&2 "WARNING: Cannot change the permissions of the directory ${x} to 0755..."
283 done
284
285 # fix apps.plugin to be setuid to root
286 run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
287 run chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
288
289
290 # -----------------------------------------------------------------------------
291 # check if we can re-start netdata
292
293 if [ ${DONOTSTART} -eq 1 ]
294         then
295         if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
296                 then
297                 echo >&2 "Generating empty config file in: ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
298                 echo "# Get config from http://127.0.0.1:${NETDATA_PORT}/netdata.conf" >"${NETDATA_PREFIX}/etc/netdata/netdata.conf"
299                 chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
300                 chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
301         fi
302         echo >&2 "OK. It is now installed and ready."
303         exit 0
304 fi
305
306 # -----------------------------------------------------------------------------
307 # stop a running netdata
308
309 printf >&2 "Stopping a (possibly) running netdata..."
310 ret=0
311 count=0
312 while [ $ret -eq 0 ]
313 do
314         if [ $count -gt 15 ]
315                 then
316                 echo >&2 "Cannot stop the running netdata."
317                 exit 1
318         fi
319
320         count=$((count + 1))
321         run killall netdata 2>/dev/null
322         ret=$?
323         test $ret -eq 0 && printf >&2 "." && sleep 2
324 done
325 echo >&2
326
327
328 # -----------------------------------------------------------------------------
329 # run netdata
330
331 echo >&2 "Starting netdata..."
332 run ${NETDATA_PREFIX}/usr/sbin/netdata "${@}"
333
334 if [ $? -ne 0 ]
335         then
336         echo >&2
337         echo >&2 "SORRY! FAILED TO START NETDATA!"
338         exit 1
339 else
340         echo >&2 "OK. NetData Started!"
341 fi
342
343
344 # -----------------------------------------------------------------------------
345 # save a config file, if it is not already there
346
347 if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
348         then
349         echo >&2 "Downloading default configuration from netdata..."
350         sleep 5
351
352         # remove a possibly obsolete download
353         [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
354
355         # try wget
356         wget 2>/dev/null -O "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
357         ret=$?
358
359         # try curl
360         if [ $ret -ne 0 -o ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
361                 then
362                 curl -s -o "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
363                 ret=$?
364         fi
365
366         if [ $ret -eq 0 -a -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
367                 then
368                 mv "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
369                 echo >&2 "New configuration saved for you to edit at ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
370
371                 chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
372                 chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
373         else
374                 echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
375                 [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
376         fi
377 fi
378
379 cat <<END
380
381
382 -------------------------------------------------------------------------------
383
384 ok. NetData is installed and is running.
385
386 Hit http://localhost:${NETDATA_PORT}/ from your browser.
387
388 To stop netdata, just kill it, with:
389
390   killall netdata
391
392 To start it, just run it:
393
394   ${NETDATA_PREFIX}/usr/sbin/netdata
395
396 Enjoy!
397
398 END