]> arthur.barton.de Git - netdata.git/blob - netdata-installer.sh
installation: generate netdata-uninstaller.sh script and netdata-installer.log, #103
[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 # keep a log of this command
9 printf "\n# " >>netdata-installer.log
10 date >>netdata-installer.log
11 printf "CFLAGS=\"%s\" " "${CFLAGS}" >>netdata-installer.log
12 printf "%q " "$0" "${@}" >>netdata-installer.log
13 printf "\n" >>netdata-installer.log
14
15 ME="$0"
16 DONOTSTART=0
17 DONOTWAIT=0
18 NETDATA_PREFIX=
19 ZLIB_IS_HERE=0
20
21 usage() {
22         cat <<USAGE
23
24 ${ME} <installer options>
25
26 Valid <installer options> are:
27
28    --install /PATH/TO/INSTALL
29
30         If your give: --install /opt
31         netdata will be installed in /opt/netdata
32
33    --dont-start-it
34
35         Do not (re)start netdata.
36         Just install it.
37
38    --dont-wait
39
40         Do not wait for the user to press ENTER.
41         Start immediately building it.
42
43    --zlib-is-really-here
44
45         If you get errors about missing zlib,
46         but you know it is available,
47         you have a broken pkg-config.
48         Use this option to allow it continue
49         without checking pkg-config.
50
51 Netdata will by default be compiled with gcc optimization -O3
52 If you need to pass different CFLAGS, use something like this:
53
54   CFLAGS="<gcc options>" $ME <installer options>
55
56 For the installer to complete successfully, you will need
57 these packages installed:
58
59    gcc make autoconf automake pkg-config zlib1g-dev
60
61 For the plugins, you will at least need:
62
63    curl node
64
65 USAGE
66 }
67
68 while [ ! -z "${1}" ]
69 do
70         if [ "$1" = "--install" ]
71                 then
72                 NETDATA_PREFIX="${2}/netdata"
73                 shift 2
74         elif [ "$1" = "--zlib-is-really-here" ]
75                 then
76                 ZLIB_IS_HERE=1
77                 shift 1
78         elif [ "$1" = "--dont-start-it" ]
79                 then
80                 DONOTSTART=1
81                 shift 1
82         elif [ "$1" = "--dont-wait" ]
83                 then
84                 DONOTWAIT=1
85                 shift 1
86         elif [ "$1" = "--help" -o "$1" = "-h" ]
87                 then
88                 usage
89                 exit 1
90         else
91                 echo >&2
92                 echo >&2 "ERROR:"
93                 echo >&2 "I cannot understand option '$1'."
94                 usage
95                 exit 1
96         fi
97 done
98
99 cat <<BANNER
100
101 Welcome to netdata!
102 Nice to see you are giving it a try!
103
104 You are about to build and install netdata to your system.
105
106 It will be installed at these locations:
107
108   - the daemon    at ${NETDATA_PREFIX}/usr/sbin/netdata
109   - config files  at ${NETDATA_PREFIX}/etc/netdata
110   - web files     at ${NETDATA_PREFIX}/usr/share/netdata
111   - plugins       at ${NETDATA_PREFIX}/usr/libexec/netdata
112   - cache files   at ${NETDATA_PREFIX}/var/cache/netdata
113   - log files     at ${NETDATA_PREFIX}/var/log/netdata
114
115 This installer allows you to change the installation path.
116 Press Control-C and run the same command with --help for help.
117
118 BANNER
119
120 if [ ! "${UID}" = 0 -o "$1" = "-h" -o "$1" = "--help" ]
121         then
122         echo >&2
123         echo >&2 "You have to run netdata as root."
124         echo >&2 "The netdata daemon will drop priviliges"
125         echo >&2 "but you have to start it as root."
126         echo >&2
127         exit 1
128 fi
129
130 if [ ${DONOTWAIT} -eq 0 ]
131         then
132         if [ ! -z "${NETDATA_PREFIX}" ]
133                 then
134                 read -p "Press ENTER to build and install netdata to '${NETDATA_PREFIX}' > "
135         else
136                 read -p "Press ENTER to build and install netdata to your system > "
137         fi
138 fi
139
140 # reload the profile
141 [ -f /etc/profile ] && . /etc/profile
142
143 build_error() {
144         cat <<EOF
145
146 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147
148 Sorry! NetData failed to build...
149
150 You many need to check these:
151
152 1. The package zlib1g-dev has to be installed.
153
154 2. You need basic build tools installed, like:
155
156    gcc make autoconf automake pkg-config
157
158 3. If your system cannot find ZLIB, although it is installed
159    run me with the option:  --zlib-is-really-here
160
161 If you still cannot get it to build, ask for help at github:
162
163    https://github.com/firehol/netdata/issues
164
165
166 EOF
167         trap - EXIT
168         exit 1
169 }
170
171 run() {
172         printf >&2 ":-----------------------------------------------------------------------------\n"
173         printf >&2 "Running command:\n"
174         printf >&2 "\n"
175         printf >&2 "%q " "${@}"
176         printf >&2 "\n"
177         printf >&2 "\n"
178
179         "${@}"
180 }
181
182 if [ ${ZLIB_IS_HERE} -eq 1 ]
183         then
184         shift
185         echo >&2 "ok, assuming zlib is really installed."
186         export ZLIB_CFLAGS=" "
187         export ZLIB_LIBS="-lz"
188 fi
189
190 trap build_error EXIT
191
192 echo >&2 "Running ./autogen.sh ..."
193 run ./autogen.sh || exit 1
194
195 echo >&2 "Running ./configure ..."
196 run ./configure \
197         --prefix="${NETDATA_PREFIX}/usr" \
198         --sysconfdir="${NETDATA_PREFIX}/etc" \
199         --localstatedir="${NETDATA_PREFIX}/var" \
200         --with-zlib --with-math --with-user=netdata \
201         CFLAGS="${CFLAGS}" || exit 1
202
203 # remove the build_error hook
204 trap - EXIT
205
206 if [ -f src/netdata ]
207         then
208         echo >&2 "Cleaning a possibly old compilation ..."
209         run make clean
210 fi
211
212 echo >&2 "Compiling netdata ..."
213 run make || exit 1
214
215 echo >&2 "Installing netdata ..."
216 run make install || exit 1
217
218 echo >&2 "Adding netdata user group ..."
219 getent group netdata > /dev/null || run groupadd -r netdata
220
221 echo >&2 "Adding netdata user account ..."
222 getent passwd netdata > /dev/null || run useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
223
224
225
226 # -----------------------------------------------------------------------------
227 # load options from the configuration file
228
229 # create an empty config if it does not exist
230 [ ! -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ] && touch "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
231
232 # function to extract values from the config file
233 config_option() {
234         local key="${1}" value="${2}" line=
235
236         if [ -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
237                 then
238                 line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "${NETDATA_PREFIX}/etc/netdata/netdata.conf" | head -n 1 )"
239                 [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
240         fi
241
242         echo "${value}"
243 }
244
245 # user
246 defuser="netdata"
247 [ ! "${UID}" = "0" ] && defuser="${USER}"
248 NETDATA_USER="$( config_option "run as user" "${defuser}" )"
249
250 # debug flags
251 defdebug=0
252 NETDATA_DEBUG="$( config_option "debug flags" ${defdebug} )"
253
254 # port
255 defport=19999
256 NETDATA_PORT="$( config_option "port" ${defport} )"
257
258 # directories
259 NETDATA_CACHE_DIR="$( config_option "cache directory" "${NETDATA_PREFIX}/var/cache/netdata" )"
260 NETDATA_WEB_DIR="$( config_option "web files directory" "${NETDATA_PREFIX}/usr/share/netdata/web" )"
261 NETDATA_LOG_DIR="$( config_option "log directory" "${NETDATA_PREFIX}/var/log/netdata" )"
262 NETDATA_CONF_DIR="$( config_option "config directory" "${NETDATA_PREFIX}/etc/netdata" )"
263
264
265 # -----------------------------------------------------------------------------
266 # prepare the directories
267
268 echo >&2 "Fixing directory permissions for user ${NETDATA_USER}..."
269 for x in "${NETDATA_WEB_DIR}" "${NETDATA_CONF_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"
270 do
271         if [ ! -d "${x}" ]
272                 then
273                 echo >&2 "Creating directory '${x}'"
274                 run mkdir -p "${x}" || exit 1
275         fi
276         run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${x}" || echo >&2 "WARNING: Cannot change the ownership of the files in directory ${x} to ${NETDATA_USER}..."
277         run chmod 0775 "${x}" || echo >&2 "WARNING: Cannot change the permissions of the directory ${x} to 0755..."
278 done
279
280 # fix apps.plugin to be setuid to root
281 run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
282 run chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
283
284
285 # -----------------------------------------------------------------------------
286 # check if we can re-start netdata
287
288 if [ ${DONOTSTART} -eq 1 ]
289         then
290         if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
291                 then
292                 echo >&2 "Generating empty config file in: ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
293                 echo "# Get config from http://127.0.0.1:${NETDATA_PORT}/netdata.conf" >"${NETDATA_PREFIX}/etc/netdata/netdata.conf"
294                 chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
295                 chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
296         fi
297         echo >&2 "OK. It is now installed and ready."
298         exit 0
299 fi
300
301 # -----------------------------------------------------------------------------
302 # stop a running netdata
303
304 isnetdata() {
305         [ -z "$1" -o ! -f "/proc/$1/stat" ] && return 1
306         [ "$(cat "/proc/$1/stat" | cut -d '(' -f 2 | cut -d ')' -f 1)" = "netdata" ] && return 0
307         return 1
308 }
309
310
311 printf >&2 "Stopping a (possibly) running netdata..."
312 ret=0
313 count=0
314 while [ $ret -eq 0 ]
315 do
316         if [ $count -gt 15 ]
317                 then
318                 echo >&2 "Cannot stop the running netdata."
319                 exit 1
320         fi
321
322         count=$((count + 1))
323
324         pid=$(cat /var/run/netdata.pid 2>/dev/null)
325         # backwards compatibility
326         [ -z "${pid}" ] && pid=$(cat /var/run/netdata/netdata.pid 2>/dev/null)
327         
328         isnetdata $pid || pid=
329         if [ ! -z "${pid}" ]
330                 then
331                 run kill $pid 2>/dev/null
332                 ret=$?
333         else
334                 run killall netdata 2>/dev/null
335                 ret=$?
336         fi
337
338         test $ret -eq 0 && printf >&2 "." && sleep 2
339 done
340 echo >&2
341
342
343 # -----------------------------------------------------------------------------
344 # run netdata
345
346 echo >&2 "Starting netdata..."
347 run ${NETDATA_PREFIX}/usr/sbin/netdata -pidfile /var/run/netdata.pid "${@}"
348
349 if [ $? -ne 0 ]
350         then
351         echo >&2
352         echo >&2 "SORRY! FAILED TO START NETDATA!"
353         exit 1
354 else
355         echo >&2 "OK. NetData Started!"
356 fi
357
358
359 # -----------------------------------------------------------------------------
360 # save a config file, if it is not already there
361
362 if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
363         then
364         echo >&2 "Downloading default configuration from netdata..."
365         sleep 5
366
367         # remove a possibly obsolete download
368         [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
369
370         # disable a proxy to get data from the local netdata
371         export http_proxy=
372         export https_proxy=
373
374         # try wget
375         wget 2>/dev/null -O "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
376         ret=$?
377
378         # try curl
379         if [ $ret -ne 0 -o ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
380                 then
381                 curl -s -o "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
382                 ret=$?
383         fi
384
385         if [ $ret -eq 0 -a -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
386                 then
387                 mv "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
388                 echo >&2 "New configuration saved for you to edit at ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
389
390                 chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
391                 chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
392         else
393                 echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
394                 [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
395         fi
396 fi
397
398 # -----------------------------------------------------------------------------
399
400 cat <<END
401
402
403 -------------------------------------------------------------------------------
404
405 ok. NetData is installed and is running.
406
407 Hit http://localhost:${NETDATA_PORT}/ from your browser.
408
409 To stop netdata, just kill it, with:
410
411   killall netdata
412
413 To start it, just run it:
414
415   ${NETDATA_PREFIX}/usr/sbin/netdata
416
417 Enjoy!
418
419 END
420
421 # -----------------------------------------------------------------------------
422 # Check for KSM
423
424 ksm_is_available_but_disabled() {
425         cat <<KSM1
426
427 INFORMATION:
428
429 I see you have kernel memory de-duper (called Kernel Same-page Merging,
430 or KSM) available, but it is not currently enabled.
431
432 To enable it run:
433
434 echo 1 >/sys/kernel/mm/ksm/run
435 echo 1000 >/sys/kernel/mm/ksm/sleep_millisecs
436
437 If you enable it, you will save 20-60% of netdata memory.
438
439 KSM1
440 }
441
442 ksm_is_not_available() {
443         cat <<KSM2
444
445 INFORMATION:
446
447 I see you do not have kernel memory de-duper (called Kernel Same-page
448 Merging, or KSM) available.
449
450 To enable it, you need a kernel built with CONFIG_KSM=y
451
452 If you can have it, you will save 20-60% of netdata memory.
453
454 KSM2
455 }
456
457 if [ -f "/sys/kernel/mm/ksm/run" ]
458         then
459         if [ $(cat "/sys/kernel/mm/ksm/run") != "1" ]
460                 then
461                 ksm_is_available_but_disabled
462         fi
463 else
464         ksm_is_not_available
465 fi
466
467 # -----------------------------------------------------------------------------
468 # Check for version.txt
469
470 if [ ! -s web/version.txt ]
471         then
472 cat <<VERMSG
473
474 VERSION UPDATE CHECK:
475
476 The way you downloaded netdata, we cannot find its version.
477 This means the Update check on the dashboard, will not work.
478
479 If you want to have version update check, please re-install it
480 following the procedure in:
481
482 https://github.com/firehol/netdata/wiki/Installation
483
484
485 VERMSG
486 fi
487
488 # -----------------------------------------------------------------------------
489 # Keep un-install info
490
491 cat >netdata-uninstaller.sh <<UNINSTALL
492 #!/bin/bash
493
494 # this script will uninstall netdata
495
496 if [ "\$1" != "--force" ]
497         then
498         echo >&2 "This script will REMOVE netdata from your system."
499         echo >&2 "Run it again with --force to do it."
500         exit 1
501 fi
502
503 echo >&2 "Stopping a possibly running netdata..."
504 killall netdata
505 sleep 2
506
507 deletedir() {
508         if [ ! -z "\$1" -a -d "\$1" ]
509                 then
510                 echo
511                 echo "Deleting directory '\$1' ..."
512                 rm -I -R "\$1"
513         fi
514 }
515
516 if [ ! -z "${NETDATA_PREFIX}" -a -d "${NETDATA_PREFIX}" ]
517         then
518         # installation prefix was given
519
520         deletedir "${NETDATA_PREFIX}"
521
522 else
523         # installation prefix was NOT given
524
525         if [ -f "${NETDATA_PREFIX}/usr/sbin/netdata" ]
526                 then
527                 echo "Deleting ${NETDATA_PREFIX}/usr/sbin/netdata ..."
528                 rm -i "${NETDATA_PREFIX}/usr/sbin/netdata"
529         fi
530
531         deletedir "${NETDATA_PREFIX}/etc/netdata"
532         deletedir "${NETDATA_PREFIX}/usr/share/netdata"
533         deletedir "${NETDATA_PREFIX}/usr/libexec/netdata"
534         deletedir "${NETDATA_PREFIX}/var/cache/netdata"
535         deletedir "${NETDATA_PREFIX}/var/log/netdata"
536 fi
537
538 getent passwd netdata > /dev/null
539 if [ $? -eq 0 ]
540         then
541         echo
542         echo "You may also want to remove the user netdata"
543         echo "by running:"
544         echo "   userdel netdata"
545 fi
546
547 getent group netdata > /dev/null
548 if [ $? -eq 0 ]
549         then
550         echo
551         echo "You may also want to remove the group netdata"
552         echo "by running:"
553         echo "   groupdel netdata"
554 fi
555
556 UNINSTALL
557 chmod 750 netdata-uninstaller.sh
558
559 echo >&2 "Uninstall script generated: ./netdata-uninstaller.sh"