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