]> arthur.barton.de Git - netdata.git/blob - netdata.start
can now become another user
[netdata.git] / netdata.start
1 #!/bin/bash
2
3 base="`dirname "$0"`"
4
5 if [ ! -d "$base" -o ! -f "$base/netdata.c" -o ! -d "$base/web" ]
6 then
7         echo >&2 "Cannot find my home directory '${base}'."
8         exit 1
9 fi
10 cd "$base" || exit 1
11
12 # every how many seconds to show the graphs
13 NETDATA_CONFIG_UPDATE_EVERY=2
14
15 # the detail of the data that will be kept in netdata
16 NETDATA_CONFIG_INTERNAL_UPDATE_EVERY=1
17
18 # how many points should the graphs have
19 NETDATA_CONFIG_HISTORY_POINTS=120
20
21 # how many points should be kept in memory
22 NETDATA_CONFIG_HISTORY_DATA=3600
23
24 # every how many graph refreshes, the page should be reloaded
25 # this fixes a bug in the google graphs API which leaks memory
26 # when refreshes graphs
27 NETDATA_CONFIG_RELOAD_EVERY=500
28
29 # the user to run netdata under
30 NETDATA_CONFIG_USER=nobody
31
32 # set to 1, to enable debugging
33 NETDATA_CONFIG_DEBUG=0
34
35 # our port
36 NETDATA_CONFIG_PORT=19999
37
38 # get user configuration
39 if [ -f netdata.conf ]
40 then
41         source netdata.conf
42 fi
43
44 # how many history lines to keep in netdata
45 NETDATA_HISTORY_LINES=$[NETDATA_CONFIG_UPDATE_EVERY * NETDATA_CONFIG_HISTORY_POINTS / NETDATA_CONFIG_INTERNAL_UPDATE_EVERY]
46
47 echo "Stopping a (possibly) running netdata..."
48 killall netdata 2>/dev/null
49 killall tc-all.sh 2>/dev/null
50 sleep 2
51
52 echo "Compiling netdata"
53 if [ $NETDATA_CONFIG_DEBUG -eq 1 ]
54 then
55         ulimit -c unlimited
56         gcc -Wall -ggdb -o netdata netdata.c -lpthread || exit 1
57 else
58         gcc -Wall -O3 -o netdata netdata.c -lpthread || exit 1
59 fi
60
61 echo "Starting netdata"
62 if [ "$USER" = "root" ]
63 then
64         chown -R "$NETDATA_CONFIG_USER" web || exit 1
65         chmod    0775 web   || exit 1
66         chmod -R 0664 web/* || exit 1
67         ./netdata -d -u $NETDATA_CONFIG_USER -t $NETDATA_CONFIG_INTERNAL_UPDATE_EVERY -l $NETDATA_HISTORY_LINES -p $NETDATA_CONFIG_PORT || exit 1
68 else
69         echo >&2 "WARNING: NOT RUNNING AS ROOT - CANNOT SWITCH TO USER $NETDATA_CONFIG_USER"
70         echo >&2 "WARNING: MAKE SURE FILES IN web/ ARE OWNED BY $USER - or it will not work"
71         ./netdata -d -t $NETDATA_CONFIG_INTERNAL_UPDATE_EVERY -l $NETDATA_HISTORY_LINES -p $NETDATA_CONFIG_PORT || exit 1
72 fi
73 sleep 2
74
75 # count all graphs
76 all=`wget http://127.0.0.1:$NETDATA_CONFIG_PORT/list -O - 2>/dev/null`
77 count=0
78 for x in $all
79 do
80         y=`echo "$x" | tr ".-" "__"`
81         eval "t=\${NETDATA_PRIORITY_${y}}"
82         if [ ! "$t" = "IGNORE" ]
83         then
84                 count=$[count + 1]
85         fi
86 done
87
88 echo "We have $count graphs..."
89
90 echo "Generating web/index.html"
91 host="`hostname`"
92 cat >web/index.html <<EOF
93 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
94 <html xmlns="http://www.w3.org/1999/xhtml">
95 <style>
96         * {font-family:Arial}
97         div {float: left; margin: 0 0 0 0; }
98 </style>
99 <title>${host} netdata</title>
100 <head>
101         <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
102
103         <!--Load the AJAX API-->
104         <script type="text/javascript" src="https://www.google.com/jsapi"></script>
105         <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
106         <script type="text/javascript" src="/file/netdata.js"></script>
107         <script type="text/javascript">
108         
109         // Set a callback to run when the Google Visualization API is loaded.
110         google.setOnLoadCallback(drawCharts);
111         
112         function drawCharts() {
113                 // EDIT: add one line per interface you have
114                 // EDIT: 
115                 // EDIT:   name    div id                    json data           graph                                  vertical axis title
116                 // EDIT: --------------------------------------------------------------------------------------------------
117 EOF
118
119 for x in $all
120 do
121         y=`echo "$x" | tr ".-" "__"`
122
123         title=
124         vtitle=
125         case "$x" in
126                 net.*)
127                         title="Network Usage for "
128                         vtitle="bandwidth in kilobits/s"
129                         ;;
130
131                 disk.*)
132                         title="Disk I/O for "
133                         vtitle="I/O in kilobytes/s"
134                         ;;
135
136                 tc.*)
137                         title="Class utilization for "
138                         vtitle="bandwidth in kilobits/s"
139                         ;;
140
141         esac
142
143         eval "NETDATA_TITLE_${y}=\${NETDATA_TITLE_${y}:-${x}}"
144         eval "t=\${NETDATA_TITLE_${y}}"
145         
146         eval "p=\${NETDATA_PRIORITY_${y}}"
147         if [ ! "$p" = "IGNORE" ]
148         then
149                 cat >>web/index.html <<EOF2
150                 addChart('${x}', '${y}_div', 0, 0, "data/${x}/${NETDATA_CONFIG_HISTORY_POINTS}/${NETDATA_CONFIG_UPDATE_EVERY}/average/", "${title}${t} (${x})", "${vtitle}");
151 EOF2
152         fi
153 done
154
155 cat >>web/index.html <<EOF3
156                 refreshCharts(999999);
157         }
158         
159         var refreshCount = 0;
160         function myChartsRefresh() {
161                 refreshCount += 2;
162                 if(refreshCount > $NETDATA_CONFIG_RELOAD_EVERY) location.reload();
163
164                 // refresh up to 2 charts per second
165                 refreshCharts(2);
166         }
167         
168         setInterval(myChartsRefresh, 1000);
169
170         //window.onresize = function(event) {
171         //      refreshCharts(999999);
172         //};
173         </script>
174
175         </head>
176
177 <body>
178         <!--
179                 EDIT: add one div per interface you have
180                 EDIT: use the same id above and bellow!
181         -->
182 EOF3
183
184 tmp="/tmp/$RANDOM.netdata.$$.$RANDOM"
185 for x in $all
186 do
187         y=`echo "$x" | tr ".-" "__"`
188         eval "NETDATA_PRIORITY_${y}=\${NETDATA_PRIORITY_${y}:-${x}}"
189         eval "t=\${NETDATA_PRIORITY_${y}}"
190         if [ ! "$t" = "IGNORE" ]
191         then
192                 echo "${t}|${y}"
193         fi
194 done >"${tmp}"
195
196 for x in `cat "${tmp}" | sort`
197 do
198         n="`echo "$x" | cut -d '|' -f 2-`"
199         cat >>web/index.html <<EOF4
200         <div id="${n}_div"></div>
201 EOF4
202 done
203 rm -f "${tmp}"
204
205 cat >>web/index.html <<EOF5
206  </body>
207 </html>
208 EOF5
209
210 if [ "$USER" = "root" ]
211 then
212         chown -R "$NETDATA_CONFIG_USER" web || exit 1
213         chmod    0775 web   || exit 1
214         chmod -R 0664 web/* || exit 1
215 fi
216
217 # save config back
218 set|grep ^NETDATA_ >netdata.conf
219
220 echo "All Done."
221 echo "Just hit http://127.0.0.1:$NETDATA_CONFIG_PORT/ from your browser."
222 echo
223 echo "You can edit config options in file netdata.conf"
224 echo