]> arthur.barton.de Git - netdata.git/blob - netdata-installer.sh
faster number parsing
[netdata.git] / netdata-installer.sh
1 #!/usr/bin/env bash
2
3 # reload the user profile
4 [ -f /etc/profile ] && . /etc/profile
5
6 export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
7
8 # fix PKG_CHECK_MODULES error
9 if [ -d /usr/share/aclocal ]
10 then
11         ACLOCAL_PATH=${ACLOCAL_PATH-/usr/share/aclocal}
12         export ACLOCAL_PATH
13 fi
14
15 LC_ALL=C
16 umask 002
17
18 # Be nice on production environments
19 renice 19 $$ >/dev/null 2>/dev/null
20
21 processors=$(cat /proc/cpuinfo  | grep ^processor | wc -l)
22 [ $(( processors )) -lt 1 ] && processors=1
23
24 # you can set CFLAGS before running installer
25 CFLAGS="${CFLAGS--O2}"
26 [ "z${CFLAGS}" = "z-O3" ] && CFLAGS="-O2"
27
28 # keep a log of this command
29 printf "\n# " >>netdata-installer.log
30 date >>netdata-installer.log
31 printf "CFLAGS=\"%s\" " "${CFLAGS}" >>netdata-installer.log
32 printf "%q " "$0" "${@}" >>netdata-installer.log
33 printf "\n" >>netdata-installer.log
34
35 REINSTALL_PWD="${PWD}"
36 REINSTALL_COMMAND="$(printf "%q " "$0" "${@}"; printf "\n")"
37
38 banner() {
39     local   l1="  ^"                                                                      \
40             l2="  |.-.   .-.   .-.   .-.   .-.   .-.   .-.   .-.   .-.   .-.   .-.   .-.   .-"  \
41             l3="  |   '-'   '-'   '-'   '-'   '-'   '-'   '-'   '-'   '-'   '-'   '-'   '-'  "  \
42             l4="  +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+--->" \
43             sp="                                                                        " \
44             netdata="netdata" start end msg="${*}"
45
46     [ ${#msg} -lt ${#netdata} ] && msg="${msg}${sp:0:$(( ${#netdata} - ${#msg}))}"
47     [ ${#msg} -gt $(( ${#l2} - 20 )) ] && msg="${msg:0:$(( ${#l2} - 23 ))}..."
48
49     start="$(( ${#l2} / 2 - 4 ))"
50     [ $(( start + ${#msg} + 4 )) -gt ${#l2} ] && start=$((${#l2} - ${#msg} - 4))
51     end=$(( ${start} + ${#msg} + 4 ))
52
53     echo >&2
54     echo >&2 "${l1}"
55     echo >&2 "${l2:0:start}${sp:0:2}${netdata}${sp:0:$((end - start - 2 - ${#netdata}))}${l2:end:$((${#l2} - end))}"
56     echo >&2 "${l3:0:start}${sp:0:2}${msg}${sp:0:2}${l3:end:$((${#l2} - end))}"
57     echo >&2 "${l4}"
58     echo >&2
59 }
60
61 service="$(which service 2>/dev/null || command -v service 2>/dev/null)"
62 systemctl="$(which systemctl 2>/dev/null || command -v systemctl 2>/dev/null)"
63 service() {
64     local cmd="${1}" action="${2}"
65
66     if [ ! -z "${service}" ]
67     then
68         run "${service}" "${cmd}" "${action}"
69         return $?
70     elif [ ! -z "${systemctl}" ]
71     then
72         run "${systemctl}" "${action}" "${cmd}"
73         return $?
74     fi
75     return 1
76 }
77
78 ME="$0"
79 DONOTSTART=0
80 DONOTWAIT=0
81 NETDATA_PREFIX=
82 LIBS_ARE_HERE=0
83
84 usage() {
85     banner "installer command line options"
86     cat <<USAGE
87
88 ${ME} <installer options>
89
90 Valid <installer options> are:
91
92    --install /PATH/TO/INSTALL
93
94         If your give: --install /opt
95         netdata will be installed in /opt/netdata
96
97    --dont-start-it
98
99         Do not (re)start netdata.
100         Just install it.
101
102    --dont-wait
103
104         Do not wait for the user to press ENTER.
105         Start immediately building it.
106
107    --zlib-is-really-here
108    --libs-are-really-here
109
110         If you get errors about missing zlib,
111         or libuuid but you know it is available,
112         you have a broken pkg-config.
113         Use this option to allow it continue
114         without checking pkg-config.
115
116 Netdata will by default be compiled with gcc optimization -O2
117 If you need to pass different CFLAGS, use something like this:
118
119   CFLAGS="<gcc options>" ${ME} <installer options>
120
121 For the installer to complete successfully, you will need
122 these packages installed:
123
124    gcc make autoconf automake pkg-config zlib1g-dev (or zlib-devel)
125    uuid-dev (or libuuid-devel)
126
127 For the plugins, you will at least need:
128
129    curl nodejs
130
131 USAGE
132 }
133
134 md5sum="$(which md5sum 2>/dev/null || command -v md5sum 2>/dev/null)"
135 get_git_config_signatures() {
136     local x s file md5
137
138     [ ! -d "conf.d" ] && echo >&2 "Wrong directory." && return 1
139     [ -z "${md5sum}" -o ! -x "${md5sum}" ] && echo >&2 "No md5sum command." && return 1
140
141     echo >configs.signatures.tmp
142
143     for x in $(find conf.d -name \*.conf)
144     do
145             x="${x/conf.d\//}"
146             echo "${x}"
147             for c in $(git log --follow "conf.d/${x}" | grep ^commit | cut -d ' ' -f 2)
148             do
149                     git checkout ${c} "conf.d/${x}" || continue
150                     s="$(cat "conf.d/${x}" | md5sum | cut -d ' ' -f 1)"
151                     echo >>configs.signatures.tmp "${s}:${x}"
152                     echo "    ${s}"
153             done
154             git checkout HEAD "conf.d/${x}" || break
155     done
156
157     cat configs.signatures.tmp |\
158         grep -v "^$" |\
159         sort -u |\
160         {
161             echo "declare -A configs_signatures=("
162             IFS=":"
163             while read md5 file
164             do
165                 echo "  ['${md5}']='${file}'"
166             done
167             echo ")"
168         } >configs.signatures
169
170     rm configs.signatures.tmp
171
172     return 0
173 }
174
175
176 while [ ! -z "${1}" ]
177 do
178     if [ "$1" = "--install" ]
179         then
180         NETDATA_PREFIX="${2}/netdata"
181         shift 2
182     elif [ "$1" = "--zlib-is-really-here" -o "$1" = "--libs-are-really-here" ]
183         then
184         LIBS_ARE_HERE=1
185         shift 1
186     elif [ "$1" = "--dont-start-it" ]
187         then
188         DONOTSTART=1
189         shift 1
190     elif [ "$1" = "--dont-wait" ]
191         then
192         DONOTWAIT=1
193         shift 1
194     elif [ "$1" = "--help" -o "$1" = "-h" ]
195         then
196         usage
197         exit 1
198     elif [ "$1" = "get_git_config_signatures" ]
199         then
200         get_git_config_signatures && exit 0
201         exit 1
202     else
203         echo >&2
204         echo >&2 "ERROR:"
205         echo >&2 "I cannot understand option '$1'."
206         usage
207         exit 1
208     fi
209 done
210
211 banner "real-time performance monitoring, done right!"
212 cat <<BANNER
213
214   You are about to build and install netdata to your system.
215
216   It will be installed at these locations:
217
218    - the daemon    at ${NETDATA_PREFIX}/usr/sbin/netdata
219    - config files  at ${NETDATA_PREFIX}/etc/netdata
220    - web files     at ${NETDATA_PREFIX}/usr/share/netdata
221    - plugins       at ${NETDATA_PREFIX}/usr/libexec/netdata
222    - cache files   at ${NETDATA_PREFIX}/var/cache/netdata
223    - db files      at ${NETDATA_PREFIX}/var/lib/netdata
224    - log files     at ${NETDATA_PREFIX}/var/log/netdata
225    - pid file      at ${NETDATA_PREFIX}/var/run
226
227   This installer allows you to change the installation path.
228   Press Control-C and run the same command with --help for help.
229
230 BANNER
231
232 if [ "${UID}" -ne 0 ]
233     then
234     if [ -z "${NETDATA_PREFIX}" ]
235         then
236         banner "wrong command line options!"
237         cat <<NONROOTNOPREFIX
238
239 Sorry! This will fail!
240
241 You are attempting to install netdata as non-root, but you plan to install it
242 in system paths.
243
244 Please set an installation prefix, like this:
245
246     $0 ${@} --install /tmp
247
248 or, run the installer as root:
249
250     sudo $0 ${@}
251
252 We suggest to install it as root, or certain data collectors will not be able
253 to work. Netdata drops root privileges when running. So, if you plan to keep
254 it, install it as root to get the full functionality.
255
256 NONROOTNOPREFIX
257         exit 1
258
259     else
260         cat <<NONROOT
261
262 IMPORTANT:
263 You are about to install netdata as a non-root user.
264 Netdata will work, but a few data collection modules that
265 require root access will fail.
266
267 If you installing permanently on your system, run the
268 installer like this:
269
270     sudo $0 ${@}
271
272 NONROOT
273     fi
274 fi
275
276 have_autotools=
277 if [ "$(type autoreconf 2> /dev/null)" ]
278 then
279     autoconf_maj_min() {
280         local maj min IFS=.-
281
282         maj=$1
283         min=$2
284
285         set -- $(autoreconf -V | sed -ne '1s/.* \([^ ]*\)$/\1/p')
286         eval $maj=\$1 $min=\$2
287     }
288     autoconf_maj_min AMAJ AMIN
289
290     if [ "$AMAJ" -gt 2 ]
291     then
292         have_autotools=Y
293     elif [ "$AMAJ" -eq 2 -a "$AMIN" -ge 60 ]
294     then
295         have_autotools=Y
296     else
297         echo "Found autotools $AMAJ.$AMIN"
298     fi
299 else
300     echo "No autotools found"
301 fi
302
303 if [ ! "$have_autotools" ]
304 then
305     if [ -f configure ]
306     then
307         echo "Will skip autoreconf step"
308     else
309         banner "autotools v2.60 required"
310         cat <<"EOF"
311
312 -------------------------------------------------------------------------------
313 autotools 2.60 or later is required
314
315 Sorry, you do not seem to have autotools 2.60 or later, which is
316 required to build from the git sources of netdata.
317
318 You can either install a suitable version of autotools and automake
319 or download a netdata package which does not have these dependencies.
320
321 Source packages where autotools have already been run are available
322 here:
323        https://firehol.org/download/netdata/
324
325 The unsigned/master folder tracks the head of the git tree and released
326 packages are also available.
327 EOF
328         exit 1
329     fi
330 fi
331
332 if [ ${DONOTWAIT} -eq 0 ]
333     then
334     if [ ! -z "${NETDATA_PREFIX}" ]
335         then
336         read -p "Press ENTER to build and install netdata to '${NETDATA_PREFIX}' > "
337     else
338         read -p "Press ENTER to build and install netdata to your system > "
339     fi
340 fi
341
342 build_error() {
343     banner "sorry, it failed to build..."
344     cat <<EOF
345
346 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
347
348 Sorry! netdata failed to build...
349
350 You many need to check these:
351
352 1. The package uuid-dev (or libuuid-devel) has to be installed.
353
354    If your system cannot find libuuid, although it is installed
355    run me with the option:  --libs-are-really-here
356
357 2. The package zlib1g-dev (or zlib-devel) has to be installed.
358
359    If your system cannot find zlib, although it is installed
360    run me with the option:  --libs-are-really-here
361
362 3. You need basic build tools installed, like:
363
364    gcc make autoconf automake pkg-config
365
366    Autoconf version 2.60 or higher is required.
367
368 If you still cannot get it to build, ask for help at github:
369
370    https://github.com/firehol/netdata/issues
371
372
373 EOF
374     trap - EXIT
375     exit 1
376 }
377
378 run() {
379     printf >>netdata-installer.log "# "
380     printf >>netdata-installer.log "%q " "${@}"
381     printf >>netdata-installer.log " ... "
382
383     printf >&2 "\n"
384     printf >&2 ":-----------------------------------------------------------------------------\n"
385     printf >&2 "Running command:\n"
386     printf >&2 "\n"
387     printf >&2 "%q " "${@}"
388     printf >&2 "\n"
389
390     "${@}"
391
392     local ret=$?
393     if [ ${ret} -ne 0 ]
394         then
395         printf >>netdata-installer.log "FAILED!\n"
396     else
397         printf >>netdata-installer.log "OK\n"
398     fi
399
400     return ${ret}
401 }
402
403 if [ ${LIBS_ARE_HERE} -eq 1 ]
404     then
405     shift
406     echo >&2 "ok, assuming libs are really installed."
407     export ZLIB_CFLAGS=" "
408     export ZLIB_LIBS="-lz"
409     export UUID_CFLAGS=" "
410     export UUID_LIBS="-luuid"
411 fi
412
413 trap build_error EXIT
414
415 if [ "$have_autotools" ]
416 then
417     run ./autogen.sh || exit 1
418 fi
419
420 run ./configure \
421     --prefix="${NETDATA_PREFIX}/usr" \
422     --sysconfdir="${NETDATA_PREFIX}/etc" \
423     --localstatedir="${NETDATA_PREFIX}/var" \
424     --with-zlib --with-math --with-user=netdata \
425     CFLAGS="${CFLAGS}" || exit 1
426
427 # remove the build_error hook
428 trap - EXIT
429
430 if [ -f src/netdata ]
431     then
432     echo >&2 "Cleaning a possibly old compilation ..."
433     run make clean
434 fi
435
436 echo >&2 "Compiling netdata ..."
437 run make -j${processors} || exit 1
438
439 if [ "${BASH_VERSINFO[0]}" -ge "4" ]
440 then
441     declare -A configs_signatures=()
442     if [ -f "configs.signatures" ]
443         then
444         source "configs.signatures" || echo >&2 "ERROR: Failed to load configs.signatures !"
445     fi
446 fi
447
448 # migrate existing configuration files
449 # for node.d and charts.d
450 if [ -d "${NETDATA_PREFIX}/etc/netdata" ]
451     then
452     # the configuration directory exists
453
454     if [ ! -d "${NETDATA_PREFIX}/etc/netdata/charts.d" ]
455         then
456         run mkdir "${NETDATA_PREFIX}/etc/netdata/charts.d"
457     fi
458
459     # move the charts.d config files
460     for x in apache ap cpu_apps cpufreq example exim hddtemp load_average mem_apps mysql nginx nut opensips phpfpm postfix sensors squid tomcat
461     do
462         for y in "" ".old" ".orig"
463         do
464             if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" -a ! -f "${NETDATA_PREFIX}/etc/netdata/charts.d/${x}.conf${y}" ]
465                 then
466                 run mv -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" "${NETDATA_PREFIX}/etc/netdata/charts.d/${x}.conf${y}"
467             fi
468         done
469     done
470
471     if [ ! -d "${NETDATA_PREFIX}/etc/netdata/node.d" ]
472         then
473         run mkdir "${NETDATA_PREFIX}/etc/netdata/node.d"
474     fi
475
476     # move the node.d config files
477     for x in named sma_webbox snmp
478     do
479         for y in "" ".old" ".orig"
480         do
481             if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" -a ! -f "${NETDATA_PREFIX}/etc/netdata/node.d/${x}.conf${y}" ]
482                 then
483                 run mv -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" "${NETDATA_PREFIX}/etc/netdata/node.d/${x}.conf${y}"
484             fi
485         done
486     done
487 fi
488
489 # backup user configurations
490 installer_backup_suffix="${PID}.${RANDOM}"
491 for x in $(find "${NETDATA_PREFIX}/etc/netdata/" -name '*.conf' -type f)
492 do
493     if [ -f "${x}" ]
494         then
495         # make a backup of the configuration file
496         cp -p "${x}" "${x}.old"
497
498         if [ -z "${md5sum}" -o ! -x "${md5sum}" ]
499             then
500             # we don't have md5sum - keep it
501             cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
502         else
503             # find it relative filename
504             f="${x/*\/etc\/netdata\//}"
505
506             # find its checksum
507             md5="$(cat "${x}" | ${md5sum} | cut -d ' ' -f 1)"
508
509             # copy the original
510             if [ -f "conf.d/${f}" ]
511                 then
512                 cp "conf.d/${f}" "${x}.orig"
513             fi
514
515             if [ "${BASH_VERSINFO[0]}" -ge "4" ]
516             then
517                 if [ "${configs_signatures[${md5}]}" = "${f}" ]
518                 then
519                     # it is a stock version - don't keep it
520                     echo >&2 "File '${x}' is stock version."
521                 else
522                     # edited by user - keep it
523                     echo >&2 "File '${x}' has been edited by user."
524                     cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
525                 fi
526             else
527                 echo >&2 "File '${x}' cannot be check for custom edits."
528                 cp -p "${x}" "${x}.installer_backup.${installer_backup_suffix}"
529             fi
530         fi
531
532     elif [ -f "${x}.installer_backup.${installer_backup_suffix}" ]
533         then
534         rm -f "${x}.installer_backup.${installer_backup_suffix}"
535     fi
536 done
537
538 echo >&2 "Installing netdata ..."
539 run make install || exit 1
540
541 # restore user configurations
542 for x in $(find "${NETDATA_PREFIX}/etc/netdata/" -name '*.conf' -type f)
543 do
544     if [ -f "${x}.installer_backup.${installer_backup_suffix}" ]
545         then
546         cp -p "${x}.installer_backup.${installer_backup_suffix}" "${x}"
547         rm -f "${x}.installer_backup.${installer_backup_suffix}"
548     fi
549 done
550
551 echo >&2 "Fixing permissions ..."
552
553 check_cmd() {
554     which "${1}" >/dev/null 2>&1 && return 0
555     command -v "${1}" >/dev/null 2>&1 && return 0
556     return 1
557 }
558
559 portable_add_user() {
560     local username="${1}"
561
562     getent passwd "${username}" > /dev/null 2>&1
563     [ $? -eq 0 ] && return 0
564
565     echo >&2 "Adding ${username} user account ..."
566
567     local nologin="$(which nologin 2>/dev/null || command -v nologin 2>/dev/null || echo '/bin/false')"
568
569     # Linux
570     if check_cmd useradd
571     then
572         run useradd -r -g "${username}" -c "${username}" -s "${nologin}" -d / "${username}" && return 0
573     fi
574
575     # FreeBSD
576     if check_cmd pw
577     then
578         run pw useradd "${username}" -d / -g "${username}" -s "${nologin}" && return 0
579     fi
580
581     # BusyBox
582     if check_cmd adduser
583     then
584         run adduser -D -G "${username}" "${username}" && return 0
585     fi
586
587     echo >&2 "Failed to add ${username} user account !"
588
589     return 1
590 }
591
592 portable_add_group() {
593     local groupname="${1}"
594
595     getent group "${groupname}" > /dev/null 2>&1
596     [ $? -eq 0 ] && return 0
597
598     echo >&2 "Adding ${groupname} user group ..."
599
600     # Linux
601     if check_cmd groupadd
602     then
603         run groupadd -r "${groupname}" && return 0
604     fi
605
606     # FreeBSD
607     if check_cmd pw
608     then
609         run pw groupadd "${groupname}" && return 0
610     fi
611
612     # BusyBox
613     if check_cmd addgroup
614     then
615         run addgroup "${groupname}" && return 0
616     fi
617
618     echo >&2 "Failed to add ${groupname} user group !"
619     return 1
620 }
621
622 portable_add_user_to_group() {
623     local groupname="${1}" username="${2}"
624
625     getent group "${groupname}" > /dev/null 2>&1
626     [ $? -ne 0 ] && return 1
627
628     # find the user is already in the group
629     local users=$(getent group "${groupname}" | cut -d ':' -f 4)
630     if [[ ",${users}," =~ ,${username}, ]]
631         then
632         # username is already there
633         return 0
634     else
635         # username is not in group
636         echo >&2 "Adding ${username} user to the ${groupname} group ..."
637
638         # Linux
639         if check_cmd usermod
640         then
641             run usermod -a -G "${groupname}" "${username}" && return 0
642         fi
643
644         # FreeBSD
645         if check_cmd pw
646         then
647             run pw groupmod "${groupname}" -m "${username}" && return 0
648         fi
649
650         # BusyBox
651         if check_cmd addgroup
652         then
653             run addgroup "${username}" "${groupname}" && return 0
654         fi
655
656         echo >&2 "Failed to add user ${username} to group ${groupname} !"
657         return 1
658     fi
659 }
660
661 iscontainer() {
662     # man systemd-detect-virt
663     local cmd=$(which systemd-detect-virt 2>/dev/null || command -v systemd-detect-virt 2>/dev/null)
664     if [ ! -z "${cmd}" -a -x "${cmd}" ]
665         then
666         "${cmd}" --container >/dev/null 2>&1 && return 0
667     fi
668
669     # /proc/1/sched exposes the host's pid of our init !
670     # http://stackoverflow.com/a/37016302
671     local pid=$( cat /proc/1/sched | head -n 1 | { IFS='(),#:' read name pid th threads; echo $pid; } )
672     local p=$(( pid + 0 ))
673     [ ${pid} -ne 1 ] && return 0
674
675     # lxc sets environment variable 'container'
676     [ ! -z "${container}" ] && return 0
677
678     # docker creates /.dockerenv
679     # http://stackoverflow.com/a/25518345
680     [ -f "/.dockerenv" ] && return 0
681
682     # ubuntu and debian supply /bin/running-in-container
683     # https://www.apt-browse.org/browse/ubuntu/trusty/main/i386/upstart/1.12.1-0ubuntu4/file/bin/running-in-container
684     if [ -x "/bin/running-in-container" ]
685         then
686         "/bin/running-in-container" >/dev/null 2>&1 && return 0
687     fi
688
689     return 1
690 }
691
692 run find ./system/ -type f -a \! -name \*.in -a \! -name Makefile\* -a \! -name \*.conf -a \! -name \*.service -a \! -name \*.logrotate -exec chmod 755 {} \;
693
694 NETDATA_ADDED_TO_DOCKER=0
695 NETDATA_ADDED_TO_NGINX=0
696 NETDATA_ADDED_TO_VARNISH=0
697 if [ ${UID} -eq 0 ]
698     then
699     portable_add_group netdata
700     portable_add_user netdata
701     portable_add_user_to_group docker netdata && NETDATA_ADDED_TO_DOCKER=1
702     portable_add_user_to_group nginx  netdata && NETDATA_ADDED_TO_NGINX=1
703     portable_add_user_to_group varnish  netdata && NETDATA_ADDED_TO_VARNISH=1
704
705     if [ -d /etc/logrotate.d -a ! -f /etc/logrotate.d/netdata ]
706         then
707         echo >&2 "Adding netdata logrotate configuration ..."
708         run cp system/netdata.logrotate /etc/logrotate.d/netdata
709     fi
710     
711     if [ -f /etc/logrotate.d/netdata ]
712         then
713         echo >&2 "Fixing netdata logrotate permissions ..."
714         run chmod 644 /etc/logrotate.d/netdata
715     fi
716 fi
717
718
719 # -----------------------------------------------------------------------------
720 # load options from the configuration file
721
722 # create an empty config if it does not exist
723 [ ! -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ] && touch "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
724
725 # function to extract values from the config file
726 config_option() {
727     local key="${1}" value="${2}" line=
728
729     if [ -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
730         then
731         line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "${NETDATA_PREFIX}/etc/netdata/netdata.conf" | head -n 1 )"
732         [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
733     fi
734
735     echo "${value}"
736 }
737
738 # the user netdata will run as
739 if [ "${UID}" = "0" ]
740     then
741     NETDATA_USER="$( config_option "run as user" "netdata" )"
742 else
743     NETDATA_USER="${USER}"
744 fi
745
746 # the owners of the web files
747 NETDATA_WEB_USER="$(  config_option "web files owner" "${NETDATA_USER}" )"
748 NETDATA_WEB_GROUP="$( config_option "web files group" "${NETDATA_WEB_USER}" )"
749
750 # debug flags
751 NETDATA_DEBUG="$( config_option "debug flags" 0 )"
752
753 # port
754 defport=19999
755 NETDATA_PORT="$( config_option "default port" ${defport} )"
756 NETDATA_PORT2="$( config_option "port" ${defport} )"
757
758 if [ "${NETDATA_PORT}" != "${NETDATA_PORT2}" ]
759 then
760     if [ "${NETDATA_PORT2}" != "${defport}" ]
761     then
762         NETDATA_PORT="${NETDATA_PORT2}"
763     fi
764 fi
765
766 # directories
767 NETDATA_LIB_DIR="$( config_option "lib directory" "${NETDATA_PREFIX}/var/lib/netdata" )"
768 NETDATA_CACHE_DIR="$( config_option "cache directory" "${NETDATA_PREFIX}/var/cache/netdata" )"
769 NETDATA_WEB_DIR="$( config_option "web files directory" "${NETDATA_PREFIX}/usr/share/netdata/web" )"
770 NETDATA_LOG_DIR="$( config_option "log directory" "${NETDATA_PREFIX}/var/log/netdata" )"
771 NETDATA_CONF_DIR="$( config_option "config directory" "${NETDATA_PREFIX}/etc/netdata" )"
772 NETDATA_RUN_DIR="${NETDATA_PREFIX}/var/run"
773
774
775 # -----------------------------------------------------------------------------
776 # prepare the directories
777
778 # this is needed if NETDATA_PREFIX is not empty
779 if [ ! -d "${NETDATA_RUN_DIR}" ]
780     then
781     mkdir -p "${NETDATA_RUN_DIR}" || exit 1
782 fi
783
784 echo >&2
785 echo >&2 "Fixing directories (user: ${NETDATA_USER})..."
786
787 # --- conf dir ----
788
789 for x in "python.d" "charts.d" "node.d"
790 do
791     if [ ! -d "${NETDATA_CONF_DIR}/${x}" ]
792         then
793         echo >&2 "Creating directory '${NETDATA_CONF_DIR}/${x}'"
794         run mkdir -p "${NETDATA_CONF_DIR}/${x}" || exit 1
795     fi
796 done
797 run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${NETDATA_CONF_DIR}"
798 run find "${NETDATA_CONF_DIR}" -type f -exec chmod 0660 {} \;
799 run find "${NETDATA_CONF_DIR}" -type d -exec chmod 0775 {} \;
800
801 # --- web dir ----
802
803 if [ ! -d "${NETDATA_WEB_DIR}" ]
804     then
805     echo >&2 "Creating directory '${NETDATA_WEB_DIR}'"
806     run mkdir -p "${NETDATA_WEB_DIR}" || exit 1
807 fi
808 run chown -R "${NETDATA_WEB_USER}:${NETDATA_WEB_GROUP}" "${NETDATA_WEB_DIR}"
809 run find "${NETDATA_WEB_DIR}" -type f -exec chmod 0664 {} \;
810 run find "${NETDATA_WEB_DIR}" -type d -exec chmod 0775 {} \;
811
812 # --- data dirs ----
813
814 for x in "${NETDATA_LIB_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"
815 do
816     if [ ! -d "${x}" ]
817         then
818         echo >&2 "Creating directory '${x}'"
819         run mkdir -p "${x}" || exit 1
820     fi
821
822     run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${x}"
823     #run find "${x}" -type f -exec chmod 0660 {} \;
824     #run find "${x}" -type d -exec chmod 0770 {} \;
825 done
826
827 run chmod 755 "${NETDATA_LOG_DIR}"
828
829 # --- plugins ----
830
831 if [ ${UID} -eq 0 ]
832     then
833     run chown "${NETDATA_USER}:root" "${NETDATA_LOG_DIR}"
834     run chown -R root "${NETDATA_PREFIX}/usr/libexec/netdata"
835     run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type d -exec chmod 0755 {} \;
836     run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -exec chmod 0644 {} \;
837     run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -a -name \*.plugin -exec chmod 0755 {} \;
838     run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -a -name \*.sh -exec chmod 0755 {} \;
839
840     setcap_ret=1
841     if ! iscontainer
842         then
843         run setcap cap_dac_read_search,cap_sys_ptrace+ep "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
844         setcap_ret=$?
845
846         if [ ${setcap_ret} -eq 0 ]
847             then
848             # if we managed to setcap
849             # but we fail to execute apps.plugin
850             # trigger setuid to root
851             "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin" -v >/dev/null 2>&1
852             setcap_ret=$?
853         fi
854     fi
855
856     if [ ${setcap_ret} -ne 0 ]
857         then
858         # fix apps.plugin to be setuid to root
859         run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
860         run chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
861     fi
862 else
863     run chown "${NETDATA_USER}:${NETDATA_USER}" "${NETDATA_LOG_DIR}"
864     run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${NETDATA_PREFIX}/usr/libexec/netdata"
865     run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -exec chmod 0755 {} \;
866     run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type d -exec chmod 0755 {} \;
867 fi
868
869 # --- fix #1292 bug ---
870
871 [ -d "${NETDATA_PREFIX}/usr/libexec" ] && run chmod a+rX "${NETDATA_PREFIX}/usr/libexec"
872 [ -d "${NETDATA_PREFIX}/usr/share/netdata" ] && run chmod a+rX "${NETDATA_PREFIX}/usr/share/netdata"
873
874
875 # -----------------------------------------------------------------------------
876 # check if we can re-start netdata
877
878 if [ ${DONOTSTART} -eq 1 ]
879     then
880     if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
881         then
882         echo >&2 "Generating empty config file in: ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
883         echo "# Get config from http://127.0.0.1:${NETDATA_PORT}/netdata.conf" >"${NETDATA_PREFIX}/etc/netdata/netdata.conf"
884
885         if [ "${UID}" -eq 0 ]
886             then
887             chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
888         fi
889         chmod 0644 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
890     fi
891     banner "is installed now!"
892     echo >&2 "  enjoy real-time performance and health monitoring..."
893     exit 0
894 fi
895
896 # -----------------------------------------------------------------------------
897 # stop a running netdata
898
899 isnetdata() {
900     if [ -d /proc/self ]
901     then
902         [ -z "$1" -o ! -f "/proc/$1/stat" ] && return 1
903         [ "$(cat "/proc/$1/stat" | cut -d '(' -f 2 | cut -d ')' -f 1)" = "netdata" ] && return 0
904         return 1
905     fi
906     return 0
907 }
908
909 stop_netdata_on_pid() {
910     local pid="${1}" ret=0 count=0
911
912     isnetdata ${pid} || return 0
913
914     printf >&2 "Stopping netdata on pid ${pid} ..."
915     while [ ! -z "$pid" -a ${ret} -eq 0 ]
916     do
917         if [ ${count} -gt 45 ]
918             then
919             echo >&2 "Cannot stop the running netdata on pid ${pid}."
920             return 1
921         fi
922
923         count=$(( count + 1 ))
924
925         run kill ${pid} 2>/dev/null
926         ret=$?
927
928         test ${ret} -eq 0 && printf >&2 "." && sleep 2
929     done
930
931     echo >&2
932     if [ ${ret} -eq 0 ]
933     then
934         echo >&2 "SORRY! CANNOT STOP netdata ON PID ${pid} !"
935         return 1
936     fi
937
938     echo >&2 "netdata on pid ${pid} stopped."
939     return 0
940 }
941
942 stop_all_netdata() {
943     local p myns ns
944
945     myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
946
947     echo >&2 "Stopping a (possibly) running netdata..."
948
949     for p in $(cat "${NETDATA_RUN_DIR}/netdata.pid" 2>/dev/null) \
950         $(cat /var/run/netdata.pid 2>/dev/null) \
951         $(cat /var/run/netdata/netdata.pid 2>/dev/null) \
952         $(pidof netdata 2>/dev/null)
953     do
954         ns="$(readlink /proc/${p}/ns/pid 2>/dev/null)"
955
956         if [ -z "${myns}" -o -z "${ns}" -o "${myns}" = "${ns}" ]
957             then
958             stop_netdata_on_pid ${p}
959         fi
960     done
961 }
962
963 # -----------------------------------------------------------------------------
964 # check for systemd
965
966 issystemd() {
967     local pids p myns ns systemctl
968
969     # if the directory /etc/systemd/system does not exit, it is not systemd
970     [ ! -d /etc/systemd/system ] && return 1
971
972     # if there is no systemctl command, it is not systemd
973     systemctl=$(which systemctl 2>/dev/null || command -v systemctl 2>/dev/null)
974     [ -z "${systemctl}" -o ! -x "${systemctl}" ] && return 1
975
976     # if pid 1 is systemd, it is systemd
977     [ "$(basename $(readlink /proc/1/exe) 2>/dev/null)" = "systemd" ] && return 0
978
979     # if systemd is not running, it is not systemd
980     pids=$(pidof systemd 2>/dev/null)
981     [ -z "${pids}" ] && return 1
982
983     # check if the running systemd processes are not in our namespace
984     myns="$(readlink /proc/self/ns/pid 2>/dev/null)"
985     for p in ${pids}
986     do
987         ns="$(readlink /proc/${p}/ns/pid 2>/dev/null)"
988
989         # if pid of systemd is in our namespace, it is systemd
990         [ ! -z "${myns}" && "${myns}" = "${ns}" ] && return 0
991     done
992
993     # else, it is not systemd
994     return 1
995 }
996
997 installed_init_d=0
998 install_non_systemd_init() {
999     [ "${UID}" != 0 ] && return 1
1000
1001     local key="unknown"
1002     if [ -f /etc/os-release ]
1003         then
1004         source /etc/os-release || return 1
1005         key="${ID}-${VERSION_ID}"
1006
1007     elif [ -f /etc/centos-release ]
1008         then
1009         key=$(</etc/centos-release)
1010     fi
1011
1012     if [ -d /etc/init.d -a ! -f /etc/init.d/netdata ]
1013         then
1014         if [ "${key}" = "gentoo" ]
1015             then
1016             run cp system/netdata-openrc /etc/init.d/netdata && \
1017             run chmod 755 /etc/init.d/netdata && \
1018             run rc-update add netdata default && \
1019             installed_init_d=1
1020         
1021         elif [ "${key}" = "ubuntu-12.04" -o "${key}" = "ubuntu-14.04" -o "${key}" = "debian-7" ]
1022             then
1023             run cp system/netdata-lsb /etc/init.d/netdata && \
1024             run chmod 755 /etc/init.d/netdata && \
1025             run update-rc.d netdata defaults && \
1026             run update-rc.d netdata enable && \
1027             installed_init_d=1
1028
1029         elif [ "${key}" = "CentOS release 6.8 (Final)" -o "${key}" = "amzn-2016.09" ]
1030             then
1031             run cp system/netdata-init-d /etc/init.d/netdata && \
1032             run chmod 755 /etc/init.d/netdata && \
1033             run chkconfig netdata on && \
1034             installed_init_d=1
1035         fi
1036     fi
1037
1038     return 0
1039 }
1040
1041 started=0
1042 if [ "${UID}" -eq 0 ]
1043     then
1044
1045     if issystemd
1046     then
1047         # systemd is running on this system
1048
1049         if [ ! -f /etc/systemd/system/netdata.service ]
1050         then
1051             echo >&2 "Installing systemd service..."
1052             run cp system/netdata.service /etc/systemd/system/netdata.service && \
1053                 run systemctl daemon-reload && \
1054                 run systemctl enable netdata
1055         else
1056             service netdata stop
1057         fi
1058
1059         stop_all_netdata
1060         service netdata restart && started=1
1061     else
1062         install_non_systemd_init
1063     fi
1064
1065     if [ ${started} -eq 0 ]
1066     then
1067         # check if we can use the system service
1068         service netdata stop
1069         stop_all_netdata
1070         service netdata restart && started=1
1071         if [ ${started} -eq 0 ]
1072         then
1073             service netdata start && started=1
1074         fi
1075     fi
1076 fi
1077
1078 if [ ${started} -eq 0 ]
1079 then
1080     # still not started...
1081
1082     stop_all_netdata
1083
1084     echo >&2 "Starting netdata..."
1085     run ${NETDATA_PREFIX}/usr/sbin/netdata -P ${NETDATA_RUN_DIR}/netdata.pid "${@}"
1086     if [ $? -ne 0 ]
1087         then
1088         echo >&2
1089         echo >&2 "SORRY! FAILED TO START NETDATA!"
1090         exit 1
1091     else
1092         echo >&2 "OK. NetData Started!"
1093     fi
1094
1095     echo >&2
1096 fi
1097
1098 # -----------------------------------------------------------------------------
1099 # save a config file, if it is not already there
1100
1101 if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
1102     then
1103     echo >&2
1104     echo >&2 "-------------------------------------------------------------------------------"
1105     echo >&2
1106     echo >&2 "Downloading default configuration from netdata..."
1107     sleep 5
1108
1109     # remove a possibly obsolete download
1110     [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
1111
1112     # disable a proxy to get data from the local netdata
1113     export http_proxy=
1114     export https_proxy=
1115
1116     # try wget
1117     wget 2>/dev/null -O "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
1118     ret=$?
1119
1120     # try curl
1121     if [ ${ret} -ne 0 -o ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
1122         then
1123         curl -s -o "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
1124         ret=$?
1125     fi
1126
1127     if [ ${ret} -eq 0 -a -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
1128         then
1129         mv "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
1130         echo >&2 "New configuration saved for you to edit at ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
1131
1132         if [ "${UID}" -eq 0 ]
1133             then
1134             chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
1135         fi
1136         chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
1137     else
1138         echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
1139         [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
1140     fi
1141 fi
1142
1143 # -----------------------------------------------------------------------------
1144 # Check for KSM
1145
1146 ksm_is_available_but_disabled() {
1147     cat <<KSM1
1148
1149 -------------------------------------------------------------------------------
1150 Memory de-duplication instructions
1151
1152 You have kernel memory de-duper (called Kernel Same-page Merging,
1153 or KSM) available, but it is not currently enabled.
1154
1155 To enable it run:
1156
1157 echo 1 >/sys/kernel/mm/ksm/run
1158 echo 1000 >/sys/kernel/mm/ksm/sleep_millisecs
1159
1160 If you enable it, you will save 40-60% of netdata memory.
1161
1162 KSM1
1163 }
1164
1165 ksm_is_not_available() {
1166     cat <<KSM2
1167
1168 -------------------------------------------------------------------------------
1169 Memory de-duplication not present in your kernel
1170
1171 It seems you do not have kernel memory de-duper (called Kernel Same-page
1172 Merging, or KSM) available.
1173
1174 To enable it, you need a kernel built with CONFIG_KSM=y
1175
1176 If you can have it, you will save 40-60% of netdata memory.
1177
1178 KSM2
1179 }
1180
1181 if [ -f "/sys/kernel/mm/ksm/run" ]
1182     then
1183     if [ $(cat "/sys/kernel/mm/ksm/run") != "1" ]
1184         then
1185         ksm_is_available_but_disabled
1186     fi
1187 else
1188     ksm_is_not_available
1189 fi
1190
1191 # -----------------------------------------------------------------------------
1192 # Check for version.txt
1193
1194 if [ ! -s web/version.txt ]
1195     then
1196     cat <<VERMSG
1197
1198 -------------------------------------------------------------------------------
1199 Version update check warning
1200
1201 The way you downloaded netdata, we cannot find its version. This means the
1202 Update check on the dashboard, will not work.
1203
1204 If you want to have version update check, please re-install it
1205 following the procedure in:
1206
1207 https://github.com/firehol/netdata/wiki/Installation
1208
1209 VERMSG
1210 fi
1211
1212 # -----------------------------------------------------------------------------
1213 # apps.plugin warning
1214
1215 if [ "${UID}" -ne 0 ]
1216     then
1217     cat <<SETUID_WARNING
1218
1219 -------------------------------------------------------------------------------
1220 apps.plugin needs privileges
1221
1222 Since you have installed netdata as a normal user, to have apps.plugin collect
1223 all the needed data, you have to give it the access rights it needs, by running
1224 either of the following sets of commands:
1225
1226 To run apps.plugin with escalated capabilities:
1227
1228     sudo chown root:${NETDATA_USER} "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
1229     sudo chmod 0750 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
1230     sudo setcap cap_dac_read_search,cap_sys_ptrace+ep "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
1231
1232 or, to run apps.plugin as root:
1233
1234     sudo chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
1235     sudo chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
1236
1237 apps.plugin is performing a hard-coded function of data collection for all
1238 running processes. It cannot be instructed from the netdata daemon to perform
1239 any task, so it is pretty safe to do this.
1240
1241 SETUID_WARNING
1242 fi
1243
1244 # -----------------------------------------------------------------------------
1245 # Keep un-install info
1246
1247 cat >netdata-uninstaller.sh <<UNINSTALL
1248 #!/usr/bin/env bash
1249
1250 # this script will uninstall netdata
1251
1252 if [ "\$1" != "--force" ]
1253     then
1254     echo >&2 "This script will REMOVE netdata from your system."
1255     echo >&2 "Run it again with --force to do it."
1256     exit 1
1257 fi
1258
1259 echo >&2 "Stopping a possibly running netdata..."
1260 for p in \$(pidof netdata); do kill \$p; done
1261 sleep 2
1262
1263 deletedir() {
1264     if [ ! -z "\$1" -a -d "\$1" ]
1265         then
1266         echo
1267         echo "Deleting directory '\$1' ..."
1268         rm -I -R "\$1"
1269     fi
1270 }
1271
1272 if [ ! -z "${NETDATA_PREFIX}" -a -d "${NETDATA_PREFIX}" ]
1273     then
1274     # installation prefix was given
1275
1276     deletedir "${NETDATA_PREFIX}"
1277
1278 else
1279     # installation prefix was NOT given
1280
1281     if [ -f "${NETDATA_PREFIX}/usr/sbin/netdata" ]
1282         then
1283         echo "Deleting ${NETDATA_PREFIX}/usr/sbin/netdata ..."
1284         rm -i "${NETDATA_PREFIX}/usr/sbin/netdata"
1285     fi
1286
1287     deletedir "${NETDATA_PREFIX}/etc/netdata"
1288     deletedir "${NETDATA_PREFIX}/usr/share/netdata"
1289     deletedir "${NETDATA_PREFIX}/usr/libexec/netdata"
1290     deletedir "${NETDATA_PREFIX}/var/lib/netdata"
1291     deletedir "${NETDATA_PREFIX}/var/cache/netdata"
1292     deletedir "${NETDATA_PREFIX}/var/log/netdata"
1293 fi
1294
1295 if [ -f /etc/logrotate.d/netdata ]
1296     then
1297     echo "Deleting /etc/logrotate.d/netdata ..."
1298     rm -i /etc/logrotate.d/netdata
1299 fi
1300
1301 if [ -f /etc/systemd/system/netdata.service ]
1302     then
1303     echo "Deleting /etc/systemd/system/netdata.service ..."
1304     rm -i /etc/systemd/system/netdata.service
1305 fi
1306
1307 if [ -f /etc/init.d/netdata ]
1308     then
1309     echo "Deleting /etc/init.d/netdata ..."
1310     rm -i /etc/init.d/netdata
1311 fi
1312
1313 getent passwd netdata > /dev/null
1314 if [ $? -eq 0 ]
1315     then
1316     echo
1317     echo "You may also want to remove the user netdata"
1318     echo "by running:"
1319     echo "   userdel netdata"
1320 fi
1321
1322 getent group netdata > /dev/null
1323 if [ $? -eq 0 ]
1324     then
1325     echo
1326     echo "You may also want to remove the group netdata"
1327     echo "by running:"
1328     echo "   groupdel netdata"
1329 fi
1330
1331 getent group docker > /dev/null
1332 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_DOCKER}" = "1" ]
1333     then
1334     echo
1335     echo "You may also want to remove the netdata user from the docker group"
1336     echo "by running:"
1337     echo "   gpasswd -d netdata docker"
1338 fi
1339
1340 getent group nginx > /dev/null
1341 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_NGINX}" = "1" ]
1342     then
1343     echo
1344     echo "You may also want to remove the netdata user from the nginx group"
1345     echo "by running:"
1346     echo "   gpasswd -d netdata nginx"
1347 fi
1348
1349 getent group varnish > /dev/null
1350 if [ $? -eq 0 -a "${NETDATA_ADDED_TO_VARNISH}" = "1" ]
1351     then
1352     echo
1353     echo "You may also want to remove the netdata user from the varnish group"
1354     echo "by running:"
1355     echo "   gpasswd -d netdata varnish"
1356 fi
1357
1358 UNINSTALL
1359 chmod 750 netdata-uninstaller.sh
1360
1361 # -----------------------------------------------------------------------------
1362
1363 cat <<END
1364
1365
1366 -------------------------------------------------------------------------------
1367
1368 OK. NetData is installed and it is running.
1369
1370 -------------------------------------------------------------------------------
1371
1372 By default netdata listens on all IPs on port ${NETDATA_PORT},
1373 so you can access it with:
1374
1375 http://this.machine.ip:${NETDATA_PORT}/
1376
1377 To stop netdata, just kill it, with:
1378
1379   killall netdata
1380
1381 To start it, just run it:
1382
1383   ${NETDATA_PREFIX}/usr/sbin/netdata
1384
1385
1386 END
1387 echo >&2 "Uninstall script generated: ./netdata-uninstaller.sh"
1388
1389 if [ -d .git ]
1390     then
1391     cat >netdata-updater.sh.new <<REINSTALL
1392 #!/usr/bin/env bash
1393
1394 force=0
1395 [ "\${1}" = "-f" ] && force=1
1396
1397 export PATH="\${PATH}:${PATH}"
1398 export CFLAGS="${CFLAGS}"
1399
1400 INSTALL_UID="${UID}"
1401 if [ "\${INSTALL_UID}" != "\${UID}" ]
1402     then
1403     echo >&2 "This script should be run as user with uid \${INSTALL_UID} but it now runs with uid \${UID}"
1404     exit 1
1405 fi
1406
1407 # make sure we cd to the working directory
1408 cd "${REINSTALL_PWD}" || exit 1
1409
1410 # make sure there is .git here
1411 [ \${force} -eq 0 -a ! -d .git ] && echo >&2 "No git structures found at: ${REINSTALL_PWD} (use -f for force re-install)" && exit 1
1412
1413 # signal netdata to start saving its database
1414 # this is handy if your database is big
1415 pids=\$(pidof netdata)
1416 [ ! -z "\${pids}" ] && kill -USR1 \${pids}
1417
1418 tmp=
1419 if [ -t 2 ]
1420     then
1421     # we are running on a terminal
1422     # open fd 3 and send it to stderr
1423     exec 3>&2
1424 else
1425     # we are headless
1426     # create a temporary file for the log
1427     tmp=\$(mktemp /tmp/netdata-updater-log-XXXXXX.log)
1428     # open fd 3 and send it to tmp
1429     exec 3>\${tmp}
1430 fi
1431
1432 info() {
1433     echo >&3 "\$(date) : INFO: " "\${@}"
1434 }
1435
1436 emptyline() {
1437     echo >&3
1438 }
1439
1440 error() {
1441     echo >&3 "\$(date) : ERROR: " "\${@}"
1442 }
1443
1444 # this is what we will do if it fails (head-less only)
1445 failed() {
1446     error "FAILED TO UPDATE NETDATA : \${1}"
1447
1448     if [ ! -z "\${tmp}" ]
1449     then
1450         cat >&2 "\${tmp}"
1451         rm "\${tmp}"
1452     fi
1453     exit 1
1454 }
1455
1456 get_latest_commit_id() {
1457     git log -1           2>&3 |\\
1458         grep ^commit     2>&3 |\\
1459         head -n 1        2>&3 |\\
1460         cut -d ' ' -f 2  2>&3
1461 }
1462
1463 update() {
1464     [ -z "\${tmp}" ] && info "Running on a terminal - (this script also supports running headless from crontab)"
1465
1466     emptyline
1467
1468     if [ -d .git ]
1469         then
1470         info "Updating netdata source from github..."
1471
1472         last_commit="\$(get_latest_commit_id)"
1473         [ \${force} -eq 0 -a -z "\${last_commit}" ] && failed "CANNOT GET LAST COMMIT ID (use -f for force re-install)"
1474
1475         git pull >&3 2>&3 || failed "CANNOT FETCH LATEST SOURCE (use -f for force re-install)"
1476
1477         new_commit="\$(get_latest_commit_id)"
1478         if [ \${force} -eq 0 ]
1479             then
1480             [ -z "\${new_commit}" ] && failed "CANNOT GET NEW LAST COMMIT ID (use -f for force re-install)"
1481             [ "\${new_commit}" = "\${last_commit}" ] && info "Nothing to be done! (use -f to force re-install)" && exit 0
1482         fi
1483     elif [ \${force} -eq 0 ]
1484         then
1485         failed "CANNOT FIND GIT STRUCTURES IN \$(pwd) (use -f for force re-install)"
1486     fi
1487
1488     emptyline
1489     info "Re-installing netdata..."
1490     ${REINSTALL_COMMAND// --dont-wait/} --dont-wait >&3 2>&3 || failed "FAILED TO COMPILE/INSTALL NETDATA"
1491
1492     [ ! -z "\${tmp}" ] && rm "\${tmp}" && tmp=
1493     return 0
1494 }
1495
1496 # the installer updates this script - so we run and exit in a single line
1497 update && exit 0
1498 ###############################################################################
1499 ###############################################################################
1500 REINSTALL
1501     chmod 755 netdata-updater.sh.new
1502     mv -f netdata-updater.sh.new netdata-updater.sh
1503     echo >&2 "Update script generated   : ./netdata-updater.sh"
1504 elif [ -f "netdata-updater.sh" ]
1505     then
1506     rm "netdata-updater.sh"
1507 fi
1508
1509 banner "is installed and running now!"
1510 echo >&2 "  enjoy real-time performance and health monitoring..."
1511 echo >&2 
1512 exit 0