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