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