]> arthur.barton.de Git - netdata.git/blob - netdata-installer.sh
build: move pid to /var/run
[netdata.git] / netdata-installer.sh
1 #!/bin/bash
2
3 LC_ALL=C
4
5 # you can set CFLAGS before running installer
6 CFLAGS="${CFLAGS--O3}"
7
8 ME="$0"
9 DONOTSTART=0
10 DONOTWAIT=0
11 NETDATA_PREFIX=
12 ZLIB_IS_HERE=0
13
14 usage() {
15         cat <<USAGE
16
17 ${ME} <installer options>
18
19 Valid <installer options> are:
20
21    --install /PATH/TO/INSTALL
22
23         If your give: --install /opt
24         netdata will be installed in /opt/netdata
25
26    --dont-start-it
27
28         Do not (re)start netdata.
29         Just install it.
30
31    --dont-wait
32
33         Do not wait for the user to press ENTER.
34         Start immediately building it.
35
36    --zlib-is-really-here
37
38         If you get errors about missing zlib,
39         but you know it is available,
40         you have a broken pkg-config.
41         Use this option to allow it continue
42         without checking pkg-config.
43
44 Netdata will by default be compiled with gcc optimization -O3
45 If you need to pass different CFLAGS, use something like this:
46
47   CFLAGS="<gcc options>" $ME <installer options>
48
49 For the installer to complete successfully, you will need
50 these packages installed:
51
52    gcc make autoconf automake pkg-config zlib1g-dev
53
54 For the plugins, you will at least need:
55
56    curl node
57
58 USAGE
59 }
60
61 while [ ! -z "${1}" ]
62 do
63         if [ "$1" = "--install" ]
64                 then
65                 NETDATA_PREFIX="${2}/netdata"
66                 shift 2
67         elif [ "$1" = "--zlib-is-really-here" ]
68                 then
69                 ZLIB_IS_HERE=1
70                 shift 1
71         elif [ "$1" = "--dont-start-it" ]
72                 then
73                 DONOTSTART=1
74                 shift 1
75         elif [ "$1" = "--dont-wait" ]
76                 then
77                 DONOTWAIT=1
78                 shift 1
79         elif [ "$1" = "--help" -o "$1" = "-h" ]
80                 then
81                 usage
82                 exit 1
83         else
84                 echo >&2
85                 echo >&2 "ERROR:"
86                 echo >&2 "I cannot understand option '$1'."
87                 usage
88                 exit 1
89         fi
90 done
91
92 cat <<BANNER
93
94 Welcome to netdata!
95 Nice to see you are giving it a try!
96
97 You are about to build and install netdata to your system.
98
99 It will be installed at these locations:
100
101   - the daemon    at ${NETDATA_PREFIX}/usr/sbin/netdata
102   - config files  at ${NETDATA_PREFIX}/etc/netdata
103   - web files     at ${NETDATA_PREFIX}/usr/share/netdata
104   - plugins       at ${NETDATA_PREFIX}/usr/libexec/netdata
105   - cache files   at ${NETDATA_PREFIX}/var/cache/netdata
106   - log files     at ${NETDATA_PREFIX}/var/log/netdata
107
108 This installer allows you to change the installation path.
109 Press Control-C and run the same command with --help for help.
110
111 BANNER
112
113 if [ ! "${UID}" = 0 -o "$1" = "-h" -o "$1" = "--help" ]
114         then
115         echo >&2
116         echo >&2 "You have to run netdata as root."
117         echo >&2 "The netdata daemon will drop priviliges"
118         echo >&2 "but you have to start it as root."
119         echo >&2
120         exit 1
121 fi
122
123 if [ ${DONOTWAIT} -eq 0 ]
124         then
125         if [ ! -z "${NETDATA_PREFIX}" ]
126                 then
127                 read -p "Press ENTER to build and install netdata to '${NETDATA_PREFIX}' > "
128         else
129                 read -p "Press ENTER to build and install netdata to your system > "
130         fi
131 fi
132
133 # reload the profile
134 [ -f /etc/profile ] && . /etc/profile
135
136 build_error() {
137         cat <<EOF
138
139 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
140
141 Sorry! NetData failed to build...
142
143 You many need to check these:
144
145 1. The package zlib1g-dev has to be installed.
146
147 2. You need basic build tools installed, like:
148
149    gcc make autoconf automake pkg-config
150
151 3. If your system cannot find ZLIB, although it is installed
152    run me with the option:  --zlib-is-really-here
153
154 If you still cannot get it to build, ask for help at github:
155
156    https://github.com/firehol/netdata/issues
157
158
159 EOF
160         trap - EXIT
161         exit 1
162 }
163
164 run() {
165         printf >&2 ":-----------------------------------------------------------------------------\n"
166         printf >&2 "Running command:\n"
167         printf >&2 "\n"
168         printf >&2 "%q " "${@}"
169         printf >&2 "\n"
170         printf >&2 "\n"
171
172         "${@}"
173 }
174
175 if [ ${ZLIB_IS_HERE} -eq 1 ]
176         then
177         shift
178         echo >&2 "ok, assuming zlib is really installed."
179         export ZLIB_CFLAGS=" "
180         export ZLIB_LIBS="-lz"
181 fi
182
183 trap build_error EXIT
184
185 echo >&2 "Running ./autogen.sh ..."
186 run ./autogen.sh || exit 1
187
188 echo >&2 "Running ./configure ..."
189 run ./configure \
190         --prefix="${NETDATA_PREFIX}/usr" \
191         --sysconfdir="${NETDATA_PREFIX}/etc" \
192         --localstatedir="${NETDATA_PREFIX}/var" \
193         --with-zlib --with-math --with-user=netdata \
194         CFLAGS="${CFLAGS}" || exit 1
195
196 # remove the build_error hook
197 trap - EXIT
198
199 if [ -f src/netdata ]
200         then
201         echo >&2 "Cleaning a possibly old compilation ..."
202         run make clean
203 fi
204
205 echo >&2 "Compiling netdata ..."
206 run make || exit 1
207
208 echo >&2 "Installing netdata ..."
209 run make install || exit 1
210
211 echo >&2 "Adding netdata user group ..."
212 getent group netdata > /dev/null || run groupadd -r netdata
213
214 echo >&2 "Adding netdata user account ..."
215 getent passwd netdata > /dev/null || run useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
216
217
218
219 # -----------------------------------------------------------------------------
220 # load options from the configuration file
221
222 # create an empty config if it does not exist
223 [ ! -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ] && touch "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
224
225 # function to extract values from the config file
226 config_option() {
227         local key="${1}" value="${2}" line=
228
229         if [ -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
230                 then
231                 line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "${NETDATA_PREFIX}/etc/netdata/netdata.conf" | head -n 1 )"
232                 [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
233         fi
234
235         echo "${value}"
236 }
237
238 # user
239 defuser="netdata"
240 [ ! "${UID}" = "0" ] && defuser="${USER}"
241 NETDATA_USER="$( config_option "run as user" "${defuser}" )"
242
243 # debug flags
244 defdebug=0
245 NETDATA_DEBUG="$( config_option "debug flags" ${defdebug} )"
246
247 # port
248 defport=19999
249 NETDATA_PORT="$( config_option "port" ${defport} )"
250
251 # directories
252 NETDATA_CACHE_DIR="$( config_option "cache directory" "${NETDATA_PREFIX}/var/cache/netdata" )"
253 NETDATA_WEB_DIR="$( config_option "web files directory" "${NETDATA_PREFIX}/usr/share/netdata/web" )"
254 NETDATA_LOG_DIR="$( config_option "log directory" "${NETDATA_PREFIX}/var/log/netdata" )"
255 NETDATA_CONF_DIR="$( config_option "config directory" "${NETDATA_PREFIX}/etc/netdata" )"
256
257
258 # -----------------------------------------------------------------------------
259 # prepare the directories
260
261 echo >&2 "Fixing directory permissions for user ${NETDATA_USER}..."
262 for x in "${NETDATA_WEB_DIR}" "${NETDATA_CONF_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"
263 do
264         if [ ! -d "${x}" ]
265                 then
266                 echo >&2 "Creating directory '${x}'"
267                 run mkdir -p "${x}" || exit 1
268         fi
269         run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${x}" || echo >&2 "WARNING: Cannot change the ownership of the files in directory ${x} to ${NETDATA_USER}..."
270         run chmod 0775 "${x}" || echo >&2 "WARNING: Cannot change the permissions of the directory ${x} to 0755..."
271 done
272
273 # fix apps.plugin to be setuid to root
274 run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
275 run chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
276
277
278 # -----------------------------------------------------------------------------
279 # check if we can re-start netdata
280
281 if [ ${DONOTSTART} -eq 1 ]
282         then
283         if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
284                 then
285                 echo >&2 "Generating empty config file in: ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
286                 echo "# Get config from http://127.0.0.1:${NETDATA_PORT}/netdata.conf" >"${NETDATA_PREFIX}/etc/netdata/netdata.conf"
287                 chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
288                 chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
289         fi
290         echo >&2 "OK. It is now installed and ready."
291         exit 0
292 fi
293
294 # -----------------------------------------------------------------------------
295 # stop a running netdata
296
297 isnetdata() {
298         [ -z "$1" -o ! -f "/proc/$1/stat" ] && return 1
299         [ "$(cat "/proc/$1/stat" | cut -d '(' -f 2 | cut -d ')' -f 1)" = "netdata" ] && return 0
300         return 1
301 }
302
303
304 printf >&2 "Stopping a (possibly) running netdata..."
305 ret=0
306 count=0
307 while [ $ret -eq 0 ]
308 do
309         if [ $count -gt 15 ]
310                 then
311                 echo >&2 "Cannot stop the running netdata."
312                 exit 1
313         fi
314
315         count=$((count + 1))
316
317         pid=$(cat /var/run/netdata.pid 2>/dev/null)
318         isnetdata $pid || pid=
319         if [ ! -z "${pid}" ]
320                 then
321                 run kill $pid 2>/dev/null
322                 ret=$?
323         else
324                 run killall netdata 2>/dev/null
325                 ret=$?
326         fi
327
328         test $ret -eq 0 && printf >&2 "." && sleep 2
329 done
330 echo >&2
331
332
333 # -----------------------------------------------------------------------------
334 # run netdata
335
336 echo >&2 "Starting netdata..."
337 run ${NETDATA_PREFIX}/usr/sbin/netdata "${@}"
338
339 if [ $? -ne 0 ]
340         then
341         echo >&2
342         echo >&2 "SORRY! FAILED TO START NETDATA!"
343         exit 1
344 else
345         echo >&2 "OK. NetData Started!"
346 fi
347
348
349 # -----------------------------------------------------------------------------
350 # save a config file, if it is not already there
351
352 if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
353         then
354         echo >&2 "Downloading default configuration from netdata..."
355         sleep 5
356
357         # remove a possibly obsolete download
358         [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
359
360         # disable a proxy to get data from the local netdata
361         export http_proxy=
362         export https_proxy=
363
364         # try wget
365         wget 2>/dev/null -O "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
366         ret=$?
367
368         # try curl
369         if [ $ret -ne 0 -o ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
370                 then
371                 curl -s -o "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
372                 ret=$?
373         fi
374
375         if [ $ret -eq 0 -a -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
376                 then
377                 mv "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
378                 echo >&2 "New configuration saved for you to edit at ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
379
380                 chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
381                 chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
382         else
383                 echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
384                 [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
385         fi
386 fi
387
388 cat <<END
389
390
391 -------------------------------------------------------------------------------
392
393 ok. NetData is installed and is running.
394
395 Hit http://localhost:${NETDATA_PORT}/ from your browser.
396
397 To stop netdata, just kill it, with:
398
399   killall netdata
400
401 To start it, just run it:
402
403   ${NETDATA_PREFIX}/usr/sbin/netdata
404
405 Enjoy!
406
407 END
408
409 ksm_is_available_but_disabled() {
410         cat <<KSM1
411
412 INFORMATION:
413
414 I see you have kernel memory de-duper (called Kernel Same-page Merging,
415 or KSM) available, but it is not currently enabled.
416
417 To enable it run:
418
419 echo 1 >/sys/kernel/mm/ksm/run
420 echo 1000 >/sys/kernel/mm/ksm/sleep_millisecs
421
422 If you enable it, you will save 20-60% of netdata memory.
423
424 KSM1
425 }
426
427 ksm_is_not_available() {
428         cat <<KSM2
429
430 INFORMATION:
431
432 I see you do not have kernel memory de-duper (called Kernel Same-page
433 Merging, or KSM) available.
434
435 To enable it, you need a kernel built with CONFIG_KSM=y
436
437 If you can have it, you will save 20-60% of netdata memory.
438
439 KSM2
440 }
441
442 if [ -f "/sys/kernel/mm/ksm/run" ]
443         then
444         if [ $(cat "/sys/kernel/mm/ksm/run") != "1" ]
445                 then
446                 ksm_is_available_but_disabled
447         fi
448 else
449         ksm_is_not_available
450 fi