]> arthur.barton.de Git - netdata.git/blob - netdata-installer.sh
Merge pull request #277 from jgeromero/tomcat.chart.sh
[netdata.git] / netdata-installer.sh
1 #!/bin/bash
2
3 # reload the user profile
4 [ -f /etc/profile ] && . /etc/profile
5
6 LC_ALL=C
7
8 # you can set CFLAGS before running installer
9 CFLAGS="${CFLAGS--O3}"
10
11 # keep a log of this command
12 printf "\n# " >>netdata-installer.log
13 date >>netdata-installer.log
14 printf "CFLAGS=\"%s\" " "${CFLAGS}" >>netdata-installer.log
15 printf "%q " "$0" "${@}" >>netdata-installer.log
16 printf "\n" >>netdata-installer.log
17
18 ME="$0"
19 DONOTSTART=0
20 DONOTWAIT=0
21 NETDATA_PREFIX=
22 ZLIB_IS_HERE=0
23
24 usage() {
25         cat <<USAGE
26
27 ${ME} <installer options>
28
29 Valid <installer options> are:
30
31    --install /PATH/TO/INSTALL
32
33         If your give: --install /opt
34         netdata will be installed in /opt/netdata
35
36    --dont-start-it
37
38         Do not (re)start netdata.
39         Just install it.
40
41    --dont-wait
42
43         Do not wait for the user to press ENTER.
44         Start immediately building it.
45
46    --zlib-is-really-here
47
48         If you get errors about missing zlib,
49         but you know it is available,
50         you have a broken pkg-config.
51         Use this option to allow it continue
52         without checking pkg-config.
53
54 Netdata will by default be compiled with gcc optimization -O3
55 If you need to pass different CFLAGS, use something like this:
56
57   CFLAGS="<gcc options>" $ME <installer options>
58
59 For the installer to complete successfully, you will need
60 these packages installed:
61
62    gcc make autoconf automake pkg-config zlib1g-dev
63
64 For the plugins, you will at least need:
65
66    curl node
67
68 USAGE
69 }
70
71 while [ ! -z "${1}" ]
72 do
73         if [ "$1" = "--install" ]
74                 then
75                 NETDATA_PREFIX="${2}/netdata"
76                 shift 2
77         elif [ "$1" = "--zlib-is-really-here" ]
78                 then
79                 ZLIB_IS_HERE=1
80                 shift 1
81         elif [ "$1" = "--dont-start-it" ]
82                 then
83                 DONOTSTART=1
84                 shift 1
85         elif [ "$1" = "--dont-wait" ]
86                 then
87                 DONOTWAIT=1
88                 shift 1
89         elif [ "$1" = "--help" -o "$1" = "-h" ]
90                 then
91                 usage
92                 exit 1
93         else
94                 echo >&2
95                 echo >&2 "ERROR:"
96                 echo >&2 "I cannot understand option '$1'."
97                 usage
98                 exit 1
99         fi
100 done
101
102 cat <<BANNER
103
104 Welcome to netdata!
105 Nice to see you are giving it a try!
106
107 You are about to build and install netdata to your system.
108
109 It will be installed at these locations:
110
111   - the daemon    at ${NETDATA_PREFIX}/usr/sbin/netdata
112   - config files  at ${NETDATA_PREFIX}/etc/netdata
113   - web files     at ${NETDATA_PREFIX}/usr/share/netdata
114   - plugins       at ${NETDATA_PREFIX}/usr/libexec/netdata
115   - cache files   at ${NETDATA_PREFIX}/var/cache/netdata
116   - log files     at ${NETDATA_PREFIX}/var/log/netdata
117   - pid file      at ${NETDATA_PREFIX}/var/run
118
119 This installer allows you to change the installation path.
120 Press Control-C and run the same command with --help for help.
121
122 BANNER
123
124 if [ "${UID}" -ne 0 ]
125         then
126         if [ -z "${NETDATA_PREFIX}" ]
127                 then
128                 cat <<NONROOTNOPREFIX
129
130 Sorry! This will wrong!
131
132 You are attempting to install netdata as non-root, but you plan to install it
133 in system paths.
134
135 Please set an installation prefix, like this:
136
137     $0 ${@} --install /tmp
138
139 or, run the installer as root:
140
141     sudo $0 ${@}
142
143 We suggest to install it as root, or certain data collectors will not be able
144 to work. Netdata drops root privileges when running. So, if you plan to keep
145 it, install it as root to get the full functionality.
146
147 NONROOTNOPREFIX
148                 exit 1
149
150         else
151                 cat <<NONROOT
152
153 IMPORTANT:
154 You are about to install netdata as a non-root user.
155 Netdata will work, but a few data collection modules that
156 require root access will fail.
157
158 If you installing permanently on your system, run the
159 installer like this:
160
161     sudo $0 ${@}
162
163 NONROOT
164         fi
165 fi
166
167 have_autotools=
168 if [ "$(type autoreconf 2> /dev/null)" ]
169 then
170         autoconf_maj_min() {
171                 local maj min IFS=.-
172
173                 maj=$1
174                 min=$2
175
176                 set -- $(autoreconf -V | sed -ne '1s/.* \([^ ]*\)$/\1/p')
177                 eval $maj=\$1 $min=\$2
178         }
179         autoconf_maj_min AMAJ AMIN
180
181         if [ "$AMAJ" -gt 2 ]
182         then
183                 have_autotools=Y
184         elif [ "$AMAJ" -eq 2 -a "$AMIN" -ge 60 ]
185         then
186                 have_autotools=Y
187         else
188                 echo "Found autotools $AMAJ.$AMIN"
189         fi
190 else
191         echo "No autotools found"
192 fi
193
194 if [ ! "$have_autotools" ]
195 then
196         if [ -f configure ]
197         then
198                 echo "Will skip autoreconf step"
199         else
200                 cat <<-"EOF"
201
202         -------------------------------------------------------------------------------
203         autotools 2.60 or later is required
204
205         Sorry, you do not seem to have autotools 2.60 or later, which is
206         required to build from the git sources of netdata.
207
208         You can either install a suitable version of autotools and automake
209         or download a netdata package which does not have these dependencies.
210
211         Source packages where autotools have already been run are available
212         here:
213                https://firehol.org/download/netdata/
214
215         The unsigned/master folder tracks the head of the git tree and released
216         packages are also available.
217         EOF
218                 exit 1
219         fi
220 fi
221
222 if [ ${DONOTWAIT} -eq 0 ]
223         then
224         if [ ! -z "${NETDATA_PREFIX}" ]
225                 then
226                 read -p "Press ENTER to build and install netdata to '${NETDATA_PREFIX}' > "
227         else
228                 read -p "Press ENTER to build and install netdata to your system > "
229         fi
230 fi
231
232 build_error() {
233         cat <<EOF
234
235 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
236
237 Sorry! NetData failed to build...
238
239 You many need to check these:
240
241 1. The package zlib1g-dev has to be installed.
242
243 2. You need basic build tools installed, like:
244
245    gcc make autoconf automake pkg-config
246
247    Autoconf version 2.60 or higher is required
248
249 3. If your system cannot find ZLIB, although it is installed
250    run me with the option:  --zlib-is-really-here
251
252
253 If you still cannot get it to build, ask for help at github:
254
255    https://github.com/firehol/netdata/issues
256
257
258 EOF
259         trap - EXIT
260         exit 1
261 }
262
263 run() {
264         printf >&2 "\n"
265         printf >&2 ":-----------------------------------------------------------------------------\n"
266         printf >&2 "Running command:\n"
267         printf >&2 "\n"
268         printf >&2 "%q " "${@}"
269         printf >&2 "\n"
270         printf >&2 "\n"
271
272         "${@}"
273 }
274
275 if [ ${ZLIB_IS_HERE} -eq 1 ]
276         then
277         shift
278         echo >&2 "ok, assuming zlib is really installed."
279         export ZLIB_CFLAGS=" "
280         export ZLIB_LIBS="-lz"
281 fi
282
283 trap build_error EXIT
284
285 if [ "$have_autotools" ]
286 then
287         run ./autogen.sh || exit 1
288 fi
289
290 run ./configure \
291         --prefix="${NETDATA_PREFIX}/usr" \
292         --sysconfdir="${NETDATA_PREFIX}/etc" \
293         --localstatedir="${NETDATA_PREFIX}/var" \
294         --with-zlib --with-math --with-user=netdata \
295         CFLAGS="${CFLAGS}" || exit 1
296
297 # remove the build_error hook
298 trap - EXIT
299
300 if [ -f src/netdata ]
301         then
302         echo >&2 "Cleaning a possibly old compilation ..."
303         run make clean
304 fi
305
306 echo >&2 "Compiling netdata ..."
307 run make || exit 1
308
309 # backup user configurations
310 for x in apps_groups.conf charts.d.conf
311 do
312         if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}" ]
313                 then
314                 cp -p "${NETDATA_PREFIX}/etc/netdata/${x}" "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup"
315         fi
316 done
317
318 echo >&2 "Installing netdata ..."
319 run make install || exit 1
320
321 # restore user configurations
322 for x in apps_groups.conf charts.d.conf
323 do
324         if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup" ]
325                 then
326                 cp -p "${NETDATA_PREFIX}/etc/netdata/${x}.installer_backup" "${NETDATA_PREFIX}/etc/netdata/${x}"
327         fi
328 done
329
330 if [ ${UID} -eq 0 ]
331         then
332         echo >&2 "Adding netdata user group ..."
333         getent group netdata > /dev/null || run groupadd -r netdata
334
335         echo >&2 "Adding netdata user account ..."
336         getent passwd netdata > /dev/null || run useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
337 fi
338
339
340 # -----------------------------------------------------------------------------
341 # load options from the configuration file
342
343 # create an empty config if it does not exist
344 [ ! -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ] && touch "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
345
346 # function to extract values from the config file
347 config_option() {
348         local key="${1}" value="${2}" line=
349
350         if [ -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
351                 then
352                 line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "${NETDATA_PREFIX}/etc/netdata/netdata.conf" | head -n 1 )"
353                 [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
354         fi
355
356         echo "${value}"
357 }
358
359 # user
360 defuser="netdata"
361 [ ! "${UID}" = "0" ] && defuser="${USER}"
362 NETDATA_USER="$( config_option "run as user" "${defuser}" )"
363
364 NETDATA_WEB_USER="$( config_option "web files owner" "${defuser}" )"
365 NETDATA_WEB_GROUP="$( config_option "web files group" "${NETDATA_WEB_USER}" )"
366
367 # debug flags
368 defdebug=0
369 NETDATA_DEBUG="$( config_option "debug flags" ${defdebug} )"
370
371 # port
372 defport=19999
373 NETDATA_PORT="$( config_option "port" ${defport} )"
374
375 # directories
376 NETDATA_CACHE_DIR="$( config_option "cache directory" "${NETDATA_PREFIX}/var/cache/netdata" )"
377 NETDATA_WEB_DIR="$( config_option "web files directory" "${NETDATA_PREFIX}/usr/share/netdata/web" )"
378 NETDATA_LOG_DIR="$( config_option "log directory" "${NETDATA_PREFIX}/var/log/netdata" )"
379 NETDATA_CONF_DIR="$( config_option "config directory" "${NETDATA_PREFIX}/etc/netdata" )"
380 NETDATA_RUN_DIR="${NETDATA_PREFIX}/var/run"
381
382
383 # -----------------------------------------------------------------------------
384 # prepare the directories
385
386 # this is needed if NETDATA_PREFIX is not empty
387 if [ ! -d "${NETDATA_RUN_DIR}" ]
388         then
389         mkdir -p "${NETDATA_RUN_DIR}" || exit 1
390 fi
391
392 echo >&2 "Fixing directories (user: ${NETDATA_USER})..."
393 for x in "${NETDATA_WEB_DIR}" "${NETDATA_CONF_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"
394 do
395         if [ ! -d "${x}" ]
396                 then
397                 echo >&2 "Creating directory '${x}'"
398                 run mkdir -p "${x}" || exit 1
399         fi
400
401         if [ ${UID} -eq 0 ]
402                 then
403                 if [ "${x}" = "${NETDATA_WEB_DIR}" ]
404                         then
405                         run chown -R "${NETDATA_WEB_USER}:${NETDATA_WEB_GROUP}" "${x}" || echo >&2 "WARNING: Cannot change the ownership of the files in directory ${x} to ${NETDATA_WEB_USER}:${NETDATA_WEB_GROUP}..."
406                 else
407                         run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${x}" || echo >&2 "WARNING: Cannot change the ownership of the files in directory ${x} to ${NETDATA_USER}..."
408                 fi
409         fi
410
411         run chmod 0775 "${x}" || echo >&2 "WARNING: Cannot change the permissions of the directory ${x} to 0755..."
412 done
413
414 if [ ${UID} -eq 0 ]
415         then
416         # fix apps.plugin to be setuid to root
417         run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
418         run chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
419 fi
420
421 # -----------------------------------------------------------------------------
422 # check if we can re-start netdata
423
424 if [ ${DONOTSTART} -eq 1 ]
425         then
426         if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
427                 then
428                 echo >&2 "Generating empty config file in: ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
429                 echo "# Get config from http://127.0.0.1:${NETDATA_PORT}/netdata.conf" >"${NETDATA_PREFIX}/etc/netdata/netdata.conf"
430
431                 if [ "${UID}" -eq 0 ]
432                         then
433                         chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
434                 fi
435                 chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
436         fi
437         echo >&2 "OK. It is now installed and ready."
438         exit 0
439 fi
440
441 # -----------------------------------------------------------------------------
442 # stop a running netdata
443
444 isnetdata() {
445         [ -z "$1" -o ! -f "/proc/$1/stat" ] && return 1
446         [ "$(cat "/proc/$1/stat" | cut -d '(' -f 2 | cut -d ')' -f 1)" = "netdata" ] && return 0
447         return 1
448 }
449
450
451 printf >&2 "Stopping a (possibly) running netdata..."
452 ret=0
453 count=0
454 while [ $ret -eq 0 ]
455 do
456         if [ $count -gt 30 ]
457                 then
458                 echo >&2 "Cannot stop the running netdata."
459                 exit 1
460         fi
461
462         count=$((count + 1))
463
464         pid=$(cat "${NETDATA_RUN_DIR}/netdata.pid" 2>/dev/null)
465         # backwards compatibility
466         [ -z "${pid}" ] && pid=$(cat /var/run/netdata.pid 2>/dev/null)
467         [ -z "${pid}" ] && pid=$(cat /var/run/netdata/netdata.pid 2>/dev/null)
468         
469         isnetdata $pid || pid=
470         if [ ! -z "${pid}" ]
471                 then
472                 run kill $pid 2>/dev/null
473                 ret=$?
474         else
475                 run killall netdata 2>/dev/null
476                 ret=$?
477         fi
478
479         test $ret -eq 0 && printf >&2 "." && sleep 2
480 done
481 echo >&2
482 echo >&2
483
484
485 # -----------------------------------------------------------------------------
486 # run netdata
487
488 echo >&2 "Starting netdata..."
489 run ${NETDATA_PREFIX}/usr/sbin/netdata -pidfile ${NETDATA_RUN_DIR}/netdata.pid "${@}"
490
491 if [ $? -ne 0 ]
492         then
493         echo >&2
494         echo >&2 "SORRY! FAILED TO START NETDATA!"
495         exit 1
496 else
497         echo >&2 "OK. NetData Started!"
498 fi
499
500
501 # -----------------------------------------------------------------------------
502 # save a config file, if it is not already there
503
504 if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
505         then
506         echo >&2 "Downloading default configuration from netdata..."
507         sleep 5
508
509         # remove a possibly obsolete download
510         [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
511
512         # disable a proxy to get data from the local netdata
513         export http_proxy=
514         export https_proxy=
515
516         # try wget
517         wget 2>/dev/null -O "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
518         ret=$?
519
520         # try curl
521         if [ $ret -ne 0 -o ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
522                 then
523                 curl -s -o "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
524                 ret=$?
525         fi
526
527         if [ $ret -eq 0 -a -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
528                 then
529                 mv "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
530                 echo >&2 "New configuration saved for you to edit at ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
531
532                 if [ "${UID}" -eq 0 ]
533                         then
534                         chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
535                 fi
536                 chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
537         else
538                 echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
539                 [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
540         fi
541 fi
542
543 # -----------------------------------------------------------------------------
544 # Check for KSM
545
546 ksm_is_available_but_disabled() {
547         cat <<KSM1
548
549 -------------------------------------------------------------------------------
550 Memory de-duplication instructions
551
552 I see you have kernel memory de-duper (called Kernel Same-page Merging,
553 or KSM) available, but it is not currently enabled.
554
555 To enable it run:
556
557 echo 1 >/sys/kernel/mm/ksm/run
558 echo 1000 >/sys/kernel/mm/ksm/sleep_millisecs
559
560 If you enable it, you will save 40-60% of netdata memory.
561
562 KSM1
563 }
564
565 ksm_is_not_available() {
566         cat <<KSM2
567
568 -------------------------------------------------------------------------------
569 Memory de-duplication not present in your kernel
570
571 It seems you do not have kernel memory de-duper (called Kernel Same-page
572 Merging, or KSM) available.
573
574 To enable it, you need a kernel built with CONFIG_KSM=y
575
576 If you can have it, you will save 40-60% of netdata memory.
577
578 KSM2
579 }
580
581 if [ -f "/sys/kernel/mm/ksm/run" ]
582         then
583         if [ $(cat "/sys/kernel/mm/ksm/run") != "1" ]
584                 then
585                 ksm_is_available_but_disabled
586         fi
587 else
588         ksm_is_not_available
589 fi
590
591 # -----------------------------------------------------------------------------
592 # Check for version.txt
593
594 if [ ! -s web/version.txt ]
595         then
596 cat <<VERMSG
597
598 -------------------------------------------------------------------------------
599 Version update check warning
600
601 The way you downloaded netdata, we cannot find its version. This means the
602 Update check on the dashboard, will not work.
603
604 If you want to have version update check, please re-install it
605 following the procedure in:
606
607 https://github.com/firehol/netdata/wiki/Installation
608
609 VERMSG
610 fi
611
612 # -----------------------------------------------------------------------------
613 # apps.plugin warning
614
615 if [ "${UID}" -ne 0 ]
616         then
617 cat <<SETUID_WARNING
618
619 -------------------------------------------------------------------------------
620 apps.plugin needs privileges
621
622 Since you have installed netdata as a normal user, to have apps.plugin collect
623 all the needed data, you have to give it the access rights it needs, by running
624 these commands:
625
626         sudo chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
627         sudo chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
628
629 The commands allow apps.plugin to run as root.
630
631 apps.plugin is performing a hard-coded function of data collection for all
632 running processes. It cannot be instructed from the netdata daemon to perform
633 any task, so it is pretty safe to do this.
634
635 SETUID_WARNING
636 fi
637
638 # -----------------------------------------------------------------------------
639 # Keep un-install info
640
641 cat >netdata-uninstaller.sh <<UNINSTALL
642 #!/bin/bash
643
644 # this script will uninstall netdata
645
646 if [ "\$1" != "--force" ]
647         then
648         echo >&2 "This script will REMOVE netdata from your system."
649         echo >&2 "Run it again with --force to do it."
650         exit 1
651 fi
652
653 echo >&2 "Stopping a possibly running netdata..."
654 killall netdata
655 sleep 2
656
657 deletedir() {
658         if [ ! -z "\$1" -a -d "\$1" ]
659                 then
660                 echo
661                 echo "Deleting directory '\$1' ..."
662                 rm -I -R "\$1"
663         fi
664 }
665
666 if [ ! -z "${NETDATA_PREFIX}" -a -d "${NETDATA_PREFIX}" ]
667         then
668         # installation prefix was given
669
670         deletedir "${NETDATA_PREFIX}"
671
672 else
673         # installation prefix was NOT given
674
675         if [ -f "${NETDATA_PREFIX}/usr/sbin/netdata" ]
676                 then
677                 echo "Deleting ${NETDATA_PREFIX}/usr/sbin/netdata ..."
678                 rm -i "${NETDATA_PREFIX}/usr/sbin/netdata"
679         fi
680
681         deletedir "${NETDATA_PREFIX}/etc/netdata"
682         deletedir "${NETDATA_PREFIX}/usr/share/netdata"
683         deletedir "${NETDATA_PREFIX}/usr/libexec/netdata"
684         deletedir "${NETDATA_PREFIX}/var/cache/netdata"
685         deletedir "${NETDATA_PREFIX}/var/log/netdata"
686 fi
687
688 getent passwd netdata > /dev/null
689 if [ $? -eq 0 ]
690         then
691         echo
692         echo "You may also want to remove the user netdata"
693         echo "by running:"
694         echo "   userdel netdata"
695 fi
696
697 getent group netdata > /dev/null
698 if [ $? -eq 0 ]
699         then
700         echo
701         echo "You may also want to remove the group netdata"
702         echo "by running:"
703         echo "   groupdel netdata"
704 fi
705
706 UNINSTALL
707 chmod 750 netdata-uninstaller.sh
708
709 # -----------------------------------------------------------------------------
710
711 cat <<END
712
713
714 -------------------------------------------------------------------------------
715
716 OK. NetData is installed and it is running.
717
718 -------------------------------------------------------------------------------
719
720
721 Hit http://localhost:${NETDATA_PORT}/ from your browser.
722
723 To stop netdata, just kill it, with:
724
725   killall netdata
726
727 To start it, just run it:
728
729   ${NETDATA_PREFIX}/usr/sbin/netdata
730
731
732 Enjoy!
733
734                  Give netdata a Github Star, at:
735
736              https://github.com/firehol/netdata/wiki
737
738
739 END
740 echo >&2 "Uninstall script generated: ./netdata-uninstaller.sh"