]> arthur.barton.de Git - netdata.git/blob - netdata-installer.sh
build: add remaining files to dist tarball
[netdata.git] / netdata-installer.sh
1 #!/bin/bash
2
3 cat <<BANNER
4
5
6 This script will build and install netdata to your system.
7
8 By default netdata will be installed as any other package.
9 If you want to install it to another directory, run me
10 with the option:
11
12 $0 --install /path/to/install
13
14 A new directory called netdata will be created as
15
16 /path/to/install/netdata
17
18
19 BANNER
20
21 if [ ! "${UID}" = 0 -o "$1" = "-h" -o "$1" = "--help" ]
22         then
23         echo >&2
24         echo >&2 "You have to run netdata are root."
25         echo >&2 "The netdata daemon will drop priviliges"
26         echo >&2 "but you have to start it as root."
27         echo >&2
28         exit 1
29 fi
30
31 NETDATA_PREFIX=
32 if [ "$1" = "--install" ]
33         then
34         NETDATA_PREFIX="${2}/netdata"
35         shift 2
36 fi
37
38 if [ ! -z "${NETDATA_PREFIX}" ]
39         then
40         read -p "Press ENTER to build and install netdata to '${NETDATA_PREFIX}' > "
41 else
42         read -p "Press ENTER to build and install netdata to your system > "
43 fi
44
45 # reload the profile
46 [ -f /etc/profile ] && . /etc/profile
47
48 build_error() {
49         cat <<EOF
50
51 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
52
53 We are very sorry! NetData failed to build...
54
55 You many need to check these:
56
57 1. The package zlib1g-dev has to be installed.
58
59 2. You need basic build tools installed, like:
60
61    gcc autoconf automake pgk-config
62
63 3. If your system cannot find ZLIB, although it is installed
64    run me with the option:  --zlib-is-really-here
65
66 If you still cannot get it to build, ask for help at github:
67
68    https://github.com/firehol/netdata/issues
69
70
71 EOF
72         trap - EXIT
73         exit 1
74 }
75
76 run() {
77         printf >&2 ":-----------------------------------------------------------------------------\n"
78         printf >&2 "Running command:\n"
79         printf >&2 "\n"
80         printf >&2 "%q " "${@}"
81         printf >&2 "\n"
82         printf >&2 "\n"
83
84         "${@}"
85 }
86
87 if [ "$1" = "--zlib-is-really-here" ]
88         then
89         shift
90         echo >&2 "ok, assuming zlib is really installed."
91         export ZLIB_CFLAGS=" "
92         export ZLIB_LIBS="-lz"
93 fi
94
95 trap build_error EXIT
96
97 echo >&2 "Running ./autogen.sh ..."
98 run ./autogen.sh || exit 1
99
100 echo >&2 "Running ./configure ..."
101 run ./configure \
102         --prefix="${NETDATA_PREFIX}/usr" \
103         --sysconfdir="${NETDATA_PREFIX}/etc" \
104         --localstatedir="${NETDATA_PREFIX}/var" \
105         --with-zlib --with-math --with-user=netdata \
106         CFLAGS="-march=native -O3" || exit 1
107
108 # remove the build_error hook
109 trap - EXIT
110
111 if [ -f src/netdata ]
112         then
113         echo >&2 "Cleaning a possibly old compilation ..."
114         run make clean
115 fi
116
117 echo >&2 "Compiling netdata ..."
118 run make || exit 1
119
120 echo >&2 "Installing netdata ..."
121 run make install || exit 1
122
123 echo >&2 "Adding netdata user group ..."
124 getent group netdata > /dev/null || run groupadd -r netdata
125
126 echo >&2 "Adding netdata user account ..."
127 getent passwd netdata > /dev/null || run useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
128
129
130
131 # -----------------------------------------------------------------------------
132 # load options from the configuration file
133
134 # create an empty config if it does not exist
135 [ ! -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ] && touch "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
136
137 # function to extract values from the config file
138 config_option() {
139         local key="${1}" value="${2}" line=
140
141         if [ -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
142                 then
143                 line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "${NETDATA_PREFIX}/etc/netdata/netdata.conf" | head -n 1 )"
144                 [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
145         fi
146
147         echo "${value}"
148 }
149
150 # user
151 defuser="netdata"
152 [ ! "${UID}" = "0" ] && defuser="${USER}"
153 NETDATA_USER="$( config_option "run as user" "${defuser}" )"
154
155 # debug flags
156 defdebug=0
157 NETDATA_DEBUG="$( config_option "debug flags" ${defdebug} )"
158
159 # port
160 defport=19999
161 NETDATA_PORT="$( config_option "port" ${defport} )"
162
163 # directories
164 NETDATA_CACHE_DIR="$( config_option "cache directory" "${NETDATA_PREFIX}/var/cache/netdata" )"
165 NETDATA_WEB_DIR="$( config_option "web files directory" "${NETDATA_PREFIX}/usr/share/netdata/web" )"
166 NETDATA_LOG_DIR="$( config_option "log directory" "${NETDATA_PREFIX}/var/log/netdata" )"
167 NETDATA_CONF_DIR="$( config_option "config directory" "${NETDATA_PREFIX}/etc/netdata" )"
168
169
170 # -----------------------------------------------------------------------------
171 # prepare the directories
172
173 echo >&2 "Fixing directory permissions for user ${NETDATA_USER}..."
174 for x in "${NETDATA_WEB_DIR}" "${NETDATA_CONF_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"
175 do
176         if [ ! -d "${x}" ]
177                 then
178                 echo >&2 "Creating directory '${x}'"
179                 run mkdir -p "${x}" || exit 1
180         fi
181         run chown -R "${NETDATA_USER}:${NETDATA_USER}" "${x}" || echo >&2 "WARNING: Cannot change the ownership of the files in directory ${x} to ${NETDATA_USER}..."
182         run chmod 0775 "${x}" || echo >&2 "WARNING: Cannot change the permissions of the directory ${x} to 0755..."
183 done
184
185 # fix apps.plugin to be setuid to root
186 run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
187 run chmod 4755 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
188
189
190 # -----------------------------------------------------------------------------
191 # stop a running netdata
192
193 printf >&2 "Stopping a (possibly) running netdata..."
194 ret=0
195 count=0
196 while [ $ret -eq 0 ]
197 do
198         if [ $count -gt 15 ]
199                 then
200                 echo >&2 "Cannot stop the running netdata."
201                 exit 1
202         fi
203
204         count=$((count + 1))
205         run killall netdata 2>/dev/null
206         ret=$?
207         test $ret -eq 0 && printf >&2 "." && sleep 2
208 done
209 echo >&2
210
211
212 # -----------------------------------------------------------------------------
213 # run netdata
214
215 echo >&2 "Starting netdata..."
216 run ${NETDATA_PREFIX}/usr/sbin/netdata "${@}"
217
218 if [ $? -ne 0 ]
219         then
220         echo >&2
221         echo >&2 "SORRY! FAILED TO START NETDATA!"
222         exit 1
223 else
224         echo >&2 "OK. NetData Started!"
225 fi
226
227
228 # -----------------------------------------------------------------------------
229 # save a config file, if it is not already there
230
231 if [ ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]
232         then
233         echo >&2 "Downloading default configuration from netdata..."
234         sleep 5
235
236         # remove a possibly obsolete download
237         [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
238
239         # try wget
240         wget 2>/dev/null -O "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
241         ret=$?
242
243         # try curl
244         if [ $ret -ne 0 -o ! -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
245                 then
246                 curl -s -o "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "http://localhost:${NETDATA_PORT}/netdata.conf"
247                 ret=$?
248         fi
249
250         if [ $ret -eq 0 -a -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ]
251                 then
252                 mv "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
253                 echo >&2 "New configuration saved for you to edit at ${NETDATA_PREFIX}/etc/netdata/netdata.conf"
254
255                 chown "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
256                 chmod 0664 "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
257         else
258                 echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
259                 [ -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new" ] && rm "${NETDATA_PREFIX}/etc/netdata/netdata.conf.new"
260         fi
261 fi
262
263 cat <<END
264
265
266 -------------------------------------------------------------------------------
267
268 ok. NetData is installed and is running.
269
270 Hit http://localhost:${NETDATA_PORT}/ from your browser.
271
272 To stop netdata, just kill it, with:
273
274   killall netdata
275
276 To start it, just run it:
277
278   ${NETDATA_PREFIX}/usr/sbin/netdata
279
280 Enjoy!
281
282 END