]> arthur.barton.de Git - netdata.git/blob - netdata.start
fixed charts.d.plugin to get directories from netdata server; netdata now exports...
[netdata.git] / netdata.start
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 "Cleaning a possibly old compilation ..."
14         make clean || exit 1
15
16 fi
17
18 echo >&2 "Compiling netdata ..."
19 make || exit 1
20
21 if [ -z "${netdata}" -o src/netdata -nt "${netdata}" ]
22 then
23         echo >&2 "Installing netdata ..."
24         make install
25
26         echo >&2 "Adding netdata group ..."
27         getent group netdata > /dev/null || groupadd -r netdata
28
29         echo >&2 "Adding netdata user ..."
30         getent passwd netdata > /dev/null || useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
31 fi
32
33 netdata=$(which netdata)
34
35
36 # -----------------------------------------------------------------------------
37 # load options from the configuration file
38
39 # create an empty config if it does not exist
40 [ ! -f conf.d/netdata.conf ] && touch conf.d/netdata.conf
41
42 # function to extract values from the config file
43 config_option() {
44         local key="${1}" value="${2}" line=
45
46         if [ -s "/etc/netdata/netdata.conf" ]
47                 then
48                 line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "/etc/netdata/netdata.conf" | head -n 1 )"
49                 [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
50         fi
51
52         echo "${value}"
53 }
54
55 # user
56 defuser="netdata"
57 [ ! "${UID}" = "0" ] && defuser="${USER}"
58 NETDATA_USER="$( config_option "run as user" "${defuser}" )"
59
60 # debug flags
61 defdebug=0
62 NETDATA_DEBUG="$( config_option "debug flags" ${defdebug} )"
63
64 # port
65 defport=19999
66 NETDATA_PORT="$( config_option "port" ${defport} )"
67
68 # directories
69 NETDATA_CACHE_DIR="$( config_option "cache directory" "/var/cache/netdata" )"
70 NETDATA_WEB_DIR="$( config_option "web files directory" "/usr/share/netdata/web" )"
71 NETDATA_LOG_DIR="$( config_option "log directory" "/var/log/netdata" )"
72 NETDATA_CONF_DIR="$( config_option "config directory" "/etc/netdata" )"
73
74
75 # -----------------------------------------------------------------------------
76 # prepare the directories
77
78 echo >&2 "Fixing directory permissions for user ${NETDATA_USER}..."
79 for x in "${NETDATA_WEB_DIR}" "${NETDATA_CONF_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"
80 do
81         if [ ! -d "${x}" ]
82                 then
83                 echo >&2 "Creating directory '${x}'"
84                 mkdir -p "${x}" || exit 1
85         fi
86         chown -R "${NETDATA_USER}" "${x}" || echo >&2 "WARNING: Cannot change the ownership of the files in directory ${x} to ${NETDATA_USER}..."
87         chmod 0775 "${x}" "${x}" || echo >&2 "WARNING: Cannot change the permissions of the directory ${x} to 0755..."
88 done
89
90 # fix apps.plugin to be setuid to root
91 chown root '/usr/libexec/netdata/plugins.d/apps.plugin' && chmod 4755 '/usr/libexec/netdata/plugins.d/apps.plugin'
92
93
94 # -----------------------------------------------------------------------------
95 # stop a running netdata
96
97 printf >&2 "Stopping a (possibly) running netdata..."
98 ret=0
99 count=0
100 while [ $ret -eq 0 ]
101 do
102         if [ $count -gt 15 ]
103                 then
104                 echo >&2 "Cannot stop the running netdata."
105                 exit 1
106         fi
107
108         count=$((count + 1))
109         killall netdata 2>/dev/null
110         ret=$?
111         test $ret -eq 0 && printf >&2 "." && sleep 2
112 done
113 echo >&2
114
115
116 # -----------------------------------------------------------------------------
117 # run netdata
118
119 echo >&2 "Starting netdata..."
120 netdata "${@}"
121
122 if [ $? -ne 0 ]
123         then
124         echo >&2
125         echo >&2 "SORRY! FAILED TO START NETDATA!"
126         exit 1
127 else
128         echo >&2 "OK. NetData Started!"
129 fi
130
131
132 # -----------------------------------------------------------------------------
133 # save a config file, if it is not already there
134
135 if [ ! -s /etc/netdata/netdata.conf ]
136         then
137         echo >&2 "Downloading default configuration from netdata..."
138         sleep 5
139
140         # remove a possibly obsolete download
141         [ -f /etc/netdata/netdata.conf.new ] && rm /etc/netdata/netdata.conf.new
142
143         # try wget
144         wget 2>/dev/null -O /etc/netdata/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf"
145         ret=$?
146
147         # try curl
148         if [ $ret -ne 0 -o ! -s /etc/netdata/netdata.conf.net ]
149                 then
150                 curl -s -o /etc/netdata/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf"
151                 ret=$?
152         fi
153
154         if [ $ret -eq 0 -a -s /etc/netdata/netdata.conf.new ]
155                 then
156                 mv /etc/netdata/netdata.conf.new /etc/netdata/netdata.conf
157                 echo >&2 "New configuration saved for you to edit at /etc/netdata/netdata.conf"
158
159                 chown "${NETDATA_USER}" /etc/netdata/netdata.conf
160                 chmod 0664 /etc/netdata/netdata.conf
161         else
162                 echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
163                 [ -f /etc/netdata/netdata.conf.new ] && rm /etc/netdata/netdata.conf.new
164         fi
165 fi
166
167 echo >&2
168 echo >&2 "Hit http://localhost:${NETDATA_PORT}/ from your browser."
169 echo >&2
170 echo >&2 "Enjoy..."
171 echo >&2