removed NETDATA_DAEMON - it can be controlled via command line; now it opens log files before releasing root permissions; added preprocessor warnings if zlib or math are disabled; netdata.start adapted for installed netdata
Costa Tsaousis (ktsaou) committed
Nov 16, 2015 at 00:49 UTC
2306044fc85c2285c3f37666e7c2825d88b11aee
7 files changed
+65
-58
configure.ac
-6
@@ -26,12 +26,6 @@ AC_PROG_INSTALL
26
PKG_PROG_PKG_CONFIG
27
AC_USE_SYSTEM_EXTENSIONS
28
29
-AC_ARG_ENABLE(
30
- [daemon],
31
- [AS_HELP_STRING([--disable-daemon], [disable daemon])],
32
- ,
33
- [enable_daemon="yes"]
34
-)
29
AC_ARG_ENABLE(
30
[plugin-plugin-nfacct],
31
[AS_HELP_STRING([--enable-plugin-plugin-nfacct], [enable nfacct plugin, requires root])],
netdata.start
+44
-32
@@ -1,5 +1,33 @@
1
#!/bin/bash
2
3
+netdata=$(which netdata)
4
+
5
+if [ -z "${netdata}" -o ! -f src/netdata ]
6
+then
7
+ echo >&2 "Running ./autogen.sh ..."
8
+ ./autogen.sh || exit 1
9
+
10
+ echo >&2 "Running ./configure ..."
11
+ ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --with-zlib --with-math --with-user=netdata CFLAGS="-march=native -O3 -Wall -Wextra" || exit 1
12
+
13
+ echo >&2 "Compiling netdata ..."
14
+ make || exit 1
15
+fi
16
+
17
+if [ -z "${netdata}" -o src/netdata -nt "${netdata}" ]
18
+then
19
+ echo >&2 "Installing netdata ..."
20
+ make install
21
+
22
+ echo >&2 "Adding netdata group ..."
23
+ getent group netdata > /dev/null || groupadd -r netdata
24
+
25
+ echo >&2 "Adding netdata user ..."
26
+ getent passwd netdata > /dev/null || useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
27
+fi
28
+
29
+netdata=$(which netdata)
30
+
31
base="`dirname "$0"`"
32
33
if [ ! -d "${base}" ]
@@ -20,7 +48,7 @@ cd "${base}" || exit 1
48
config_option() {
49
local key="${1}" value="${2}" line=
50
23
- if [ -s "conf.d/netdata.conf" ]
51
+ if [ -s "/etc/netdata/netdata.conf" ]
52
then
53
line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "conf.d/netdata.conf" | head -n 1 )"
54
[ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
@@ -30,7 +58,7 @@ config_option() {
58
}
59
60
# user
33
-defuser="nobody"
61
+defuser="netdata"
62
[ ! "${UID}" = "0" ] && defuser="${USER}"
63
NETDATA_USER="$( config_option "run as user" "${defuser}" )"
64
@@ -43,28 +71,12 @@ defport=19999
71
NETDATA_PORT="$( config_option "port" ${defport} )"
72
73
# directories
46
-NETDATA_CACHE_DIR="$( config_option "database directory" "cache" )"
47
-NETDATA_WEB_DIR="$( config_option "web files directory" "web" )"
74
+NETDATA_CACHE_DIR="$( config_option "database directory" "/var/cache/netdata" )"
75
+NETDATA_WEB_DIR="$( config_option "web files directory" "/usr/share/netdata/web" )"
76
NETDATA_LOG_DIR="log"
77
NETDATA_CONF_DIR="conf.d"
78
79
52
-# -----------------------------------------------------------------------------
53
-# compile netdata
54
-
55
-echo >&2 "Compiling netdata (debug flags = $NETDATA_DEBUG)..."
56
-if [ $[ NETDATA_DEBUG ] -ne 0 ]
57
- then
58
- make install debug=1 || exit 1 # this installs in the current directory
59
-
60
- # let netdata core dump if it crashes
61
- ulimit -c unlimited
62
-
63
-else
64
- make install || exit 1 # this installs in the current directory
65
-fi
66
-
67
-
80
# -----------------------------------------------------------------------------
81
# prepare the directories
82
@@ -110,7 +122,7 @@ echo >&2
122
export TZ=":/etc/localtime"
123
124
echo >&2 "Starting netdata..."
113
-`pwd`/netdata "${@}"
125
+netdata "${@}"
126
127
if [ $? -ne 0 ]
128
then
@@ -125,35 +137,35 @@ fi
137
# -----------------------------------------------------------------------------
138
# save a config file, if it is not already there
139
128
-if [ ! -s conf.d/netdata.conf ]
140
+if [ ! -s /etc/netdata/netdata.conf ]
141
then
142
echo >&2 "Downloading default configuration from netdata..."
143
sleep 5
144
145
# remove a possibly obsolete download
134
- [ -f conf.d/netdata.conf.new ] && rm conf.d/netdata.conf.new
146
+ [ -f /etc/netdata/netdata.conf.new ] && rm /etc/netdata/netdata.conf.new
147
148
# try wget
137
- wget 2>/dev/null -O conf.d/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf"
149
+ wget 2>/dev/null -O /etc/netdata/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf"
150
ret=$?
151
152
# try curl
141
- if [ $ret -ne 0 -o ! -s conf.d/netdata.conf.net ]
153
+ if [ $ret -ne 0 -o ! -s /etc/netdata/netdata.conf.net ]
154
then
143
- curl -s -o conf.d/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf"
155
+ curl -s -o /etc/netdata/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf"
156
ret=$?
157
fi
158
147
- if [ $ret -eq 0 -a -s conf.d/netdata.conf.new ]
159
+ if [ $ret -eq 0 -a -s /etc/netdata/netdata.conf.new ]
160
then
149
- mv conf.d/netdata.conf.new conf.d/netdata.conf
150
- echo >&2 "New configuration saved for you to edit at conf.d/netdata.conf"
161
+ mv /etc/netdata/netdata.conf.new /etc/netdata/netdata.conf
162
+ echo >&2 "New configuration saved for you to edit at /etc/netdata/netdata.conf"
163
152
- chown -R "${NETDATA_USER}" conf.d/netdata.conf
153
- chmod 0664 conf.d/netdata.conf
164
+ chown "${NETDATA_USER}" /etc/netdata/netdata.conf
165
+ chmod 0664 /etc/netdata/netdata.conf
166
else
167
echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
156
- [ -f conf.d/netdata.conf.new ] && rm conf.d/netdata.conf.new
168
+ [ -f /etc/netdata/netdata.conf.new ] && rm /etc/netdata/netdata.conf.new
169
fi
170
fi
171
src/daemon.c
+12
-7
@@ -1,7 +1,6 @@
1
#ifdef HAVE_CONFIG_H
2
#include <config.h>
3
#endif
4
-#ifdef NETDATA_DAEMON
4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <unistd.h>
@@ -142,7 +141,7 @@ int become_user(const char *username)
141
return(0);
142
}
143
145
-int become_daemon(int dont_fork, int close_all_files, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp)
144
+int become_daemon(int dont_fork, int close_all_files, const char *user, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp)
145
{
146
fflush(NULL);
147
@@ -151,14 +150,14 @@ int become_daemon(int dont_fork, int close_all_files, const char *input, const c
150
151
if(input && *input) {
152
if((input_fd = open(input, O_RDONLY, 0666)) == -1) {
154
- fprintf(stderr, "Cannot open input file '%s' (%s).", input, strerror(errno));
153
+ fprintf(stderr, "Cannot open input file '%s' (%s).\n", input, strerror(errno));
154
return -1;
155
}
156
}
157
158
if(output && *output) {
159
if((output_fd = open(output, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
161
- fprintf(stderr, "Cannot open output log file '%s' (%s).", output, strerror(errno));
160
+ fprintf(stderr, "Cannot open output log file '%s' (%s).\n", output, strerror(errno));
161
if(input_fd != -1) close(input_fd);
162
return -1;
163
}
@@ -166,7 +165,7 @@ int become_daemon(int dont_fork, int close_all_files, const char *input, const c
165
166
if(error && *error) {
167
if((error_fd = open(error, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
169
- fprintf(stderr, "Cannot open error log file '%s' (%s).", error, strerror(errno));
168
+ fprintf(stderr, "Cannot open error log file '%s' (%s).\n", error, strerror(errno));
169
if(input_fd != -1) close(input_fd);
170
if(output_fd != -1) close(output_fd);
171
return -1;
@@ -175,7 +174,7 @@ int become_daemon(int dont_fork, int close_all_files, const char *input, const c
174
175
if(access && *access && access_fd) {
176
if((*access_fd = open(access, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) {
178
- fprintf(stderr, "Cannot open access log file '%s' (%s).", access, strerror(errno));
177
+ fprintf(stderr, "Cannot open access log file '%s' (%s).\n", access, strerror(errno));
178
if(input_fd != -1) close(input_fd);
179
if(output_fd != -1) close(output_fd);
180
if(error_fd != -1) close(error_fd);
@@ -316,6 +315,12 @@ int become_daemon(int dont_fork, int close_all_files, const char *input, const c
315
}
316
}
317
318
+ if(user && *user) {
319
+ if(become_user(user) != 0) {
320
+ error("Cannot become user '%s'. Continuing as we are.", user);
321
+ }
322
+ else debug(D_OPTIONS, "Successfully became user '%s'.", user);
323
+ }
324
+
325
return(0);
326
}
321
-#endif
src/daemon.h
+1
-1
@@ -7,6 +7,6 @@ extern void prepare_rundir();
7
8
extern int become_user(const char *username);
9
10
-extern int become_daemon(int dont_fork, int close_all_files, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp);
10
+extern int become_daemon(int dont_fork, int close_all_files, const char *user, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp);
11
12
#endif /* NETDATA_DAEMON_H */
src/main.c
+4
-12
@@ -239,6 +239,7 @@ int main(int argc, char **argv)
239
char *output_log_file = NULL;
240
char *error_log_file = NULL;
241
char *access_log_file = NULL;
242
+ char *user = NULL;
243
{
244
char buffer[1024];
245
@@ -343,14 +344,7 @@ int main(int argc, char **argv)
344
// --------------------------------------------------------------------
345
346
prepare_rundir();
346
- char *user = config_get("global", "run as user", (getuid() == 0)?NETDATA_USER:"");
347
- if(*user) {
348
- if(become_user(user) != 0) {
349
- fprintf(stderr, "Cannot become user %s.\n", user);
350
- exit(1);
351
- }
352
- else debug(D_OPTIONS, "Successfully became user %s.", user);
353
- }
347
+ user = config_get("global", "run as user", (getuid() == 0)?NETDATA_USER:"");
348
349
// --------------------------------------------------------------------
350
@@ -382,12 +376,10 @@ int main(int argc, char **argv)
376
// never become a problem
377
if(nice(20) == -1) fprintf(stderr, "Cannot lower my CPU priority. Error: %s.\n", strerror(errno));
378
385
-#ifdef NETDATA_DAEMON
386
- if(become_daemon(dont_fork, 0, input_log_file, output_log_file, error_log_file, access_log_file, &access_fd, &stdaccess) == -1) {
387
- fprintf(stderr, "Cannot demonize myself (%s).", strerror(errno));
379
+ if(become_daemon(dont_fork, 0, user, input_log_file, output_log_file, error_log_file, access_log_file, &access_fd, &stdaccess) == -1) {
380
+ fatal("Cannot demonize myself (%s).", strerror(errno));
381
exit(1);
382
}
390
-#endif
383
384
if(output_log_syslog || error_log_syslog || access_log_syslog)
385
openlog("netdata", LOG_PID, LOG_DAEMON);
src/storage_number.c
+2
@@ -3,6 +3,8 @@
3
#endif
4
#ifdef STORAGE_WITH_MATH
5
#include <math.h>
6
+#else
7
+#warning "You are building without math. math allows accurate calculations. It should be enabled."
8
#endif
9
10
#include "common.h"
src/web_client.c
+2
@@ -229,6 +229,8 @@ void web_client_reset(struct web_client *w)
229
230
#ifdef NETDATA_WITH_ZLIB
231
if(likely(w->zoutput)) sent = (long)w->zstream.total_out;
232
+#else
233
+#warning "You are building without zlib. zlib allows netdata to trasnfer a lot less data with web clients. It should be enabled."
234
#endif
235
236
long size = (w->mode == WEB_CLIENT_MODE_FILECOPY)?w->data->rbytes:w->data->bytes;