]> arthur.barton.de Git - netdata.git/blob - netdata-installer.sh
apps.plugin should not log to syslog #225
[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   - pid file      at ${NETDATA_PREFIX}/var/run
115
116 This installer allows you to change the installation path.
117 Press Control-C and run the same command with --help for help.
118
119 BANNER
120
121 if [ ! "${UID}" = 0 -o "$1" = "-h" -o "$1" = "--help" ]
122         then
123         echo >&2
124         echo >&2 "You have to run netdata as root."
125         echo >&2 "The netdata daemon will drop priviliges"
126         echo >&2 "but you have to start it as root."
127         echo >&2
128         exit 1
129 fi
130
131 have_autotools=
132 if [ "$(type autoreconf 2> /dev/null)" ]
133 then
134         autoconf_maj_min() {
135                 local maj min IFS=.-
136
137                 maj=$1
138                 min=$2
139
140                 set -- $(autoreconf -V | sed -ne '1s/.* \([^ ]*\)$/\1/p')
141                 eval $maj=\$1 $min=\$2
142         }
143         autoconf_maj_min AMAJ AMIN
144
145         if [ "$AMAJ" -gt 2 ]
146         then
147                 have_autotools=Y
148         elif [ "$AMAJ" -eq 2 -a "$AMIN" -ge 60 ]
149         then
150                 have_autotools=Y
151         else
152                 echo "Found autotools $AMAJ.$AMIN"
153         fi
154 else
155         echo "No autotools found"
156 fi
157
158 if [ ! "$have_autotools" ]
159 then
160         if [ -f configure ]
161         then
162                 echo "Will skip autoreconf step"
163         else
164                 cat <<-"EOF"
165
166         Sorry, you do not seem to have autotools 2.60 or later, which is
167         required to build from the git sources of netdata.
168
169         You can either install a suitable version of autotools and automake
170         or download a netdata package which does not have these dependencies.
171
172         Source packages where autotools have already been run are available
173         here:
174                https://firehol.org/download/netdata/
175
176         The unsigned/master folder tracks the head of the git tree and released
177         packages are also available.
178         EOF
179                 exit 1
180         fi
181 fi
182
183 if [ ${DONOTWAIT} -eq 0 ]
184         then
185         if [ ! -z "${NETDATA_PREFIX}" ]
186                 then
187                 read -p "Press ENTER to build and install netdata to '${NETDATA_PREFIX}' > "
188         else
189                 read -p "Press ENTER to build and install netdata to your system > "
190         fi
191 fi
192
193 # reload the profile
194 [ -f /etc/profile ] && . /etc/profile
195
196 build_error() {
197         cat <<EOF
198
199 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
200
201 Sorry! NetData failed to build...
202
203 You many need to check these:
204
205 1. The package zlib1g-dev has to be installed.
206
207 2. You need basic build tools installed, like:
208
209    gcc make autoconf automake pkg-config
210
211    Autoconf version 2.60 or higher is required
212
213 3. If your system cannot find ZLIB, although it is installed
214    run me with the option:  --zlib-is-really-here
215
216 If you still cannot get it to build, ask for help at github:
217
218    https://github.com/firehol/netdata/issues
219
220
221 EOF
222         trap - EXIT
223         exit 1
224 }
225
226 run() {
227         printf >&2 ":-----------------------------------------------------------------------------\n"
228         printf >&2 "Running command:\n"
229         printf >&2 "\n"
230         printf >&2 "%q " "${@}"
231         printf >&2 "\n"
232         printf >&2 "\n"
233
234         "${@}"
235 }
236
237 if [ ${ZLIB_IS_HERE} -eq 1 ]
238         then
239         shift
240         echo >&2 "ok, assuming zlib is really installed."
241         export ZLIB_CFLAGS=" "
242         export ZLIB_LIBS="-lz"
243 fi
244
245 trap build_error EXIT
246
247 if [ "$have_autotools" ]
248 then
249         echo >&2 "Running ./autogen.sh ..."
250         run ./autogen.sh || exit 1
251 fi
252
253 echo >&2 "Running ./configure ..."
254 run ./configure \
255         --prefix="${NETDATA_PREFIX}/usr" \
256         --sysconfdir="${NETDATA_PREFIX}/etc" \
257         --localstatedir="${NETDATA_PREFIX}/var" \
258         --with-zlib --with-math --with-user=netdata \
259         CFLAGS="${CFLAGS}" || exit 1
260
261 # remove the build_error hook
262 trap - EXIT
263
264 if [ -f src/netdata ]
265         then
266         echo >&2 "Cleaning a possibly old compilation ..."
267         run make clean
268 fi
269
270 echo >&2 "Compiling netdata ..."
271 run make || exit 1
272
273 echo >&2 "Installing netdata ..."
274 run make install || exit 1
275
276 echo >&2 "Adding netdata user group ..."
277 getent group netdata > /dev/null || run groupadd -r netdata
278
279 echo >&2 "Adding netdata user account ..."
280 getent passwd netdata > /dev/null || run useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
281
282
283
284 # -----------------------------------------------------------------------------
285 # load options from the configuration file
286
287 # create an empty config if it does not exist
288 [ ! -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ] && touch "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
289
290 # function to extract values from the config file
291 config_option() {
292         local key="${1}" value="${2}" line=
293
294         if [ -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
295                 then
296                 line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "${NETDATA_PREFIX}/etc/netdata/netdata.conf" | head -n 1 )"
297                 [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
298         fi
299
300         echo "${value}"
301 }
302
303 # user
304 defuser="netdata"
305 [ ! "${UID}" = "0" ] && defuser="${USER}"
306 NETDATA_USER="$( config_option "run as user" "${defuser}" )"
307
308 # debug flags
309 defdebug=0
310 NETDATA_DEBUG="$( config_option "debug flags" ${defdebug} )"
311
312 # port
313 defport=19999
314 NETDATA_PORT="$( config_option "port" ${defport} )"
315
316 # directories
317 NETDATA_CACHE_DIR="$( config_option "cache directory" "${NETDATA_PREFIX}/var/cache/netdata" )"
318 NETDATA_WEB_DIR="$( config_option "web files directory" "${NETDATA_PREFIX}/usr/share/netdata/web" )"
319 NETDATA_LOG_DIR="$( config_option "log directory" "${NETDATA_PREFIX}/var/log/netdata" )"
320 NETDATA_CONF_DIR="$( config_option "config directory" "${NETDATA_PREFIX}/etc/netdata" )"
321 NETDATA_RUN_DIR="${NETDATA_PREFIX}/var/run"
322
323
324 # -----------------------------------------------------------------------------
325 # prepare the directories
326
327 # this is needed if NETDATA_PREFIX is not empty
328 if [ ! -d "${NETDATA_RUN_DIR}" ]
329         then
330         mkdir -p "${NETDATA_RUN_DIR}" || exit 1
331 fi
332
333 echo >&2 "Fixing directory permissions for user ${NETDATA_USER}..."
334 for x in "${NETDATA_WEB_DIR}" "${NETDATA_CONF_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"
335 do
336         if [ ! -d "${x}" ]
337                 then
338                 echo >&2 "Creating directory '${x}'"
339                 run mkdir -p "${x}" || exit 1
340         fi
341         run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${x}" || echo >&2 "WARNING: Cannot change the ownership of the files in directory ${x} to ${NETDATA_USER}..."
342         run chmod 0775 "${x}" || echo >&2 "WARNING: Cannot change the permissions of the directory ${x} to 0755..."
343 done
344
345 # fix apps.plugin to be setuid to root
346 run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
347 run chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
348
349
350 # -----------------------------------------------------------------------------
351 # check if we can re-start netdata
352
353 if [ ${DONOTSTART} -eq 1 ]
354         then
355         if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
356                 then
357                 echo >&2 "Generating empty config file in: ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
358                 echo "# Get config from http://127.0.0.1:${NETDATA_PORT}/netdata.conf" >"${NETDATA_PREFIX}/etc/netdata/netdata.conf"
359                 chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
360                 chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
361         fi
362         echo >&2 "OK. It is now installed and ready."
363         exit 0
364 fi
365
366 # -----------------------------------------------------------------------------
367 # stop a running netdata
368
369 isnetdata() {
370         [ -z "$1" -o ! -f "/proc/$1/stat" ] && return 1
371         [ "$(cat "/proc/$1/stat" | cut -d '(' -f 2 | cut -d ')' -f 1)" = "netdata" ] && return 0
372         return 1
373 }
374
375
376 printf >&2 "Stopping a (possibly) running netdata..."
377 ret=0
378 count=0
379 while [ $ret -eq 0 ]
380 do
381         if [ $count -gt 30 ]
382                 then
383                 echo >&2 "Cannot stop the running netdata."
384                 exit 1
385         fi
386
387         count=$((count + 1))
388
389         pid=$(cat "${NETDATA_RUN_DIR}/netdata.pid" 2>/dev/null)
390         # backwards compatibility
391         [ -z "${pid}" ] && pid=$(cat /var/run/netdata.pid 2>/dev/null)
392         [ -z "${pid}" ] && pid=$(cat /var/run/netdata/netdata.pid 2>/dev/null)
393         
394         isnetdata $pid || pid=
395         if [ ! -z "${pid}" ]
396                 then
397                 run kill $pid 2>/dev/null
398                 ret=$?
399         else
400                 run killall netdata 2>/dev/null
401                 ret=$?
402         fi
403
404         test $ret -eq 0 && printf >&2 "." && sleep 2
405 done
406 echo >&2
407 echo >&2
408
409
410 # -----------------------------------------------------------------------------
411 # run netdata
412
413 echo >&2 "Starting netdata..."
414 run ${NETDATA_PREFIX}/usr/sbin/netdata -pidfile ${NETDATA_RUN_DIR}/netdata.pid "${@}"
415
416 if [ $? -ne 0 ]
417         then
418         echo >&2
419         echo >&2 "SORRY! FAILED TO START NETDATA!"
420         exit 1
421 else
422         echo >&2 "OK. NetData Started!"
423 fi
424
425
426 # -----------------------------------------------------------------------------
427 # save a config file, if it is not already there
428
429 if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
430         then
431         echo >&2 "Downloading default configuration from netdata..."
432         sleep 5
433
434         # remove a possibly obsolete download
435         [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
436
437         # disable a proxy to get data from the local netdata
438         export http_proxy=
439         export https_proxy=
440
441         # try wget
442         wget 2>/dev/null -O "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
443         ret=$?
444
445         # try curl
446         if [ $ret -ne 0 -o ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
447                 then
448                 curl -s -o "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
449                 ret=$?
450         fi
451
452         if [ $ret -eq 0 -a -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
453                 then
454                 mv "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
455                 echo >&2 "New configuration saved for you to edit at ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
456
457                 chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
458                 chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
459         else
460                 echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
461                 [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
462         fi
463 fi
464
465 # -----------------------------------------------------------------------------
466
467 cat <<END
468
469
470 -------------------------------------------------------------------------------
471
472 ok. NetData is installed and is running.
473
474 Hit http://localhost:${NETDATA_PORT}/ from your browser.
475
476 To stop netdata, just kill it, with:
477
478   killall netdata
479
480 To start it, just run it:
481
482   ${NETDATA_PREFIX}/usr/sbin/netdata
483
484 Enjoy!
485
486 END
487
488 # -----------------------------------------------------------------------------
489 # Check for KSM
490
491 ksm_is_available_but_disabled() {
492         cat <<KSM1
493
494 INFORMATION:
495
496 I see you have kernel memory de-duper (called Kernel Same-page Merging,
497 or KSM) available, but it is not currently enabled.
498
499 To enable it run:
500
501 echo 1 >/sys/kernel/mm/ksm/run
502 echo 1000 >/sys/kernel/mm/ksm/sleep_millisecs
503
504 If you enable it, you will save 20-60% of netdata memory.
505
506 KSM1
507 }
508
509 ksm_is_not_available() {
510         cat <<KSM2
511
512 INFORMATION:
513
514 I see you do not have kernel memory de-duper (called Kernel Same-page
515 Merging, or KSM) available.
516
517 To enable it, you need a kernel built with CONFIG_KSM=y
518
519 If you can have it, you will save 20-60% of netdata memory.
520
521 KSM2
522 }
523
524 if [ -f "/sys/kernel/mm/ksm/run" ]
525         then
526         if [ $(cat "/sys/kernel/mm/ksm/run") != "1" ]
527                 then
528                 ksm_is_available_but_disabled
529         fi
530 else
531         ksm_is_not_available
532 fi
533
534 # -----------------------------------------------------------------------------
535 # Check for version.txt
536
537 if [ ! -s web/version.txt ]
538         then
539 cat <<VERMSG
540
541 VERSION UPDATE CHECK:
542
543 The way you downloaded netdata, we cannot find its version.
544 This means the Update check on the dashboard, will not work.
545
546 If you want to have version update check, please re-install it
547 following the procedure in:
548
549 https://github.com/firehol/netdata/wiki/Installation
550
551
552 VERMSG
553 fi
554
555 # -----------------------------------------------------------------------------
556 # Keep un-install info
557
558 cat >netdata-uninstaller.sh <<UNINSTALL
559 #!/bin/bash
560
561 # this script will uninstall netdata
562
563 if [ "\$1" != "--force" ]
564         then
565         echo >&2 "This script will REMOVE netdata from your system."
566         echo >&2 "Run it again with --force to do it."
567         exit 1
568 fi
569
570 echo >&2 "Stopping a possibly running netdata..."
571 killall netdata
572 sleep 2
573
574 deletedir() {
575         if [ ! -z "\$1" -a -d "\$1" ]
576                 then
577                 echo
578                 echo "Deleting directory '\$1' ..."
579                 rm -I -R "\$1"
580         fi
581 }
582
583 if [ ! -z "${NETDATA_PREFIX}" -a -d "${NETDATA_PREFIX}" ]
584         then
585         # installation prefix was given
586
587         deletedir "${NETDATA_PREFIX}"
588
589 else
590         # installation prefix was NOT given
591
592         if [ -f "${NETDATA_PREFIX}/usr/sbin/netdata" ]
593                 then
594                 echo "Deleting ${NETDATA_PREFIX}/usr/sbin/netdata ..."
595                 rm -i "${NETDATA_PREFIX}/usr/sbin/netdata"
596         fi
597
598         deletedir "${NETDATA_PREFIX}/etc/netdata"
599         deletedir "${NETDATA_PREFIX}/usr/share/netdata"
600         deletedir "${NETDATA_PREFIX}/usr/libexec/netdata"
601         deletedir "${NETDATA_PREFIX}/var/cache/netdata"
602         deletedir "${NETDATA_PREFIX}/var/log/netdata"
603 fi
604
605 getent passwd netdata > /dev/null
606 if [ $? -eq 0 ]
607         then
608         echo
609         echo "You may also want to remove the user netdata"
610         echo "by running:"
611         echo "   userdel netdata"
612 fi
613
614 getent group netdata > /dev/null
615 if [ $? -eq 0 ]
616         then
617         echo
618         echo "You may also want to remove the group netdata"
619         echo "by running:"
620         echo "   groupdel netdata"
621 fi
622
623 UNINSTALL
624 chmod 750 netdata-uninstaller.sh
625
626 echo >&2 "Uninstall script generated: ./netdata-uninstaller.sh"