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