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