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