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